|
3 | 3 | * Javascript code for Ajax Chain Select module.
|
4 | 4 | */
|
5 | 5 |
|
6 |
| -function f3region_get_element(el, $previous_level) { |
7 |
| - var previous_level_id = el.value, |
8 |
| - url = Drupal.settings.basePath + 'ajax_chain_select/callback/', |
9 |
| - $formitem = jQuery(el).closest('.form-item'), |
10 |
| - $nextall = {}, |
11 |
| - $nextel = null, |
12 |
| - $dc = null, |
13 |
| - $dch = null, |
14 |
| - $next_level = $previous_level + 1; |
15 |
| - |
16 |
| - $nextall = $formitem.nextAll(); |
17 |
| - $nextel = jQuery($nextall[0]); |
18 |
| - |
19 |
| - for (var $i = 1; $i < $nextall.length; $i++) { |
20 |
| - var $next_ptr = jQuery($nextall[$i]); |
21 |
| - if ($next_ptr.hasClass('form-type-textfield')) { |
22 |
| - continue; |
23 |
| - } |
24 |
| - else { |
25 |
| - $next_ptr.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
26 |
| - if ($next_ptr.find('select').find('option') != 'undefined') { |
27 |
| - $next_ptr.find('select').find('option:eq(0)').attr('selected', 'selected'); |
28 |
| - } |
29 |
| - } |
30 |
| - } |
| 6 | +(function($) { |
| 7 | + Drupal.behaviors.AjaxChainSelect = { |
| 8 | + attach: function(context, settings) { |
| 9 | + $("select.ajax-chain-select-select", context).change(function() { |
| 10 | + var current_el = this, |
| 11 | + parent_fieldset = $(current_el).parents('fieldset'), |
| 12 | + current_level = $(current_el).attr('level'), |
| 13 | + fieldset_settings = settings['ajax_chain_select'][$(parent_fieldset).attr('id')], |
| 14 | + current_level_id = this.value, |
| 15 | + url = Drupal.settings.basePath + 'ajax_chain_select/callback/', |
| 16 | + formitem = $(current_el).closest('.form-item'), |
| 17 | + next_all = formitem.nextAll(), |
| 18 | + next_el = $(next_all[0]), |
| 19 | + dc = null, |
| 20 | + dch = null, |
| 21 | + next_level = (current_level * 1) + 1; |
31 | 22 |
|
32 |
| - $dc = jQuery(el).closest('fieldset').find('.form-type-textfield input.acs-dc').val(); |
33 |
| - $dch = jQuery(el).closest('fieldset').find('.form-type-textfield input.acs-dch').val(); |
| 23 | + for (var $i = 1; $i < next_all.length; $i++) { |
| 24 | + var next_ptr = $(next_all[$i]); |
| 25 | + if (next_ptr.hasClass('form-type-textfield')) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + else { |
| 29 | + //Disabling all next select elements and setting them to their first value. |
| 30 | + next_ptr.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
| 31 | + if (next_ptr.find('select').find('option') !== undefined) { |
| 32 | + next_ptr.find('select').find('option:eq(0)').attr('selected', 'selected'); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
34 | 36 |
|
35 |
| - if (previous_level_id == "" || isNaN(Number(previous_level_id))) { |
36 |
| - $nextel.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
37 |
| - if ($nextel.find('select').find('option') != 'undefined') { |
38 |
| - $nextel.find('select').find('option:eq(0)').attr('selected', 'selected'); |
39 |
| - } |
40 |
| - } |
41 |
| - else { |
42 |
| - jQuery.getJSON(url + $next_level + '/' + previous_level_id + '/' + $dc + '/' + $dch, function(response) { |
43 |
| - if (response.has_data == '1') { |
44 |
| - var $temp = $nextel.find('select').find('option:eq(0)'); |
45 |
| - $nextel.find('select').html($temp); |
46 |
| - $nextel.find('select').append(response.options); |
47 |
| - $nextel.removeClass('form-disabled').find('select').removeAttr('disabled'); |
48 |
| - } |
49 |
| - else { |
50 |
| - $nextel.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
51 |
| - if ($nextel.find('select').find('option') != 'undefined') { |
52 |
| - $nextel.find('select').find('option:eq(0)').attr('selected', 'selected'); |
| 37 | + dc = $(parent_fieldset).find('.form-type-textfield input.acs-dc').val(); |
| 38 | + dch = $(parent_fieldset).find('.form-type-textfield input.acs-dch').val(); |
| 39 | + |
| 40 | + if (current_level_id === "" || isNaN(Number(current_level_id))) { |
| 41 | + next_el.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
| 42 | + if (next_el.find('select').find('option') !== undefined) { |
| 43 | + next_el.find('select').find('option:eq(0)').attr('selected', 'selected'); |
| 44 | + } |
| 45 | + } |
| 46 | + else { |
| 47 | + |
| 48 | + $(current_el).addClass('progress-disabled').attr('disabled', true); |
| 49 | + |
| 50 | + if (fieldset_settings.show_throbber) { |
| 51 | + var progress_el = $('<div class="ajax-progress ajax-progress-throbber"><div class="throbber"> </div></div>'); |
| 52 | + if (fieldset_settings.progress_message) { |
| 53 | + $('.throbber', progress_el).after('<div class="message">' + fieldset_settings.progress_message + '</div>'); |
| 54 | + } |
| 55 | + $(current_el).after(progress_el); |
| 56 | + } |
| 57 | + |
| 58 | + $.getJSON(url + next_level + '/' + current_level_id + '/' + dc + '/' + dch, function(response) { |
| 59 | + |
| 60 | + $(current_el).removeClass('progress-disabled').removeAttr('disabled'); |
| 61 | + |
| 62 | + if (fieldset_settings.show_throbber) { |
| 63 | + $(progress_el).remove(); |
| 64 | + } |
| 65 | + |
| 66 | + if (response.has_data === '1') { |
| 67 | + var temp = next_el.find('select').find('option:eq(0)'); |
| 68 | + next_el.find('select').html(temp); |
| 69 | + next_el.find('select').append(response.options); |
| 70 | + next_el.removeClass('form-disabled').find('select').removeAttr('disabled'); |
| 71 | + } |
| 72 | + else { |
| 73 | + next_el.addClass('form-disabled').find('select').attr('disabled', 'disabled'); |
| 74 | + if (next_el.find('select').find('option') !== undefined) { |
| 75 | + next_el.find('select').find('option:eq(0)').attr('selected', 'selected'); |
| 76 | + } |
| 77 | + } |
| 78 | + }); |
53 | 79 | }
|
54 |
| - } |
55 |
| - }); |
56 |
| - } |
57 |
| -} |
| 80 | + }); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | +})(jQuery); |
0 commit comments