1 /** |
|
2 * toggle visibility of an element by its id & set current visibility status in a cookie |
|
3 * |
|
4 */ |
|
5 |
|
6 var prefsValues = {}; |
|
7 |
|
8 function togglePrefVisibility(elemId) { |
|
9 clearPreviousMessages(); |
|
10 jQuery('#' + elemId).toggleClass('hidden'); |
|
11 } |
|
12 |
|
13 function closeFieldset(fieldsetid) { |
|
14 var linklabel = _("open all"); |
|
15 var linkhref = 'javascript:openFieldset("' + fieldsetid + '")'; |
|
16 _toggleFieldset(fieldsetid, 1, linklabel, linkhref); |
|
17 } |
|
18 |
|
19 function openFieldset(fieldsetid) { |
|
20 var linklabel = _("close all"); |
|
21 var linkhref = 'javascript:closeFieldset("' + fieldsetid + '")'; |
|
22 _toggleFieldset(fieldsetid, 0, linklabel, linkhref); |
|
23 } |
|
24 |
|
25 function _toggleFieldset(fieldsetid, closeaction, linklabel, linkhref) { |
|
26 jQuery('#' + fieldsetid).find('div.openlink').each(function() { |
|
27 var link = A({ |
|
28 'href': "javascript:$.noop();", |
|
29 'onclick': linkhref |
|
30 }, |
|
31 linklabel); |
|
32 jQuery(this).empty().append(link); |
|
33 }); |
|
34 jQuery('#' + fieldsetid).find('fieldset[id]').each(function() { |
|
35 var fieldset = jQuery(this); |
|
36 if (closeaction) { |
|
37 fieldset.addClass('hidden'); |
|
38 } else { |
|
39 fieldset.removeClass('hidden'); |
|
40 linkLabel = (_("open all")); |
|
41 } |
|
42 }); |
|
43 } |
|
44 |
|
45 function validatePrefsForm(formid) { |
|
46 clearPreviousMessages(); |
|
47 _clearPreviousErrors(formid); |
|
48 return validateForm(formid, null, submitSuccess, submitFailure); |
|
49 } |
|
50 |
|
51 function submitFailure(result, formid, cbargs) { |
|
52 var $form = jQuery('#' + formid); |
|
53 var dom = DIV({'class': 'critical'}, _("please correct errors below")); |
|
54 $form.find('div.formsg').empty().append(dom); |
|
55 unfreezeFormButtons(formid); |
|
56 var descr = result[1]; |
|
57 _displayValidationerrors(formid, descr[0], descr[1]); |
|
58 $form.find('span.error').next().focus(); |
|
59 return false; // so handleFormValidationResponse doesn't try to display error |
|
60 } |
|
61 |
|
62 function submitSuccess(result, formid, cbargs) { |
|
63 var $form = jQuery('#' + formid); |
|
64 setCurrentValues($form); |
|
65 var dom = DIV({'class': 'msg'}, _("changes applied")); |
|
66 $form.find('div.formsg').empty().append(dom); |
|
67 $form.find('input').removeClass('changed'); |
|
68 checkValues($form, true); |
|
69 return; |
|
70 } |
|
71 |
|
72 function clearPreviousMessages() { |
|
73 jQuery('div#appMsg').addClass('hidden'); |
|
74 jQuery('div.formsg').empty(); |
|
75 } |
|
76 |
|
77 function checkValues(form, success) { |
|
78 var unfreezeButtons = false; |
|
79 jQuery(form).find('select').each(function() { |
|
80 unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons); |
|
81 }); |
|
82 jQuery(form).find('[type=text]').each(function() { |
|
83 unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons); |
|
84 }); |
|
85 jQuery(form).find('input[type=radio]:checked').each(function() { |
|
86 unfreezeButtons = _checkValue(jQuery(this), unfreezeButtons); |
|
87 }); |
|
88 |
|
89 if (unfreezeButtons) { |
|
90 unfreezeFormButtons(form.attr('id')); |
|
91 } else { |
|
92 if (!success) { |
|
93 clearPreviousMessages(); |
|
94 } |
|
95 _clearPreviousErrors(form.attr('id')); |
|
96 freezeFormButtons(form.attr('id')); |
|
97 } |
|
98 } |
|
99 |
|
100 function _checkValue(input, unfreezeButtons) { |
|
101 var currentValue = prefsValues[input.attr('name')]; |
|
102 if (currentValue != input.val()) { |
|
103 input.addClass('changed'); |
|
104 unfreezeButtons = true; |
|
105 } else { |
|
106 input.removeClass('changed'); |
|
107 jQuery('span[id="err-' + input.attr('id') + '"]').remove(); |
|
108 } |
|
109 input.removeClass('error'); |
|
110 return unfreezeButtons; |
|
111 } |
|
112 |
|
113 function setCurrentValues(form) { |
|
114 jQuery(form).find('[name^=value]').each(function() { |
|
115 var input = jQuery(this); |
|
116 var name = input.attr('name'); |
|
117 if (input.attr('type') == 'radio') { |
|
118 // NOTE: there seems to be a bug with jQuery(input).attr('checked') |
|
119 // in our case, we can't rely on its value, we use |
|
120 // the DOM API instead. |
|
121 if (input[0].checked) { |
|
122 prefsValues[name] = input.val(); |
|
123 } |
|
124 } else { |
|
125 prefsValues[name] = input.val(); |
|
126 } |
|
127 jQuery(form).find('input[name="edits-' + name + '"]').val(prefsValues[name]); |
|
128 }); |
|
129 } |
|
130 |
|
131 function initEvents() { |
|
132 jQuery('form').each(function() { |
|
133 var form = jQuery(this); |
|
134 //freezeFormButtons(form.attr('id')); |
|
135 form.find('.validateButton').attr('disabled', 'disabled'); |
|
136 form.find('input[type=text]').keyup(function() { |
|
137 checkValues(form); |
|
138 }); |
|
139 form.find('input[type=radio]').change(function() { |
|
140 checkValues(form); |
|
141 }); |
|
142 form.find('select').change(function() { |
|
143 checkValues(form); |
|
144 }); |
|
145 setCurrentValues(form); |
|
146 }); |
|
147 } |
|
148 |
|
149 $(document).ready(function() { |
|
150 initEvents(); |
|
151 }); |
|
152 |
|