Module: ChinaRegionFu::Utils

Included in:
Helpers::FormHelper, SimpleForm::RegionInput, RegionInput
Defined in:
lib/china_region_fu/helpers/utils.rb

Instance Method Summary collapse

Instance Method Details

#append_html_class_option(html_options) ⇒ Object



49
50
51
52
53
54
# File 'lib/china_region_fu/helpers/utils.rb', line 49

def append_html_class_option(html_options)
  _html_options = html_options.deep_dup
  _html_options[:class] ||= ''
  _html_options[:class] << ' china-region-select' unless _html_options[:class].include?('china-region-select')
  _html_options
end

#append_html_data_option(current_region, sub_region, html_options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/china_region_fu/helpers/utils.rb', line 56

def append_html_data_option(current_region, sub_region, html_options)
  _html_options = html_options.deep_dup
  _html_options[:data] ||= {}
  _html_options[:data][:region_name] = current_region
  if sub_region
    _html_options[:data][:sub_region] = sub_region
  else
    _html_options[:data].delete(:sub_region)
  end
  _html_options
end

#append_html_options(current_region, sub_region, html_options) ⇒ Object



68
69
70
71
72
73
# File 'lib/china_region_fu/helpers/utils.rb', line 68

def append_html_options(current_region, sub_region, html_options)
  _html_options = html_options.deep_dup
  _html_options = append_html_class_option(_html_options)
  _html_options = append_html_data_option(current_region, sub_region, _html_options)
  _html_options
end

#append_prompt(current_region, options) ⇒ Object



75
76
77
78
79
80
# File 'lib/china_region_fu/helpers/utils.rb', line 75

def append_prompt(current_region, options)
  current_name = current_region.to_s.sub(/_id\Z/, '')
  options_without_prompt = options.except(:province_prompt, :city_prompt, :district_prompt)
  options_without_prompt[:prompt] = options["#{current_name}_prompt".to_sym]
  options_without_prompt
end

#china_region_fu_jsObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/china_region_fu/helpers/utils.rb', line 6

def china_region_fu_js
  js = "    <script type=\"text/javascript\">\n      //<![CDATA[\n        window.chinaRegionFu = window.chinaRegionFu || {};\n        $(function(){\n          $('body').off('change', '.china-region-select').on('change', '.china-region-select', function(event) {\n            var $self = $(event.currentTarget),\n              $subRegionDom = $('[data-region-name=\"' + $self.data('sub-region') + '\"]'),\n              subName = $self.data('sub-region'),\n              parentName = $self.data('region-name'),\n              parentId = $self.val();\n            if ($subRegionDom.size() > 0 && subName && parentName && parentId) {\n              $.getJSON('/china_region_fu/fetch_options', {\n                  columns: window.chinaRegionFu.fetchColumns || 'id,name',\n                  sub_name: subName,\n                  parent_name: parentName,\n                  parent_id: parentId\n                }, function(json) {\n                  if (window.chinaRegionFu.ajaxDone) {\n                    window.chinaRegionFu.ajaxDone(json);\n                  } else {\n                    var options = [];\n                    $self.parent().nextAll().find('.china-region-select > option[value!=\"\"]').remove()\n                    $.each(json.data, function(_, value) {\n                      options.push(\"<option value='\" + value.id + \"'>\" + value.name + \"</option>\");\n                    });\n                    $subRegionDom.append(options.join(''));\n                  }\n              }).fail(function(xhr, textStatus, error) {\n                window.chinaRegionFu.ajaxFail && window.chinaRegionFu.ajaxFail(xhr, textStatus, error);\n              }).always(function(event, xhr, settings) {\n                window.chinaRegionFu.ajaxAlways && window.chinaRegionFu.ajaxAlways(event, xhr, settings);\n              });\n            }\n          });\n        });\n      //]]>\n    </script>\n  JAVASCRIPT\n  js.html_safe\nend\n"