From efaeb1c595fe21f2726b8a6a905d998f51b7b987 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 5 Aug 2021 09:56:08 +0545 Subject: [PATCH] fix custom id component --- public/dist/scripts/app-all.js | 2 ++ public/dist/scripts/app.js | 2 ++ public/scripts/views/forms/custom-id.js | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/public/dist/scripts/app-all.js b/public/dist/scripts/app-all.js index 651d27dc3e..6c529f066b 100644 --- a/public/dist/scripts/app-all.js +++ b/public/dist/scripts/app-all.js @@ -2393,6 +2393,8 @@ const setValidity=async function(promise,target){try{await promise;target.setCus const setIdType=function(idType){element.setAttribute("data-id-type",idType);if(idType=="custom"){info.innerHTML="Allowed Characters A-Z, a-z, 0-9, and non-leading underscore";writer.value=prevData;writer.disabled=false;element.value=prevData;writer.focus();writer.addEventListener('blur',validate);}else{info.innerHTML="Appwrite will generate a unique ID";prevData=writer.value;writer.disabled=true;writer.value='auto-generated';element.value='unique()';} button.className=idType=="custom"?"icon-shuffle copy":"icon-edit copy";} const syncEditorWithID=function(event){if(element.value!=='unique()'){writer.value=element.value;}} +const keypress=function(e){const key=e.which||e.keyCode;const ZERO=48;const NINE=57;const SMALL_A=97;const SMALL_Z=122;const CAPITAL_A=65;const CAPITAL_Z=90;const UNDERSCORE=95;const isNotValidDigit=keyNINE;const isNotValidSmallAlphabet=keySMALL_Z;const isNotValidCapitalAlphabet=keyCAPITAL_Z;if(key==UNDERSCORE&&e.target.value.length==0){e.preventDefault();} +if(key!=UNDERSCORE&&isNotValidDigit&&isNotValidSmallAlphabet&&isNotValidCapitalAlphabet){e.preventDefault();}} syncEditorWithID();setIdType(idType);writer.addEventListener("change",function(event){element.value=writer.value;});writer.addEventListener('keypress',keypress);button.addEventListener("click",switchType);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-document",controller:function(element,container,search){var formsDocument=(element.dataset["formsDocument"]||'');var searchButton=(element.dataset["search"]||0);let path=container.scope(searchButton);element.addEventListener('click',function(){search.selected=element.value;search.path=path;document.dispatchEvent(new CustomEvent(formsDocument,{bubbles:false,cancelable:true}));});}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-document-preview",controller:function(element,container,search){element.addEventListener('change',function(){console.log(element.value);});}});})(window);(function(window){window.ls.container.get("view").add({selector:"data-forms-filter",controller:function(document,container,expression,element,form,di){let name=element.dataset["formsFilter"]||"";let events=element.dataset["event"]||"";let serialize=function(obj,prefix){let str=[],p;for(p in obj){if(obj.hasOwnProperty(p)){let k=prefix?prefix+"["+p+"]":p,v=obj[p];if(v===""){continue;} str.push(v!==null&&typeof v==="object"?serialize(v,k):encodeURIComponent(k)+"="+encodeURIComponent(v));}} return str.join("&");};let parse=function(filter){if(filter===""){return null;} diff --git a/public/dist/scripts/app.js b/public/dist/scripts/app.js index 3794a36bb9..13eac0b510 100644 --- a/public/dist/scripts/app.js +++ b/public/dist/scripts/app.js @@ -365,6 +365,8 @@ const setValidity=async function(promise,target){try{await promise;target.setCus const setIdType=function(idType){element.setAttribute("data-id-type",idType);if(idType=="custom"){info.innerHTML="Allowed Characters A-Z, a-z, 0-9, and non-leading underscore";writer.value=prevData;writer.disabled=false;element.value=prevData;writer.focus();writer.addEventListener('blur',validate);}else{info.innerHTML="Appwrite will generate a unique ID";prevData=writer.value;writer.disabled=true;writer.value='auto-generated';element.value='unique()';} button.className=idType=="custom"?"icon-shuffle copy":"icon-edit copy";} const syncEditorWithID=function(event){if(element.value!=='unique()'){writer.value=element.value;}} +const keypress=function(e){const key=e.which||e.keyCode;const ZERO=48;const NINE=57;const SMALL_A=97;const SMALL_Z=122;const CAPITAL_A=65;const CAPITAL_Z=90;const UNDERSCORE=95;const isNotValidDigit=keyNINE;const isNotValidSmallAlphabet=keySMALL_Z;const isNotValidCapitalAlphabet=keyCAPITAL_Z;if(key==UNDERSCORE&&e.target.value.length==0){e.preventDefault();} +if(key!=UNDERSCORE&&isNotValidDigit&&isNotValidSmallAlphabet&&isNotValidCapitalAlphabet){e.preventDefault();}} syncEditorWithID();setIdType(idType);writer.addEventListener("change",function(event){element.value=writer.value;});writer.addEventListener('keypress',keypress);button.addEventListener("click",switchType);}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-document",controller:function(element,container,search){var formsDocument=(element.dataset["formsDocument"]||'');var searchButton=(element.dataset["search"]||0);let path=container.scope(searchButton);element.addEventListener('click',function(){search.selected=element.value;search.path=path;document.dispatchEvent(new CustomEvent(formsDocument,{bubbles:false,cancelable:true}));});}});})(window);(function(window){"use strict";window.ls.container.get("view").add({selector:"data-forms-document-preview",controller:function(element,container,search){element.addEventListener('change',function(){console.log(element.value);});}});})(window);(function(window){window.ls.container.get("view").add({selector:"data-forms-filter",controller:function(document,container,expression,element,form,di){let name=element.dataset["formsFilter"]||"";let events=element.dataset["event"]||"";let serialize=function(obj,prefix){let str=[],p;for(p in obj){if(obj.hasOwnProperty(p)){let k=prefix?prefix+"["+p+"]":p,v=obj[p];if(v===""){continue;} str.push(v!==null&&typeof v==="object"?serialize(v,k):encodeURIComponent(k)+"="+encodeURIComponent(v));}} return str.join("&");};let parse=function(filter){if(filter===""){return null;} diff --git a/public/scripts/views/forms/custom-id.js b/public/scripts/views/forms/custom-id.js index c04e7cdda7..034a19d252 100644 --- a/public/scripts/views/forms/custom-id.js +++ b/public/scripts/views/forms/custom-id.js @@ -103,6 +103,30 @@ } } + const keypress = function (e) { + // which key is pressed, keyPressed = e.which || e.keyCode; + const key = e.which || e.keyCode; + const ZERO = 48; + const NINE = 57; + const SMALL_A = 97; + const SMALL_Z = 122; + const CAPITAL_A = 65; + const CAPITAL_Z = 90; + const UNDERSCORE = 95; + + const isNotValidDigit = key < ZERO || key > NINE; + const isNotValidSmallAlphabet = key < SMALL_A || key > SMALL_Z; + const isNotValidCapitalAlphabet = key < CAPITAL_A || key > CAPITAL_Z; + + //Leading underscore is prevented + if (key == UNDERSCORE && e.target.value.length == 0) { + e.preventDefault(); + } + if (key != UNDERSCORE && isNotValidDigit && isNotValidSmallAlphabet && isNotValidCapitalAlphabet) { + e.preventDefault(); + } + } + syncEditorWithID(); setIdType(idType); writer.addEventListener("change", function (event) {