From 733ceb42647d965e6c75583b52ca7d345527d8a5 Mon Sep 17 00:00:00 2001 From: eldadfux Date: Sun, 12 May 2019 21:15:19 +0300 Subject: [PATCH] Added form to JSON service --- gulpfile.js | 1 + public/dist/scripts/app-all.js | 19 +++++- public/dist/scripts/app.js | 19 +++++- public/scripts/services/form.js | 115 ++++++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 public/scripts/services/form.js diff --git a/gulpfile.js b/gulpfile.js index 646d33c4c..fa108bcad 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -20,6 +20,7 @@ const configApp = { 'public/scripts/services/console.js', 'public/scripts/services/date.js', 'public/scripts/services/env.js', + 'public/scripts/services/form.js', 'public/scripts/services/markdown.js', 'public/scripts/services/sdk.js', 'public/scripts/services/timezone.js', diff --git a/public/dist/scripts/app-all.js b/public/dist/scripts/app-all.js index e8189b538..a58df9175 100644 --- a/public/dist/scripts/app-all.js +++ b/public/dist/scripts/app-all.js @@ -2150,7 +2150,24 @@ match=text.match(new RegExp(regex,'gi')) if(!match){return fail} for(i=0,len=match.length;i0){json[name]=element.children[element.selectedIndex].value;} +else if('radio'===type){if(element.checked){json[name]=element.value;}} +else if('checkbox'===type){if(!Array.isArray(json[name])){json[name]=[];} +if(element.checked){json[name].push(element.value);}} +else if(undefined!==element.value){if((json[name]!==undefined)&&(!Array.isArray(json[name]))){json[name]=[json[name]];} +if(Array.isArray(json[name])){json[name].push(element.value);} +else{json[name]=element.value;}} +json[name]=cast(json[name],castTo);}} +for(let i=0;i>>(32-d))}function K(G,k){let I,d,F,H,x;F=(G&2147483648);H=(k&2147483648);I=(G&1073741824);d=(k&1073741824);x=(G&1073741823)+(k&1073741823);if(I&d){return(x^2147483648^F^H)}if(I|d){if(x&1073741824){return(x^3221225472^F^H)}else{return(x^1073741824^F^H)}}else{return(x^F^H)}}function r(d,F,k){return(d&F)|((~d)&k)}function q(d,F,k){return(d&k)|(F&(~k))}function p(d,F,k){return(d^F^k)}function n(d,F,k){return(F^(d|(~k)))}function u(G,F,aa,Z,k,H,I){G=K(G,K(K(r(F,aa,Z),k),I));return K(L(G,H),F)}function f(G,F,aa,Z,k,H,I){G=K(G,K(K(q(F,aa,Z),k),I));return K(L(G,H),F)}function D(G,F,aa,Z,k,H,I){G=K(G,K(K(p(F,aa,Z),k),I));return K(L(G,H),F)}function t(G,F,aa,Z,k,H,I){G=K(G,K(K(n(F,aa,Z),k),I));return K(L(G,H),F)}function e(G){let Z;let F=G.length;let x=F+8;let k=(x-(x%64))/64;let I=(k+1)*16;let aa=Array(I-1);let d=0;let H=0;while(H>>29;return aa}function B(x){let k="",F="",G,d;for(d=0;d<=3;d++){G=(x>>>(d*8))&255;F="0"+G.toString(16);k=k+F.substr(F.length-2,2)}return k}function J(k){k=k.replace(/rn/g,"n");let d="";for(let F=0;F127)&&(x<2048)){d+=String.fromCharCode((x>>6)|192);d+=String.fromCharCode((x&63)|128)}else{d+=String.fromCharCode((x>>12)|224);d+=String.fromCharCode(((x>>6)&63)|128);d+=String.fromCharCode((x&63)|128)}}}return d}let C=Array();let P,h,E,v,g,Y,X,W,V;let S=7,Q=12,N=17,M=22;let A=5,z=9,y=14,w=20;let o=4,m=11,l=16,j=23;let U=6,T=10,R=15,O=21;s=J(s);C=e(s);Y=1732584193;X=4023233417;W=2562383102;V=271733878;for(P=0;P=60){if($value%60===0){return Math.ceil($value/60)+' hours';} diff --git a/public/dist/scripts/app.js b/public/dist/scripts/app.js index f6b48c903..7240d7be7 100644 --- a/public/dist/scripts/app.js +++ b/public/dist/scripts/app.js @@ -208,7 +208,24 @@ match=text.match(new RegExp(regex,'gi')) if(!match){return fail} for(i=0,len=match.length;i0){json[name]=element.children[element.selectedIndex].value;} +else if('radio'===type){if(element.checked){json[name]=element.value;}} +else if('checkbox'===type){if(!Array.isArray(json[name])){json[name]=[];} +if(element.checked){json[name].push(element.value);}} +else if(undefined!==element.value){if((json[name]!==undefined)&&(!Array.isArray(json[name]))){json[name]=[json[name]];} +if(Array.isArray(json[name])){json[name].push(element.value);} +else{json[name]=element.value;}} +json[name]=cast(json[name],castTo);}} +for(let i=0;i>>(32-d))}function K(G,k){let I,d,F,H,x;F=(G&2147483648);H=(k&2147483648);I=(G&1073741824);d=(k&1073741824);x=(G&1073741823)+(k&1073741823);if(I&d){return(x^2147483648^F^H)}if(I|d){if(x&1073741824){return(x^3221225472^F^H)}else{return(x^1073741824^F^H)}}else{return(x^F^H)}}function r(d,F,k){return(d&F)|((~d)&k)}function q(d,F,k){return(d&k)|(F&(~k))}function p(d,F,k){return(d^F^k)}function n(d,F,k){return(F^(d|(~k)))}function u(G,F,aa,Z,k,H,I){G=K(G,K(K(r(F,aa,Z),k),I));return K(L(G,H),F)}function f(G,F,aa,Z,k,H,I){G=K(G,K(K(q(F,aa,Z),k),I));return K(L(G,H),F)}function D(G,F,aa,Z,k,H,I){G=K(G,K(K(p(F,aa,Z),k),I));return K(L(G,H),F)}function t(G,F,aa,Z,k,H,I){G=K(G,K(K(n(F,aa,Z),k),I));return K(L(G,H),F)}function e(G){let Z;let F=G.length;let x=F+8;let k=(x-(x%64))/64;let I=(k+1)*16;let aa=Array(I-1);let d=0;let H=0;while(H>>29;return aa}function B(x){let k="",F="",G,d;for(d=0;d<=3;d++){G=(x>>>(d*8))&255;F="0"+G.toString(16);k=k+F.substr(F.length-2,2)}return k}function J(k){k=k.replace(/rn/g,"n");let d="";for(let F=0;F127)&&(x<2048)){d+=String.fromCharCode((x>>6)|192);d+=String.fromCharCode((x&63)|128)}else{d+=String.fromCharCode((x>>12)|224);d+=String.fromCharCode(((x>>6)&63)|128);d+=String.fromCharCode((x&63)|128)}}}return d}let C=Array();let P,h,E,v,g,Y,X,W,V;let S=7,Q=12,N=17,M=22;let A=5,z=9,y=14,w=20;let o=4,m=11,l=16,j=23;let U=6,T=10,R=15,O=21;s=J(s);C=e(s);Y=1732584193;X=4023233417;W=2562383102;V=271733878;for(P=0;P=60){if($value%60===0){return Math.ceil($value/60)+' hours';} diff --git a/public/scripts/services/form.js b/public/scripts/services/form.js new file mode 100644 index 000000000..a8b7f8379 --- /dev/null +++ b/public/scripts/services/form.js @@ -0,0 +1,115 @@ +(function (window) { + "use strict"; + + window.ls.container.set('form', function() { + + function cast(value, to) { + switch (to) { + case 'int': + case 'integer': + value = parseInt(value); + break; + case 'string': + value = value.toString(); + break; + case 'json': + value = (value) ? JSON.parse(value) : []; + break; + case 'array': + value = (value.constructor === Array) ? value : [value]; + break; + case 'array-empty': + value = []; + break; + case 'bool': + case 'boolean': + value = (value === 'false') ? false : value; + value = !!value; + break; + } + + return value; + } + + function toJson(element, json) { + json = json || {}; + let name = element.getAttribute('name'); + let type = element.getAttribute('type'); + let castTo = element.getAttribute('data-cast-to'); + let ref = json; + + if(name && 'FORM' !== element.tagName) { + if('FIELDSET' === element.tagName) { // Fieldset Array / Object + if(castTo === 'object') { + + if(json[name] === undefined) { + json[name] = {}; + } + + ref = json[name]; + } + else { + if(!Array.isArray(json[name])) { + json[name] = []; + } + + json[name].push({}); + + ref = json[name][json[name].length - 1]; + } + } + else if (undefined !== element.value) { + if ('SELECT' === element.tagName && element.children > 0) { // Select + json[name] = element.children[element.selectedIndex].value; + } + else if ('radio' === type) { // Radio + if(element.checked) { + json[name] = element.value; + } + } + else if ('checkbox' === type) { // Checkbox + if(!Array.isArray(json[name])) { + json[name] = []; + } + + if(element.checked) { + json[name].push(element.value); + } + } + else if (undefined !== element.value) { // Normal + + if ((json[name] !== undefined) && (!Array.isArray(json[name]))) { // Support for list array when name is repeating more than once + json[name] = [json[name]]; + } + + if(Array.isArray(json[name])) { + json[name].push(element.value); + } + else { + json[name] = element.value; + } + } + + json[name] = cast(json[name], castTo); + } + } + + for (let i = 0; i < element.children.length; i++) { + if(Array.isArray(ref)) { + ref.push({}); + toJson(element.children[i], ref[ref.length]); + } + else { + toJson(element.children[i], ref); + } + } + + return json; + } + + return { + 'toJson': toJson + } +}, true, false); + +})(window); \ No newline at end of file