Révision a45e4bc1
Ajouté par Assos Assos il y a environ 9 ans
drupal7/sites/all/modules/webform/js/webform.js | ||
---|---|---|
1 |
|
|
2 | 1 |
/** |
2 |
* @file |
|
3 | 3 |
* JavaScript behaviors for the front-end display of webforms. |
4 | 4 |
*/ |
5 | 5 |
|
6 | 6 |
(function ($) { |
7 | 7 |
|
8 |
Drupal.behaviors.webform = Drupal.behaviors.webform || {}; |
|
9 |
|
|
10 |
Drupal.behaviors.webform.attach = function(context) { |
|
11 |
// Calendar datepicker behavior. |
|
12 |
Drupal.webform.datepicker(context); |
|
13 |
}; |
|
8 |
"use strict"; |
|
14 | 9 |
|
15 |
Drupal.webform = Drupal.webform || {};
|
|
10 |
Drupal.behaviors.webform = Drupal.behaviors.webform || {};
|
|
16 | 11 |
|
17 |
Drupal.webform.datepicker = function(context) { |
|
18 |
$('div.webform-datepicker').each(function() { |
|
19 |
var $webformDatepicker = $(this); |
|
20 |
var $calendar = $webformDatepicker.find('input.webform-calendar'); |
|
12 |
Drupal.behaviors.webform.attach = function (context) { |
|
13 |
// Calendar datepicker behavior. |
|
14 |
Drupal.webform.datepicker(context); |
|
21 | 15 |
|
22 |
// Ensure the page we're on actually contains a datepicker.
|
|
23 |
if ($calendar.length == 0) {
|
|
24 |
return;
|
|
16 |
// Conditional logic.
|
|
17 |
if (Drupal.settings.webform && Drupal.settings.webform.conditionals) {
|
|
18 |
Drupal.webform.conditional(context);
|
|
25 | 19 |
} |
20 |
}; |
|
21 |
|
|
22 |
Drupal.webform = Drupal.webform || {}; |
|
23 |
|
|
24 |
Drupal.webform.datepicker = function (context) { |
|
25 |
$('div.webform-datepicker').each(function () { |
|
26 |
var $webformDatepicker = $(this); |
|
27 |
var $calendar = $webformDatepicker.find('input.webform-calendar'); |
|
28 |
|
|
29 |
// Ensure the page we're on actually contains a datepicker. |
|
30 |
if ($calendar.length == 0) { |
|
31 |
return; |
|
32 |
} |
|
33 |
|
|
34 |
var startDate = $calendar[0].className.replace(/.*webform-calendar-start-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); |
|
35 |
var endDate = $calendar[0].className.replace(/.*webform-calendar-end-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); |
|
36 |
var firstDay = $calendar[0].className.replace(/.*webform-calendar-day-(\d).*/, '$1'); |
|
37 |
// Convert date strings into actual Date objects. |
|
38 |
startDate = new Date(startDate[0], startDate[1] - 1, startDate[2]); |
|
39 |
endDate = new Date(endDate[0], endDate[1] - 1, endDate[2]); |
|
40 |
|
|
41 |
// Ensure that start comes before end for datepicker. |
|
42 |
if (startDate > endDate) { |
|
43 |
var laterDate = startDate; |
|
44 |
startDate = endDate; |
|
45 |
endDate = laterDate; |
|
46 |
} |
|
47 |
|
|
48 |
var startYear = startDate.getFullYear(); |
|
49 |
var endYear = endDate.getFullYear(); |
|
50 |
|
|
51 |
// Set up the jQuery datepicker element. |
|
52 |
$calendar.datepicker({ |
|
53 |
dateFormat: 'yy-mm-dd', |
|
54 |
yearRange: startYear + ':' + endYear, |
|
55 |
firstDay: parseInt(firstDay), |
|
56 |
minDate: startDate, |
|
57 |
maxDate: endDate, |
|
58 |
onSelect: function (dateText, inst) { |
|
59 |
var date = dateText.split('-'); |
|
60 |
$webformDatepicker.find('select.year, input.year').val(+date[0]).trigger('change'); |
|
61 |
$webformDatepicker.find('select.month').val(+date[1]).trigger('change'); |
|
62 |
$webformDatepicker.find('select.day').val(+date[2]).trigger('change'); |
|
63 |
}, |
|
64 |
beforeShow: function (input, inst) { |
|
65 |
// Get the select list values. |
|
66 |
var year = $webformDatepicker.find('select.year, input.year').val(); |
|
67 |
var month = $webformDatepicker.find('select.month').val(); |
|
68 |
var day = $webformDatepicker.find('select.day').val(); |
|
69 |
|
|
70 |
// If empty, default to the current year/month/day in the popup. |
|
71 |
var today = new Date(); |
|
72 |
year = year ? year : today.getFullYear(); |
|
73 |
month = month ? month : today.getMonth() + 1; |
|
74 |
day = day ? day : today.getDate(); |
|
75 |
|
|
76 |
// Make sure that the default year fits in the available options. |
|
77 |
year = (year < startYear || year > endYear) ? startYear : year; |
|
26 | 78 |
|
27 |
var startDate = $calendar[0].className.replace(/.*webform-calendar-start-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); |
|
28 |
var endDate = $calendar[0].className.replace(/.*webform-calendar-end-(\d{4}-\d{2}-\d{2}).*/, '$1').split('-'); |
|
29 |
var firstDay = $calendar[0].className.replace(/.*webform-calendar-day-(\d).*/, '$1'); |
|
30 |
// Convert date strings into actual Date objects. |
|
31 |
startDate = new Date(startDate[0], startDate[1] - 1, startDate[2]); |
|
32 |
endDate = new Date(endDate[0], endDate[1] - 1, endDate[2]); |
|
33 |
|
|
34 |
// Ensure that start comes before end for datepicker. |
|
35 |
if (startDate > endDate) { |
|
36 |
var laterDate = startDate; |
|
37 |
startDate = endDate; |
|
38 |
endDate = laterDate; |
|
79 |
// jQuery UI Datepicker will read the input field and base its date off |
|
80 |
// of that, even though in our case the input field is a button. |
|
81 |
$(input).val(year + '-' + month + '-' + day); |
|
82 |
} |
|
83 |
}); |
|
84 |
|
|
85 |
// Prevent the calendar button from submitting the form. |
|
86 |
$calendar.click(function (event) { |
|
87 |
$(this).focus(); |
|
88 |
event.preventDefault(); |
|
89 |
}); |
|
90 |
}); |
|
91 |
}; |
|
92 |
|
|
93 |
Drupal.webform.conditional = function (context) { |
|
94 |
// Add the bindings to each webform on the page. |
|
95 |
$.each(Drupal.settings.webform.conditionals, function (formKey, settings) { |
|
96 |
var $form = $('.' + formKey + ':not(.webform-conditional-processed)'); |
|
97 |
$form.each(function (index, currentForm) { |
|
98 |
var $currentForm = $(currentForm); |
|
99 |
$currentForm.addClass('webform-conditional-processed'); |
|
100 |
$currentForm.bind('change', {'settings': settings}, Drupal.webform.conditionalCheck); |
|
101 |
|
|
102 |
// Trigger all the elements that cause conditionals on this form. |
|
103 |
Drupal.webform.doConditions($form, settings); |
|
104 |
}); |
|
105 |
}); |
|
106 |
}; |
|
107 |
|
|
108 |
/** |
|
109 |
* Event handler to respond to field changes in a form. |
|
110 |
* |
|
111 |
* This event is bound to the entire form, not individual fields. |
|
112 |
*/ |
|
113 |
Drupal.webform.conditionalCheck = function (e) { |
|
114 |
var $triggerElement = $(e.target).closest('.webform-component'); |
|
115 |
var $form = $triggerElement.closest('form'); |
|
116 |
var triggerElementKey = $triggerElement.attr('class').match(/webform-component--[^ ]+/)[0]; |
|
117 |
var settings = e.data.settings; |
|
118 |
if (settings.sourceMap[triggerElementKey]) { |
|
119 |
Drupal.webform.doConditions($form, settings); |
|
39 | 120 |
} |
121 |
}; |
|
122 |
|
|
123 |
/** |
|
124 |
* Processes all conditional. |
|
125 |
*/ |
|
126 |
Drupal.webform.doConditions = function ($form, settings) { |
|
127 |
// Track what has be set/shown for each target component. |
|
128 |
var targetLocked = []; |
|
129 |
|
|
130 |
$.each(settings.ruleGroups, function (rgid_key, rule_group) { |
|
131 |
var ruleGroup = settings.ruleGroups[rgid_key]; |
|
132 |
|
|
133 |
// Perform the comparison callback and build the results for this group. |
|
134 |
var conditionalResult = true; |
|
135 |
var conditionalResults = []; |
|
136 |
$.each(ruleGroup['rules'], function (m, rule) { |
|
137 |
var elementKey = rule['source']; |
|
138 |
var element = $form.find('.' + elementKey)[0]; |
|
139 |
var existingValue = settings.values[elementKey] ? settings.values[elementKey] : null; |
|
140 |
conditionalResults.push(window['Drupal']['webform'][rule.callback](element, existingValue, rule['value'])); |
|
141 |
}); |
|
142 |
|
|
143 |
// Filter out false values. |
|
144 |
var filteredResults = []; |
|
145 |
for (var i = 0; i < conditionalResults.length; i++) { |
|
146 |
if (conditionalResults[i]) { |
|
147 |
filteredResults.push(conditionalResults[i]); |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
// Calculate the and/or result. |
|
152 |
if (ruleGroup['andor'] === 'or') { |
|
153 |
conditionalResult = filteredResults.length > 0; |
|
154 |
} |
|
155 |
else { |
|
156 |
conditionalResult = filteredResults.length === conditionalResults.length; |
|
157 |
} |
|
158 |
|
|
159 |
$.each(ruleGroup['actions'], function (aid, action) { |
|
160 |
var $target = $form.find('.' + action['target']); |
|
161 |
var actionResult = action['invert'] ? !conditionalResult : conditionalResult; |
|
162 |
switch (action['action']) { |
|
163 |
case 'show': |
|
164 |
if (actionResult != Drupal.webform.isVisible($target)) { |
|
165 |
var $targetElements = actionResult |
|
166 |
? $target.find('.webform-conditional-disabled').removeClass('webform-conditional-disabled') |
|
167 |
: $target.find(':input').addClass('webform-conditional-disabled'); |
|
168 |
$targetElements.webformProp('disabled', !actionResult); |
|
169 |
$target.toggleClass('webform-conditional-hidden', !actionResult); |
|
170 |
if (actionResult) { |
|
171 |
$target.show(); |
|
172 |
} |
|
173 |
else { |
|
174 |
$target.hide(); |
|
175 |
// Record that the target was hidden. |
|
176 |
targetLocked[action['target']] = 'hide'; |
|
177 |
} |
|
178 |
} |
|
179 |
break; |
|
180 |
case 'require': |
|
181 |
var $requiredSpan = $target.find('.form-required, .form-optional').first(); |
|
182 |
if (actionResult != $requiredSpan.hasClass('form-required')) { |
|
183 |
var $targetInputElements = $target.find("input:text,textarea,input[type='email'],select,input:radio,input:file"); |
|
184 |
// Rather than hide the required tag, remove it so that other jQuery can respond via Drupal behaviors. |
|
185 |
Drupal.detachBehaviors($requiredSpan); |
|
186 |
$targetInputElements |
|
187 |
.webformProp('required', actionResult) |
|
188 |
.toggleClass('required', actionResult); |
|
189 |
if (actionResult) { |
|
190 |
$requiredSpan.replaceWith('<span class="form-required" title="' + Drupal.t('This field is required.') + '">*</span>'); |
|
191 |
} |
|
192 |
else { |
|
193 |
$requiredSpan.replaceWith('<span class="form-optional"></span>'); |
|
194 |
} |
|
195 |
Drupal.attachBehaviors($requiredSpan); |
|
196 |
} |
|
197 |
break; |
|
198 |
case 'set': |
|
199 |
var isLocked = targetLocked[action['target']]; |
|
200 |
var $texts = $target.find("input:text,textarea,input[type='email']"); |
|
201 |
var $selects = $target.find('select,select option,input:radio,input:checkbox'); |
|
202 |
var $markups = $target.filter('.webform-component-markup'); |
|
203 |
if (actionResult) { |
|
204 |
var multiple = $.map(action['argument'].split(','), $.trim); |
|
205 |
$selects.webformVal(multiple); |
|
206 |
$texts.val([action['argument']]); |
|
207 |
// A special case is made for markup. It is sanitized with filter_xss_admin on the server. |
|
208 |
// otherwise text() should be used to avoid an XSS vulnerability. text() however would |
|
209 |
// preclude the use of tags like <strong> or <a> |
|
210 |
$markups.html(action['argument']); |
|
211 |
} |
|
212 |
else { |
|
213 |
// Markup not set? Then restore original markup as provided in |
|
214 |
// the attribute data-webform-markup. |
|
215 |
$markups.each(function() { |
|
216 |
var $this = $(this); |
|
217 |
var original = $this.data('webform-markup'); |
|
218 |
if (original !== undefined) { |
|
219 |
$this.html(original); |
|
220 |
} |
|
221 |
}); |
|
222 |
} |
|
223 |
if (!isLocked) { |
|
224 |
// If not previously hidden or set, disable the element readonly or readonly-like behavior. |
|
225 |
$selects.webformProp('disabled', actionResult); |
|
226 |
$texts.webformProp('readonly', actionResult); |
|
227 |
targetLocked[action['target']] = actionResult ? 'set' : false; |
|
228 |
} |
|
229 |
break; |
|
230 |
} |
|
231 |
}); // End look on each action for one conditional |
|
232 |
}); // End loop on each conditional |
|
233 |
}; |
|
234 |
|
|
235 |
/** |
|
236 |
* Event handler to prevent propogation of events, typically click for disabling |
|
237 |
* radio and checkboxes. |
|
238 |
*/ |
|
239 |
Drupal.webform.stopEvent = function () { |
|
240 |
return false; |
|
241 |
}; |
|
242 |
|
|
243 |
Drupal.webform.conditionalOperatorStringEqual = function (element, existingValue, ruleValue) { |
|
244 |
var returnValue = false; |
|
245 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
246 |
$.each(currentValue, function (n, value) { |
|
247 |
if (value.toLowerCase() === ruleValue.toLowerCase()) { |
|
248 |
returnValue = true; |
|
249 |
return false; // break. |
|
250 |
} |
|
251 |
}); |
|
252 |
return returnValue; |
|
253 |
}; |
|
254 |
|
|
255 |
Drupal.webform.conditionalOperatorStringNotEqual = function (element, existingValue, ruleValue) { |
|
256 |
var found = false; |
|
257 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
258 |
$.each(currentValue, function (n, value) { |
|
259 |
if (value.toLowerCase() === ruleValue.toLowerCase()) { |
|
260 |
found = true; |
|
261 |
} |
|
262 |
}); |
|
263 |
return !found; |
|
264 |
}; |
|
265 |
|
|
266 |
Drupal.webform.conditionalOperatorStringContains = function (element, existingValue, ruleValue) { |
|
267 |
var returnValue = false; |
|
268 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
269 |
$.each(currentValue, function (n, value) { |
|
270 |
if (value.toLowerCase().indexOf(ruleValue.toLowerCase()) > -1) { |
|
271 |
returnValue = true; |
|
272 |
return false; // break. |
|
273 |
} |
|
274 |
}); |
|
275 |
return returnValue; |
|
276 |
}; |
|
40 | 277 |
|
41 |
var startYear = startDate.getFullYear(); |
|
42 |
var endYear = endDate.getFullYear(); |
|
43 |
|
|
44 |
// Set up the jQuery datepicker element. |
|
45 |
$calendar.datepicker({ |
|
46 |
dateFormat: 'yy-mm-dd', |
|
47 |
yearRange: startYear + ':' + endYear, |
|
48 |
firstDay: parseInt(firstDay), |
|
49 |
minDate: startDate, |
|
50 |
maxDate: endDate, |
|
51 |
onSelect: function(dateText, inst) { |
|
52 |
var date = dateText.split('-'); |
|
53 |
$webformDatepicker.find('select.year, input.year').val(+date[0]).trigger('change'); |
|
54 |
$webformDatepicker.find('select.month').val(+date[1]).trigger('change'); |
|
55 |
$webformDatepicker.find('select.day').val(+date[2]).trigger('change'); |
|
56 |
}, |
|
57 |
beforeShow: function(input, inst) { |
|
58 |
// Get the select list values. |
|
59 |
var year = $webformDatepicker.find('select.year, input.year').val(); |
|
60 |
var month = $webformDatepicker.find('select.month').val(); |
|
61 |
var day = $webformDatepicker.find('select.day').val(); |
|
62 |
|
|
63 |
// If empty, default to the current year/month/day in the popup. |
|
64 |
var today = new Date(); |
|
65 |
year = year ? year : today.getFullYear(); |
|
66 |
month = month ? month : today.getMonth() + 1; |
|
67 |
day = day ? day : today.getDate(); |
|
68 |
|
|
69 |
// Make sure that the default year fits in the available options. |
|
70 |
year = (year < startYear || year > endYear) ? startYear : year; |
|
71 |
|
|
72 |
// jQuery UI Datepicker will read the input field and base its date off |
|
73 |
// of that, even though in our case the input field is a button. |
|
74 |
$(input).val(year + '-' + month + '-' + day); |
|
278 |
Drupal.webform.conditionalOperatorStringDoesNotContain = function (element, existingValue, ruleValue) { |
|
279 |
var found = false; |
|
280 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
281 |
$.each(currentValue, function (n, value) { |
|
282 |
if (value.toLowerCase().indexOf(ruleValue.toLowerCase()) > -1) { |
|
283 |
found = true; |
|
75 | 284 |
} |
76 | 285 |
}); |
286 |
return !found; |
|
287 |
}; |
|
288 |
|
|
289 |
Drupal.webform.conditionalOperatorStringBeginsWith = function (element, existingValue, ruleValue) { |
|
290 |
var returnValue = false; |
|
291 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
292 |
$.each(currentValue, function (n, value) { |
|
293 |
if (value.toLowerCase().indexOf(ruleValue.toLowerCase()) === 0) { |
|
294 |
returnValue = true; |
|
295 |
return false; // break. |
|
296 |
} |
|
297 |
}); |
|
298 |
return returnValue; |
|
299 |
}; |
|
300 |
|
|
301 |
Drupal.webform.conditionalOperatorStringEndsWith = function (element, existingValue, ruleValue) { |
|
302 |
var returnValue = false; |
|
303 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
304 |
$.each(currentValue, function (n, value) { |
|
305 |
if (value.toLowerCase().lastIndexOf(ruleValue.toLowerCase()) === value.length - ruleValue.length) { |
|
306 |
returnValue = true; |
|
307 |
return false; // break. |
|
308 |
} |
|
309 |
}); |
|
310 |
return returnValue; |
|
311 |
}; |
|
312 |
|
|
313 |
Drupal.webform.conditionalOperatorStringEmpty = function (element, existingValue, ruleValue) { |
|
314 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
315 |
var returnValue = true; |
|
316 |
$.each(currentValue, function (n, value) { |
|
317 |
if (value !== '') { |
|
318 |
returnValue = false; |
|
319 |
return false; // break. |
|
320 |
} |
|
321 |
}); |
|
322 |
return returnValue; |
|
323 |
}; |
|
324 |
|
|
325 |
Drupal.webform.conditionalOperatorStringNotEmpty = function (element, existingValue, ruleValue) { |
|
326 |
return !Drupal.webform.conditionalOperatorStringEmpty(element, existingValue, ruleValue); |
|
327 |
}; |
|
328 |
|
|
329 |
Drupal.webform.conditionalOperatorSelectGreaterThan = function (element, existingValue, ruleValue) { |
|
330 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
331 |
return Drupal.webform.compare_select(currentValue[0], ruleValue, element) > 0; |
|
332 |
}; |
|
333 |
|
|
334 |
Drupal.webform.conditionalOperatorSelectGreaterThanEqual = function (element, existingValue, ruleValue) { |
|
335 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
336 |
var comparison = Drupal.webform.compare_select(currentValue[0], ruleValue, element); |
|
337 |
return comparison > 0 || comparison === 0; |
|
338 |
}; |
|
77 | 339 |
|
78 |
// Prevent the calendar button from submitting the form. |
|
79 |
$calendar.click(function(event) { |
|
80 |
$(this).focus(); |
|
81 |
event.preventDefault(); |
|
340 |
Drupal.webform.conditionalOperatorSelectLessThan = function (element, existingValue, ruleValue) { |
|
341 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
342 |
return Drupal.webform.compare_select(currentValue[0], ruleValue, element) < 0; |
|
343 |
}; |
|
344 |
|
|
345 |
Drupal.webform.conditionalOperatorSelectLessThanEqual = function (element, existingValue, ruleValue) { |
|
346 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
347 |
var comparison = Drupal.webform.compare_select(currentValue[0], ruleValue, element); |
|
348 |
return comparison < 0 || comparison === 0; |
|
349 |
}; |
|
350 |
|
|
351 |
Drupal.webform.conditionalOperatorNumericEqual = function (element, existingValue, ruleValue) { |
|
352 |
// See float comparison: http://php.net/manual/en/language.types.float.php |
|
353 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
354 |
var epsilon = 0.000001; |
|
355 |
// An empty string does not match any number. |
|
356 |
return currentValue[0] === '' ? false : (Math.abs(parseFloat(currentValue[0]) - parseFloat(ruleValue)) < epsilon); |
|
357 |
}; |
|
358 |
|
|
359 |
Drupal.webform.conditionalOperatorNumericNotEqual = function (element, existingValue, ruleValue) { |
|
360 |
// See float comparison: http://php.net/manual/en/language.types.float.php |
|
361 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
362 |
var epsilon = 0.000001; |
|
363 |
// An empty string does not match any number. |
|
364 |
return currentValue[0] === '' ? true : (Math.abs(parseFloat(currentValue[0]) - parseFloat(ruleValue)) >= epsilon); |
|
365 |
}; |
|
366 |
|
|
367 |
Drupal.webform.conditionalOperatorNumericGreaterThan = function (element, existingValue, ruleValue) { |
|
368 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
369 |
return parseFloat(currentValue[0]) > parseFloat(ruleValue); |
|
370 |
}; |
|
371 |
|
|
372 |
Drupal.webform.conditionalOperatorNumericGreaterThanEqual = function (element, existingValue, ruleValue) { |
|
373 |
return Drupal.webform.conditionalOperatorNumericGreaterThan(element, existingValue, ruleValue) || |
|
374 |
Drupal.webform.conditionalOperatorNumericEqual(element, existingValue, ruleValue); |
|
375 |
}; |
|
376 |
|
|
377 |
Drupal.webform.conditionalOperatorNumericLessThan = function (element, existingValue, ruleValue) { |
|
378 |
var currentValue = Drupal.webform.stringValue(element, existingValue); |
|
379 |
return parseFloat(currentValue[0]) < parseFloat(ruleValue); |
|
380 |
}; |
|
381 |
|
|
382 |
Drupal.webform.conditionalOperatorNumericLessThanEqual = function (element, existingValue, ruleValue) { |
|
383 |
return Drupal.webform.conditionalOperatorNumericLessThan(element, existingValue, ruleValue) || |
|
384 |
Drupal.webform.conditionalOperatorNumericEqual(element, existingValue, ruleValue); |
|
385 |
}; |
|
386 |
|
|
387 |
Drupal.webform.conditionalOperatorDateEqual = function (element, existingValue, ruleValue) { |
|
388 |
var currentValue = Drupal.webform.dateValue(element, existingValue); |
|
389 |
return currentValue === ruleValue; |
|
390 |
}; |
|
391 |
|
|
392 |
Drupal.webform.conditionalOperatorDateNotEqual = function (element, existingValue, ruleValue) { |
|
393 |
return !Drupal.webform.conditionalOperatorDateEqual(element, existingValue, ruleValue); |
|
394 |
}; |
|
395 |
|
|
396 |
Drupal.webform.conditionalOperatorDateBefore = function (element, existingValue, ruleValue) { |
|
397 |
var currentValue = Drupal.webform.dateValue(element, existingValue); |
|
398 |
return (currentValue !== false) && currentValue < ruleValue; |
|
399 |
}; |
|
400 |
|
|
401 |
Drupal.webform.conditionalOperatorDateBeforeEqual = function (element, existingValue, ruleValue) { |
|
402 |
var currentValue = Drupal.webform.dateValue(element, existingValue); |
|
403 |
return (currentValue !== false) && (currentValue < ruleValue || currentValue === ruleValue); |
|
404 |
}; |
|
405 |
|
|
406 |
Drupal.webform.conditionalOperatorDateAfter = function (element, existingValue, ruleValue) { |
|
407 |
var currentValue = Drupal.webform.dateValue(element, existingValue); |
|
408 |
return (currentValue !== false) && currentValue > ruleValue; |
|
409 |
}; |
|
410 |
|
|
411 |
Drupal.webform.conditionalOperatorDateAfterEqual = function (element, existingValue, ruleValue) { |
|
412 |
var currentValue = Drupal.webform.dateValue(element, existingValue); |
|
413 |
return (currentValue !== false) && (currentValue > ruleValue || currentValue === ruleValue); |
|
414 |
}; |
|
415 |
|
|
416 |
Drupal.webform.conditionalOperatorTimeEqual = function (element, existingValue, ruleValue) { |
|
417 |
var currentValue = Drupal.webform.timeValue(element, existingValue); |
|
418 |
return currentValue === ruleValue; |
|
419 |
}; |
|
420 |
|
|
421 |
Drupal.webform.conditionalOperatorTimeNotEqual = function (element, existingValue, ruleValue) { |
|
422 |
return !Drupal.webform.conditionalOperatorTimeEqual(element, existingValue, ruleValue); |
|
423 |
}; |
|
424 |
|
|
425 |
Drupal.webform.conditionalOperatorTimeBefore = function (element, existingValue, ruleValue) { |
|
426 |
// Date and time operators intentionally exclusive for "before". |
|
427 |
var currentValue = Drupal.webform.timeValue(element, existingValue); |
|
428 |
return (currentValue !== false) && (currentValue < ruleValue); |
|
429 |
}; |
|
430 |
|
|
431 |
Drupal.webform.conditionalOperatorTimeBeforeEqual = function (element, existingValue, ruleValue) { |
|
432 |
// Date and time operators intentionally exclusive for "before". |
|
433 |
var currentValue = Drupal.webform.timeValue(element, existingValue); |
|
434 |
return (currentValue !== false) && (currentValue < ruleValue || currentValue === ruleValue); |
|
435 |
}; |
|
436 |
|
|
437 |
Drupal.webform.conditionalOperatorTimeAfter = function (element, existingValue, ruleValue) { |
|
438 |
// Date and time operators intentionally inclusive for "after". |
|
439 |
var currentValue = Drupal.webform.timeValue(element, existingValue); |
|
440 |
return (currentValue !== false) && (currentValue > ruleValue); |
|
441 |
}; |
|
442 |
|
|
443 |
Drupal.webform.conditionalOperatorTimeAfterEqual = function (element, existingValue, ruleValue) { |
|
444 |
// Date and time operators intentionally inclusive for "after". |
|
445 |
var currentValue = Drupal.webform.timeValue(element, existingValue); |
|
446 |
return (currentValue !== false) && (currentValue > ruleValue || currentValue === ruleValue); |
|
447 |
}; |
|
448 |
|
|
449 |
/** |
|
450 |
* Utility function to compare values of a select component. |
|
451 |
* @param string a |
|
452 |
* First select option key to compare |
|
453 |
* @param string b |
|
454 |
* Second select option key to compare |
|
455 |
* @param array options |
|
456 |
* Associative array where the a and b are within the keys |
|
457 |
* @return integer based upon position of $a and $b in $options |
|
458 |
* -N if $a above (<) $b |
|
459 |
* 0 if $a = $b |
|
460 |
* +N if $a is below (>) $b |
|
461 |
*/ |
|
462 |
Drupal.webform.compare_select = function (a, b, element) { |
|
463 |
var optionList = []; |
|
464 |
$('option,input:radio,input:checkbox', element).each(function () { |
|
465 |
optionList.push($(this).val()); |
|
466 |
}); |
|
467 |
var a_position = optionList.indexOf(a); |
|
468 |
var b_position = optionList.indexOf(b); |
|
469 |
return (a_position < 0 || b_position < 0) ? null : a_position - b_position; |
|
470 |
}; |
|
471 |
|
|
472 |
/** |
|
473 |
* Utility to return current visibility. Uses actual visibility, except for |
|
474 |
* hidden components which use the applied disabled class. |
|
475 |
*/ |
|
476 |
Drupal.webform.isVisible = function ($element) { |
|
477 |
return $element.hasClass('webform-component-hidden') |
|
478 |
? !$element.find('input').first().hasClass('webform-conditional-disabled') |
|
479 |
: $element.closest('.webform-conditional-hidden').length == 0; |
|
480 |
}; |
|
481 |
|
|
482 |
/** |
|
483 |
* Utility function to get a string value from a select/radios/text/etc. field. |
|
484 |
*/ |
|
485 |
Drupal.webform.stringValue = function (element, existingValue) { |
|
486 |
var value = []; |
|
487 |
if (element) { |
|
488 |
var $element = $(element); |
|
489 |
if (Drupal.webform.isVisible($element)) { |
|
490 |
// Checkboxes and radios. |
|
491 |
$element.find('input[type=checkbox]:checked,input[type=radio]:checked').each(function () { |
|
492 |
value.push(this.value); |
|
493 |
}); |
|
494 |
// Select lists. |
|
495 |
if (!value.length) { |
|
496 |
var selectValue = $element.find('select').val(); |
|
497 |
if (selectValue) { |
|
498 |
if ($.isArray(selectValue)) { |
|
499 |
value = selectValue; |
|
500 |
} |
|
501 |
else { |
|
502 |
value.push(selectValue); |
|
503 |
} |
|
504 |
} |
|
505 |
} |
|
506 |
// Simple text fields. This check is done last so that the select list in |
|
507 |
// select-or-other fields comes before the "other" text field. |
|
508 |
if (!value.length) { |
|
509 |
$element.find('input:not([type=checkbox],[type=radio]),textarea').each(function () { |
|
510 |
value.push(this.value); |
|
511 |
}); |
|
512 |
} |
|
513 |
} |
|
514 |
} |
|
515 |
else { |
|
516 |
switch ($.type(existingValue)) { |
|
517 |
case 'array': |
|
518 |
value = existingValue; |
|
519 |
break; |
|
520 |
case 'string': |
|
521 |
value.push(existingValue); |
|
522 |
break; |
|
523 |
} |
|
524 |
} |
|
525 |
return value; |
|
526 |
}; |
|
527 |
|
|
528 |
/** |
|
529 |
* Utility function to calculate a millisecond timestamp from a time field. |
|
530 |
*/ |
|
531 |
Drupal.webform.dateValue = function (element, existingValue) { |
|
532 |
var value = false; |
|
533 |
if (element) { |
|
534 |
var $element = $(element); |
|
535 |
if (Drupal.webform.isVisible($element)) { |
|
536 |
var day = $element.find('[name*=day]').val(); |
|
537 |
var month = $element.find('[name*=month]').val(); |
|
538 |
var year = $element.find('[name*=year]').val(); |
|
539 |
// Months are 0 indexed in JavaScript. |
|
540 |
if (month) { |
|
541 |
month--; |
|
542 |
} |
|
543 |
if (year !== '' && month !== '' && day !== '') { |
|
544 |
value = Date.UTC(year, month, day) / 1000; |
|
545 |
} |
|
546 |
} |
|
547 |
} |
|
548 |
else { |
|
549 |
if ($.type(existingValue) === 'array' && existingValue.length) { |
|
550 |
existingValue = existingValue[0]; |
|
551 |
} |
|
552 |
if ($.type(existingValue) === 'string') { |
|
553 |
existingValue = existingValue.split('-'); |
|
554 |
} |
|
555 |
if (existingValue.length === 3) { |
|
556 |
value = Date.UTC(existingValue[0], existingValue[1], existingValue[2]) / 1000; |
|
557 |
} |
|
558 |
} |
|
559 |
return value; |
|
560 |
}; |
|
561 |
|
|
562 |
/** |
|
563 |
* Utility function to calculate a millisecond timestamp from a time field. |
|
564 |
*/ |
|
565 |
Drupal.webform.timeValue = function (element, existingValue) { |
|
566 |
var value = false; |
|
567 |
if (element) { |
|
568 |
var $element = $(element); |
|
569 |
if (Drupal.webform.isVisible($element)) { |
|
570 |
var hour = $element.find('[name*=hour]').val(); |
|
571 |
var minute = $element.find('[name*=minute]').val(); |
|
572 |
var ampm = $element.find('[name*=ampm]:checked').val(); |
|
573 |
|
|
574 |
// Convert to integers if set. |
|
575 |
hour = (hour === '') ? hour : parseInt(hour); |
|
576 |
minute = (minute === '') ? minute : parseInt(minute); |
|
577 |
|
|
578 |
if (hour !== '') { |
|
579 |
hour = (hour < 12 && ampm == 'pm') ? hour + 12 : hour; |
|
580 |
hour = (hour === 12 && ampm == 'am') ? 0 : hour; |
|
581 |
} |
|
582 |
if (hour !== '' && minute !== '') { |
|
583 |
value = Date.UTC(1970, 0, 1, hour, minute) / 1000; |
|
584 |
} |
|
585 |
} |
|
586 |
} |
|
587 |
else { |
|
588 |
if ($.type(existingValue) === 'array' && existingValue.length) { |
|
589 |
existingValue = existingValue[0]; |
|
590 |
} |
|
591 |
if ($.type(existingValue) === 'string') { |
|
592 |
existingValue = existingValue.split(':'); |
|
593 |
} |
|
594 |
if (existingValue.length >= 2) { |
|
595 |
value = Date.UTC(1970, 0, 1, existingValue[0], existingValue[1]) / 1000; |
|
596 |
} |
|
597 |
} |
|
598 |
return value; |
|
599 |
}; |
|
600 |
|
|
601 |
/** |
|
602 |
* Make a prop shim for jQuery < 1.9. |
|
603 |
*/ |
|
604 |
$.fn.webformProp = $.fn.webformProp || function (name, value) { |
|
605 |
if (value) { |
|
606 |
return $.fn.prop ? this.prop(name, true) : this.attr(name, true); |
|
607 |
} |
|
608 |
else { |
|
609 |
return $.fn.prop ? this.prop(name, false) : this.removeAttr(name); |
|
610 |
} |
|
611 |
}; |
|
612 |
|
|
613 |
/** |
|
614 |
* Make a multi-valued val() function for setting checkboxes, radios, and select |
|
615 |
* elements. |
|
616 |
*/ |
|
617 |
$.fn.webformVal = function (values) { |
|
618 |
this.each(function () { |
|
619 |
var $this = $(this); |
|
620 |
var value = $this.val(); |
|
621 |
var on = $.inArray($this.val(), values) != -1; |
|
622 |
if (this.nodeName == 'OPTION') { |
|
623 |
$this.webformProp('selected', on ? value : false); |
|
624 |
} |
|
625 |
else { |
|
626 |
$this.val(on ? [value] : false); |
|
627 |
} |
|
82 | 628 |
}); |
83 |
});
|
|
84 |
}
|
|
629 |
return this;
|
|
630 |
};
|
|
85 | 631 |
|
86 | 632 |
})(jQuery); |
Formats disponibles : Unified diff
Update webform to 4.10