spacerjs
Version:
Manipulate the DOM easily
1 lines • 8.88 kB
JavaScript
;let customFunctions={};function _(elements,create,contents,strict){let element=null,nodelist=[],meta={},state={hidden:!1,hovered:!1,active:!1};var fn=customFunctions;element="string"==typeof elements?document.querySelector(elements):elements;const methods={__:function(selector){return nodelist=document.querySelectorAll(selector)||null,methods.nodelist=nodelist,methods},append:function(elem){return null===element||void 0===element||element.append(elem),methods},appendTo:function(parent){var _a;"string"==typeof parent?!element||null!==(_a=document.querySelector(parent))&&void 0!==_a&&_a.appendChild(element):element&&parent.appendChild(element);return methods},appendChild:function(child){return element&&element.appendChild(child),methods},appendList:function(elementlist){return elementlist.forEach(element=>{element&&element.appendChild(element)}),methods},html:function(str){return element&&(element.innerHTML+=str),methods},text:function(str){return element&&(element.innerText+=str),methods},click:function(){return element&&element.click(),methods},focus:function(){return element&&element.focus(),methods},unfocus:function(){return element&&element.blur(),methods},setTitle:function(str){return element&&(element.title=str),methods},sToView:function(alignTo){return element&&(alignTo?element.scrollIntoView(alignTo):element.scrollIntoView()),methods},toStr:function(){return element?element.toString():null},contains:function(node){return element?element.contains(node):null},clone:function(clone){clone=clone?null===element||void 0===element?void 0:element.cloneNode(clone):null===element||void 0===element?void 0:element.cloneNode();return clone},hasAttr:function(attribute){return element?element.hasAttribute(attribute):null},attr:function(attribute,value){return null===element||void 0===element||element.setAttribute(attribute,value),methods},kill:function(){return null===element||void 0===element||element.remove(),methods},hasChildren:function(normalize){return normalize&&(null===element||void 0===element||element.normalize()),null===element||void 0===element?void 0:element.hasChildNodes()},compareNode:function(node){return null===element||void 0===element?void 0:element.isSameNode(node)},normalize:function(){return null===element||void 0===element||element.normalize(),methods},removeChild:function(node){return null===element||void 0===element||element.removeChild(node),methods},replaceChild:function(newChild,oldChild){return null===element||void 0===element||element.replaceChild(newChild,oldChild),methods},setMeta:function(obj){return meta=obj,methods},show:function(){return meta.hiddenClass&&(null===element||void 0===element||element.classList.remove(meta.hiddenClass)),state.hidden=!1,methods},hide:function(){return meta.hiddenClass&&(null===element||void 0===element||element.classList.add(meta.hiddenClass)),state.hidden=!0,methods},on:function(event,cb){let eventArr=event.split(" ");return eventArr.forEach((value,index)=>{Array.isArray(cb)?null!==element&&void 0!==element&&element.addEventListener(value,cb[index]):null!==element&&void 0!==element&&element.addEventListener(value,cb)}),methods},addClass:addClass,removeClass:function(className){return null===element||void 0===element||element.classList.remove(className),methods},children:function(){return null===element||void 0===element?void 0:element.childNodes},dbclick:function(){return null===element||void 0===element||element.click(),null===element||void 0===element||element.click(),methods},sleep:function(ms){return new Promise(resolve=>setTimeout(resolve,ms))},keydown:function(cb){return null===element||void 0===element||element.addEventListener("keydown",cb()),methods},keyup:function(cb){return null===element||void 0===element||element.addEventListener("keydown",cb()),methods},debug:function(...args){return console.log(...args),methods},hover:function(){return meta.hoverClass&&(null===element||void 0===element||element.classList.add(meta.hoverClass)),state.hovered=!0,methods},unhover:function(){return meta.hoverClass&&(null===element||void 0===element||element.classList.remove(meta.hoverClass)),state.hovered=!1,methods},active:function(){return meta.activeClass&&(null===element||void 0===element||element.classList.add(meta.activeClass)),state.active=!0,methods},unactive:function(){return meta.activeClass&&(null===element||void 0===element||element.classList.remove(meta.activeClass)),state.active=!1,methods},css:function(pseudoElement){let returned=null;return returned=element?pseudoElement?getComputedStyle(element,pseudoElement):getComputedStyle(element):null,returned},removeChildren:function(){for(;null!==element&&void 0!==element&&element.firstChild;)null!==element&&void 0!==element&&element.removeChild(element.firstChild);return methods},empty:function(){if(null!==element&&void 0!==element&&element.hasChildNodes())for(let i=0;i<(null===element||void 0===element?void 0:element.children.length);i++){let htmlstring=null===element||void 0===element?void 0:element.children[i].innerHTML;htmlstring=htmlstring.trim?htmlstring.trim():htmlstring.replace(/^\s+/,""),""==htmlstring&&null!==element&&void 0!==element&&element.children[i].remove()}},parent:function(){return null===element||void 0===element?void 0:element.parentElement},first:function(node){return node?null===element||void 0===element?void 0:element.firstChild:null===element||void 0===element?void 0:element.firstElementChild},last:function(node){return node?null===element||void 0===element?void 0:element.lastChild:null===element||void 0===element?void 0:element.lastElementChild},at:function(index){0==index&&console.warn("SpacerJS: _().at(), index is 0, index is not array 0 based");return null===element||void 0===element?void 0:element.children[index-1]},each:function(cb){return null===nodelist||void 0===nodelist||nodelist.forEach(cb.bind(methods)),methods},trigger:function(event){return null===element||void 0===element||element.dispatchEvent(new Event(event)),methods},size:size,prepend:function(elem){return null===element||void 0===element||element.prepend(elem),methods},prependTo:function(parent){var _a;"string"==typeof parent?!element||null!==(_a=document.querySelector(parent))&&void 0!==_a&&_a.appendChild(element):element&&parent.prepend(element);return methods},prependList:function(elementlist){return elementlist.forEach(element=>{element&&element.prepend(element)}),methods},randomChild:function(){return null!==element&&void 0!==element&&element.children?Math.floor(Math.random()*(+(null===element||void 0===element?void 0:element.children.length)+1)+0):null},fadeOut:function(intensity){let tempNum=1,inten=intensity||.05;element&&(element.style.opacity="1");let int=setInterval(()=>{element&&(0<tempNum?(tempNum-=inten,element.style.opacity=`${tempNum}`):(element.style.opacity="0",clearInterval(int)))},100);return methods},fadeIn:function(intensity){let tempNum=0,inten=intensity||.05;element&&(element.style.opacity="0");let int=setInterval(()=>{element&&(tempNum<1?(tempNum+=inten,element.style.opacity=`${tempNum}`):(element.style.opacity="1",clearInterval(int)))},100);return methods},animate:function(cssProperty,value,time){let className=`SpacerJS-Animation-${Math.floor(Math.random()*(1e5+1)+0)}`,style=document.createElement("style");return style.innerHTML=`.${className} { ${cssProperty}: ${value}; transition: ${cssProperty} ${time}; }`,head.appendChild(style),addClass(className),methods},transform:function(cssValue){element&&(element.style.transform=cssValue);return methods},rotate:function(cssValue){element&&(element.style.transform=`rotate(${cssValue})`);return methods},removeMetaProp:function(metaKey){"hiddenClass"==metaKey&&(meta.hiddenClass="");"hoverClass"==metaKey&&(meta.hoverClass="");"activeClass"==metaKey&&(meta.activeClass="");return methods},deleteMeta:function(){return meta.hiddenClass="",meta.hoverClass="",meta.activeClass="",methods},val:function(newValue){if(element instanceof HTMLInputElement)return newValue&&(element.value=newValue),element.value},offset:function(){var _b;return{top:null===(_b=size())||void 0===_b?void 0:_b.top,left:null===(_b=size())||void 0===_b?void 0:_b.left}},element:element,nodelist:nodelist,meta:meta,state:state,fn:fn};function addClass(className){return null!==element&&void 0!==element&&element.classList.add(className),methods}function size(){return null===element||void 0===element?void 0:element.getBoundingClientRect()}return create&&"string"==typeof elements&&(element=document.createElement(elements)),contents&&"string"==typeof elements&&(elements=document.querySelectorAll(elements),null==strict&&(strict=!0),nodelist=0==strict?[...elements].filter(e=>e.innerHTML.includes(contents)):[...elements].filter(e=>e.innerHTML==contents)),methods}let body=document.body,head=document.head;function _appendCustom(key,func){return customFunctions[key]=func,customFunctions}