<commit_before>################################################################################
# This widget expects parameters:
# (factor, first_label, second_label) where factor is the ratio between the units 
# of the first textfield and the second textfield.  
class FactorTextFieldsWidget < Widget
  
  ################################################################################
  def self.render_form_object(form,field_instance_id,value,options)
   params = options[:params]
    if params
      (factor,first_label,second_label) = params.split(/,/)
      if value
        (first_value,second_value) = parse_value(value,factor)
      	<<-EOHTML
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'first_box')}" id="#{build_html_multi_id(field_instance_id,'first_box')}" value="#{first_value}" /> #{first_label}
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'second_box')}" id="#{build_html_multi_id(field_instance_id,'second_box')}" value="#{second_value}" /> #{second_label}
EOHTML
      else
      	<<-EOHTML
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'first_box')}" id="#{build_html_multi_id(field_instance_id,'first_box')}"/> #{first_label}
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'second_box')}" id="#{build_html_multi_id(field_instance_id,'second_box')}"  /> #{second_label}
EOHTML
      end
    end
  end

  def self.parse_value(value,factor)
  	first_value = value.to_i / factor.to_i
  	second_value = value.to_i % factor.to_i
  	[first_value,second_value]
  end

  ################################################################################
  def self.humanize_value(value,options=nil)
    params = options[:params]
    if params
      (factor,first_label,second_label) = params.split(/,/)
      if value 
        (first_value,second_value) = parse_value(value,factor)
        "#{first_value} #{first_label} #{second_value} #{second_label}"
      end
  	end
  end

  ################################################################################
  def self.javascript_get_value_function (field_instance_id)
    %Q|$DF('#{build_html_id(field_instance_id)}')|
  end

  ################################################################################
  def self.javascript_build_observe_function(field_instance_id,script,options)
    result = ""
    %w(first_box second_box).each do |field|
      result << %Q|Event.observe('#{build_html_multi_id(field_instance_id,field)}', 'change', function(e){ #{script} });\n|
    end
    result
  end

  ################################################################################
  def self.convert_html_value(value,params={})
    begin<commit_msg>fixed factor_textfields javascript_get_value_function to actually return the correct calculated value<commit_after>################################################################################
# This widget expects parameters:
# (factor, first_label, second_label) where factor is the ratio between the units 
# of the first textfield and the second textfield.  
class FactorTextFieldsWidget < Widget
  
  ################################################################################
  def self.render_form_object(form,field_instance_id,value,options)
   params = options[:params]
    if params
      (factor,first_label,second_label) = params.split(/,/)
      result = ''
      if value
        (first_value,second_value) = parse_value(value,factor)
      	result <<  <<-EOHTML
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'first_box')}" id="#{build_html_multi_id(field_instance_id,'first_box')}" value="#{first_value}" /> #{first_label}
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'second_box')}" id="#{build_html_multi_id(field_instance_id,'second_box')}" value="#{second_value}" /> #{second_label}
EOHTML
      else
      	result << <<-EOHTML
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'first_box')}" id="#{build_html_multi_id(field_instance_id,'first_box')}"/> #{first_label}
<input type="text" size=2 name="#{build_html_multi_name(field_instance_id,'second_box')}" id="#{build_html_multi_id(field_instance_id,'second_box')}"  /> #{second_label}
EOHTML
      end
      result << %Q|<input type="hidden" name="#{build_html_multi_name(field_instance_id,'factor')}"  id="#{build_html_multi_id(field_instance_id,'factor')}"/ value="#{factor}">|
    end
  end

  def self.parse_value(value,factor)
  	first_value = value.to_i / factor.to_i
  	second_value = value.to_i % factor.to_i
  	[first_value,second_value]
  end

  ################################################################################
  def self.humanize_value(value,options=nil)
    params = options[:params]
    if params
      (factor,first_label,second_label) = params.split(/,/)
      if value 
        (first_value,second_value) = parse_value(value,factor)
        "#{first_value} #{first_label} #{second_value} #{second_label}"
      end
  	end
  end

  ################################################################################
  def self.javascript_get_value_function (field_instance_id)
    %Q|parseInt($F('#{build_html_multi_id(field_instance_id,'first_box')}')) * parseFloat($F('#{build_html_multi_id(field_instance_id,'factor')}')) + parseInt($F('#{build_html_multi_id(field_instance_id,'second_box')}')) |
  end

  ################################################################################
  def self.javascript_build_observe_function(field_instance_id,script,options)
    result = ""
    %w(first_box second_box).each do |field|
      result << %Q|Event.observe('#{build_html_multi_id(field_instance_id,field)}', 'change', function(e){ #{script} });\n|
    end
    result
  end

  ################################################################################
  def self.convert_html_value(value,params={})
    begin
{'fraction_non_alphanumeric': 0.279582561858273, 'fraction_numerical': 0.0013465746507322, 'mean_word_length': 6.716883116883117, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618415', 'n_tokens_mistral': 1794, 'n_tokens_neox': 1750, 'n_words': 271}