webshims.register('form-validation', function($, webshims, window, document, undefined, options){ "use strict"; var isWebkit = 'webkitURL' in window; var chromeBugs = isWebkit && Modernizr.formvalidation && !webshims.bugs.bustedValidity; var webkitVersion = chromeBugs && parseFloat((navigator.userAgent.match(/Safari\/([\d\.]+)/) || ['', '999999'])[1], 10); var iVal = options.iVal; if(!iVal.fieldWrapper){ iVal.fieldWrapper = ':not(span), :not(label), :not(em), :not(strong), :not(p)'; } var invalidClass = iVal.errorClass || (iVal.errorClass = 'user-error'); var validClass = iVal.successClass || 'user-success'; var invalidWrapperClass = iVal.errorWrapperClass || (iVal.errorWrapperClass = 'ws-invalid'); var successWrapperClass = iVal.successWrapperClass || (iVal.successWrapperClass = 'ws-success'); var errorBoxClass = iVal.errorBoxClass || (iVal.errorBoxClass = 'ws-errorbox'); var errorMessageClass = iVal.errorMessageClass || (iVal.errorMessageClass = 'ws-errormessage'); var checkTypes = {checkbox: 1, radio: 1}; var loader = webshims.loader; var addModule = loader.addModule; var emptyJ = $([]); var isValid = function(elem){ return ($.prop(elem, 'validity') || {valid: 1}).valid; }; var nonFormFilter = function(){ return !$.prop(this, 'form'); }; var getGroupElements = function(elem){ elem = $(elem); var name; var form; var ret = emptyJ; if(elem[0].type == 'radio'){ form = elem.prop('form'); name = elem[0].name; if(!name){ ret = elem; } else if(form){ ret = $(form).jProp(name); } else { ret = $(document.getElementsByName(name)).filter(nonFormFilter); } ret = ret.filter('[type="radio"]'); } return ret; }; var returnValidityCause = function(validity, elem){ var ret; $.each(validity, function(name, value){ if(value){ ret = name + $.prop(elem, 'validationMessage'); return false; } }); return ret; }; var isInGroup = function(name){ var ret; try { ret = document.activeElement.name === name; } catch(e){} return ret; }; //actually we could always use the change event, but chrome messed it up and does not respect the commit action definition of the html spec //see: http://code.google.com/p/chromium/issues/detail?id=155747 var changeTypes = { radio: 1, checkbox: 1, 'select-one': 1, 'select-multiple': 1, file: 1, date: 1, month: 1, week: 1, text: 1 }; //see: http://code.google.com/p/chromium/issues/detail?id=179708 and bug above var noFocusWidgets = { time: 1, date: 1, month: 1, datetime: 1, week: 1, 'datetime-local': 1 }; var switchValidityClass = function(e){ if(!iVal.sel){return;} var elem, timer, shadowElem, shadowType; if(!e.target){return;} elem = $(e.target).getNativeElement()[0]; shadowElem = $(elem).getShadowElement(); if(elem.type == 'submit' || !$.prop(elem, 'willValidate') || (e.type == 'change' && (shadowType = shadowElem.prop('type')) && !changeTypes[shadowType])){return;} timer = $.data(elem, 'webshimsswitchvalidityclass'); var switchClass = function(){ if(!shadowType){ shadowType = shadowElem.prop('type'); } if( (chromeBugs && (e.type == 'change' || webkitVersion < 537.36) && noFocusWidgets[shadowType] && $(e.target).is(':focus')) || (e.type == 'focusout' && elem.type == 'radio' && isInGroup(elem.name)) ){ return; } if(webshims.refreshCustomValidityRules){ if(webshims.refreshCustomValidityRules(elem) == 'async'){ $(elem).one('refreshvalidityui', switchValidityClass); return; } } var validity = $.prop(elem, 'validity'); var addClass, removeClass, trigger, generaltrigger, validityCause; if(validity.valid){ if(!shadowElem.hasClass(validClass)){ addClass = validClass; removeClass = invalidClass; generaltrigger = 'changedvaliditystate'; trigger = 'changedvalid'; if(checkTypes[elem.type] && elem.checked){ getGroupElements(elem).not(elem).removeClass(removeClass).addClass(addClass).removeAttr('aria-invalid'); } shadowElem.removeAttr('aria-invalid'); $.removeData(elem, 'webshimsinvalidcause'); } } else { validityCause = returnValidityCause(validity, elem); if($.data(elem, 'webshimsinvalidcause') != validityCause){ $.data(elem, 'webshimsinvalidcause', validityCause); generaltrigger = 'changedvaliditystate'; } if(!shadowElem.hasClass(invalidClass)){ addClass = invalidClass; removeClass = validClass; if (checkTypes[elem.type] && !elem.checked) { getGroupElements(elem).not(elem).removeClass(removeClass).addClass(addClass).attr('aria-invalid', 'true'); } shadowElem.attr('aria-invalid', 'true'); trigger = 'changedinvalid'; } } if(addClass){ shadowElem.addClass(addClass).removeClass(removeClass); //jQuery 1.6.1 IE9 bug (doubble trigger bug) setTimeout(function(){ $(elem).trigger(trigger); }, 0); } if(generaltrigger){ setTimeout(function(){ $(elem).trigger(generaltrigger); }, 0); } $.removeData(elem, 'webshimsswitchvalidityclass'); }; if(shadowElem.triggerHandler('wsallowinstantvalidation', [e]) !== false){ if(timer){ clearTimeout(timer); } if(e.type == 'refreshvalidityui'){ switchClass(); } else { $.data(elem, 'webshimsswitchvalidityclass', setTimeout(switchClass, 9)); } } }; $(document.body || 'html') .on(options.validityUIEvents || 'focusout change refreshvalidityui invalid', switchValidityClass) .on('reset resetvalui', function(e){ var elems = $(e.target); if(elems.is('form, fieldset')){ elems = elems.jProp('elements'); } elems .filter('.user-error, .user-success') .removeAttr('aria-invalid') .removeClass('user-error') .removeClass('user-success') .getNativeElement() .each(function(){ $.removeData(this, 'webshimsinvalidcause'); }) .trigger('resetvalidityui') ; }) ; var setRoot = function(){ webshims.scrollRoot = (isWebkit || document.compatMode == 'BackCompat') ? $(document.body) : $(document.documentElement) ; }; var minWidth = (Modernizr.boxSizing || Modernizr['display-table'] || $.support.getSetAttribute || $.support.boxSizing) ? 'minWidth' : 'width' ; var hasTransition = ('transitionDelay' in document.documentElement.style); var resetPos = {display: 'inline-block', left: 0, top: 0, marginTop: 0, marginLeft: 0, marginRight: 0, marginBottom: 0}; setRoot(); webshims.ready('DOM', setRoot); webshims.getRelOffset = function(posElem, relElem, opts){ var offset, bodyOffset, dirs; posElem = $(posElem); $.swap($(posElem)[0], resetPos, function(){ if($.position && opts && $.position.getScrollInfo){ if(!opts.of){ opts.of = relElem; } opts.using = function(calced, data){ posElem.attr({'data-horizontal': data.horizontal, 'data-vertical': data.vertical}); offset = calced; }; posElem.attr({ 'data-horizontal': '', 'data-vertical': '', 'data-my': opts.my || 'center', 'data-at': opts.at || 'center' }); posElem.position(opts); } else { offset = $(relElem).offset(); bodyOffset = posElem.offset(); offset.top -= bodyOffset.top; offset.left -= bodyOffset.left; offset.top += relElem.outerHeight(); } }); return offset; }; $.extend(webshims.wsPopover, { isInElement: function(containers, contained){ if(!$.isArray(containers)){ containers = [containers]; } var i, len, container; var ret = false; for(i = 0, len = containers.length; i < len; i++){ container = containers[i]; if(container && container.jquery){ container = container[0]; } if(container && (container == contained || $.contains(container, contained))){ ret = true; break; } } return ret; }, show: function(element){ if(this.isVisible){return;} var e = $.Event('wspopoverbeforeshow'); this.element.trigger(e); if(e.isDefaultPrevented()){return;} this.isVisible = true; element = $(element || this.options.prepareFor).getNativeElement() ; var that = this; var visual = $(element).getShadowElement(); var delayedRepos = function(e){ clearTimeout(that.timers.repos); that.timers.repos = setTimeout(function(){ that.position(visual); }, e && e.type == 'pospopover' ? 4 : 200); }; this.clear(); this.element.removeClass('ws-po-visible').css('display', 'none'); this.prepareFor(element, visual); this.position(visual); that.timers.show = setTimeout(function(){ that.element.css('display', ''); that.timers.show = setTimeout(function(){ that.element.addClass('ws-po-visible').trigger('wspopovershow'); }, 14); }, 4); $(document.body || document).on('focusin'+this.eventns+' mousedown'+this.eventns, function(e){ if(that.options.hideOnBlur && !that.stopBlur && !that.isInElement([that.lastElement[0], element[0], that.element[0]], e.target)){ that.hide(); } }); this.element.off('pospopover').on('pospopover', delayedRepos); $(window).on('resize'+this.eventns + ' pospopover'+this.eventns, delayedRepos); }, _getAutoAppendElement: (function(){ var invalidParent = /^(?:span|i|label|b|p|tr|thead|tbody|table|strong|em|ul|ol|dl|html)$/i; return function(element){ var appendElement; var parent = element[0]; var body = document.body; while((parent = parent[appendElement ? 'offsetParent' : 'parentNode']) && parent.nodeType == 1 && parent != body){ if(!appendElement && !invalidParent.test(parent.nodeName)){ appendElement = parent; } if(appendElement && $.css(parent, 'overflow') == 'hidden' && $.css(parent, 'position') != 'static'){ appendElement = false; } } return $(appendElement || body); }; })(), prepareFor: function(element, visual){ var onBlur, parentElem; var that = this; var css = {}; var opts = $.extend(true, {}, this.options, element.jProp('form').data('wspopover') || {}, element.data('wspopover')); this.lastOpts = opts; this.lastElement = $(element).getShadowFocusElement(); if(!this.prepared || !this.options.prepareFor){ if(opts.appendTo == 'element'){ parentElem = element.parent(); } else if(opts.appendTo == 'auto'){ parentElem = this._getAutoAppendElement(element); } else { parentElem = $(opts.appendTo); } if(!this.prepared || parentElem[0] != this.element[0].parentNode){ this.element.appendTo(parentElem); } } this.element.attr({ 'data-class': element.prop('className'), 'data-id': element.prop('id') }); css[minWidth] = opts.constrainWidth ? visual.outerWidth() : ''; this.element.css(css); if(opts.hideOnBlur){ onBlur = function(e){ if(that.stopBlur){ e.stopImmediatePropagation(); } else { that.hide(); } }; that.timers.bindBlur = setTimeout(function(){ that.lastElement.off(that.eventns).on('focusout'+that.eventns + ' blur'+that.eventns, onBlur); that.lastElement.getNativeElement().off(that.eventns); }, 10); } this.prepared = true; }, clear: function(){ $(window).off(this.eventns); $(document).off(this.eventns); $(document.body).off(this.eventns); this.element.off('transitionend'+this.eventns); this.stopBlur = false; this.lastOpts = false; $.each(this.timers, function(timerName, val){ clearTimeout(val); }); }, hide: function(){ var e = $.Event('wspopoverbeforehide'); this.element.trigger(e); if(e.isDefaultPrevented() || !this.isVisible){return;} this.isVisible = false; var that = this; var forceHide = function(e){ if(!(e && e.type == 'transitionend' && (e = e.originalEvent) && e.target == that.element[0] && that.element.css('visibility') == 'hidden')){ that.element.off('transitionend'+that.eventns).css('display', 'none').attr({'data-id': '', 'data-class': '', 'hidden': 'hidden'}); clearTimeout(that.timers.forcehide); $(window).off('resize'+that.eventns); } }; this.clear(); this.element.removeClass('ws-po-visible').trigger('wspopoverhide'); $(window).on('resize'+this.eventns, forceHide); if(hasTransition){ this.element.off('transitionend'+this.eventns).on('transitionend'+this.eventns, forceHide); } that.timers.forcehide = setTimeout(forceHide, hasTransition ? 600 : 40); }, position: function(element){ var offset = webshims.getRelOffset(this.element.removeAttr('hidden'), element, (this.lastOpts || this.options).position); this.element.css(offset); } }); /* some extra validation UI */ webshims.validityAlert = (function(){ options.messagePopover.position = $.extend({}, { at: 'left bottom', my: 'left top', collision: 'none' }, options.messagePopover.position || {}); var focusTimer = false; var api = webshims.objectCreate(webshims.wsPopover, {}, options.messagePopover); var boundHide = api.hide.bind(api); api.element.addClass('validity-alert').attr({role: 'alert'}); $.extend(api, { hideDelay: 5000, showFor: function(elem, message, noFocusElem, noBubble){ elem = $(elem).getNativeElement(); this.clear(); this.hide(); if(!noBubble){ this.getMessage(elem, message); this.show(elem); if(this.hideDelay){ this.timers.delayedHide = setTimeout(boundHide, this.hideDelay); } } if(!noFocusElem){ this.setFocus(elem); } }, setFocus: function(element){ var focusElem = $(element).getShadowFocusElement(); var scrollTop = webshims.scrollRoot.scrollTop() + (options.viewportOffset || 0); var elemTop = focusElem.offset().top - (options.scrollOffset || 30); var focus = function(){ try { focusElem[0].focus(); } catch(e){} api.element.triggerHandler('pospopover'); }; if(scrollTop > elemTop){ webshims.scrollRoot.animate( {scrollTop: elemTop - 5 - (options.viewportOffset || 0)}, { queue: false, duration: Math.max( Math.min( 600, (scrollTop - elemTop) * 1.5 ), 80 ), complete: focus } ); } else { focus(); } }, getMessage: function(elem, message){ if (!message) { message = elem.getErrorMessage(); } if (message) { api.contentElement.text(message); } else { this.hide(); } } }); return api; })(); var fx = { slide: { show: 'slideDown', hide: 'slideUp' }, fade: { show: 'fadeIn', hide: 'fadeOut' }, no: { show: 'show', hide: 'hide' } }; if(!iVal.fx || !fx[iVal.fx]){ iVal.fx = 'slide'; } if(!$.fn[fx[iVal.fx].show]){ iVal.fx = 'no'; } var errorBoxId = 0; webshims.errorbox = { create: function(elem, fieldWrapper){ if(!fieldWrapper){ fieldWrapper = this.getFieldWrapper(elem); } var errorBox = $('div.'+errorBoxClass, fieldWrapper); if(!errorBox.length){ errorBox = $('