Created
July 23, 2017 17:44
-
-
Save arguiot/e08dc2cd6e3c5dca0f17a9a2a9a692ff to your computer and use it in GitHub Desktop.
Compiled version of DisplayJS v0.3.2 for CDNJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DisplayJS{constructor(a){this.obj=a}var(a){const b=()=>{this.if(),this.else();const a=document.querySelectorAll("[var]");for(let b=0;b<a.length;b++){const c=a[b].getAttribute("var");a[b].innerHTML=this.obj[c]}};a?!0==a?(b(),this.live(this.obj,()=>{b()})):window.setInterval(()=>{b()},a):b()}xss(a){const b=/</g,c=/>/g,d=/'/g,e=/"/g;return a.toString().replace(b,"<").replace(c,">").replace(d,"'").replace(e,""")}xssURI(a){return encodeURI(a)}target(b=()=>{this.var()}){const c=(()=>{return document.addEventListener?(a,b,c)=>{a.addEventListener(b,c,!1)}:(a,b,c)=>{a.attachEvent(`on${b}`,c)}})(),d=this.obj;[].forEach.call(document.querySelectorAll("[target]"),(e,f,g)=>{c(g[f],"change",function(){const a=g[f].getAttribute("target");d[a]="checkbox"==this.type?this.checked:this.value,b()}),c(g[f],"keydown",function(){const a=g[f].getAttribute("target");d[a]="checkbox"==this.type?this.checked:this.value,b()}),c(g[f],"input",function(){const a=g[f].getAttribute("target");d[a]="checkbox"==this.type?this.checked:this.value,b()}),c(g[f],"paste",function(){const a=g[f].getAttribute("target");d[a]="checkbox"==this.type?this.checked:this.value,b()})})}if(a){const b=()=>{const a=document.querySelectorAll("[if]");for(let b=0;b<a.length;b++){const c=a[b].getAttribute("if"),d=[];d.push(a[b]),eval(this.obj[c])?this.show(d):this.hide(d)}};a?!0==a?(b(),this.live(this.obj,()=>{b()})):window.setInterval(()=>{b()},a):b()}else(a){const b=()=>{const a=document.querySelectorAll("[else]");for(let b=0;b<a.length;b++){const c=a[b].getAttribute("else"),d=[];d.push(a[b]),eval(this.obj[c])?this.hide(d):this.show(d)}};a?!0==a?(b(),this.live(this.obj,()=>{b()})):window.setInterval(()=>{b()},a):b()}repeat(a,b,c,d="",e=""){let f=d;if("object"==typeof c)for(let a=0;a<b.length;a++)f+=c[a]+(b[a]+"");else for(let a=0;a<b.length;a++)f+=c+(b[a]+"");f+=e,a[0].innerHTML=f}custom(a,b,c){const d=()=>{const c=document.querySelectorAll(`[${a}]`);for(let d=0;d<c.length;d++){const e=c[d].getAttribute(a);b(c[d],e)}};c?!0==c?(d(),this.live(this.obj,()=>{d()})):window.setInterval(()=>{d()},c):d()}live(a,b){const c={watch(a,b,c){const d=Object.getOwnPropertyDescriptor(a,b);if("undefined"==typeof d)throw new Error(`DisplayJS: Invalid descriptor for property: ${b}, object: ${a}`);if("function"!=typeof c)throw new Error(`DisplayJS: Invalid onPropertyChange handler: ${c}`);let e=a[b];Object.defineProperty(a,b,{enumerable:!0,configurable:!0,get(){return e},set(d){if(d!==e)return c(a,b,d,e),e=d}})},watchAll(a,b){if("function"!=typeof b)throw new Error(`DisplayJS: Invalid onPropertyChange handler: ${b}`);for(const c in a)this.watch(a,c,b)}};c.watchAll(a,(a,c,d,e)=>{b(a,c,d,e)})}onEvent(){const a=document.querySelectorAll("[on]");for(let b=0;b<a.length;b++){const c=a[b].getAttribute("on"),d=a[b].getAttribute("action");a[b].addEventListener(c,()=>{eval(d)})}}all(a,b){a.forEach((a)=>{const c=[];c.push(a),b(c)})}text(a,b){a[0].innerHTML=this.xss(b)}html(a,b){a[0].innerHTML=b}append(a,b){a[0].innerHTML+=b}after(a,b){a[0].insertAdjacentHTML("afterend",b)}before(a,b){a[0].insertAdjacentHTML("beforebegin",b)}clone(a){a[0].cloneNode(!0)}is(a,b){return!(a[0]!==b[0])}select(a){const b=document.querySelectorAll(a);return b}single(a){const b=document.querySelectorAll(a),c=[];return c.push(b[0]),c}empty(a){a[0].innerHTML=null}valEmpty(a){a[0].value=null}remove(a){a[0].parentNode.removeChild(a)}on(a,b,c){a[0].addEventListener(b,c)}ready(a){(document.attachEvent?"complete"===document.readyState:"loading"!==document.readyState)?a():document.addEventListener("DOMContentLoaded",a)}show(a){return a[0].style.display="block",!0}hide(a){return a[0].style.display="none",!0}ajax(a,b,c,d,e="application/x-www-form-urlencoded; charset=UTF-8"){const f=new XMLHttpRequest;f.open(b,a,!0),f.setRequestHeader("Content-Type",e),f.onload=()=>{if(200<=f.status&&400>f.status){const a=f.responseText;d(a)}else console.error("DisplayJS error: The ajax request returned an error.")},f.onerror=()=>{console.error("DisplayJS error: The ajax request returned an error.")},f.send(c)}hasClass(a,b){return a[0].classList?a[0].classList.contains(b):!!a[0].className.match(new RegExp(`(\\s|^)${b}(\\s|$)`))}addClass(a,b){a[0].classList?a[0].classList.add(b):!this.hasClass(a,b)&&(a[0].className+=` ${b}`)}removeClass(a,b){if(a[0].classList)a[0].classList.remove(b);else if(this.hasClass(a,b)){const c=new RegExp(`(\\s|^)${b}(\\s|$)`);a[0].className=a[0].className.replace(c," ")}}toggleClass(a,b){this.hasClass(a,b)?this.removeClass(a,b):this.addClass(a,b)}css(a,b,c){a[0].style[b]=c}getStyle(a,b){return a[0].style[b]}fadeOut(a,b=0.1){const c=a[0];c.style.opacity=1,function a(){0>(c.style.opacity-=b)?c.style.display="none":requestAnimationFrame(a)}()}fadeIn(a,b=0.1,c){const d=a[0];d.style.opacity=0,d.style.display=c||"block",function a(){let c=parseFloat(d.style.opacity);1<(c+=b)||(d.style.opacity=c,requestAnimationFrame(a))}()}extend(a,b){const c={};for(var d in a)Object.prototype.hasOwnProperty.call(a,d)&&(c[d]=a[d]);for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(c[d]=b[d]);return c}fn(){return DisplayJS.prototype}arange(a,b,c,d){const e=(Math.abs(b-a)+2*(d||0))/(c||1)+1,f=a<b?1:-1;return Array(e).fill(0).map((b,e)=>a-f*(d||0)+f*(c||1)*e)}range(a){return this.arange(0,a,1)}linespace(a,b,c){return this.arange(a,b,(b-a)/c)}reshape(a,b){const c=[];for(let d=0;d<a.length;d+=b)c.push(a.slice(d,d+b));return c}sum(a){return a.reduce((c,a)=>c+a,0)}multiply(a){return a.reduce((c,a)=>c*a,0)}flatten(a){return a.reduce((c,a)=>c.concat(a),[])}drop(a,b){return 0<b?a.slice(b,a.length):a.slice(0,a.length-b)}isIn(a,b){return!!a.includes(b)}rmFromArray(a,b){const c=[];for(const d in a)b(d)||c.push(a[d]);return c}average(a){const b=this.sum(a);return b/a.length}median(a){a.sort((c,a)=>c-a);const b=Math.floor(a.length/2);return a.length%2?a[b]:(a[b-1]+a[b])/2}predict(c,a,b=!1){function d(d,e){const f=c[0][0],g=c[1][0],h=c[0][1],i=c[1][1],j=(h-i)/(f-g),a=i-g*j;return!0==e?`f(x) = ${j}x+${a}; f(${d}) = ${d*j+a}`:d*j+a}return function(a){return!(2<a.length)||d(a[2][0],!1)==a[2][1]}(c)?d(a,b):"DisplayJS: Error, can't find any pattern."}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment