-
Notifications
You must be signed in to change notification settings - Fork 2
/
netteForms.js
1 lines (1 loc) · 6.8 KB
/
netteForms.js
1
var Nette=Nette||{};Nette.addEvent=function(c,a,d){var b=c["on"+a];c["on"+a]=function(){if(typeof b==="function"&&b.apply(c,arguments)===false){return false}return d.apply(c,arguments)}};Nette.getValue=function(f){var e,a;if(!f){return null}else{if(!f.nodeName){for(e=0,a=f.length;e<a;e++){if(f[e].checked){return f[e].value}}return null}else{if(f.nodeName.toLowerCase()==="select"){var d=f.selectedIndex,c=f.options,b=[];if(d<0){return null}else{if(f.type==="select-one"){return c[d].value}}for(e=0,a=c.length;e<a;e++){if(c[e].selected){b.push(c[e].value)}}return b}else{if(f.type==="checkbox"){return f.checked}else{if(f.type==="radio"){return Nette.getValue(f.form.elements[f.name].nodeName?[f]:f.form.elements[f.name])}else{if(f.type==="file"){return f.files||f.value}else{return f.value.replace("\r","").replace(/^\s+|\s+$/g,"")}}}}}}};Nette.validateControl=function(c,i,k){i=i||Nette.parseJSON(c.getAttribute("data-nette-rules"));for(var a=0,f=i.length;a<f;a++){var g=i[a],e=g.op.match(/(~)?([^?]+)/);g.neg=e[1];g.op=e[2];g.condition=!!g.rules;var b=g.control?c.form.elements[g.control]:c;var h=Nette.validateRule(b,g.op,g.arg);if(h===null){continue}if(g.neg){h=!h}if(g.condition&&h){if(!Nette.validateControl(c,g.rules,k)){return false}}else{if(!g.condition&&!h){if(b.disabled){continue}if(!k){var d=Nette.isArray(g.arg)?g.arg:[g.arg];var j=g.msg.replace(/%(value|\d+)/g,function(n,l){return Nette.getValue(l==="value"?b:c.form.elements[d[l].control])});Nette.addError(b,j)}return false}}}return true};Nette.validateForm=function(b){var e=b.form||b,c=false;if(e["nette-submittedBy"]&&e["nette-submittedBy"].getAttribute("formnovalidate")!==null){var f=Nette.parseJSON(e["nette-submittedBy"].getAttribute("data-nette-validation-scope"));if(f.length){c=new RegExp("^("+f.join("-|")+"-)")}else{return true}}for(var a=0;a<e.elements.length;a++){var d=e.elements[a];if(!(d.nodeName.toLowerCase() in {input:1,select:1,textarea:1})||(d.type in {hidden:1,submit:1,image:1,reset:1})||(c&&!d.name.replace(/]\[|\[|]|$/g,"-").match(c))||d.disabled||d.readonly){continue}if(!Nette.validateControl(d)){return false}}return true};Nette.addError=function(b,a){if(b.focus){b.focus()}if(a){alert(a)}};Nette.validateRule=function(e,g,c){var f=Nette.getValue(e);if(e.getAttribute){if(f===e.getAttribute("data-nette-empty-value")){f=""}}if(g.charAt(0)===":"){g=g.substr(1)}g=g.replace("::","_");g=g.replace(/\\/g,"");var b=Nette.isArray(c)?c.slice(0):[c];for(var d=0,a=b.length;d<a;d++){if(b[d]&&b[d].control){b[d]=Nette.getValue(e.form.elements[b[d].control])}}return Nette.validators[g]?Nette.validators[g](e,Nette.isArray(c)?b:b[0],f):null};Nette.validators={filled:function(b,a,c){return c!==""&&c!==false&&c!==null},blank:function(b,a,c){return !Nette.validators.filled(b,a,c)},valid:function(b,a,c){return Nette.validateControl(b,null,true)},equal:function(d,b,e){if(b===undefined){return null}b=Nette.isArray(b)?b:[b];for(var c=0,a=b.length;c<a;c++){if(e==b[c]){return true}}return false},notEqual:function(b,a,c){return a===undefined?null:!Nette.validators.equal(b,a,c)},minLength:function(b,a,c){return c.length>=a},maxLength:function(b,a,c){return c.length<=a},length:function(b,a,c){a=Nette.isArray(a)?a:[a,a];return(a[0]===null||c.length>=a[0])&&(a[1]===null||c.length<=a[1])},email:function(b,a,c){return(/^("([ !\x23-\x5B\x5D-\x7E]*|\\[ -~])+"|[-a-z0-9!#$%&'*+\/=?^_`{|}~]+(\.[-a-z0-9!#$%&'*+\/=?^_`{|}~]+)*)@([0-9a-z\u00C0-\u02FF\u0370-\u1EFF]([-0-9a-z\u00C0-\u02FF\u0370-\u1EFF]{0,61}[0-9a-z\u00C0-\u02FF\u0370-\u1EFF])?\.)+[a-z\u00C0-\u02FF\u0370-\u1EFF][-0-9a-z\u00C0-\u02FF\u0370-\u1EFF]{0,17}[a-z\u00C0-\u02FF\u0370-\u1EFF]$/i).test(c)},url:function(b,a,c){return(/^(https?:\/\/|(?=.*\.))([0-9a-z\u00C0-\u02FF\u0370-\u1EFF](([-0-9a-z\u00C0-\u02FF\u0370-\u1EFF]{0,61}[0-9a-z\u00C0-\u02FF\u0370-\u1EFF])?\.)*[a-z\u00C0-\u02FF\u0370-\u1EFF][-0-9a-z\u00C0-\u02FF\u0370-\u1EFF]{0,17}[a-z\u00C0-\u02FF\u0370-\u1EFF]|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[[0-9a-f:]{3,39}\])(:\d{1,5})?(\/\S*)?$/i).test(c)},regexp:function(b,a,f){var d=typeof a==="string"?a.match(/^\/(.*)\/([imu]*)$/):false;if(d){try{return(new RegExp(d[1],d[2].replace("u",""))).test(f)}catch(c){}}},pattern:function(b,a,d){try{return typeof a==="string"?(new RegExp("^("+a+")$")).test(d):null}catch(c){}},integer:function(b,a,c){return(/^-?[0-9]+$/).test(c)},"float":function(b,a,c){return(/^-?[0-9]*[.,]?[0-9]+$/).test(c)},range:function(b,a,c){return Nette.isArray(a)?((a[0]===null||parseFloat(c)>=a[0])&&(a[1]===null||parseFloat(c)<=a[1])):null},submitted:function(b,a,c){return b.form["nette-submittedBy"]===b},fileSize:function(c,a,d){if(window.FileList){for(var b=0;b<d.length;b++){if(d[b].size>a){return false}}}return true}};Nette.toggleForm=function(c,a){var b;Nette.toggles={};for(b=0;b<c.elements.length;b++){if(c.elements[b].nodeName.toLowerCase() in {input:1,select:1,textarea:1,button:1}){Nette.toggleControl(c.elements[b],null,null,a)}}for(b in Nette.toggles){Nette.toggle(b,Nette.toggles[b])}};Nette.toggleControl=function(c,n,p,h){n=n||Nette.parseJSON(c.getAttribute("data-nette-rules"));var l=false,g=Object.prototype.hasOwnProperty,o=function(){Nette.toggleForm(c.form)};for(var a=0,f=n.length;a<f;a++){var j=n[a],e=j.op.match(/(~)?([^?]+)/);j.neg=e[1];j.op=e[2];j.condition=!!j.rules;if(!j.condition){continue}var b=j.control?c.form.elements[j.control]:c;var m=p;if(m!==false){m=Nette.validateRule(b,j.op,j.arg);if(m===null){continue}if(j.neg){m=!m}}if(Nette.toggleControl(c,j.rules,m,h)||j.toggle){l=true;if(h){if(!b.nodeName){for(var d=0;d<b.length;d++){Nette.addEvent(b[d],"click",o)}}else{if(b.nodeName.toLowerCase()==="select"){Nette.addEvent(b,"change",o)}else{Nette.addEvent(b,"click",o)}}}for(var k in j.toggle||[]){if(g.call(j.toggle,k)){Nette.toggles[k]=Nette.toggles[k]||(m&&j.toggle[k])}}}}return l};Nette.parseJSON=function(s){s=s||"[]";if(s.substr(0,3)==="{op"){return eval("["+s+"]")}return window.JSON&&window.JSON.parse?JSON.parse(s):eval(s)};Nette.toggle=function(c,b){var a=document.getElementById(c);if(a){a.style.display=b?"":"none"}};Nette.initForm=function(a){a.noValidate="novalidate";Nette.addEvent(a,"submit",function(b){if(!Nette.validateForm(a)){if(b&&b.stopPropagation){b.stopPropagation()}else{if(window.event){event.cancelBubble=true}}return false}});Nette.addEvent(a,"click",function(c){c=c||event;var b=c.target||c.srcElement;a["nette-submittedBy"]=(b.type in {submit:1,image:1})?b:null});Nette.toggleForm(a,true)};Nette.isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"};Nette.addEvent(window,"load",function(){for(var a=0;a<document.forms.length;a++){Nette.initForm(document.forms[a])}});Nette.webalize=function(d){d=d.toLowerCase();var b="",a,c;for(a=0;a<d.length;a++){c=Nette.webalizeTable[d.charAt(a)];b+=c?c:d.charAt(a)}return b.replace(/[^a-z0-9]+/g,"-").replace(/^-|-$/g,"")};Nette.webalizeTable={á:"a",č:"c",ď:"d",é:"e",ě:"e",í:"i",ň:"n",ó:"o",ř:"r",š:"s",ť:"t",ú:"u",ů:"u",ý:"y",ž:"z"};