agenda-libre-ruby/public/webshims/shims/form-core.js
2014-07-20 02:43:13 +02:00

326 lines
9.2 KiB
JavaScript

webshims.register('form-core', function($, webshims, window, document, undefined, options){
"use strict";
webshims.capturingEventPrevented = function(e){
if(!e._isPolyfilled){
var isDefaultPrevented = e.isDefaultPrevented;
var preventDefault = e.preventDefault;
e.preventDefault = function(){
clearTimeout($.data(e.target, e.type + 'DefaultPrevented'));
$.data(e.target, e.type + 'DefaultPrevented', setTimeout(function(){
$.removeData(e.target, e.type + 'DefaultPrevented');
}, 30));
return preventDefault.apply(this, arguments);
};
e.isDefaultPrevented = function(){
return !!(isDefaultPrevented.apply(this, arguments) || $.data(e.target, e.type + 'DefaultPrevented') || false);
};
e._isPolyfilled = true;
}
};
var modules = webshims.modules;
var support = webshims.support;
var isValid = function(elem){
return ($.prop(elem, 'validity') || {valid: 1}).valid;
};
var lazyLoad = function(){
var toLoad = ['form-validation'];
$(document).off('.lazyloadvalidation');
if(options.lazyCustomMessages){
options.customMessages = true;
toLoad.push('form-message');
}
if(webshims._getAutoEnhance(options.customDatalist)){
options.fD = true;
toLoad.push('form-datalist');
}
if(options.addValidators){
toLoad.push('form-validators');
}
webshims.reTest(toLoad);
};
/*
* Selectors for all browsers
*/
var extendSels = function(){
var matches, matchesOverride;
var exp = $.expr[":"];
var rElementsGroup = /^(?:form|fieldset)$/i;
var hasInvalid = function(elem){
var ret = false;
$(elem).jProp('elements').each(function(){
if(!rElementsGroup.test(this.nodeName || '')){
ret = exp.invalid(this);
if(ret){
return false;
}
}
});
return ret;
};
$.extend(exp, {
"valid-element": function(elem){
return rElementsGroup.test(elem.nodeName || '') ? !hasInvalid(elem) : !!($.prop(elem, 'willValidate') && isValid(elem));
},
"invalid-element": function(elem){
return rElementsGroup.test(elem.nodeName || '') ? hasInvalid(elem) : !!($.prop(elem, 'willValidate') && !isValid(elem));
},
"required-element": function(elem){
return !!($.prop(elem, 'willValidate') && $.prop(elem, 'required'));
},
"user-error": function(elem){
return ($.prop(elem, 'willValidate') && $(elem).getShadowElement().hasClass((options.iVal.errorClass || 'user-error')));
},
"optional-element": function(elem){
return !!($.prop(elem, 'willValidate') && $.prop(elem, 'required') === false);
}
});
['valid', 'invalid', 'required', 'optional'].forEach(function(name){
exp[name] = $.expr[":"][name+"-element"];
});
// sizzle/jQuery has a bug with :disabled/:enabled selectors
if(support.fieldsetdisabled && !$('<fieldset disabled=""><input /><input /></fieldset>').find(':disabled').filter(':disabled').is(':disabled')){
matches = $.find.matches;
matchesOverride = {':disabled': 1, ':enabled': 1};
$.find.matches = function(expr, elements){
if(matchesOverride[expr]){
return matches.call(this, '*'+expr, elements);
}
return matches.apply(this, arguments);
};
$.extend(exp, {
"enabled": function( elem ) {
return 'disabled' in elem && elem.disabled === false && !$.find.matchesSelector(elem, 'fieldset[disabled] *');
},
"disabled": function( elem ) {
return elem.disabled === true || ('disabled' in elem && $.find.matchesSelector(elem, 'fieldset[disabled] *'));
}
});
}
//bug was partially fixed in 1.10.0 for IE9, but not IE8 (move to es5 as soon as 1.10.2 is used)
if(typeof document.activeElement == 'unknown'){
var pseudoFocus = exp.focus;
exp.focus = function(){
try {
return pseudoFocus.apply(this, arguments);
} catch(e){
webshims.error(e);
}
return false;
};
}
};
var formExtras = {
noAutoCallback: true,
options: options
};
var addModule = webshims.loader.addModule;
var lazyLoadProxy = function(obj, fn, args){
lazyLoad();
webshims.ready('form-validation', function(){
obj[fn].apply(obj, args);
});
};
var transClass = ('transitionDelay' in document.documentElement.style) ? '' : ' no-transition';
var poCFG = webshims.cfg.wspopover;
addModule('form-validation', $.extend({d: ['form-message']}, formExtras));
addModule('form-validators', $.extend({}, formExtras));
if(support.formvalidation && !webshims.bugs.bustedValidity){
//create delegatable events
webshims.capturingEvents(['invalid'], true);
}
if($.expr.filters){
extendSels();
} else {
webshims.ready('sizzle', extendSels);
}
webshims.triggerInlineForm = function(elem, event){
$(elem).trigger(event);
};
if(!poCFG.position && poCFG.position !== false){
poCFG.position = {
at: 'left bottom',
my: 'left top',
collision: 'fit flip'
};
}
webshims.wsPopover = {
id: 0,
_create: function(){
this.options = $.extend(true, {}, poCFG, this.options);
this.id = webshims.wsPopover.id++;
this.eventns = '.wsoverlay' + this.id;
this.timers = {};
this.element = $('<div class="ws-popover'+transClass+'" tabindex="-1"><div class="ws-po-outerbox"><div class="ws-po-arrow"><div class="ws-po-arrowbox" /></div><div class="ws-po-box" /></div></div>');
this.contentElement = $('.ws-po-box', this.element);
this.lastElement = $([]);
this.bindElement();
this.element.data('wspopover', this);
},
options: {},
content: function(html){
this.contentElement.html(html);
},
bindElement: function(){
var that = this;
var stopBlur = function(){
that.stopBlur = false;
};
this.preventBlur = function(e){
that.stopBlur = true;
clearTimeout(that.timers.stopBlur);
that.timers.stopBlur = setTimeout(stopBlur, 9);
};
this.element.on({
'mousedown': this.preventBlur
});
},
show: function(){
lazyLoadProxy(this, 'show', arguments);
}
};
/* some extra validation UI */
webshims.validityAlert = {
showFor: function(){
lazyLoadProxy(this, 'showFor', arguments);
}
};
webshims.getContentValidationMessage = function(elem, validity, key){
var customRule;
if(webshims.errorbox && webshims.errorbox.initIvalContentMessage){
webshims.errorbox.initIvalContentMessage(elem);
}
var message = (webshims.getOptions && webshims.errorbox ? webshims.getOptions(elem, 'errormessage', false, true) : $(elem).data('errormessage')) || elem.getAttribute('x-moz-errormessage') || '';
if(key && message[key]){
message = message[key];
} else if(message) {
validity = validity || $.prop(elem, 'validity') || {valid: 1};
if(validity.valid){
message = '';
}
}
if(typeof message == 'object'){
validity = validity || $.prop(elem, 'validity') || {valid: 1};
if(validity.customError && (customRule = $.data(elem, 'customMismatchedRule')) && message[customRule] && typeof message[customRule] == 'string'){
message = message[customRule];
} else if(!validity.valid){
$.each(validity, function(name, prop){
if(prop && name != 'valid' && message[name]){
message = message[name];
return false;
}
});
if(typeof message == 'object'){
if(validity.typeMismatch && message.badInput){
message = message.badInput;
}
if(validity.badInput && message.typeMismatch){
message = message.typeMismatch;
}
}
}
}
if(typeof message == 'object'){
message = message.defaultMessage;
}
if(webshims.replaceValidationplaceholder){
message = webshims.replaceValidationplaceholder(elem, message);
}
return message || '';
};
$.fn.getErrorMessage = function(key){
var message = '';
var elem = this[0];
if(elem){
message = webshims.getContentValidationMessage(elem, false, key) || $.prop(elem, 'customValidationMessage') || $.prop(elem, 'validationMessage');
}
return message;
};
$.event.special.valuevalidation = {
setup: function(){
webshims.error('valuevalidation was renamed to validatevalue!');
}
};
$.event.special.validatevalue = {
setup: function(){
var data = $(this).data() || $.data(this, {});
if(!('validatevalue' in data)){
data.validatevalue = true;
}
}
};
$(document).on('focusin.lazyloadvalidation mousedown.lazyloadvalidation touchstart.lazyloadvalidation', function(e){
if('form' in e.target){
lazyLoad();
}
});
webshims.ready('WINDOWLOAD', lazyLoad);
if(modules['form-number-date-ui'].loaded && !options.customMessages && (modules['form-number-date-api'].test() || (support.inputtypes.range && support.inputtypes.color))){
webshims.isReady('form-number-date-ui', true);
}
webshims.ready('DOM', function(){
if(document.querySelector('.ws-custom-file')){
webshims.reTest(['form-validation']);
}
});
$(function(){
var fileReaderReady = ('FileReader' in window && 'FormData' in window);
if(!fileReaderReady){
webshims.addReady(function(context){
if(!fileReaderReady && !modules.filereader.loaded && !modules.moxie.loaded){
if(context.querySelector('input.ws-filereader')){
webshims.reTest(['filereader', 'moxie']);
fileReaderReady = true;
}
}
});
}
});
if(options.addValidators && options.fastValidators){
webshims.reTest(['form-validators', 'form-validation']);
}
if(document.readyState == 'complete'){
webshims.isReady('WINDOWLOAD', true);
}
});