From 441926313d3f7ef78e9bc6996661310ae6f1c2f7 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 5 May 2024 02:48:01 +0200 Subject: [PATCH] updated netteForms.min.js --- package.json | 2 +- src/assets/netteForms.min.js | 29 +++++++---------------------- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 62de97d3..8f240ea2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nette-forms", - "version": "3.3.6", + "version": "3.4.0", "description": "Client side script for Nette Forms Component", "keywords": [ "nette", diff --git a/src/assets/netteForms.min.js b/src/assets/netteForms.min.js index 17f189a1..272db1f9 100644 --- a/src/assets/netteForms.min.js +++ b/src/assets/netteForms.min.js @@ -1,22 +1,7 @@ -(function(p,e){if(p.JSON)if("function"===typeof define&&define.amd)define(function(){return e(p)});else if("object"===typeof module&&"object"===typeof module.exports)module.exports=e(p);else{var u=!p.Nette||!p.Nette.noInit;p.Nette=e(p);u&&p.Nette.initOnLoad()}})("undefined"!==typeof window?window:this,function(p){var e={},u={},r={},x=new p.WeakMap;e.formErrors=[];e.version="3.3.0";e.invalidNumberMessage="Please enter a valid value.";e.onDocumentReady=function(a){"loading"!==document.readyState?a.call(this): -document.addEventListener("DOMContentLoaded",a)};e.getValue=function(a){var b;if(a){if(a.tagName){if("radio"===a.type){var c=a.form.elements;for(b=0;bb?null:c[b].value;for(b=0;ba.indexOf(g)&&(a.push(g),!c&&f.focus&&(c=f))}a.length&&e.showModal(a.join("\n"),function(){c&&c.focus()})};e.showModal=function(a,b){var c=document.createElement("dialog");if(c.showModal){var d=document.createElement("style");d.innerText=".netteFormsModal { text-align: center; margin: auto; border: 2px solid black; padding: 1rem } .netteFormsModal button { padding: .1em 2em }";var f=document.createElement("button");f.innerText= -"OK";f.onclick=function(){c.remove();b()};c.setAttribute("class","netteFormsModal");c.innerText=a+"\n\n";c.append(d,f);document.body.append(c);c.showModal()}else alert(a),b()};e.validateRule=function(a,b,c,d){if("number"===a.type&&a.validity.badInput)return"filled"===b;d=void 0===d?{value:e.getEffectiveValue(a,!0)}:d;":"===b.charAt(0)&&(b=b.substr(1));b=b.replace("::","_");b=b.replace(/\\/g,"");for(var f=Array.isArray(c)?c.slice(0):[c],g=0,q=f.length;g=b},maxLength:function(a,b,c){return c.length<=b},length:function(a,b,c){b=Array.isArray(b)?b:[b,b];return(null===b[0]||c.length>=b[0])&&(null===b[1]||c.length<=b[1])},email:function(a,b,c){return/^("([ !#-[\]-~]|\\[ -~])+"|[-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(a,b,c,d){/^[a-z\d+.-]+:/.test(c)||(c="https://"+c);return/^https?:\/\/((([-_0-9a-z\u00C0-\u02FF\u0370-\u1EFF]+\.)*[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)?(d.value=c,!0):!1},regexp:function(a,b,c){a="string"===typeof b?b.match(/^\/(.*)\/([imu]*)$/): -!1;try{return a&&(new RegExp(a[1],a[2].replace("u",""))).test(c)}catch(d){}},pattern:function(a,b,c,d,f){if("string"!==typeof b)return null;try{try{var g=new RegExp("^(?:"+b+")$",f?"ui":"u")}catch(q){g=new RegExp("^(?:"+b+")$",f?"i":"")}if(c instanceof FileList){for(a=0;a=b},max:function(a,b,c){Number.isFinite(b)&&(c=parseFloat(c));return c<=b},range:function(a,b,c){return Array.isArray(b)?"time"===a.type&&b[0]>b[1]?c>=b[0]||c<=b[1]:(null===b[0]||e.validators.min(a,b[0],c))&&(null===b[1]||e.validators.max(a, -b[1],c)):null},submitted:function(a){return a.form["nette-submittedBy"]===a},fileSize:function(a,b,c){for(a=0;ab)return!1;return!0},mimeType:function(a,b,c){b=Array.isArray(b)?b:[b];for(var d=0,f=b.length,g=[];dt(e)));else if("object"==typeof module&&"object"==typeof module.exports)module.exports=t(e);else{var n;let r=!(null!==(n=e.Nette)&&void 0!==n&&n.noInit);e.Nette=t(e),r&&e.Nette.initOnLoad()}}("undefined"!=typeof window?window:void 0,(e=>{const t={};let n={},r={},o=new e.WeakMap;function l(e,t){let n=e.elements.namedItem(t);return n instanceof RadioNodeList?n[0]:n}function a(e){let t=e.form.elements.namedItem(e.name);return t instanceof RadioNodeList?Array.from(t):[t]}return t.formErrors=[],t.version="3.3.0",t.onDocumentReady=function(e){"loading"!==document.readyState?e.call(this):document.addEventListener("DOMContentLoaded",e)},t.getValue=function(e){var n,r;return e instanceof HTMLInputElement?"radio"===e.type?(null===(n=a(e).find((e=>e.checked)))||void 0===n?void 0:n.value)??null:"file"===e.type?e.files:"checkbox"===e.type?e.name.endsWith("[]")?a(e).filter((e=>e.checked)).map((e=>e.value)):e.checked:e.value.trim():e instanceof HTMLSelectElement?e.multiple?Array.from(e.selectedOptions,(e=>e.value)):(null===(r=e.selectedOptions[0])||void 0===r?void 0:r.value)??null:e instanceof HTMLTextAreaElement?e.value:e instanceof RadioNodeList?t.getValue(e[0]):null},t.getEffectiveValue=function(e,r=!1){let o=t.getValue(e);if(o===e.getAttribute("data-nette-empty-value")&&(o=""),r&&void 0===n[e.name]){n[e.name]=!0;let r={value:o};t.validateControl(e,null,!0,r),o=r.value,delete n[e.name]}return o},t.validateControl=function(e,n,r=!1,o=null,a=null){n??=JSON.parse(e.getAttribute("data-nette-rules")??"[]"),o??={value:t.getEffectiveValue(e)},a??=!t.validateRule(e,":filled",null,o);for(let i of n){let n=i.op.match(/(~)?([^?]+)/),u=i.control?l(e.form,i.control):e;if(i.neg=n[1],i.op=n[2],i.condition=!!i.rules,!u)continue;if(a&&!i.condition&&":filled"!==i.op)continue;let f=t.validateRule(u,i.op,i.arg,e===u?o:void 0);if(null!==f)if(i.neg&&(f=!f),i.condition&&f){if(!t.validateControl(e,i.rules,r,o,":blank"!==i.op&&a))return!1}else if(!i.condition&&!f){if(t.isDisabled(u))continue;if(!r){let n=Array.isArray(i.arg)?i.arg:[i.arg],r=i.msg.replace(/%(value|\d+)/g,((r,o)=>t.getValue("value"===o?u:e.form.elements.namedItem(n[o].control))));t.addError(u,r)}return!1}}return!0},t.validateForm=function(e,n=!1){let r,o=e.form??e;if(t.formErrors=[],o["nette-submittedBy"]&&null!==o["nette-submittedBy"].getAttribute("formnovalidate")){let e=JSON.parse(o["nette-submittedBy"].getAttribute("data-nette-validation-scope")??"[]");if(!e.length)return t.showFormErrors(o,[]),!0;r=new RegExp("^("+e.join("-|")+"-)")}for(let e of o.elements)if(e.willValidate&&e.validity.badInput)return e.reportValidity(),!1;for(let e of Array.from(o.elements))if(e.getAttribute("data-nette-rules")&&(!r||e.name.replace(/]\[|\[|]|$/g,"-").match(r))&&!t.isDisabled(e)&&!t.validateControl(e,null,n)&&!t.formErrors.length)return!1;let l=!t.formErrors.length;return t.showFormErrors(o,t.formErrors),l},t.isDisabled=function(e){return"radio"===e.type?a(e).every((e=>e.disabled)):e.disabled},t.addError=function(e,n){t.formErrors.push({element:e,message:n})},t.showFormErrors=function(e,n){let r,o=[];for(let e of n)o.indexOf(e.message)<0&&(o.push(e.message),!r&&e.element.focus&&(r=e.element));o.length&&t.showModal(o.join("\n"),(()=>{r&&r.focus()}))},t.showModal=function(e,t){let n=document.createElement("dialog");if(!n.showModal)return alert(e),void t();let r=document.createElement("style");r.innerText=".netteFormsModal { text-align: center; margin: auto; border: 2px solid black; padding: 1rem } .netteFormsModal button { padding: .1em 2em }";let o=document.createElement("button");o.innerText="OK",o.onclick=()=>{n.remove(),t()},n.setAttribute("class","netteFormsModal"),n.innerText=e+"\n\n",n.append(r,o),document.body.append(n),n.showModal()},t.validateRule=function(e,n,r,o){if(e.validity.badInput)return":filled"===n;o??={value:t.getEffectiveValue(e,!0)};let a=":"===n.charAt(0)?n.substring(1):n;a=a.replace("::","_").replaceAll("\\","");let i=Array.isArray(r)?r:[r];return i=i.map((n=>{if(null!=n&&n.control){let r=l(e.form,n.control);return r===e?o.value:t.getEffectiveValue(r,!0)}return n})),t.validators[a]?t.validators[a](e,Array.isArray(r)?i:i[0],o.value,o):null},t.validators={filled:function(e,t,n){return""!==n&&!1!==n&&null!==n&&(!Array.isArray(n)||!!n.length)&&(!(n instanceof FileList)||n.length)},blank:function(e,n,r){return!t.validators.filled(e,n,r)},valid:function(e){return t.validateControl(e,null,!0)},equal:function(e,t,n){if(void 0===t)return null;let r=e=>"number"==typeof e||"string"==typeof e?""+e:!0===e?"1":"";n=Array.isArray(n)?n:[n],t=Array.isArray(t)?t:[t];e:for(let e of n){for(let n of t)if(r(e)===r(n))continue e;return!1}return n.length>0},notEqual:function(e,n,r){return void 0===n?null:!t.validators.equal(e,n,r)},minLength:function(e,t,n){return(n="number"==typeof n?n.toString():n).length>=t},maxLength:function(e,t,n){return(n="number"==typeof n?n.toString():n).length<=t},length:function(e,t,n){return n="number"==typeof n?n.toString():n,(null===(t=Array.isArray(t)?t:[t,t])[0]||n.length>=t[0])&&(null===t[1]||n.length<=t[1])},email:function(e,t,n){return/^("([ !#-[\]-~]|\\[ -~])+"|[-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(n)},url:function(e,t,n,r){return/^[a-z\d+.-]+:/.test(n)||(n="https://"+n),!!/^https?:\/\/((([-_0-9a-z\u00C0-\u02FF\u0370-\u1EFF]+\.)*[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(n)&&(r.value=n,!0)},regexp:function(e,t,n){let r="string"==typeof t&&t.match(/^\/(.*)\/([imu]*)$/);try{return r&&new RegExp(r[1],r[2].replace("u","")).test(n)}catch{}},pattern:function(e,t,n,r,o){if("string"!=typeof t)return null;try{let e;try{e=new RegExp("^(?:"+t+")$",o?"ui":"u")}catch{e=new RegExp("^(?:"+t+")$",o?"i":"")}return n instanceof FileList?Array.from(n).every((t=>e.test(t.name))):e.test(n)}catch{}},patternCaseInsensitive:function(e,n,r){return t.validators.pattern(e,n,r,null,!0)},numeric:function(e,t,n){return/^[0-9]+$/.test(n)},integer:function(e,t,n,r){return!!/^-?[0-9]+$/.test(n)&&(r.value=parseFloat(n),!0)},float:function(e,t,n,r){return n=n.replace(/ +/g,"").replace(/,/g,"."),!!/^-?[0-9]*\.?[0-9]+$/.test(n)&&(r.value=parseFloat(n),!0)},min:function(e,t,n){return Number.isFinite(t)&&(n=parseFloat(n)),n>=t},max:function(e,t,n){return Number.isFinite(t)&&(n=parseFloat(n)),n<=t},range:function(e,n,r){return Array.isArray(n)?"time"===e.type&&n[0]>n[1]?r>=n[0]||r<=n[1]:(null===n[0]||t.validators.min(e,n[0],r))&&(null===n[1]||t.validators.max(e,n[1],r)):null},submitted:function(e){return e.form["nette-submittedBy"]===e},fileSize:function(e,t,n){return Array.from(n).every((e=>e.size<=t))},mimeType:function(e,t,n){let r=[];return(t=Array.isArray(t)?t:[t]).forEach((e=>r.push("^"+e.replace(/([^\w])/g,"\\$1").replace("\\*",".*")+"$"))),r=new RegExp(r.join("|")),Array.from(n).every((e=>!e.type||r.test(e.type)))},image:function(e,n,r){return t.validators.mimeType(e,n??["image/gif","image/png","image/jpeg","image/webp"],r)},static:function(e,t){return t}},t.toggleForm=function(e,n=null){r={};for(let r of Array.from(e.elements))r.getAttribute("data-nette-rules")&&t.toggleControl(r,null,null,!n);for(let e in r)t.toggle(e,r[e].state,r[e].elem,n)},t.toggleControl=function(e,n,i,u,f=null,d=null){n??=JSON.parse(e.getAttribute("data-nette-rules")??"[]"),f??={value:t.getEffectiveValue(e)},d??=!t.validateRule(e,":filled",null,f);let s,c=!1;for(let m of n){let n=m.op.match(/(~)?([^?]+)/),g=m.control?l(e.form,m.control):e;if(m.neg=n[1],m.op=n[2],m.condition=!!m.rules,g&&(!d||m.condition||":filled"===m.op)){if(s=i,!1!==i){if(s=t.validateRule(g,m.op,m.arg,e===g?f:void 0),null===s)continue;m.neg&&(s=!s),m.condition||(i=s)}if(m.condition&&t.toggleControl(e,m.rules,s,u,f,":blank"!==m.op&&d)||m.toggle){c=!0,u&&a(g).filter((e=>!o.has(e))).forEach((n=>{n.addEventListener("change",(n=>t.toggleForm(e.form,n))),o.set(n,null)}));for(let t in m.toggle??[])r[t]??={elem:e,state:m.toggle[t]?s:!s}}}}return c},t.toggle=function(e,t,n,r){/^\w[\w.:-]*$/.test(e)&&(e="#"+e),Array.from(document.querySelectorAll(e)).forEach((e=>e.hidden=!t))},t.compactCheckboxes=function(e,t){let n={};for(let r of e.elements)r instanceof HTMLInputElement&&"checkbox"===r.type&&r.name.endsWith("[]")&&r.checked&&!r.disabled&&(t.delete(r.name),n[r.name]??=[],n[r.name].push(r.value));for(let e in n)t.set(e.substring(0,e.length-2),n[e].join(","))},t.initForm=function(e){"get"===e.method&&e.hasAttribute("data-nette-compact")&&e.addEventListener("formdata",(n=>t.compactCheckboxes(e,n.formData))),Array.from(e.elements).some((e=>e.getAttribute("data-nette-rules")))&&(t.toggleForm(e),e.noValidate||(e.noValidate=!0,e.addEventListener("submit",(n=>{t.validateForm(e)||(n.stopPropagation(),n.preventDefault())})),e.addEventListener("reset",(()=>{setTimeout((()=>t.toggleForm(e)))}))))},t.initOnLoad=function(){t.onDocumentReady((()=>{Array.from(document.forms).forEach((e=>t.initForm(e))),document.body.addEventListener("click",(e=>{let t=e.target;for(;t;){if(t.form&&t.type in{submit:1,image:1}){t.form["nette-submittedBy"]=t;break}t=t.parentNode}}))}))},t.webalize=function(e){e=e.toLowerCase();let n,r="";for(let o=0;o