dqs-jquery-form-validator-rails 2.2.163 → 2.2.164
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +21 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/LICENSE +24 -0
- data/README.md +451 -0
- data/Rakefile +1 -0
- data/app/assets/javascripts/brazil.dev.js +115 -0
- data/app/assets/javascripts/brazil.js +9 -0
- data/app/assets/javascripts/date.dev.js +81 -0
- data/app/assets/javascripts/date.js +9 -0
- data/app/assets/javascripts/file.dev.js +413 -0
- data/app/assets/javascripts/file.js +9 -0
- data/app/assets/javascripts/html5.dev.js +168 -0
- data/app/assets/javascripts/html5.js +9 -0
- data/app/assets/javascripts/jquery.form-validator.min.js +9 -0
- data/app/assets/javascripts/jsconf.dev.js +55 -0
- data/app/assets/javascripts/jsconf.js +9 -0
- data/app/assets/javascripts/lang/cz.dev.js +65 -0
- data/app/assets/javascripts/lang/cz.js +9 -0
- data/app/assets/javascripts/lang/de.dev.js +65 -0
- data/app/assets/javascripts/lang/de.js +9 -0
- data/app/assets/javascripts/lang/es.dev.js +62 -0
- data/app/assets/javascripts/lang/es.js +9 -0
- data/app/assets/javascripts/lang/fr.dev.js +62 -0
- data/app/assets/javascripts/lang/fr.js +9 -0
- data/app/assets/javascripts/lang/it.dev.js +62 -0
- data/app/assets/javascripts/lang/it.js +9 -0
- data/app/assets/javascripts/lang/pl.dev.js +65 -0
- data/app/assets/javascripts/lang/pl.js +9 -0
- data/app/assets/javascripts/lang/pt.dev.js +65 -0
- data/app/assets/javascripts/lang/pt.js +9 -0
- data/app/assets/javascripts/lang/ro.dev.js +65 -0
- data/app/assets/javascripts/lang/ro.js +9 -0
- data/app/assets/javascripts/lang/ru.dev.js +66 -0
- data/app/assets/javascripts/lang/ru.js +9 -0
- data/app/assets/javascripts/lang/sv.dev.js +63 -0
- data/app/assets/javascripts/lang/sv.js +9 -0
- data/app/assets/javascripts/location.dev.js +78 -0
- data/app/assets/javascripts/location.js +9 -0
- data/app/assets/javascripts/sanitize.dev.js +154 -0
- data/app/assets/javascripts/sanitize.js +9 -0
- data/app/assets/javascripts/security.dev.js +523 -0
- data/app/assets/javascripts/security.js +9 -0
- data/app/assets/javascripts/src/core-validators.js +343 -0
- data/app/assets/javascripts/src/dialogs.js +123 -0
- data/app/assets/javascripts/src/jquery-plugins.js +452 -0
- data/app/assets/javascripts/src/module-loader.js +150 -0
- data/app/assets/javascripts/src/setup.js +168 -0
- data/app/assets/javascripts/src/utils.js +840 -0
- data/app/assets/javascripts/sweden.dev.js +213 -0
- data/app/assets/javascripts/sweden.js +9 -0
- data/app/assets/javascripts/theme-default.css +108 -0
- data/app/assets/javascripts/theme-default.min.css +1 -0
- data/app/assets/javascripts/toggleDisabled.dev.js +67 -0
- data/app/assets/javascripts/toggleDisabled.js +9 -0
- data/app/assets/javascripts/uk.dev.js +85 -0
- data/app/assets/javascripts/uk.js +9 -0
- data/dqs-jquery-form-validator.gemspec +27 -0
- data/lib/dqs-jquery-form-validator-rails.rb +1 -0
- data/lib/dqs/jquery/form/validator/rails.rb +13 -0
- data/lib/dqs/jquery/form/validator/rails/engine.rb +12 -0
- data/lib/dqs/jquery/form/validator/rails/version.rb +11 -0
- metadata +64 -2
@@ -0,0 +1,452 @@
|
|
1
|
+
/**
|
2
|
+
* File declaring all methods if this plugin which is applied to $.fn.
|
3
|
+
*/
|
4
|
+
(function($) {
|
5
|
+
|
6
|
+
var _helpers = 0;
|
7
|
+
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Assigns validateInputOnBlur function to elements blur event
|
11
|
+
*
|
12
|
+
* @param {Object} language Optional, will override $.formUtils.LANG
|
13
|
+
* @param {Object} conf Optional, will override the default settings
|
14
|
+
* @return {jQuery}
|
15
|
+
*/
|
16
|
+
$.fn.validateOnBlur = function (language, conf) {
|
17
|
+
this.find('*[data-validation]')
|
18
|
+
.bind('blur.validation', function () {
|
19
|
+
$(this).validateInputOnBlur(language, conf, true, 'blur');
|
20
|
+
});
|
21
|
+
if (conf.validateCheckboxRadioOnClick) {
|
22
|
+
// bind click event to validate on click for radio & checkboxes for nice UX
|
23
|
+
this.find('input[type=checkbox][data-validation],input[type=radio][data-validation]')
|
24
|
+
.bind('click.validation', function () {
|
25
|
+
$(this).validateInputOnBlur(language, conf, true, 'click');
|
26
|
+
});
|
27
|
+
}
|
28
|
+
|
29
|
+
return this;
|
30
|
+
};
|
31
|
+
|
32
|
+
/*
|
33
|
+
* Assigns validateInputOnBlur function to elements custom event
|
34
|
+
* @param {Object} language Optional, will override $.formUtils.LANG
|
35
|
+
* @param {Object} settings Optional, will override the default settings
|
36
|
+
* * @return {jQuery}
|
37
|
+
*/
|
38
|
+
$.fn.validateOnEvent = function (language, settings) {
|
39
|
+
this.find('*[data-validation-event]')
|
40
|
+
.each(function () {
|
41
|
+
var $el = $(this),
|
42
|
+
etype = $el.valAttr('event');
|
43
|
+
if (etype) {
|
44
|
+
$el
|
45
|
+
.unbind(etype + '.validation')
|
46
|
+
.bind(etype + '.validation', function (evt) {
|
47
|
+
if( (evt || {}).keyCode !== 9 ) {
|
48
|
+
$(this).validateInputOnBlur(language, settings, true, etype);
|
49
|
+
}
|
50
|
+
});
|
51
|
+
}
|
52
|
+
});
|
53
|
+
return this;
|
54
|
+
};
|
55
|
+
|
56
|
+
/**
|
57
|
+
* fade in help message when input gains focus
|
58
|
+
* fade out when input loses focus
|
59
|
+
* <input data-help="The info that I want to display for the user when input is focused" ... />
|
60
|
+
*
|
61
|
+
* @param {String} attrName - Optional, default is data-help
|
62
|
+
* @return {jQuery}
|
63
|
+
*/
|
64
|
+
$.fn.showHelpOnFocus = function (attrName) {
|
65
|
+
if (!attrName) {
|
66
|
+
attrName = 'data-validation-help';
|
67
|
+
}
|
68
|
+
|
69
|
+
// Remove previously added event listeners
|
70
|
+
this.find('.has-help-txt')
|
71
|
+
.valAttr('has-keyup-event', false)
|
72
|
+
.removeClass('has-help-txt');
|
73
|
+
|
74
|
+
// Add help text listeners
|
75
|
+
this.find('textarea,input').each(function () {
|
76
|
+
var $elem = $(this),
|
77
|
+
className = 'jquery_form_help_' + (++_helpers),
|
78
|
+
help = $elem.attr(attrName);
|
79
|
+
|
80
|
+
if (help) {
|
81
|
+
$elem
|
82
|
+
.addClass('has-help-txt')
|
83
|
+
.unbind('focus.help')
|
84
|
+
.bind('focus.help', function () {
|
85
|
+
var $help = $elem.parent().find('.' + className);
|
86
|
+
if ($help.length === 0) {
|
87
|
+
$help = $('<span />')
|
88
|
+
.addClass(className)
|
89
|
+
.addClass('help')
|
90
|
+
.addClass('help-block') // twitter bs
|
91
|
+
.text(help)
|
92
|
+
.hide();
|
93
|
+
|
94
|
+
$elem.after($help);
|
95
|
+
}
|
96
|
+
$help.fadeIn();
|
97
|
+
})
|
98
|
+
.unbind('blur.help')
|
99
|
+
.bind('blur.help', function () {
|
100
|
+
$(this)
|
101
|
+
.parent()
|
102
|
+
.find('.' + className)
|
103
|
+
.fadeOut('slow');
|
104
|
+
});
|
105
|
+
}
|
106
|
+
});
|
107
|
+
|
108
|
+
return this;
|
109
|
+
};
|
110
|
+
|
111
|
+
/**
|
112
|
+
* @param {Function} cb
|
113
|
+
* @param {Object} [conf]
|
114
|
+
* @param {Object} [lang]
|
115
|
+
*/
|
116
|
+
$.fn.validate = function(cb, conf, lang) {
|
117
|
+
var language = $.extend({}, $.formUtils.LANG, lang || {});
|
118
|
+
this.each(function() {
|
119
|
+
var $elem = $(this),
|
120
|
+
formDefaultConfig = $elem.closest('form').get(0).validationConfig || {};
|
121
|
+
|
122
|
+
$elem.one('validation', function(evt, isValid) {
|
123
|
+
if ( typeof cb === 'function' ) {
|
124
|
+
cb(isValid, this, evt);
|
125
|
+
}
|
126
|
+
});
|
127
|
+
|
128
|
+
$elem.validateInputOnBlur(
|
129
|
+
language,
|
130
|
+
$.extend({}, formDefaultConfig, conf || {}),
|
131
|
+
true
|
132
|
+
);
|
133
|
+
});
|
134
|
+
};
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Tells whether or not validation of this input will have to postpone the form submit ()
|
138
|
+
* @returns {Boolean}
|
139
|
+
*/
|
140
|
+
$.fn.willPostponeValidation = function() {
|
141
|
+
return (this.valAttr('suggestion-nr') ||
|
142
|
+
this.valAttr('postpone') ||
|
143
|
+
this.hasClass('hasDatepicker')) &&
|
144
|
+
!window.postponedValidation;
|
145
|
+
};
|
146
|
+
|
147
|
+
/**
|
148
|
+
* Validate single input when it loses focus
|
149
|
+
* shows error message in a span element
|
150
|
+
* that is appended to the parent element
|
151
|
+
*
|
152
|
+
* @param {Object} [language] Optional, will override $.formUtils.LANG
|
153
|
+
* @param {Object} [conf] Optional, will override the default settings
|
154
|
+
* @param {Boolean} attachKeyupEvent Optional
|
155
|
+
* @param {String} eventType
|
156
|
+
* @return {jQuery}
|
157
|
+
*/
|
158
|
+
$.fn.validateInputOnBlur = function (language, conf, attachKeyupEvent, eventType) {
|
159
|
+
|
160
|
+
$.formUtils.eventType = eventType;
|
161
|
+
|
162
|
+
if ( this.willPostponeValidation() ) {
|
163
|
+
// This validation has to be postponed
|
164
|
+
var _self = this,
|
165
|
+
postponeTime = this.valAttr('postpone') || 200;
|
166
|
+
|
167
|
+
window.postponedValidation = function () {
|
168
|
+
_self.validateInputOnBlur(language, conf, attachKeyupEvent, eventType);
|
169
|
+
window.postponedValidation = false;
|
170
|
+
};
|
171
|
+
|
172
|
+
setTimeout(function () {
|
173
|
+
if (window.postponedValidation) {
|
174
|
+
window.postponedValidation();
|
175
|
+
}
|
176
|
+
}, postponeTime);
|
177
|
+
|
178
|
+
return this;
|
179
|
+
}
|
180
|
+
|
181
|
+
language = $.extend({}, $.formUtils.LANG, language || {});
|
182
|
+
|
183
|
+
$.formUtils.errorDialogs.removeErrorStyling(this, conf);
|
184
|
+
|
185
|
+
var $elem = this,
|
186
|
+
$form = $elem.closest('form'),
|
187
|
+
result = $.formUtils.validateInput(
|
188
|
+
$elem,
|
189
|
+
language,
|
190
|
+
conf,
|
191
|
+
$form,
|
192
|
+
eventType
|
193
|
+
);
|
194
|
+
|
195
|
+
if (attachKeyupEvent) {
|
196
|
+
$elem.unbind('keyup.validation');
|
197
|
+
}
|
198
|
+
|
199
|
+
if ( result.isValid ) {
|
200
|
+
if( result.shouldChangeDisplay ) {
|
201
|
+
$elem.addClass('valid');
|
202
|
+
$.formUtils.errorDialogs.getParentContainer($elem)
|
203
|
+
.addClass(conf.inputParentClassOnSuccess);
|
204
|
+
}
|
205
|
+
}
|
206
|
+
else if (!result.isValid) {
|
207
|
+
|
208
|
+
$.formUtils.errorDialogs.applyErrorStyling($elem, conf);
|
209
|
+
$.formUtils.errorDialogs.setInlineErrorMessage($elem, result.errorMsg, conf, conf.errorMessagePosition);
|
210
|
+
|
211
|
+
if (attachKeyupEvent) {
|
212
|
+
$elem.bind('keyup.validation', function (evt) {
|
213
|
+
if( evt.keyCode !== 9 ) {
|
214
|
+
$(this).validateInputOnBlur(language, conf, false, 'keyup');
|
215
|
+
}
|
216
|
+
});
|
217
|
+
}
|
218
|
+
}
|
219
|
+
|
220
|
+
return this;
|
221
|
+
};
|
222
|
+
|
223
|
+
/**
|
224
|
+
* Short hand for fetching/adding/removing element attributes
|
225
|
+
* prefixed with 'data-validation-'
|
226
|
+
*
|
227
|
+
* @param {String} name
|
228
|
+
* @param {String|Boolean} [val]
|
229
|
+
* @return string|undefined
|
230
|
+
* @protected
|
231
|
+
*/
|
232
|
+
$.fn.valAttr = function (name, val) {
|
233
|
+
if (val === undefined) {
|
234
|
+
return this.attr('data-validation-' + name);
|
235
|
+
} else if (val === false || val === null) {
|
236
|
+
return this.removeAttr('data-validation-' + name);
|
237
|
+
} else {
|
238
|
+
name = ((name.length > 0) ? '-' + name : '');
|
239
|
+
return this.attr('data-validation' + name, val);
|
240
|
+
}
|
241
|
+
};
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Function that validates all inputs in active form
|
245
|
+
*
|
246
|
+
* @param {Object} [language]
|
247
|
+
* @param {Object} [conf]
|
248
|
+
* @param {Boolean} [displayError] Defaults to true
|
249
|
+
*/
|
250
|
+
$.fn.isValid = function (language, conf, displayError) {
|
251
|
+
|
252
|
+
if ($.formUtils.isLoadingModules) {
|
253
|
+
var $self = this;
|
254
|
+
setTimeout(function () {
|
255
|
+
$self.isValid(language, conf, displayError);
|
256
|
+
}, 200);
|
257
|
+
return null;
|
258
|
+
}
|
259
|
+
|
260
|
+
conf = $.extend({}, $.formUtils.defaultConfig(), conf || {});
|
261
|
+
language = $.extend({}, $.formUtils.LANG, language || {});
|
262
|
+
displayError = displayError !== false;
|
263
|
+
|
264
|
+
if ($.formUtils.errorDisplayPreventedWhenHalted) {
|
265
|
+
// isValid() was called programmatically with argument displayError set
|
266
|
+
// to false when the validation was halted by any of the validators
|
267
|
+
delete $.formUtils.errorDisplayPreventedWhenHalted;
|
268
|
+
displayError = false;
|
269
|
+
}
|
270
|
+
|
271
|
+
$.formUtils.isValidatingEntireForm = true;
|
272
|
+
$.formUtils.haltValidation = false;
|
273
|
+
|
274
|
+
/**
|
275
|
+
* Adds message to error message stack if not already in the message stack
|
276
|
+
*
|
277
|
+
* @param {String} mess
|
278
|
+
* @para {jQuery} $elem
|
279
|
+
*/
|
280
|
+
var addErrorMessage = function (mess, $elem) {
|
281
|
+
if ($.inArray(mess, errorMessages) < 0) {
|
282
|
+
errorMessages.push(mess);
|
283
|
+
}
|
284
|
+
errorInputs.push($elem);
|
285
|
+
$elem.attr('current-error', mess);
|
286
|
+
if (displayError) {
|
287
|
+
$.formUtils.errorDialogs.applyErrorStyling($elem, conf);
|
288
|
+
}
|
289
|
+
},
|
290
|
+
|
291
|
+
/** Holds inputs (of type checkox or radio) already validated, to prevent recheck of mulitple checkboxes & radios */
|
292
|
+
checkedInputs = [],
|
293
|
+
|
294
|
+
/** Error messages for this validation */
|
295
|
+
errorMessages = [],
|
296
|
+
|
297
|
+
/** Input elements which value was not valid */
|
298
|
+
errorInputs = [],
|
299
|
+
|
300
|
+
/** Form instance */
|
301
|
+
$form = this,
|
302
|
+
|
303
|
+
/**
|
304
|
+
* Tells whether or not to validate element with this name and of this type
|
305
|
+
*
|
306
|
+
* @param {String} name
|
307
|
+
* @param {String} type
|
308
|
+
* @return {Boolean}
|
309
|
+
*/
|
310
|
+
ignoreInput = function (name, type) {
|
311
|
+
if (type === 'submit' || type === 'button' || type === 'reset') {
|
312
|
+
return true;
|
313
|
+
}
|
314
|
+
return $.inArray(name, conf.ignore || []) > -1;
|
315
|
+
};
|
316
|
+
|
317
|
+
// Reset style and remove error class
|
318
|
+
if (displayError) {
|
319
|
+
$form.find('.' + conf.errorMessageClass + '.alert').remove();
|
320
|
+
$.formUtils.errorDialogs.removeErrorStyling($form.find('.' + conf.errorElementClass + ',.valid'), conf);
|
321
|
+
}
|
322
|
+
|
323
|
+
// Validate element values
|
324
|
+
$form.find('input,textarea,select').filter(':not([type="submit"],[type="button"])').each(function () {
|
325
|
+
var $elem = $(this),
|
326
|
+
elementType = $elem.attr('type'),
|
327
|
+
isCheckboxOrRadioBtn = elementType === 'radio' || elementType === 'checkbox',
|
328
|
+
elementName = $elem.attr('name');
|
329
|
+
|
330
|
+
if (!ignoreInput(elementName, elementType) && (!isCheckboxOrRadioBtn || $.inArray(elementName, checkedInputs) < 0)) {
|
331
|
+
|
332
|
+
if (isCheckboxOrRadioBtn) {
|
333
|
+
checkedInputs.push(elementName);
|
334
|
+
}
|
335
|
+
|
336
|
+
var result = $.formUtils.validateInput(
|
337
|
+
$elem,
|
338
|
+
language,
|
339
|
+
conf,
|
340
|
+
$form,
|
341
|
+
'submit'
|
342
|
+
);
|
343
|
+
|
344
|
+
if( result.shouldChangeDisplay ) {
|
345
|
+
if ( !result.isValid ) {
|
346
|
+
addErrorMessage(result.errorMsg, $elem);
|
347
|
+
} else if( result.isValid ) {
|
348
|
+
$elem
|
349
|
+
.valAttr('current-error', false)
|
350
|
+
.addClass('valid');
|
351
|
+
|
352
|
+
$.formUtils.errorDialogs.getParentContainer($elem)
|
353
|
+
.addClass(conf.inputParentClassOnSuccess);
|
354
|
+
}
|
355
|
+
}
|
356
|
+
}
|
357
|
+
});
|
358
|
+
|
359
|
+
// Run validation callback
|
360
|
+
if (typeof conf.onValidate === 'function') {
|
361
|
+
var errors = conf.onValidate($form);
|
362
|
+
if ($.isArray(errors)) {
|
363
|
+
$.each(errors, function (i, err) {
|
364
|
+
addErrorMessage(err.message, err.element);
|
365
|
+
});
|
366
|
+
}
|
367
|
+
else if (errors && errors.element && errors.message) {
|
368
|
+
addErrorMessage(errors.message, errors.element);
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
// Reset form validation flag
|
373
|
+
$.formUtils.isValidatingEntireForm = false;
|
374
|
+
|
375
|
+
// Validation failed
|
376
|
+
if (!$.formUtils.haltValidation && errorInputs.length > 0) {
|
377
|
+
|
378
|
+
if (displayError) {
|
379
|
+
// display all error messages in top of form
|
380
|
+
if (conf.errorMessagePosition === 'top') {
|
381
|
+
$.formUtils.errorDialogs.setTemplateMessage($form, language.errorTitle, errorMessages, conf);
|
382
|
+
}
|
383
|
+
// Customize display message
|
384
|
+
else if (conf.errorMessagePosition === 'custom') {
|
385
|
+
$.formUtils.warn('Use deprecated function errorMessageCustom');
|
386
|
+
if (typeof conf.errorMessageCustom === 'function') {
|
387
|
+
conf.errorMessageCustom($form, language.errorTitle, errorMessages, conf);
|
388
|
+
}
|
389
|
+
}
|
390
|
+
// Display error message below input field or in defined container
|
391
|
+
else {
|
392
|
+
$.each(errorInputs, function (i, $input) {
|
393
|
+
$.formUtils.errorDialogs.setInlineErrorMessage($input, $input.attr('current-error'), conf, conf.errorMessagePosition);
|
394
|
+
});
|
395
|
+
}
|
396
|
+
|
397
|
+
if (conf.scrollToTopOnError) {
|
398
|
+
$.formUtils.$win.scrollTop($form.offset().top - 20);
|
399
|
+
}
|
400
|
+
}
|
401
|
+
|
402
|
+
return false;
|
403
|
+
}
|
404
|
+
|
405
|
+
if (!displayError && $.formUtils.haltValidation) {
|
406
|
+
$.formUtils.errorDisplayPreventedWhenHalted = true;
|
407
|
+
}
|
408
|
+
|
409
|
+
return !$.formUtils.haltValidation;
|
410
|
+
};
|
411
|
+
|
412
|
+
/**
|
413
|
+
* @deprecated
|
414
|
+
* @param language
|
415
|
+
* @param conf
|
416
|
+
*/
|
417
|
+
$.fn.validateForm = function (language, conf) {
|
418
|
+
$.formUtils.warn('Use of deprecated function $.validateForm, use $.isValid instead');
|
419
|
+
return this.isValid(language, conf, true);
|
420
|
+
};
|
421
|
+
|
422
|
+
/**
|
423
|
+
* Plugin for displaying input length restriction
|
424
|
+
*/
|
425
|
+
$.fn.restrictLength = function (maxLengthElement) {
|
426
|
+
new $.formUtils.lengthRestriction(this, maxLengthElement);
|
427
|
+
return this;
|
428
|
+
};
|
429
|
+
|
430
|
+
/**
|
431
|
+
* Add suggestion dropdown to inputs having data-suggestions with a comma
|
432
|
+
* separated string with suggestions
|
433
|
+
* @param {Array} [settings]
|
434
|
+
* @returns {jQuery}
|
435
|
+
*/
|
436
|
+
$.fn.addSuggestions = function (settings) {
|
437
|
+
var sugs = false;
|
438
|
+
this.find('input').each(function () {
|
439
|
+
var $field = $(this);
|
440
|
+
|
441
|
+
sugs = $.split($field.attr('data-suggestions'));
|
442
|
+
|
443
|
+
if (sugs.length > 0 && !$field.hasClass('has-suggestions')) {
|
444
|
+
$.formUtils.suggest($field, sugs, settings);
|
445
|
+
$field.addClass('has-suggestions');
|
446
|
+
}
|
447
|
+
});
|
448
|
+
return this;
|
449
|
+
};
|
450
|
+
|
451
|
+
|
452
|
+
})(jQuery);
|
@@ -0,0 +1,150 @@
|
|
1
|
+
/**
|
2
|
+
* Utility methods used for handling loading of modules (attached to $.formUtils)
|
3
|
+
*/
|
4
|
+
(function($) {
|
5
|
+
|
6
|
+
'use strict';
|
7
|
+
|
8
|
+
$.formUtils = $.extend($.formUtils || {}, {
|
9
|
+
|
10
|
+
/**
|
11
|
+
* Available validators
|
12
|
+
*/
|
13
|
+
validators: {},
|
14
|
+
|
15
|
+
/**
|
16
|
+
* @var {Boolean}
|
17
|
+
*/
|
18
|
+
isLoadingModules: false,
|
19
|
+
|
20
|
+
/**
|
21
|
+
* @var {Object}
|
22
|
+
*/
|
23
|
+
loadedModules: {},
|
24
|
+
|
25
|
+
/**
|
26
|
+
* @example
|
27
|
+
* $.formUtils.loadModules('date, security.dev');
|
28
|
+
*
|
29
|
+
* Will load the scripts date.js and security.dev.js from the
|
30
|
+
* directory where this script resides. If you want to load
|
31
|
+
* the modules from another directory you can use the
|
32
|
+
* path argument.
|
33
|
+
*
|
34
|
+
* The script will be cached by the browser unless the module
|
35
|
+
* name ends with .dev
|
36
|
+
*
|
37
|
+
* @param {String} modules - Comma separated string with module file names (no directory nor file extension)
|
38
|
+
* @param {String} [path] - Optional, path where the module files is located if their not in the same directory as the core modules
|
39
|
+
* @param {Boolean|function} [fireEvent] - Optional, whether or not to fire event 'load' when modules finished loading
|
40
|
+
*/
|
41
|
+
loadModules: function (modules, path, fireEvent) {
|
42
|
+
|
43
|
+
if (fireEvent === undefined) {
|
44
|
+
fireEvent = true;
|
45
|
+
}
|
46
|
+
|
47
|
+
if ($.formUtils.isLoadingModules) {
|
48
|
+
setTimeout(function () {
|
49
|
+
$.formUtils.loadModules(modules, path, fireEvent);
|
50
|
+
}, 10);
|
51
|
+
return;
|
52
|
+
}
|
53
|
+
|
54
|
+
var hasLoadedAnyModule = false,
|
55
|
+
loadModuleScripts = function (modules, path) {
|
56
|
+
|
57
|
+
var moduleList = $.split(modules),
|
58
|
+
numModules = moduleList.length,
|
59
|
+
moduleLoadedCallback = function () {
|
60
|
+
numModules--;
|
61
|
+
if (numModules === 0) {
|
62
|
+
$.formUtils.isLoadingModules = false;
|
63
|
+
if (fireEvent && hasLoadedAnyModule) {
|
64
|
+
if( typeof fireEvent === 'function' ) {
|
65
|
+
fireEvent();
|
66
|
+
} else {
|
67
|
+
$.formUtils.$win.trigger('validatorsLoaded');
|
68
|
+
}
|
69
|
+
}
|
70
|
+
}
|
71
|
+
};
|
72
|
+
|
73
|
+
|
74
|
+
if (numModules > 0) {
|
75
|
+
$.formUtils.isLoadingModules = true;
|
76
|
+
}
|
77
|
+
|
78
|
+
var cacheSuffix = '?_=' + ( new Date().getTime() ),
|
79
|
+
appendToElement = document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0];
|
80
|
+
|
81
|
+
$.each(moduleList, function (i, modName) {
|
82
|
+
modName = $.trim(modName);
|
83
|
+
if (modName.length === 0) {
|
84
|
+
moduleLoadedCallback();
|
85
|
+
}
|
86
|
+
else {
|
87
|
+
var scriptUrl = path + modName + (modName.slice(-3) === '.js' ? '' : '.js'),
|
88
|
+
script = document.createElement('SCRIPT');
|
89
|
+
|
90
|
+
if (scriptUrl in $.formUtils.loadedModules) {
|
91
|
+
// already loaded
|
92
|
+
moduleLoadedCallback();
|
93
|
+
}
|
94
|
+
else {
|
95
|
+
|
96
|
+
// Remember that this script is loaded
|
97
|
+
$.formUtils.loadedModules[scriptUrl] = 1;
|
98
|
+
hasLoadedAnyModule = true;
|
99
|
+
|
100
|
+
// Load the script
|
101
|
+
script.type = 'text/javascript';
|
102
|
+
script.onload = moduleLoadedCallback;
|
103
|
+
script.src = scriptUrl + ( scriptUrl.slice(-7) === '.dev.js' ? cacheSuffix : '' );
|
104
|
+
script.onerror = function() {
|
105
|
+
$.formUtils.warn('Unable to load form validation module '+scriptUrl);
|
106
|
+
};
|
107
|
+
script.onreadystatechange = function () {
|
108
|
+
// IE 7 fix
|
109
|
+
if (this.readyState === 'complete' || this.readyState === 'loaded') {
|
110
|
+
moduleLoadedCallback();
|
111
|
+
// Handle memory leak in IE
|
112
|
+
this.onload = null;
|
113
|
+
this.onreadystatechange = null;
|
114
|
+
}
|
115
|
+
};
|
116
|
+
appendToElement.appendChild(script);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
});
|
120
|
+
};
|
121
|
+
|
122
|
+
if (path) {
|
123
|
+
loadModuleScripts(modules, path);
|
124
|
+
} else {
|
125
|
+
var findScriptPathAndLoadModules = function () {
|
126
|
+
var foundPath = false;
|
127
|
+
$('script[src*="form-validator"]').each(function () {
|
128
|
+
foundPath = this.src.substr(0, this.src.lastIndexOf('/')) + '/';
|
129
|
+
if (foundPath === '/') {
|
130
|
+
foundPath = '';
|
131
|
+
}
|
132
|
+
return false;
|
133
|
+
});
|
134
|
+
|
135
|
+
if (foundPath !== false) {
|
136
|
+
loadModuleScripts(modules, foundPath);
|
137
|
+
return true;
|
138
|
+
}
|
139
|
+
return false;
|
140
|
+
};
|
141
|
+
|
142
|
+
if (!findScriptPathAndLoadModules()) {
|
143
|
+
$(findScriptPathAndLoadModules);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
}
|
147
|
+
|
148
|
+
});
|
149
|
+
|
150
|
+
})(jQuery);
|