View file jcool-3.0.min.js

File size: 17.27Kb
/*!*CoolJS JavaScript Library v3.0 Beta*https://coolcms.ru*https://vk.com/fastgrid**Copyright Dmitry Kokorin*Released under the MIT license*https://coolcms.ru/license**Documentation:https://coolcms.ru/docs*Date:2019-08-04T00:00Z*/(function(w,d,u){'use strict';if(!d.querySelectorAll){alert('Ваш браузер устарел,пожалуйста,обновите браузер чтобы продолжить работу с сайтом');return;}function getPrefix(a){const c=a.split('-'),g=['Moz','Webkit','Ms'];let f='',gl=g.length;if(c.length>0)for(let i=0;i<c.length;++i)f+=c[i].substring(0,1).toUpperCase()+c[i].substring(1).toLowerCase();let h=d.createElement('div'),b=f.substring(0,1).toLowerCase()+f.substring(1),s,m,r;for(let i=0;i<gl;++i){s=g[i]+f,m=g[i]+b;r=typeof h.style[m]!=u?m:typeof h.style[s]!=u?s:null;if(r){h.remove();return r;}}return b;}function getFadeIn(a,t,v){let b=getPrefix('opacity'),duration=t||1000,start=w.performance.now();if(a.style[b]=='1')return false;if(getComputedStyle(a).display=='none')a.style.display=v||'block';w.requestAnimationFrame(function animate(now){const progress=now-start;a.style[b]=progress/duration;if(progress<duration){w.requestAnimationFrame(animate);}else{a.style[b]=1;}});return true;}function getFadeOut(a,t){let b=getPrefix('opacity'),duration=t||1000,start=w.performance.now();if(getComputedStyle(a).display=='none'||a.style[b]=='0')return false;w.requestAnimationFrame(function animate(now){const progress=now-start;a.style[b]=(duration-progress)/1000;if(progress<duration){w.requestAnimationFrame(animate);}else{a.style.display='none';a.style[b]=0;}});return true;}let scrolling={scrollTo:function(a,b,f){f=0;do{f+=a.offsetTop;a=a.offsetParent;}while(a);f=f>=0?f:1;w.event.preventDefault();this.animateScroll(f,b);return false;},animateScroll:function(a,b,c,f,g,h,i,j){c=c||d.documentElement||d.body,b=b||1000,f=c.scrollTop,g=a-f,h=0,i=20;c.scrollTop=w.scrollY+1;j=()=>{h+=i;c.scrollTop=Math.easeInOutQuad(h,f,g,b);if(h<b)setTimeout(j,i);};j();}};Math.easeInOutQuad=(t,b,c,d)=>{t/=d/2;if(t<1)return c/2*t*t+b;--t;return-c/2*(t*(t-2)-1)+b;};class App{constructor(selector,array){array=typeof selector=='string'&&/<[a-z]+>/.test(selector)?[d.createElement(selector.match(/[a-z]+/)[0])]:selector==w?[w]:typeof selector=='string'?d.querySelectorAll(selector):selector.length?selector:[selector];this.selector=selector;this.length=array.length;for(let i=0;i<this.length;++i)this[i]=array[i];}addClass(a){for(let i=0;i<this.length;++i)this[i].classList.add(a);return this;}after(a){for(let i=0;i<this.length;++i)this[i].appendChild(a);return this;}append(a){for(let i=0;i<this.length;++i)this[i].innerHTML+=a;return this;}attr(a,b){b=b||null;if(b){for(let i=0;i<this.length;++i)this[i].setAttribute(a,b);return this;}return this.element[0].getAttribute(a);}before(a){for(let i=0;i<this.length;++i)this[i].insertBefore(a,this[i].firstChild);return this;}blur(){for(let i=0;i<this.length;++i)this[i].blur();return this;}changeClass(a,b){for(let i=0;i<this.length;++i){this[i].classList.remove(a);this[i].classList.add(b);}return this;}checked(){return this[0].checked;}child(a,b,c){b=[];for(let i=0;i<this.length;++i){c=a?this[i].querySelectorAll(a):this[i].children;for(let j=0;j<c.length;++j)b.push(c[j]);}for(let i=0;i<this.length;++i)this[i]=null;for(let i=0;i<b.length;++i)this[i]=b[i];return this;}clear(a){for(let i=0;i<this.length;++i){a=this[i];if(a.tagName=='INPUT'){if(['hidden','checkbox','file'].indexOf(a.type)==-1)a.value='';}else if(a.tagName=='TEXTAREA'){a.innerHTML='';}else{continue;}}return this;}css(a){if(typeof a=='object'){for(let b in a)for(let i=0;i<this.length;++i)this[i].style[getPrefix(b)]=a[b];}else if(typeof a=='string'){return this[0].style[getPrefix(a)];}else{return null;}return this;}coolModal(a,b){a=a||{};if(a=='close'){for(let i=0;i<this.length;++i){if(this[i].style.display=='block')new Modal('.'+this[i].classList[1]).init({action:'close'});}return this;}b=new Modal(this.selector);b.init(a);return b;}coolSlider(a){if(typeof a!=u){let b=new Slider(this.selector);b.init(a);return b;}else{return new Slider(this.selector);}}data(a){return this[0].getAttribute('data-'+a);}delay(t,fn){if(typeof fn!='function')return false;setTimeout(fn,t*1000);}each(a){for(let i=0;i<this.length;++i)a.apply(this[i],arguments);return this;}empty(){for(let i=0;i<this.length;++i)this[i].innerHTML='';return this;}fadeIn(a,b){for(let i=0;i<this.length;++i)getFadeIn(this[i],a,b);return this;}fadeOut(a){for(let i=0;i<this.length;++i)getFadeOut(this[i],a);return this;}files(a){a=[];for(let i=0;i<this.element.length;++i)for(let j=0;j<this.element[i].files.length;++j)a.push(this.element[i].files[j]);return a;}find(a,b,c){if(typeof a==u)return [];b=[];for(let i=0;i<this.length;++i){c=this[i].querySelectorAll(a);for(let j=0;j<c.length;++j)b.push(c[j]);}for(let i=0;i<this.length;++i)this[i]=null;for(let i=0;i<b.length;++i)this[i]=b[i];return this;}focus(){this[0].focus();return this;}hasClass(a,b){b=b||0;return this[b].classList.contains(a);}height(a){a=a||null;if(this[0]==w||this[0]==d){return this[0].innerHeight;}else{if(a){for(let i=0;i<this.length;++i)this[i].style.height=a;return this;}else{return this[0].scrollHeight;}}}hide(){for(let i=0;i<this.length;++i)this[i].style.display='none';return this;}html(a){if(typeof a!=u){for(let i=0;i<this.length;++i)this[i].innerHTML=typeof a=='function'?a():a;return this;}else{return this[0].innerHTML;}}insertBefore(a){for(let i=0;i<this.length;++i)this[i].parentNode.insertBefore(a,this[i].parentNode.firstChild);return this;}insertAfter(a){for(let i=0;i<this.length;++i)this[i].parentNode.appendChild(a);return this;}load(fn){for(let i=0;i<this.length;++i)this[0].addEventListener('load',fn);return true;}next(a,b,c){b=[];for(let i=0;i<this.length;++i){if(a){c=this[i].parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this[i].nextElementSibling)b.push(this[i].nextElementSibling);}else{b.push(this[i].nextElementSibling);}}for(let i=0;i<this.length;++i)this[i]=null;for(let i=0;i<b.length;++i)this[i]=b[i];return this;}off(a,b){b=typeof b=='function'?b:()=>{};let i=0,t=w.setInterval(()=>{if(i<this.length){this[i].removeEventListener(a,b);++i;}else{w.clearInterval(t);}},20);return this;}offset(){return this[0].getBoundingClientRect();}on(a,b,c,f,g){if(typeof c=='function'){f=d.querySelectorAll(b);g=c;}else{f=this;g=b;}d.addEventListener(a,function(e){for(let i=0;i<f.length;++i)if(e.target==f[i]||e.target.parentNode==f[i])return g.apply(f[i],arguments);});}outerHtml(a){if(a){for(let i=0;i<this.length;++i)this[i].outerHTML=typeof a=='function'?a():a||'';return this;}else{return this[0].outerHTML;}}parent(a,b,c,f){b=[];for(let i=0;i<this.length;++i){f=this[i].parentNode;if(a){c=f.parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==f)b.push(f);}else{b.push(f);}}for(let i=0;i<this.length;++i)this[i]=null;for(let i=0;i<b.length;++i)this[i]=b[i];return this;}prepend(a){for(let i=0;i<this.length;++i)this[i].innerHTML=a+this[i].innerHTML;return this;}prev(a){let b=[];for(let i=0;i<this.length;++i){if(a){let c=this[i].parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this[i].previousElementSibling)b.push(this[i].previousElementSibling);}else{b.push(this[i].previousElementSibling);}}for(let i=0;i<this.length;++i)this[i]=null;for(let i=0;i<b.length;++i)this[i]=b[i];return this;}ready(fn){if(this[0]!=d||!fn)return false;d.addEventListener('DOMContentLoaded',fn);return true;}remove(){for(let i=0;i<this.length;++i)this[i].remove();return this;}removeAttr(a){for(let i=0;i<this.length;++i)this[i].removeAttribute(a);return this;}removeClass(a){for(let i=0;i<this.length;++i)this[i].classList.remove(a);return this;}scroll(fn,self){self=this;w.addEventListener('scroll',function(e){for(let i=0;i<self.length;++i)if(e.target==w||e.target==d||e.target==self[i]||e.target.parentNode==self[i])return fn.apply(self[i],arguments);});}scrollTo(a,b,fn){typeof a=='number'?scrolling.animateScroll(a,b):scrolling.scrollTo(d.querySelector(a),b);if(typeof fn=='function')fn();return this;}serialized(a,b){a=this[0].querySelectorAll('input,select,textarea'),b={};for(let i=0;i<a.length;++i){if(a[i].type=='file'){for(let j=0;j<a[i].files.length;++j)b[a[i].getAttribute('name')+j]=a[i].files[j];}else{b[a[i].getAttribute('name')]=a[i].value;}}return b;}slideDown(a){}slideToggle(a){}slideUp(){}show(a){for(let i=0;i<this.length;++i)this[i].style.display=a||'block';return this;}top(){return this[0].scrollY||null;}toggleClass(a){for(let i=0;i<this.length;++i)this[i].classList.toggle(a);return this;}trigger(a,b){if(typeof a==u)return;if(this[0]==w||this[0]==d){this[0].dispatchEvent(new Event(a));}else{for(let i=0;i<this.length;++i)this[i].dispatchEvent(new Event(a));}if(typeof b=='function')b();return this;}val(a){a=a||a===''?a:null;if(a){for(let i=0;i<this.length;++i)this[i].value=a;return true;}else{return this[0].value;}}validated(fn){let b=this[0].querySelectorAll('input,select,textarea'),c=0,incorrect='Некорректный номер телефона<br>пример:+79123456789,89123456789,88001234567 или 642531';for(let key in b){if(isFinite(key)){let k=b[key],valid=k.validity,error=k.getAttribute('error')||null;if(k.required){if(k.tagName==='INPUT'){if(k.type==='number'){if(k.min&&valid.rangeUnderflow){error=error||'Минимальное значение поля '+k.min;++c;}if(k.max&&valid.rangeOverflow){error=error||'Максимальное значение поля '+k.max;++c;}}else if(k.type==='tel'){let kv=k.value,kvl=kv.length;if(kvl<5){error=error||incorrect;++c;}else if(kvl>7&&(kvl<11||kvl>12)){error=error||incorrect;++c;}else if(kvl===11){if(!/^8[8|9]{1}[\d]{2}[\d]{7}$/.test(kv)){error=error||incorrect;++c;}}else if(kvl===12){if(!/^\+79[\d]{2}[\d]{7}$/.test(kv)){error=error||incorrect;++c;}}else if(kvl>4&&kvl<8){if(!/^[\d]{5,7}$/.test(kv)){error=error||incorrect;++c;}}}else if(k.type==='email'){if(valid.typeDismatch){error=error||'Некорректный ввод email<br>пример:[email protected]';++c;}}if(k.pattern&&valid.patternMismatch){error=error||'Проверьте правильность заполнения полей';++c;}if(valid.typeDismatch){error=error||'Некорректное значение "'+k.value+'"';++c;}}if(valid.valueMissing){error=error||'Обязательные поля не заполнены';++c;}if(c>0){if(typeof fn=='function')fn();return{error:error,valid:false};}}}}return{error:false,valid:true};}}if(typeof w.$!=u){w.jCool=(a)=>new App(a);}else{w.$=(a)=>new App(a);}class Set{ajax(a){let c=new XMLHttpRequest(),b={type:'POST',url:null,success:null,json:true,error:null,files:null,before:null,callback:null,asinc:true,data:null};for(let f in b)if(typeof a[f]!=u)b[f]=a[f];a=null;if(typeof b.before=='function')b.before();c.open(b.type,b.url,b.asinc);c.onreadystatechange=function(){try{if(this.readyState!==4||this.status!==200)return;if(b.success)if(b.json){b.success(JSON.parse(this.responseText));}else{b.success(this.responseText);}}catch(e){if(b.error)b.error(e);}};if(b.files){c.send(this.formFiles(b.data));}else{c.send(this.form(b.data));}return typeof b.callback=='function'?b.callback():this;}bgImage(a,b){if(b.type.match(/image.*/)){let reader=new FileReader();reader.onload=function(e){new App(a).css({'background-image':'url('+e.target.result+')'});};reader.readAsDataURL(b);}}copy(a){if(typeof a==u)return false;let b,c=d.createElement('input');c.value=a;d.body.append(c);c.select();if(d.execCommand('copy')){b=true;}else{b=false;}w.getSelection().removeAllRanges();c.remove();localStorage.setItem('clip',a);return b;}countPlus(a){let f=a.dataset,g={count:100,duration:1000,delay:100,start:0};for(let h in g)if(typeof f[h]!=u)g[h]=f[h];f=null;let step=parseFloat(g.count/100);setTimeout(function(){let interval=setInterval(function(){if(g.start+step>=parseFloat(g.count)){a.innerHTML=g.count;clearInterval(interval);}g.start+=step;a.innerHTML=parseInt(g.start);},g.duration/100);},g.delay);}delStorage(a){if(this.getStorage(a))localStorage.removeItem(a);return this;}find(a,b,c,f){a=a||null,b=b||null;if(!a||!b)return null;try{c=new RegExp(b);f=a.match(c);}catch(e){f=a.match(b);}return f;}form(a,b){b=b||new FormData();for(let key in a)if(a.hasOwnProperty(key))b.append(key,a[key]);return b;}formFiles(a,b){b=b||new FormData();for(let i=0;i<a.length;++i)b.append(i,a[i]);return b;}getStorage(a){return localStorage.getItem(a)||null;}image(a,b){if(b.type.match(/image.*/)){let reader=new FileReader();reader.onload=function(e){let img=new Image();img.src=e.target.result;a.append(img);};reader.readAsDataURL(b);}}imgPreview(a,b,c){let f=a.files,g=d.querySelector(b),h=d.querySelector(c);function reader_load(e){e=e||w.event;let img=new Image();img.src=e.target.result;h.appendChild(img);}if(f.length){h.innerHTML='';let reader=new FileReader();for(let i=0;i<f.length;++i){if(f[i].type.match(/image.*/)){reader.onload=reader_load();reader.readAsDataURL(f[i]);}}g.innerHTML=f.length==1?f[0].name+'('+(f[0].size/1024).toFixed(2)+' Кб)':'Добавлено '+f.length+' файлов';}else{g.innerHTML='Выберите файл';h.innerHTML='Предпросмотр';}return this;}json(a){return JSON.parse(a);}string(a){return JSON.stringify(a);}log(){for(let i=0;i<arguments.length;++i)console.info(arguments[i]);return this;}setStorage(a,b){if(typeof a=='object'){for(let i in a)localStorage.setItem(i,a[i]);}else{localStorage.setItem(a,b);}return this;}sleep(a,fn){setTimeout(fn||function(){},a);return this;}translite(a,b){let c=typeof b!=u?{}:{'а':'a','б':'b','в':'v','г':'g','д':'d','е':'e','ж':'g','з':'z','и':'i','й':'y','к':'k','л':'l','м':'m','н':'n','о':'o','п':'p','р':'r','с':'s','т':'t','у':'u','ф':'f','ы':'y','э':'e','А':'A','Б':'B','В':'V','Г':'G','Д':'D','Е':'E','Ж':'G','З':'Z','И':'I','Й':'Y','К':'K','Л':'L','М':'M','Н':'N','О':'O','П':'P','Р':'R','С':'S','Т':'T','У':'U','Ф':'F','Ы':'Y','Э':'E','ё':'yo','х':'h','ц':'ts','ч':'ch','ш':'sh','щ':'shch','ъ':'','ь':'','ю':'yu','я':'ya','Ё':'YO','Х':'H','Ц':'TS','Ч':'CH','Ш':'SH','Щ':'SHCH','Ъ':'','Ь':'','Ю':'YU','Я':'YA',' ':'_','.':'_',',':'_','?':'_','!':'_','-':'_','=':'_','+':'_','__':'_'};return a.replace(/[\s\S]/g,(x)=>{if(c.hasOwnProperty(x))return c[x];return x;});}window(a){let b={width:w.screen.width,height:w.screen.height,url:null,x:w.screen.width/2,y:w.screen.height/2,before:null,callback:null,full:0,tool:0,status:0,menu:0,resize:0,focus:true};for(let c in b)if(typeof a[c]!=u)b[c]=a[c];if(typeof b.before=='function')b.before();let e=w.open('/','White..','fullscreen='+b.full+',toolbar='+b.tool+',status='+b.status+',menubar='+b.menu+',resizable='+b.resize+',width='+b.width+',height='+b.height);e.blur();if(b.url)e.location.href=b.url;e.moveTo(b.x,b.y);if(b.focus)e.focus();if(typeof b.callback=='function')b.callback();return e;}}class Modal{constructor(a){this.string=a||null;this.box=d.querySelector(this.string)||this.string||null;}init(a,b){if(!this.box)return;a=a||{},b={action:'open',over:'.modal_overlay',old:null,new:null,time:null,before:()=>{},callback:()=>{}};for(let c in b)this[c]=typeof a[c]!=u?a[c]:b[c];a=null;if(this.action=='open'){this.open();}else if(this.action=='close'){this.close();}else if(this.action=='change'){this.change();}return this;}open(){if(!this.box)return;this.before();if(this.over)getFadeIn(d.querySelector(this.over),500);getFadeIn(this.box,1000);this.time?setTimeout(()=>this.close(this.callback),this.time*1000):this.callback();return this;}close(a){if(!this.box)return;if(this.over)getFadeOut(d.querySelector(this.over),1000);getFadeOut(this.box,500);if(typeof a=='function')a();return this;}change(){if(!this.box||!this.new)return;getFadeOut(this.box);getFadeIn(d.querySelector(this.new));this.box=this.new;}}class Slider{constructor(a){this.string=a;this.box=d.querySelector(a)||null;}init(a){if(!this.box)return;a=a||{};let b={autoplay:true,before:false,callback:false,currentSlide:0,delay:3000,dots:true,dots_box:null,dots_list:[],interval:null,method:'fade'};for(let c in b)this[c]=typeof a[c]!=u?a[c]:b[c];this.slides=this.box.querySelectorAll('.slide');this.length=this.slides.length;if(!this.length)return;if(this.dots===true){if(this.dots_box===null){let br=d.createElement('ul');br.className='slider_dots';for(let i=0;i<this.length;++i){let dot=d.createElement('li');dot.setAttribute('data-slide',i);dot.className='slider_dot';br.appendChild(dot);this.dots_list.push(dot);}d.querySelector(this.string).appendChild(br);}else{this.dots_list=d.querySelectorAll(this.dots_box+' .slider_dot');}}this.slides[this.currentSlide].classList.add('slide_active');if(this.dots)this.dots_list[this.currentSlide].classList.add('dot_active');if(this.autoplay===true)this.start();return this;}start(){if(!this.box||!this.length)return;this.interval=w.setInterval(()=>this.next(),this.delay);}stop(){w.clearInterval(this.interval);this.interval=null;}prev(){if(!this.box||!this.length)return;this.stop();this.go(this.currentSlide-1);this.start();}next(){if(!this.box||!this.length)return;this.stop();this.go(this.currentSlide+1);this.start();}go(n){if(!this.box||!this.length)return;if(typeof this.before=='function')this.before();this.slides[this.currentSlide].classList.remove('slide_active');if(this.dots)this.dots_list[this.currentSlide].classList.remove('dot_active');this.currentSlide=parseInt((n+this.length)% this.length);this.slides[this.currentSlide].classList.add('slide_active');if(this.dots)this.dots_list[this.currentSlide].classList.add('dot_active');return typeof this.callback=='function'?this.callback():this;}}w.fn=new Set();})(window,document,'undefined');