////////////////////////////////////////////////////////////////////// // CLib function CLib(){ //////////////////////////////////////////////////////////////////// // $shorthands function $(selector,el_ref){ return (el_ref?el_ref:document).querySelector(selector); }; function $$(selector,el_ref){ return (el_ref?el_ref:document).querySelectorAll(selector); }; function $id(id){ return document.getElementById(id); }; function $ce(tagName){ return document.createElement(tagName); }; //////////////////////////////////////////////////////////////////// // utils function emptyObject(x){ if(x){ if(x.constructor===Object){ for(const key in x){delete x[key];} }else if(x.constructor===Array){ x.length=0; } } }; function isEmptyObject(x){ for(const i in x){return false;} return true; }; function isObject(x){ return(x&&(x.constructor===Object)); }; //////////////////////////////////////////////////////////////////// // end events function onEnd(el,callback,ename){ const func=function(e){ if(el==e.srcElement){ el.removeEventListener(ename,func); if(callback){callback(e);} } }; el.addEventListener(ename,func); }; function onTransitionEnd(el,callback){ onEnd(el,callback,"transitionend"); }; function onAnimationEnd(el,callback){ onEnd(el,callback,"animationend"); }; function classDuringTransition(el,clsname,callback){ el.offsetHeight;// for reflow if(el.classList.contains(clsname)){ onTransitionEnd(el,function(e){ el.classList.add(clsname); if(callback){callback(e);} }); el.classList.remove(clsname); }else{ onTransitionEnd(el,function(e){ el.classList.remove(clsname); if(callback){callback(e);} }); el.classList.add(clsname); } }; function classDuringAnimation(el,clsname,callback){ el.offsetHeight;// for reflow if(el.classList.contains(clsname)){ onAnimationEnd(el,function(e){ el.classList.add(clsname); if(callback){callback(e);} }); el.classList.remove(clsname); }else{ onAnimationEnd(el,function(e){ el.classList.remove(clsname); if(callback){callback(e);} }); el.classList.add(clsname); } }; //////////////////////////////////////////////////////////////////// // return return{ $, $$, $ce, $id, emptyObject, isEmptyObject, isObject, onEnd, onTransitionEnd, onAnimationEnd, classDuringTransition, classDuringAnimation, }; };