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