Просмотр файла Cool JS v2.6/jcool-2.6.min.js

Размер файла: 20.04Kb
/*!
 * CoolJS JavaScript Library v2.6
 * https://coolcms.ru/
 * https://vk.com/fastgrid
 *
 * Copyright Dmitry Kokorin
 * Released under the MIT license
 * https://coolcms.ru/license/
 *
 * Documentation: https://coolcms.ru/docs/5-cool_js
 * Date: 2018-01-20T17:24Z
 */
(function(w,d){'use strict';if(!d.querySelectorAll){alert('Ваш браузер устарел,пожалуйста,обновите браузер чтобы продолжить работу с сайтом');return}function getPrefix(a){const prefixes=['Moz','Webkit','Ms'],div=d.createElement('div');let c=a.split('-'),f='';if(c.length>0)for(let i=0;i<c.length;++i)f+=c[i].substring(0,1).toUpperCase()+c[i].substring(1).toLowerCase();for(let i=0;i<prefixes.length;++i){let s=prefixes[i]+f,m=prefixes[i]+f.substring(0,1).toLowerCase()+f.substring(1);if(typeof div.style[m]!='undefined'){div.remove();return m}else if(typeof div.style[s]!='undefined'){div.remove();return s}}return f.substring(0,1).toLowerCase()+f.substring(1)}function getFadeIn(a){let s=a.style,ani;if(s.display=='block'&&s[getPrefix('opacity')]===1)return;s[getPrefix('opacity')]=0;s.display='block';ani=requestAnimationFrame(animate);function animate(){if(s[getPrefix('opacity')]>1){s[getPrefix('opacity')]=1;cancelAnimationFrame(ani)}else{s[getPrefix('opacity')]=+s[getPrefix('opacity')]+0.1;cancelAnimationFrame(ani);ani=requestAnimationFrame(animate)}}return true}function getFadeOut(a){let s=a.style,ani;if(s.display=='none'||s[getPrefix('opacity')]===0)return;ani=requestAnimationFrame(animate);function animate(){if(s[getPrefix('opacity')]<0){s.display='none';s[getPrefix('opacity')]=0;cancelAnimationFrame(ani)}else{s[getPrefix('opacity')]=+s[getPrefix('opacity')]-0.1;cancelAnimationFrame(ani);ani=requestAnimationFrame(animate)}}return true}let scrolling={scrollTo:function(anchor,duration){let scroll=0;do{scroll+=anchor.offsetTop;anchor=anchor.offsetParent}while(anchor);scroll=scroll>=0?scroll:1;if(w.event.preventDefault)w.event.preventDefault();w.event.returnValue=false;duration=duration||null;this.animateScroll(scroll,duration);return false},animateScroll:function(pos,duration){d.documentElement.scrollTop=w.scrollY+1;let element=(d.documentElement&&d.documentElement.scrollTop)?d.documentElement:d.body,start=element.scrollTop,change=pos-start,currentTime=0,increment=20;duration=duration||1000;let animateScroll=function(){currentTime+=increment;let val=Math.easeInOutQuad(currentTime,start,change,duration);element.scrollTop=val;if(currentTime<duration)setTimeout(animateScroll,increment)};animateScroll()}};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(a){this.string=a||null;this.isString=typeof this.string=='string';this.isObject=this.string=='object';if(this.isString&&/<[a-z]+><\/[a-z]+>/.test(a)){let z=a.match(/[a-z]+/);if(z.length<1)return;this.element=d.createElement(z[0])}else{this.element=this.isString?(a.charAt(0)=='['?d.getElementsByName(a.substr(1).slice(0,-1)):d.querySelectorAll(this.string)):(this.string||null)}}addClass(a){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].classList.add(a)}else{this.element.classList.add(a)}return this}after(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].appendChild(a)}else{this.element.appendChild(a)}return this}append(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].innerHTML=this.element[i].innerHTML+a}else{this.element.innerHTML=this.element.innerHTML+a}return this}attr(a,b){b=b||null;if(this.isString||this.isObject){if(b){for(let i=0;i<this.element.length;++i)this.element[i].setAttribute(a,b);return this}return this.element[0].getAttribute(a)}else{return b?(this.element.setAttribute(a,b),this):this.element.getAttribute(a)}}before(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].insertBefore(a,this.element[i].firstChild)}else{this.element.insertBefore(a,this.element.firstChild)}return this}blur(){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].blur()}else{this.element.blur()}return this}changeClass(a,b){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i){this.element[i].classList.remove(a);this.element[i].classList.add(b)}}else{this.element.classList.remove(a);this.element.classList.add(b)}return this}checked(){return(this.isString||this.isObject)?(this.element[0].getAttribute('checked')=='checked'?true:false):(this.element.getAttribute('checked')=='checked'?true:false)}child(a){let b=[];if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i){let c=a?this.element[i].querySelectorAll(a):this.element[i].children;for(let j=0;j<c.length;++j)b.push(c[i])}}else{let c=a?this.element.querySelectorAll(a):this.element.children;for(let j=0;j<c.length;++j)b.push(c[i])}this.element=b;return this}css(a){a=a||{};for(let b in a){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].style[getPrefix(b)]=a[b]}else{this.element.style[getPrefix(b)]=a[b]}}return this}data(a){return this.isString?this.element[0].getAttribute('data-'+a):this.element.getAttribute('data-'+a)}each(a){for(let i=0;i<this.element.length;++i)a.apply(this.element[i],arguments);return this}empty(){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].innerHTML=''}else{this.element.innerHTML=''}return this}fadeIn(){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)getFadeIn(this.element[i])}else{getFadeIn(this.element)}return this}fadeOut(){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)getFadeOut(this.element[i])}else{getFadeOut(this.element)}return this}files(){let a=[];if(this.isString||this.isObject){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}else{for(let i=0;i<this.element.files;++i)a.push(this.element.files[i]);return a}}focus(){if(this.isString||this.isObject){this.element[0].focus()}else{this.element.focus()}return this}hasClass(a,b){b=b||0;return(this.isString||this.isObject)?this.element[b].classList.contains(a):this.element.classList.contains(a)}height(a,b){a=typeof a!='undefined'?a:null;b=b||0;if(this.string==w){return w.innerHeight}else if(this.string==d){return d.innerHeight}else if(this.isString||this.isObject){if(a){for(let i=0;i<this.element.length;++i)this.element[i].style.height=a;return this}else{return this.element[b].scrollHeight}}else{return a?(this.element.style.height=a,this):this.element.scrollHeight}}hide(){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].style.display='none'}else{this.element.style.display='none'}return this}html(a,b){a=typeof a!='undefined'?a:null;b=b||0;if(this.isString||this.isObject){if(a){for(let i=0;i<this.element.length;++i)this.element[i].innerHTML=typeof a=='function'?a():a;return this}else{return this.element[b].innerHTML}}else{if(a){this.element.innerHTML=typeof a=='function'?a():a;return this}else{return this.element.innerHTML}}}insertBefore(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].parentNode.insertBefore(a,firstChild)}else{this.element.parentNode.insertBefore(a,firstChild)}return this}insertAfter(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].parentNode.appendChild(a)}else{this.element.parentNode.appendChild(a)}return this}next(){let b=[];if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i){if(a){let c=this.element[i].parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element[i].nextElementSibling)b.push(this.element[i].nextElementSibling)}else{b.push(this.element[i].nextElementSibling)}}}else{if(a){let c=this.element.parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element.nextElementSibling)b.push(this.element.nextElementSibling)}else{b.push(this.element.nextElementSibling)}}this.element=b;return this}off(a,b){b=typeof b=='function'?b:null;if(this.isString||this.isObject){let i=0,j=this,t=w.setInterval(()=>{if(i<j.element.length){j.element[i].removeEventListener(a,b);++i}else{w.clearInterval(t)}},20)}else{this.element.removeEventListener(a,b)}return this}offset(){return(this.isString||this.isObject)?this.element[0].getBoundingClientRect():this.element.getBoundingClientRect()}on(a,b,c){if(typeof c=='function'){d.addEventListener(a,function(e){let elem=d.querySelectorAll(b);for(let i=0;i<elem.length;++i)if(e.target==elem[i])return c.apply(e.target,arguments)},false)}else if(this.isString){let i=0,self=this,tmr=setInterval(()=>{if(i<self.element.length){self.element[i].addEventListener(a,function(e){return b.apply(e.target,arguments)},false);++i}else{clearInterval(tmr)}},20)}else{this.string.addEventListener(a,function(e){return b.apply(e.target,arguments)},false)}}outerHtml(a,b){a=a||null;b=b||0;if(this.isString||this.isObject){if(a){for(let i=0;i<this.element.length;++i)this.element[i].outerHTML=typeof a=='function'?a():a;return this}else{return this.element[b].outerHTML}}else{if(a){this.element.outerHTML=typeof a=='function'?a():a;return this}else{return this.element.outerHTML}}}parent(a){b=[];if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i){if(a){let c=this.element[i].parentNode.parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element[i].parentNode)b.push(this.element[i].parentNode)}else{b.push(this.element[i].parentNode)}}}else{if(a){let c=this.element.parentNode.parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element.parentNode)b.push(this.element.parentNode)}else{b.push(this.element.parentNode)}}this.element=b;return this}prepend(a){if(this.isString){for(let i=0;i<this.element.length;++i)this.element[i].innerHTML=a+this.element[i].innerHTML}else{this.element.innerHTML=a+this.element.innerHTML}return this}prev(a){let b=[];if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i){if(a){let c=this.element[i].parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element[i].previousElementSibling)b.push(this.element[i].previousElementSibling)}else{b.push(this.element[i].previousElementSibling)}}}else{if(a){let c=this.element.parentNode.querySelectorAll(a);for(let j=0;j<c.length;++j)if(c[j]==this.element.previousElementSibling)b.push(this.element.previousElementSibling)}else{b.push(this.element.previousElementSibling)}}this.element=b;return this}ready(fn){if(this.string!=d||!fn)return;d.addEventListener('DOMContentLoaded',fn,false);return true}remove(){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].remove()}else{this.element.remove()}return this}removeAttr(a){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].removeAttribute(a)}else{this.element.removeAttribute(a)}return this}removeClass(a){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].classList.remove(a)}else{this.element.classList.remove(a)}return this}scrollTo(a,b,fn){typeof a=='number'?scrolling.animateScroll(a,b):scrolling.scrollTo(d.querySelector(a),b);return typeof fn=='function'?fn():this}serialized(){let a=(this.isString||this.isObject)?this.element[0].querySelectorAll('input,select,textarea'):this.element.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}show(a){a=a||'block';if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].style.display=a}else{this.element.style.display=a}return this}top(){return this.string==w?w.scrollY:null}toggleClass(a){if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[i].classList.toggle(a)}else{this.element.classList.toggle(a)}return this}trigger(a,b){if(typeof a=='undefined')return;if(this.string==w){w.dispatchEvent(new Event(a))}else if(this.string==d){d.dispatchEvent(new Event(a))}else if(this.isString||this.isObject){for(let i=0;i<this.element.length;++i)this.element[0].dispatchEvent(new Event(a))}else{this.element.dispatchEvent(new Event(a))}if(typeof b=='function')b();return this}val(a){a=typeof a!='undefined'?a:null;if(this.isString||this.isObject){if(a||a===''){for(let i=0;i<this.element.length;++i)this.element[i].value=a;return true}else{return this.element[0].value}}else{return a||a===''?(this.element.value=a,true):this.element.value}}validated(){let b=(this.element[0]||this.element).querySelectorAll('input,select,textarea'),c=0;for(let key in b){if(isFinite(key)){let k=b[key],valid=k.validity,error=k.getAttribute('error');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>7&&(kvl<11||kvl>12))||(kvl===11&&!/^8[8|9]{1}[\d]{2}[\d]{7}$/.test(kv))||(kvl===12&&!/^\+79[\d]{2}[\d]{7}$/.test(kv))){error=error||'Некорректный номер телефона<br>пример: +79123456789,89123456789,88001234567 или 642531';++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){d.querySelector('.modal_info').innerHTML=(error||'Проверьте правильность заполнения полей');fn.modal({over:null,box:'.modal_event',close:3});return false}}}}return true}}class Set{ajax(a,b,c){c=new XMLHttpRequest(),b={type:'POST',url:null,success:null,json:true,error:null,files:null,callback:null,asinc:true,data:null};for(let f in b)b[f]=typeof a[f]!='undefined'?a[f]:b[f];a=null;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){$(a).css({'background-image':'url('+e.target.result+')'})};reader.readAsDataURL(b)}}copy(a){if(!a||a=='undefined')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 b={},c=0,f=a.dataset;b.step=parseFloat(f.count/100);setTimeout(function(){b.fn=setInterval(function(){if(c+b.step>=parseFloat(f.count)){a.innerHTML=f.count;clearInterval(b.fn)}c+=b.step;a.innerHTML=parseInt(c)},f.duration/100)},f.delay)}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('file['+i+']',a[i]);return b}image(a,b){if(b.type.match(/image.*/)){let reader=new FileReader();reader.onload=function(e){let img=d.createElement('img');img.src=e.target.result;$(a).after(img)};reader.readAsDataURL(b)}}imgPreview(a,b,c){let f=a.files;if(f.length){$(c).empty();[].forEach.call(f,function(file){if(file.type.match(/image.*/)){let reader=new FileReader();reader.onload=function(e){var img=d.createElement('img');img.src=e.target.result;$(c).after(img)};reader.readAsDataURL(file)}});$(b).html(f.length==1?f[0].name+' ('+(f[0].size/1024).toFixed(2)+' Кб)':'Добавлено '+f.length+' файлов')}else{h.innerHTML('Выберите файл');$(c).html('Предпросмотр')}return this}json(a){return JSON.parse(a)}jstr(a){return JSON.stringify(a)}log(){for(let i=0;i<arguments.length;++i)console.info(arguments[i]);return this}modal(a){let b={action:'open',over:'.modal_overlay',box:null,old:null,new:null,close:null,callback:null};for(let c in b)b[c]=typeof a[c]!='undefined'?a[c]:b[c];a=null;if(b.action=='open'){if(!b.box)return;if(b.over)$(b.over).fadeIn();$(b.box).fadeIn();if(b.close){setTimeout(()=>{if(b.over)$(b.over).fadeOut();$(b.box).fadeOut()},b.close*1000)}}else if(b.action=='close'){$(b.over).fadeOut();$('.modal').each(function(){if(this.style.display=='block')$(this).fadeOut()})}else if(b.action=='change'){if(!b.old||!b.new)return;$(b.old).fadeOut();$(b.new).fadeIn()}return typeof b.callback=='function'?b.callback():this}sleep(a,fn){setTimeout(fn||function(){},a);return this}translite(a,b){let c=typeof b!='undefined'?{}:{'а':'a','б':'b','в':'v','г':'g','д':'d','е':'e','ж':'g','з':'z','и':'i','й':'y','к':'k','л':'l','м':'m','н':'n','о':'o','п':'p','р':'r','с':'s','т':'t','у':'u','ф':'f','ы':'i','э':'e','А':'A','Б':'B','В':'V','Г':'G','Д':'D','Е':'E','Ж':'G','З':'Z','И':'I','Й':'Y','К':'K','Л':'L','М':'M','Н':'N','О':'O','П':'P','Р':'R','С':'S','Т':'T','У':'U','Ф':'F','Ы':'I','Э':'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,callback:null,full:0,tool:0,status:0,menu:0,resize:0,focus:true};for(let c in b)b[c]=typeof a[c]!='undefined'?a[c]:b[c];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 Slider{constructor(a){this.string=a||null;this.box=typeof this.string=='string'?d.querySelectorAll(this.string):(this.string||null)}init(a){if(!this.box.length)return;a=a||{};let b={delay:2000,dots:true,dots_list:[],autoplay:true,currentSlide:0,interval:null,before:null,callback:null},self=this;for(let c in b)this[c]=typeof a[c]!='undefined'?a[c]:b[c];this.slides=d.querySelector(this.string).children;this.length=this.slides.length;if(this.dots===true){let br=d.createElement('ul');br.className='slider_dots';for(let i=0;i<this.length;++i){let brs=d.createElement('li');brs.setAttribute('data-slide',i);brs.className='slider_dot';br.appendChild(brs)}d.querySelector(this.string).parentNode.appendChild(br);this.dots_list=d.querySelector('.slider_dots').children}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(){this.interval=w.setInterval(()=>this.next(),this.delay)}stop(){w.clearInterval(this.interval);this.interval=null}prev(){if(!this.box.length)return;this.go(this.currentSlide-1)}next(){if(!this.box.length)return;this.go(this.currentSlide+1)}go(n){if(!this.box.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}}if(typeof $!='undefined'){w.jCool=(a)=>new App(a)}else{w.$=(a)=>new App(a)}w.fn=new Set();w.sl=(a)=>new Slider(a)})(window,document);/*!
 * CoolJS JavaScript Library v2.6
 * https://coolcms.ru/
 * https://vk.com/fastgrid
 *
 * Copyright Dmitry Kokorin
 * Released under the MIT license
 * https://coolcms.ru/license/
 *
 * Documentation: https://coolcms.ru/docs/5-cool_js
 * Date: 2018-01-20T17:24Z
 */