You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

11 lines
9.4 KiB

/*
* # Semantic UI
* https://github.com/Semantic-Org/Semantic-UI
* http://www.semantic-ui.com/
*
* Copyright 2014 Contributors
* Released under the MIT license
* http://opensource.org/licenses/MIT
*
*/
!function(e,t,n,i){e.fn.form=function(t,r){var o,a=e(this),s=e.extend(!0,{},e.fn.form.settings,r),l=e.extend({},e.fn.form.settings.defaults,t),u=s.namespace,c=s.metadata,d=s.selector,f=s.className,p=(s.error,"."+u),m="module-"+u,v=a.selector||"",g=(new Date).getTime(),h=[],b=arguments[0],y="string"==typeof b,z=[].slice.call(arguments,1);return a.each(function(){var t,r=e(this),u=e(this).find(d.field),k=e(this).find(d.group),x=e(this).find(d.message),w=(e(this).find(d.prompt),e(this).find(d.submit)),E=[],C=this,F=r.data(m);t={initialize:function(){t.verbose("Initializing form validation",r,l,s),t.bindEvents(),t.instantiate()},instantiate:function(){t.verbose("Storing instance of module",t),F=t,r.data(m,t)},destroy:function(){t.verbose("Destroying previous module",F),t.removeEvents(),r.removeData(m)},refresh:function(){t.verbose("Refreshing selector cache"),u=r.find(d.field)},submit:function(){t.verbose("Submitting form",r),r.submit()},attachEvents:function(n,i){i=i||"submit",e(n).on("click",function(e){t[i](),e.preventDefault()})},bindEvents:function(){s.keyboardShortcuts&&u.on("keydown"+p,t.event.field.keydown),r.on("submit"+p,t.validate.form),u.on("blur"+p,t.event.field.blur),t.attachEvents(w,"submit"),u.each(function(){var n=e(this).prop("type"),i=t.get.changeEvent(n);e(this).on(i+p,t.event.field.change)})},removeEvents:function(){r.off(p),u.off(p),w.off(p),u.off(p)},event:{field:{keydown:function(n){var i=e(this),r=n.which,o={enter:13,escape:27};r==o.escape&&(t.verbose("Escape key pressed blurring field"),i.blur()),!n.ctrlKey&&r==o.enter&&i.is(d.input)&&i.not(d.checkbox).size()>0&&(t.debug("Enter key pressed, submitting form"),w.addClass(f.down),i.one("keyup"+p,t.event.field.keyup))},keyup:function(){t.verbose("Doing keyboard shortcut form submit"),w.removeClass(f.down),t.submit()},blur:function(){var n=e(this),i=n.closest(k);i.hasClass(f.error)?(t.debug("Revalidating field",n,t.get.validation(n)),t.validate.field(t.get.validation(n))):("blur"==s.on||"change"==s.on)&&t.validate.field(t.get.validation(n))},change:function(){var n=e(this),i=n.closest(k);("change"==s.on||i.hasClass(f.error)&&s.revalidate)&&(clearTimeout(t.timer),t.timer=setTimeout(function(){t.debug("Revalidating field",n,t.get.validation(n)),t.validate.field(t.get.validation(n))},s.delay))}}},get:{changeEvent:function(e){return"checkbox"==e||"radio"==e||"hidden"==e?"change":n.createElement("input").oninput!==i?"input":n.createElement("input").onpropertychange!==i?"propertychange":"keyup"},field:function(n){return t.verbose("Finding field with identifier",n),u.filter("#"+n).size()>0?u.filter("#"+n):u.filter('[name="'+n+'"]').size()>0?u.filter('[name="'+n+'"]'):u.filter("[data-"+c.validate+'="'+n+'"]').size()>0?u.filter("[data-"+c.validate+'="'+n+'"]'):e("<input/>")},validation:function(n){var i;return e.each(l,function(e,r){t.get.field(r.identifier).get(0)==n.get(0)&&(i=r)}),i||!1}},has:{field:function(e){return t.verbose("Checking for existence of a field with identifier",e),u.filter("#"+e).size()>0?!0:u.filter('[name="'+e+'"]').size()>0?!0:u.filter("[data-"+c.validate+'="'+e+'"]').size()>0?!0:!1}},add:{prompt:function(n,o){var a=t.get.field(n),l=a.closest(k),u=l.find(d.prompt),c=0!==u.size();o="string"==typeof o?[o]:o,t.verbose("Adding field error state",n),l.addClass(f.error),s.inline&&(c||(u=s.templates.prompt(o),u.appendTo(l)),u.html(o[0]),c?t.verbose("Inline errors are disabled, no inline error added",n):s.transition&&e.fn.transition!==i&&r.transition("is supported")?(t.verbose("Displaying error with css transition",s.transition),u.transition(s.transition+" in",s.duration)):(t.verbose("Displaying error with fallback javascript animation"),u.fadeIn(s.duration)))},errors:function(e){t.debug("Adding form error messages",e),x.html(s.templates.error(e))}},remove:{prompt:function(n){var o=t.get.field(n.identifier),a=o.closest(k),l=a.find(d.prompt);a.removeClass(f.error),s.inline&&l.is(":visible")&&(t.verbose("Removing prompt for field",n),s.transition&&e.fn.transition!==i&&r.transition("is supported")?l.transition(s.transition+" out",s.duration,function(){l.remove()}):l.fadeOut(s.duration,function(){l.remove()}))}},set:{success:function(){r.removeClass(f.error).addClass(f.success)},error:function(){r.removeClass(f.success).addClass(f.error)}},validate:{form:function(n){var o=!0;return E=[],e.each(l,function(e,n){t.validate.field(n)||(o=!1)}),o?(t.debug("Form has no validation errors, submitting"),t.set.success(),e.proxy(s.onSuccess,C)(n)):(t.debug("Form has errors"),t.set.error(),s.inline||t.add.errors(E),r.data("moduleApi")!==i&&n.stopImmediatePropagation(),e.proxy(s.onFailure,C)(E))},field:function(n){var r=t.get.field(n.identifier),o=!0,a=[];return r.prop("disabled")?(t.debug("Field is disabled. Skipping",n.identifier),o=!0):n.optional&&""===e.trim(r.val())?(t.debug("Field is optional and empty. Skipping",n.identifier),o=!0):n.rules!==i&&e.each(n.rules,function(e,i){t.has.field(n.identifier)&&!t.validate.rule(n,i)&&(t.debug("Field is invalid",n.identifier,i.type),a.push(i.prompt),o=!1)}),o?(t.remove.prompt(n,a),e.proxy(s.onValid,r)(),!0):(E=E.concat(a),t.add.prompt(n.identifier,a),e.proxy(s.onInvalid,r)(a),!1)},rule:function(n,r){var o,a,l=t.get.field(n.identifier),u=r.type,c=e.trim(l.val()+""),d=/\[(.*)\]/i,f=d.exec(u),p=!0;return f!==i&&null!==f?(o=""+f[1],a=u.replace(f[0],""),p=e.proxy(s.rules[a],C)(c,o)):p=e.proxy(s.rules[u],l)(c),p}},setting:function(t,n){if(e.isPlainObject(t))e.extend(!0,s,t);else{if(n===i)return s[t];s[t]=n}},internal:function(n,r){if(e.isPlainObject(n))e.extend(!0,t,n);else{if(r===i)return t[n];t[n]=r}},debug:function(){s.debug&&(s.performance?t.performance.log(arguments):(t.debug=Function.prototype.bind.call(console.info,console,s.name+":"),t.debug.apply(console,arguments)))},verbose:function(){s.verbose&&s.debug&&(s.performance?t.performance.log(arguments):(t.verbose=Function.prototype.bind.call(console.info,console,s.name+":"),t.verbose.apply(console,arguments)))},error:function(){t.error=Function.prototype.bind.call(console.error,console,s.name+":"),t.error.apply(console,arguments)},performance:{log:function(e){var n,i,r;s.performance&&(n=(new Date).getTime(),r=g||n,i=n-r,g=n,h.push({Name:e[0],Arguments:[].slice.call(e,1)||"",Element:C,"Execution Time":i})),clearTimeout(t.performance.timer),t.performance.timer=setTimeout(t.performance.display,100)},display:function(){var n=s.name+":",r=0;g=!1,clearTimeout(t.performance.timer),e.each(h,function(e,t){r+=t["Execution Time"]}),n+=" "+r+"ms",v&&(n+=" '"+v+"'"),a.size()>1&&(n+=" ("+a.size()+")"),(console.group!==i||console.table!==i)&&h.length>0&&(console.groupCollapsed(n),console.table?console.table(h):e.each(h,function(e,t){console.log(t.Name+": "+t["Execution Time"]+"ms")}),console.groupEnd()),h=[]}},invoke:function(t,n,r){var a,s,l,u=F;return n=n||z,r=C||r,"string"==typeof t&&u!==i&&(t=t.split(/[\. ]/),a=t.length-1,e.each(t,function(n,r){var o=n!=a?r+t[n+1].charAt(0).toUpperCase()+t[n+1].slice(1):t;if(e.isPlainObject(u[o])&&n!=a)u=u[o];else{if(u[o]!==i)return s=u[o],!1;if(!e.isPlainObject(u[r])||n==a)return u[r]!==i?(s=u[r],!1):!1;u=u[r]}})),e.isFunction(s)?l=s.apply(r,n):s!==i&&(l=s),e.isArray(o)?o.push(l):o!==i?o=[o,l]:l!==i&&(o=l),s}},y?(F===i&&t.initialize(),t.invoke(b)):(F!==i&&t.destroy(),t.initialize())}),o!==i?o:this},e.fn.form.settings={name:"Form",namespace:"form",debug:!1,verbose:!0,performance:!0,keyboardShortcuts:!0,on:"submit",inline:!1,delay:200,revalidate:!0,transition:"scale",duration:200,onValid:function(){},onInvalid:function(){},onSuccess:function(){return!0},onFailure:function(){return!1},metadata:{validate:"validate"},selector:{message:".error.message",field:"input, textarea, select",group:".field",checkbox:'input[type="checkbox"], input[type="radio"]',input:"input",prompt:".prompt",submit:".submit"},className:{error:"error",success:"success",down:"down",label:"ui prompt label"},error:{method:"The method you called is not defined."},templates:{error:function(t){var n='<ul class="list">';return e.each(t,function(e,t){n+="<li>"+t+"</li>"}),n+="</ul>",e(n)},prompt:function(t){return e("<div/>").addClass("ui red pointing prompt label").html(t[0])}},rules:{checked:function(){return e(this).filter(":checked").size()>0},contains:function(e,t){return t=t.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,"\\$&"),-1!==e.search(t)},email:function(e){var t=new RegExp("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?","i");return t.test(e)},empty:function(e){return!(e===i||""===e)},integer:function(e,t){var n,r,o,a=/^\-?\d+$/;return t===i||""===t||".."===t||(-1==t.indexOf("..")?a.test(t)&&(n=r=t-0):(o=t.split("..",2),a.test(o[0])&&(n=o[0]-0),a.test(o[1])&&(r=o[1]-0))),a.test(e)&&(n===i||e>=n)&&(r===i||r>=e)},is:function(e,t){return e==t},length:function(e,t){return e!==i?e.length>=t:!1},match:function(t,n){var r,o=e(this);return o.find("#"+n).size()>0?r=o.find("#"+n).val():o.find('[name="'+n+'"]').size()>0?r=o.find('[name="'+n+'"]').val():o.find('[data-validate="'+n+'"]').size()>0&&(r=o.find('[data-validate="'+n+'"]').val()),r!==i?t.toString()==r.toString():!1},maxLength:function(e,t){return e!==i?e.length<=t:!1},not:function(e,t){return e!=t},url:function(e){var t=/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;return t.test(e)}}}}(jQuery,window,document);