UNPKG

edo-htqw

Version:

A easier HTML element's dom operation libary.

1 lines 3.32 kB
function basic(edo){edo.proto("text",function(val){return void 0===val?this.el.innerText:this.el.innerText=val}),edo.proto("html",function(val){return void 0===val?this.el.innerHTML:this.el.innerHTML=val}),edo.proto("href",function(val){return void 0===val?this.el.href:this.el.href=val})}class edoTypeError extends Error{constructor(msg){super(msg),this.name="edoTypeError"}}function listen(edo){function prevent(event){event.preventDefault()}edo.proto("click",function(call){try{return this.el.onclick=null===call?null:event=>{const temp=function(val){const temp={prevent:prevent};for(let s in val)temp[s]=val[s];return temp}({0:event});call.call(this,temp)},!0}catch(_a){return!1}}),edo.proto("onEventL",function(n,l){if(!(l instanceof Function))throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(" ")[1].replace("]","")}`);return this.el.addEventListener(n,l),!0}),edo.proto("offEventL",function(n,l){if(!(l instanceof Function))throw new edoTypeError(`This type should is Function, fault ${Object.prototype.toString.call(l).split(" ")[1].replace("]","")}`);return this.el.removeEventListener(n,l),!0})}function style(edo){edo.css=function(options,a2){const head=document.querySelector("head");let str="";if(options instanceof Object)for(let s in options){str+=`${s}{`;let temp=options[s];for(let s2 in temp)str+=`${s2}:${temp[s2]};`;str+="}"}else{if("string"!=typeof options)throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(options).split(" ")[1].replace("]","")}`);if(str+=`${options}{`,"string"==typeof a2)str+=a2;else{if(!(a2 instanceof Object))throw new edoTypeError(`This type should is Object or string, fault ${Object.prototype.toString.call(a2).split(" ")[1].replace("]","")}`);for(let s in a2)str+=`${s}:${a2[s]};`}str+="}"}head.innerHTML+=`<style>${str}</style>`},edo.proto("css",function(a1,a2){if(void 0===a2)if("string"==typeof a1)this.el.style=a1;else for(let s in a1)this.el.style[s]=a1[s];else this.el.style[a1]=a2;return!0}),edo.proto("getCss",function(name){return this.el.style[name]})}class edoHTMLElementIsNotFoundError extends Error{constructor(message){super(message),this.name="edoHTMLElementIsNotFoundError"}}class edoCssSelectorError extends Error{constructor(msg){super(msg),this.name="edoCssSelectorError"}}var _a,errors={edoHTMLElementIsNotFoundError:edoHTMLElementIsNotFoundError,edoTypeError:edoTypeError,edoCssSelectorError:edoCssSelectorError};class edoElement{constructor(val){if(this[_a]="edoElement",this.el=document.createElement("div"),"string"==typeof val)try{if(null===document.querySelector(val))throw new errors.edoHTMLElementIsNotFoundError(`The element "${val}" is not found!!!`);this.el=document.querySelector(val)}catch(e){throw e.name.startsWith("edo")?e:new errors.edoCssSelectorError(`This Css Selector "${val}" is error`)}else{if(!(val instanceof HTMLElement))throw new errors.edoTypeError(`This type should is HTMLElement or string, fault ${Object.prototype.toString.call(val).split(" ")[1].replace("]","")}`);this.el=val}this.el}}_a=Symbol.toStringTag;const edo=function(val){return new edoElement(val)};edo.proto=function(name,value){try{return edoElement.prototype[name]=value,!0}catch(_b){return!1}},basic(edo),listen(edo),style(edo);export default edo;