-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjmini.min.js
40 lines (39 loc) · 6.46 KB
/
jmini.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
/* .first */ Array.prototype.first=function(){return this.length>0?this[0]:null};
/* .last */ Array.prototype.last=function(){return this.length>0?this[this.length-1]:null};
/* .addClass */ HTMLElement.prototype.addClass=function(t){return this.classList.add(t),this};
/* .removeClass */ HTMLElement.prototype.removeClass=function(e){return this.classList.remove(e),this};
/* .toggleClass */ HTMLElement.prototype.toggleClass=function(t){return this.classList.toggle(t),this};
/* .hasClass */ HTMLElement.prototype.hasClass=function(s){return this.classList.contains(s)};
/* HTMLElement.new */ HTMLElement.new=function(e,t=void 0){var n=null;try{if(n=document.createElement(e),t)for(let e in t)n.setAttribute(e,t[e])}catch(e){console.error(e)}return n};
/* .getAttr */ HTMLElement.prototype.getAttr=function(t){let e=this.getAttribute(t);return e||""};
/* .setAttr */ HTMLElement.prototype.setAttr=function(t,e){return this.setAttribute(t,e),this};
/* .appendText */ HTMLElement.prototype.appendText=function(e){let t=document.createTextNode(e);return this.append(t),this};
/* .prependText */ HTMLElement.prototype.prependText=function(e){return this.prepend(document.createTextNode(e)),this};
/* .getText */ HTMLElement.prototype.getText=function(){return this.textContent};
/* .setText */ HTMLElement.prototype.setText=function(t){return this.textContent=t.toString(),this};
/* .appendNew */ HTMLElement.prototype.appendNew=function(e,n=void 0){var t=null;try{t=HTMLElement.new(e,n),this.appendChild(t)}catch(t){console.error(t)}return t};
/* .prependNew */ HTMLElement.prototype.prependNew=function(e,n=void 0){var r=null;try{r=HTMLElement.new(e,n),this.prepend(r)}catch(r){console.error(r)}return r};
/* .setHtml */ HTMLElement.prototype.setHtml=function(e){let t=(new DOMParser).parseFromString("<div>"+e+"</div>","application/xml");return this.innerHTML=t.documentElement.innerHTML,this};
/* .getHtml */ HTMLElement.prototype.getHtml=function(t=null){return this.innerHTML};
/* .empty */ HTMLElement.prototype.empty=function(){for(;this.firstChild;)this.removeChild(this.lastChild);return this;};
/* .load */ HTMLElement.prototype.load=async function(t,e){return fetch(t,e).then(t=>{if(!t.ok)throw new Error("HTTP Status "+t.status+" – "+t.statusText);return t.text().then(t=>(this.innerHTML=t,t))})};
/* .getAncestors */ HTMLElement.prototype.getAncestors=function(e=void 0){let t=[];for(var o=this.parentNode;o;)e&&!e(o)||t.push(o),o=o.parentNode;return t};
/* .getSiblings */ HTMLElement.prototype.getSiblings=function(e=void 0){let t=[];if(this.parentNode)for(var i=this.parentNode.firstChild;i;)i!==this&&i.nodeType===Node.ELEMENT_NODE&&(e&&!e(i)||t.push(i)),i=i.nextSibling;return t};
/* .getChildren */ HTMLElement.prototype.getChildren=function(e=void 0){let o=[];if(this.hasChildNodes()){let t=this.childNodes;for(const d of t)d.nodeType!==Node.ELEMENT_NODE||e&&!e(d)||o.push(d)}return o};
/* .getDescendants */ HTMLElement.prototype.getDescendants=function(e=void 0){let t=[];if(this.hasChildNodes()){let o=this.childNodes;for(const n of o)n.nodeType===Node.ELEMENT_NODE&&(t=t.concat(n.getDescendants(e)),e&&!e(n)||t.push(n))}return t};
/* $p (page) */ let $p={_init:function(){$p._callPreInit($p),$p.init&&$p.init()},_callPreInit:function(i){Object.keys(i).forEach(((t,n)=>{let e=i[t];"object"==typeof e&&e._init&&e._init()}))}};document.onReady($p._init);
/* $p.gui (GUI) */ $p.gui={_init:function(){Object.keys($p.gui).forEach((i=>{let t=$p.gui[i];"object"==typeof t&&t._init&&t._init()}))}};
/* $p.gui.tabs */ $p.gui.tabs={_init:function(){document.querySelectorAll("*[role=tablist]").forEach($p.gui.tabs.add)},add:function(t){t.querySelectorAll("*[role=tab]").forEach((t=>t.onClick($p.gui.tabs._onTabClick)))},_onTabClick:function(t){const e="aria-selected",i="aria-controls",l="true";t.preventDefault();let r=this.getAttribute(e);if(r&&r==l)return;var u=null;let n=this.getAncestors((t=>"tablist"==(t.getAttribute?t.getAttribute("role"):null))).first();if(n){n.querySelectorAll("*[role=tab]").forEach((t=>{let i=t.getAttribute(e);i&&i==l&&(u=t)}))}if(this.setAttribute(e,l),u){u.setAttribute(e,"false");let t=u.getAttribute(i),l=document.getElementById(t);l&&(l.hidden=!0)}let o=this.getAttribute(i),a=document.getElementById(o);a&&(a.hidden=!1)}};
/* $p.gui.overlay */ $p.gui.overlay={_init:function(){console.log("$p.gui.overlay._init()");let e=HTMLElement.new("div",{id:"overlay",style:"z-index:9; display:none;",tabindex:"-1"}).onClick($p.gui.overlay.hide);$p.gui.overlay._element=e,document.body.appendChild(e)},_element:null,_callbacks:[],show:function(e,l,o=null){console.log("$p.gui.overlay.show()");let n=$p.gui.overlay._element;n&&(void 0!==l&&(n.style.zIndex=parseInt(l)),n.style.backgroundColor=o||"transparent",void 0!==e&&"function"==typeof e&&$p.gui.overlay._callbacks.push(e),n.style.display=null)},hide:function(){for(console.log("$p.gui.overlay.hide()");$p.gui.overlay._callbacks.length>0;){$p.gui.overlay._callbacks.pop()()}$p.gui.overlay._element.style.display="none"}};
/* $p.url (URL) */ $p.url={_init:function(){console.info("$p.url._init()"),$p._callPreInit($p.url)}};
/* $p.url.fragment */ $p.url.fragment={_init:function(){window.addEventListener("hashchange",$p.url.fragment._onHashChange)},get:function(){return location.hash.substr(1)},set:function(n){location.hash=n},onChange:function(n){$p.url.fragment._cbs.push(n)},_cbs:[],_onHashChange:function(n){$p.url.fragment._cbs.forEach((a=>{a(n)}))}};
/* .on */ HTMLElement.prototype.on=function(t,e){return this.addEventListener(t,e),this};
/* .once */ HTMLElement.prototype.once=function(e,t){return this.addEventListener(e,t,{once:!0}),this};
/* .off */ HTMLElement.prototype.off=function(e,t){return this.removeEventListener(e,t),this};
/* .onBlur */ HTMLElement.prototype.onBlur=function(t){return this.addEventListener("blur",t),this};
/* .onClick */ HTMLElement.prototype.onClick=function(t){return this.addEventListener("click",t),this};
/* .onFocus */ HTMLElement.prototype.onFocus=function(t){return this.addEventListener("focus",t),this};
/* .onSubmit */ HTMLFormElement.prototype.onSubmit=function(t){return this.addEventListener("submit",t),this};
/* .onReady */ document.onReady=function(n){return document.addEventListener('DOMContentLoaded',n),document};
/* .toBytesString */ Number.prototype.toBytesString=function(i=2,t="en-US",r=void 0){let e=["Bytes","KiB","MiB","GiB","TiB","PiB","EiB","ZiB","YiB","???"];var n=Math.floor(this);if(r&&r[n])return r[n];for(var o=0;n>980&&o<e.length;)n/=1024,o+=1;return new Intl.NumberFormat(t,{maximumSignificantDigits:i}).format(n)+" "+e[o]};