peb
Version:
Peb.js makes JS easier
1 lines • 8.09 kB
JavaScript
!function(e,t){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=t(e):"function"==typeof define&&define.amd?define("peb",[],(function(){return t(e)})):t(e)}(globalThis,(function(e){"use strict";function t(){this.name="peb",console.info("Peb.js 3.1.0 is available!")}t.version="3.1.0";class n extends Error{constructor(e){super(e),this.name="PebBasicError"}}class r extends n{constructor(e){super(e),this.name="PebExtensionError"}}class s extends n{constructor(e){super(e),this.name="PebNullObjectError"}}class i extends n{constructor(e){super(e),this.name="PebMissingEnvironmentError"}}class o extends n{constructor(e){super(e),this.name="PebMissingParameterError"}}t.PebError=n,t.PebExtensionError=r,t.PebNullObjectError=r,t.PebMissingEnvironmentError=i;let l=e=>!(void 0===e);return t.reqArg=e=>{throw new o(e?"Missing parameter "+e:"Missing required parameters")},e.document&&document instanceof Document&&(customElements.define("p-trans",class extends HTMLElement{constructor(){super(),this.style.display="inline"}}),customElements.define("p-mark",class extends HTMLElement{constructor(){super(),this.style.color="attr(color),inherit",this.style.fontFamily="attr(font), inherit"}}),t.QuickAudio=class{constructor(e){this.url=e,this.player=new Audio,this.player.style.display="none",this.player.src=this.url,document.body.appendChild(this.player)}destroy(e){document.body.removeChild(e.player)}async play(){return this.player.play()}pause(){this.player.pause()}loop(e){e?this.player.addEventListener("ended",this.play):this.player.removeEventListener("ended",this.play)}}),t.TranslationTable=class{constructor(e){"object"==typeof e&&(this.tabel=e)}set set(e){return Object.keys(e).forEach((function(t){e[t].forEach((function(n){this.tabel[t][n]=e[t][n]}))})),this}get get(){return this.tabel}translation(e){document.querySelectorAll("peb-trans").forEach((function(t){t.innerHTML=this.table[e][t.getAttribute("p-word")]}))}},t.sum=function(...e){if(e[0]instanceof Array)return t.sum(e[0]);{let t=0;return e.forEach((e=>{t+=e})),t}},t.getGlobal=function(){return e},t.genNode={element(e,t="",n={}){let r=document.createElement(e);return r.appendChild(document.createTextNode(String(t))),Object.keys(n).forEach((function(e){r.setAttribute(e,n[e])})),r},fromStr(e){let t,n=document.createElement("peb-operation-card");return n.innerHTML=e,t=n.children,1===t.length?t[0]:t},text:e=>document.createTextNode(String(e))},t.createElement=function(e,t,n="",...r){let s=document.createElement(e);var i,o;return s.innerHTML=n,i=s,o=t,Object.keys(o).forEach((e=>{i.setAttribute(e,o[e])})),((e,t)=>{if(!t)return!1;t.forEach((t=>{e.appendChild(t)}))})(s,r),s},t.RElement=class e{constructor(e){if(null===e)throw new s("Element is null");this.size=1,this.tag=e.tagName,this.id=e.id,this.element=e,this[0]=this,Object.freeze(this)}attr(e,t){if(!l(e))return this.element.attributes;if(l(t))return this.element.setAttribute(e,String(t));switch(typeof e){case"string":return this.element.getAttribute(e);case"object":Object.keys(e).forEach((function(t){this.element.setAttribute(t,e[t])}))}}animate(e,t){let n,r=this.element.style;requestAnimationFrame((function s(i){void 0!==n&&(n=i);let o=i-n,l=this.element.style;Object.keys(t).forEach((n=>{let s=r[n].match(/[^0-9]+/g),i=r[n].match(/[0-9]+/g)-0,c=t[n].match(/[0-9]+/g)-0;l[n]=Math.min((c-i)/e*o+i,t[n])+s})),requestAnimationFrame(s)}))}class(){return this.element.classList}data(e,t){if(!l(e))return this.element.dataset;if(l(t))return this.element.dataset[e]=String(t),String(t);switch(typeof e){case"string":return this.element.dataset[e];case"object":Object.keys(e).forEach((function(t){this.element.dataset[t]=e[t]}))}}get(e,t){return l(t)?(this.element[e]=t,t):this.element[e]}insert(...t){t.forEach((function(t){t instanceof e?this.element.appendChild(t.element):this.element.appendChild(t)}))}insertTo(e){e.appendChild(this.element)}del(){return this.element.parentNode.removeChild(this.element)}html(e){return l(e)?(this.element.innerHTML=String(e),String(e)):this.element.innerHTML}text(){return this.element.innerText}val(e){return l(e)?(this.element.value=String(e),String(e)):this.element.value}hide(){return this.element.dbh=this.element.style.display,this.element.style.display="none","none"}show(e){return l(e)?(this.element.style.display=String(e),String(e)):(this.element.style.display=this.element.dbh,this.element.dbh)}on(e,t){let n=function(e,t){this.element.addEventListener(e,t)};l(t)?n(e,t):"object"==typeof t&&1===arguments.length&&Object.keys(e).forEach((function(t){n(t,e[t])}))}parent(){return new e(this.element.parentElement)}child(){return new e(this.element.children[0])}next(t=!1){return new e(t?this.element.nextSibling:this.element.nextElementSibling)}prev(t=!1){return new e(t?this.element.previousSibling:this.element.previousElementSibling)}click(){this.element.click()}style(e){Object.keys(e).forEach((t=>{this.element.style[t]=e[t]}))}pause(e=!0){e?this.element.pause():this.element.play()}play(){this.element.play()}DOMReady(e){this.element.addEventListener("DOMContentLoaded",e)}forEach(e){e(this,0,this)}item(){return this}},t.RElementsCollection=class{constructor(e){if(null===e)throw new s("Element is null");this.size=this.length=e.length,this.elements=e,e.forEach(((e,t)=>{this[t]=new RElement(e)})),Object.freeze(this)}item(e=0){return this[e]}forEach(e,t=0){this.elements.forEach(((n,r)=>{r>=t&&e(this[r],r,this)}))}},t.sel=function(e,t){if("string"==typeof e){let n=document.querySelectorAll(e);return 1===n.length?new RElement(n.item(0)):l(t)?new RElement(n.item(t)):new RElementsCollection(n)}return e instanceof HTMLElement||e instanceof Node?new RElement(e):e instanceof HTMLCollection||e instanceof NodeList?new RElementsCollection(e):void 0},t.ajax=function(e){let t=new XMLHttpRequest,n=e;n.success=e.success||function(){},n.fail=e.fail||function(){},t.open(n.type,n.url,!0),t.send(n.data||null),t.responseType=e.response||"text",t.onreadystatechange=function(){4===t.readyState&&(200===t.status?n.success(t.response):n.fail())}},t.log=function(...e){console.log(...e)},t.log.error=function(...e){console.error(...e)},t.log.warn=function(...e){console.error(...e)},t.log.clear=function(){console.clear()},t.log.table=function(e,t){console.table(e,t)},t.upperCase=function(e){return e.toUpperCase()},t.lowerCase=function(e){return e.toLowerCase()},t.camelCase=function(e){return e.replace(/[ -]./g,(e=>e.replace(/[ -]/g,"").toUpperCase()))},t.classof=function(e){if(e&&e.constructor&&e.constructor.toString()){if(e.constructor.name)return e.constructor.name;let t,n=e.constructor.toString();if(t="["===n.charAt(0)?n.match(/\w+\s∗(\w+)/):n.match(/function\s*(\w+)/),t&&2===t.length)return t[1]}},t.isdigit=function(e){return!isNaN(e-0)},t.sleep=async function(e){return new Promise((t=>{setTimeout(t,e)}))},t.forEach=function(e,t){Object.keys(e).forEach(((e,n,r)=>{t(r[e],e,r)}))},t.inArray=function(e,t,n=!1){return n?e.indexOf(t)>-1?e.indexOf(t):null:e.indexOf(t)>-1},t.slice=function(e,t,n){return e.slice(t,n||t)},t.getSearchData=function(t){if(e.location){let e=t||location.search;return JSON.parse('{"'+decodeURIComponent(e.replace(/"/g,'\\"').replace(/[?]/g,"").replace(/=/g,'":"').replace(/&/g,'","'))+'"}')}throw i("Missing environment window.location")},t.navigate=function(t,n="_self"){e.window&&(e.opener=null,e.open(t,n))},t.stringTimes=function(e,t,n=""){return new Array(t).fill(e).join(n)},t.dataMap=class{constructor(){this.map=new Object(null),this.lockedType=null}lockType(e){if(void 0===e)throw new ReferenceError("Type locking of undefined is meaningless");if("function"!=typeof e)throw new TypeError("Locked type is not a constructor");this.lockedType=e}get(e){return this.map[e]}set(e,t){if(!(null!==this.lockedType&&t instanceof this.lockedType))throw new TypeError("The type is locked but the incoming value does not match");this.map[e]=t}keys(){return Object.keys(this.map)}},t.parseJson=JSON.parse,t.stringifyJson=JSON.stringify,t.now=Date.now,t.insert=[null].push.call,t.SearchParams=e.URLSearchParams,e.peb=t,t}));export default peb;