UNPKG

@pardnchiu/adminui

Version:

AdminUI is a simple backend management system template with an intuitive interface and a modular architecture, supporting custom extensions, enabling developers to quickly build and customize backend management platforms.

1 lines 8.31 kB
let page;const system_events={password_show:e=>{const _this=e.target;const is_show=_this.parentElement.dataset.show==="1";_this.className=`fa-solid ${is_show?"fa-eye-slash":"fa-eye"}`;_this.previousElementSibling.children[0].type=is_show?"password":"text";_this.parentElement.dataset.show=is_show?0:1},body_left_show:e=>{const elm=document.querySelector("section.system-block-body-left");if(elm==null){return}elm.dataset.show=parseInt(elm.dataset.show)??0?0:1},body_left_type:e=>{const elm=document.querySelector("section.system-block-body-left");if(elm==null){return}const is_min=parseInt(elm.dataset.min);elm.dataset.min=is_min?0:1;addCookie("is_body_left_min",is_min?0:1)},tab_show:e=>{const _this=e.target;const elm_left=document.querySelector("section.system-block-body-left");const elm_parent=_this.parentElement;const show=parseInt(_this.dataset.show);const is_child_selected=elm_parent.querySelector("a[data-selected='1']")!=null;const is_show=is_child_selected&&isNaN(show)||show||show===1;for(let e of[...elm_left.querySelectorAll("p[data-show='1']")]){if(e===_this){continue}e.dataset.show=0}_this.dataset.show=is_show?0:1}};function addCookie(targetKey="",body,expireSec){targetKey=String(targetKey).trim();if(typeof targetKey!=="string"||targetKey.length<1||body==null){return}targetKey=encodeURIComponent(targetKey);expireSec=(typeof expireSec==="number"?expireSec:3600)*1e3;const dateNow=Date.now();const date=new Date(dateNow+expireSec);body=encodeURIComponent(typeof body==="object"?JSON.stringify(body).trim():String(body).trim());document.cookie=`${targetKey}=${body}; expires=${date.toUTCString()}; path=/`;return document.cookie}function addEvent(config={}){let dom;if(typeof config!=="object"||!config.hasOwnProperty("dom")){return}dom=config["dom"];for(const key of Object.keys(config)){if(key==="dom"){continue}dom[key]=e=>config[key](e)}return dom}const regexCssClass=/\.([\w_-]+)?/gi;const regexCssID=/\#([\w_-]+)?/i;const regexCssTag=/^\w+(?=[\#\.]*)/i;function createElement(tag="",val0,val1){const cssTag=((tag.match(regexCssTag)||[])[0]||"").trim();const cssID=((tag.match(regexCssID)||[])[1]||"").trim();const cssClass=(regexCssClass.test(tag)?tag.match(regexCssClass):[]).map((e=>e.replace(/^\./,"")));if(cssTag.length<1){return}let isTemp=tag==="temp";let dom=isTemp?document.createDocumentFragment():document.createElement(cssTag);if(cssID.length>0){dom.id=cssID}for(const e of cssClass){dom.classList.add(e)}if(val0==null&&val1!=null){[val0,val1]=[val1,null]}let attributeValue;let childrenValue;if(val0!=null&&val1!=null){[attributeValue,childrenValue]=[val0,val1]}else if(val1==null){if(typeof val0==="string"||typeof val0==="number"||Array.isArray(val0)){childrenValue=val0}else{attributeValue=val0}}else if(val0==null){return dom}(_=>{if(typeof attributeValue!=="object"||attributeValue==null){return}for(const e in attributeValue){if(!attributeValue.hasOwnProperty(e)){continue}const value=attributeValue[e];if({value:1,innerText:1,innerHTML:1,textContent:1,contentEditable:1,selected:1,checked:1}[e]){dom[e]=value}else if({valuedisplay:1,valuecolor:1,valuebackgroundColor:1,valuebackground:1,valuewidth:1,valueheight:1,valuefloat:1}[e]){dom.style[e]=value}else if(e==="dataset"&&typeof value==="object"){for(const k of Object.keys(value)){dom.dataset[k]=value[k]}}else if(value!=null){dom.setAttribute(e,value)}}})();(_=>{if(childrenValue==null){return}const is_object=typeof childrenValue==="object";const is_array=Array.isArray(childrenValue);if(is_array){for(let e of childrenValue){const is_string=typeof e==="string";const is_number=typeof e==="number";const is_element=e instanceof Element;if(is_string||is_number){if(isTemp){dom.appendChild(document.createTextNode(e))}else{dom.innerHTML+=e}}else if(is_element){dom.appendChild(e)}}return}else if(is_object){return}const value=childrenValue;const is_img=cssTag==="img";const is_source=cssTag==="source";const is_input=cssTag==="input";const is_textarea=cssTag==="textarea";if(is_img||is_source){dom[_src]=value}else if(is_textarea||is_input){dom.value=value}else if(isTemp){dom.appendChild(document.createTextNode(childrenValue))}else{dom.innerHTML=value}})();return dom}function decodeFromSafeBase64(base64){base64=base64.replace(/[-_]/g,(e=>e==="-"?"+":"/"));const padding=base64.length%4;base64=base64+(padding?"=".repeat(4-padding):"");const binary=atob(base64);const bytes=new Uint8Array([...binary].map((char=>char.charCodeAt(0))));const json=(new TextDecoder).decode(bytes);return JSON.parse(json)}function encodeToSafeBase64(obj){const str=JSON.stringify(obj);const utf8=(new TextEncoder).encode(str);return btoa(String.fromCharCode(...utf8)).replace(/=/g,"").replace(/\+/g,"-").replace(/\//g,"_")}function getCookie(targetKey=""){targetKey=String(targetKey).trim();if(typeof targetKey!=="string"||targetKey.length<1){return}const regexp=new RegExp(targetKey+"=([^ ;]+)?");const match=document.cookie.match(regexp)||[];const length=match.length;let targetValue=match[length-1];if(length<1||targetValue==null){return}targetValue=decodeURIComponent(targetValue);try{return JSON.parse(targetValue)}catch(err){return targetValue}}let textInputErrorTimer;function textInput(e){const _this=e.target;const parent=_this.parentElement.parentElement;const type=_this.dataset.type??_this.type??"text";const wrap=_this.dataset.wrap??"1";const force=_this.dataset.force??"1";const canWrap=!(wrap==="0");const isWrap=/\n/.test(_this.value);const isForce=force==="1";let max=_this.dataset.max;let min=_this.dataset.min;switch(type){case"text":if(!canWrap&&isWrap){_this.value=_this.value.replace(/\n/g,"");parent.dataset.error=1;clearTimeout(textInputErrorTimer);textInputErrorTimer=setTimeout((_=>{parent.dataset.error=0}),1e3)};break;case"account":var regex=/[^\w]/g;if(isForce){_this.value=_this.value.replace(/\s/g,"").replace(regex,"")};const isError=regex.test(_this.value);parent.dataset.error=isError?1:0;parent.dataset.message=isError?"僅接受半形英文 / 數字 / 底線":"";break;case"number":var regex=/[^\d\-\.]/g;if(isForce||max||min){_this.value=_this.value.replace(/\s/g,"").replace(regex,"")};parent.dataset.error=regex.test(_this.value)?1:0;break;case"email":var regex=/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;if(!regex.test(_this.value)){parent.dataset.error=1;parent.dataset.message="內容不符合EMAIL格式.";clearTimeout(textInputErrorTimer);textInputErrorTimer=setTimeout((_=>{parent.dataset.error=0;parent.dataset.message=""}),1e3)};break;case"password":var regex=/[^\w\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\`\{\|\}\~\ ]/;if(isForce){_this.value=_this.value.replace(/\s/g,"").replace(regex,"")};parent.dataset.error=regex.test(_this.value)?1:0;parent.dataset.message=regex.test(_this.value)?"內容包含不支持的字符.":"";break;default:break}if(max==null&&min==null){return}max=isNaN(max)?null:parseFloat(max);min=isNaN(min)?null:parseFloat(min);switch(type){case"number":if(/^\-/.test(_this.value)&&_this.value.length===1||/\.$/.test(_this.value)||_this.value.length<1){return};_this.value=Math.max(min,Math.min(max,parseFloat(_this.value)));break;default:if(max!=null&&_this.value.length>max){_this.value=_this.value.slice(0,max)};var isMin=min!=null&&_this.value.length;if(parent.dataset.error==="1"&&isMin){parent.dataset.message=parent.dataset.message+(_this.value.length<min?" / 最小長度為 "+min:"")}else if(isMin){parent.dataset.error=_this.value.length<min?1:0;parent.dataset.message=_this.value.length<min?"最小長度為 "+min:""}break}if(_this.tagName==="TEXTAREA"&&_this.nextElementSibling!=null){_this.nextElementSibling.innerHTML=_this.value.replace(/\n/g,"<br>")}}const hasgtagKeyup=e=>{if(e.key!=="Enter"){return}const _this=e.target;const regex=/[##\,,\n\ ]+([\w\u4e00-\u9fa5\u3105-\u3129\u31A0-\u31B7\u3040-\u30FF\u3130-\u318F\uAC00-\uD7AF]+)/g;let ary=(_this.value||"").match(regex)||_this.value.split(/\s/);let map=new Map;if(ary.length<1){return}for(let i=0,e=ary[i];i<ary.length;i++,e=ary[i]){const hashtag=e.replace(/[##\,,\n\ ]/g,"").slice(0,24);if(hashtag.length<2){continue}map.set(hashtag,0)}let keys=Array.from(map.keys());if(keys.length>0){_this.value=`#${keys.join(" #")} #`.trim();_this.nextElementSibling.innerHTML=_this.value.replace(/\n/g,"<br>")}else{_this.value="#";_this.nextElementSibling.innerHTML="#"}};