commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / webform_civicrm / js / webform_civicrm_options.js
1 // Shim for old versions of jQuery
2 if (typeof jQuery.fn.prop !== 'function') {
3 jQuery.fn.prop = jQuery.fn.attr;
4 }
5
6 /**
7 * Javascript Module for managing webform_civicrm options for select elements.
8 */
9 (function ($, D) {
10 function defaultBoxes(newType, defaultName) {
11 var oldType = newType == 'radio' ? 'checkbox' : 'radio';
12 var defaultValue = $('input[name*="[civicrm_defaults]"]:checked').val() || '';
13 $('input:'+oldType+'[name*="[civicrm_defaults]"]').each(function() {
14 var ele = $(this);
15 var val = ele.attr('value');
16 var classes = ele.attr('class');
17 var id = ele.attr('id');
18 if (newType == 'radio') {
19 var name = defaultName + '[' + defaultValue + ']';
20 }
21 else {
22 var name = defaultName + '[' + val + ']';
23 }
24 ele.replaceWith('<input type="'+newType+'" class="'+classes+'" id="'+id+'" name="'+name+'" value="'+val+'">');
25 });
26 if (defaultValue) {
27 $('input:[name*="[civicrm_defaults]"][value="'+defaultValue+'"]').prop('checked', true);
28 }
29 $('input:checkbox.select-all-civi-defaults').change(function() {
30 if ($(this).is(':checked')) {
31 $('input.civicrm-default').not(':disabled').prop('checked', true);
32 }
33 else {
34 $('input.civicrm-default, input.select-all-civi-defaults').prop('checked', false);
35 }
36 });
37 $('input:radio[name*="[civicrm_defaults]"]').change(function() {
38 if ($(this).is(':checked')) {
39 $('input:radio[name*="[civicrm_defaults]"]').attr('name', defaultName + '[' + $(this).val() + ']');
40 }
41 });
42 }
43
44 D.behaviors.webform_civicrmOptions = {
45 attach: function (context) {
46 $('input.civicrm-enabled', context).once('wf-civi').change(function() {
47 if ($(this).is(':checked') ) {
48 $(this).parents('tr').find('input.civicrm-label, input.civicrm-default').prop('disabled', false);
49 }
50 else {
51 $(this).parents('tr').find('input.civicrm-label, input.civicrm-default').prop('disabled', true).prop('checked', false);
52 }
53 if ($(this).parents('tr').find('input.civicrm-label').val() == '') {
54 var val = $(this).parents('tr').find('span.civicrm-option-name').text();
55 $(this).parents('tr').find('input.civicrm-label').val(val);
56 }
57 }).change();
58
59 $('input.select-all-civi-options').once('wf-civi').change(function() {
60 if ($(this).is(':checked') ) {
61 $('input.civicrm-enabled, input.select-all-civi-options').prop('checked', true);
62 }
63 else {
64 $('input.civicrm-enabled, input.select-all-civi-options, input.select-all-civi-defaults').prop('checked', false);
65 }
66 $('input.civicrm-enabled').change();
67 });
68
69 var defaultName = 'civicrm_options_fieldset[civicrm_defaults]';
70
71 var multiple = $('input[name="extra[multiple]"]');
72 if (multiple.is(':checkbox')) {
73 multiple.once('wf-civi').change(function() {
74 var type = $(this).is(':checked') ? 'checkbox' : 'radio';
75 defaultBoxes(type, defaultName);
76 }).change();
77 }
78 else if (multiple.attr('value') !== '1') {
79 defaultBoxes('radio', defaultName);
80 }
81
82 $('a.tabledrag-handle, a.tabledrag-toggle-weight').not('.live-options-hide a').wrap('<div class="live-options-hide" />');
83
84 $('input[name="extra[civicrm_live_options]"]').once('wf-civi').change(function() {
85 if ($(this).is(':checked')) {
86 switch ($(this).attr('value')) {
87 case "0":
88 $('.live-options-hide').show();
89 $('.live-options-show').hide();
90 $('.tabledrag-hide.visible').removeClass('visible').show();
91 break;
92 case "1":
93 $('.live-options-hide').hide();
94 $('.live-options-show').show();
95 $('.tabledrag-hide:visible').addClass('visible').hide();
96 $('input.civicrm-enabled, input.select-all-civi-options').prop('checked', true);
97 $('input.civicrm-enabled').change();
98 break;
99 }
100 }
101 }).change();
102 }
103 };
104 })(jQuery, Drupal);