-
Notifications
You must be signed in to change notification settings - Fork 0
/
swIMG.min.js
1 lines (1 loc) · 6.22 KB
/
swIMG.min.js
1
swIMG=function(cfg=null){window.onload=function(){chkcfg=function(prop,defaultVal){if(cfg!=null){if(cfg[prop]!=null){return cfg[prop];} else if(cfg[prop]==null){return defaultVal;}else{alert('[!]Error:n Unknown parameter');}}else{return defaultVal;}};var cfg_default={method:chkcfg('method','small'),imagePath:chkcfg('imagePath',null),parent:chkcfg('parent','.swIMG'),thumbs:chkcfg('thumbs','.sw_image'),count:chkcfg('count','auto'),mobile:chkcfg('mobile','640px'),maxSize:chkcfg('maxSize','100%'),minSize:chkcfg('minSize','80%'),bgColor:chkcfg('bgColor','0,0,0'),opacity:chkcfg('opacity','0.8'),arrowColor:chkcfg('arrowColor','grey'),arrowColorHover:chkcfg('arrowColorHover','white'),closeColor:chkcfg('closeColor','grey'),closeColorHover:chkcfg('closeColorHover','white'),increment:chkcfg('increment',false),LargeImages:chkcfg('LargeImages',[]),gallery:'.gallery',fullSize:'#fullsize'};error=function(){this.prefix='[!] Errorn';error.prototype.check=function(cls){return alert(this.prefix+'Please attach '+cls+' to your DOM element.');} error.prototype.def=function(msg){return alert(this.prefix+msg);}} isinDOM=function(find){this.find=find;if($(document).find(this.find).length!=0){rgx=[/Image/g,/Div/g,/Anchor/g],query=document.querySelector(this.find);query+=this.toString();return query;}} addLrg=function(){if(cfg_default.method=='large'){(function(){path=cfg_default.imagePath;if(cfg_default.increment==true){for(i=1;i<(sw.count+1);i++){sw.images.push(path+i+'.jpg')}}else{if(cfg_default.LargeImages.length!=0){cfg_default.LargeImages.forEach(function(image){sw.images.push(path+image);});}else{var err=new error();err.def('Large images do not exist.');}}})();}} fetchBG=function(cls){var img=cls,style=(function(){if(img.style||img.currentStyle){return getComputedStyle(img,false);}else{var err=new error();err.def('Could not fetch Background-URL.');}})(),bi=style.backgroundImage;return bi;} var sw={images:[],count:cfg_default.count!='auto'?cfg_default.count:$(cfg_default.thumbs).length,addEvent:(function(){if(isinDOM(cfg_default.thumbs)){$(cfg_default.thumbs).click(function(){if(query.match(rgx[0])){if(cfg_default.method=='large'){sw.show(sw.images[$(cfg_default.parent+' '+cfg_default.thumbs).index($(this))]);}else{sw.show(this.src);}}});$(cfg_default.thumbs).click(function(){if(query.match(rgx[1])){if(cfg_default.method=='large'){sw.show(sw.images[$(cfg_default.parent+' '+cfg_default.thumbs).index($(this))]);}else{sw.show(fetchBG(this));}}});}})(),gallery:$(function(){var arrleft='❬',arrright='❭';var larr='.arrleft',rarr='.arrright';mobile=function(){var mobile=window.matchMedia('(max-width: '+cfg_default.mobile+')');return((mobile.matches)?cfg_default.maxSize:cfg_default.minSize)} $.fn.reset=function(){this.css({'margin':'0','padding':'0'})} $(document.body).prepend('<div class="gallery">'+'<div class="close">×</div>'+'<div id="arrows">'+'<div class="arrleft">'+'❬'+'</div>'+'<div class="arrright">'+'❭'+'</div>'+'</div>'+'<img id="fullsize"/>'+'</div>');$(cfg_default.gallery).css({'position':'fixed','z-index':'10','background':'rgba('+cfg_default.bgColor+','+cfg_default.opacity+')','width':'100%','height':'100%','display':'none','user-select':'none'}).reset();$('.close').css({'position':'fixed','right':'0px','z-index':'5','display':'inline','padding':'10px','fontSize':'5vw','color':cfg_default.closeColor,'line-height':'0.6','cursor':'pointer'}).mouseenter(function(){$(this).css('color',cfg_default.closeColorHover);}).mouseleave(function(){$(this).css('color',cfg_default.closeColor);}).reset();$(cfg_default.fullSize).css({'position':'fixed','z-index':'3','min-width':'auto','max-width':mobile(),'max-height':mobile(),'top':'50%','left':'50%','transform':'translate(-50%,-50%)','-moz-transform':'translate(-50%,-50%)','-webkit-transform':'translate(-50%,-50%)'}).reset();$('#arrows').css({'position':'fixed','color':cfg_default.arrowColor,'width':'100%','top':'50vh','transform':'translateY(-50%)'}).reset();$('.arrright').css('float','right');$('#arrows div').css({'display':'inline-block','text-align':'center','width':'10vw','line-height':'100vh','font-size':'5vw','cursor':'pointer'}).mouseenter(function(){$(this).css({'background':'rgba(0,0,0,0.5','color':cfg_default.arrowColorHover});}).mouseleave(function(){$(this).css({'background':'transparent','color':cfg_default.arrowColor});}).reset();$(larr).css('float','left');$(rarr).css('float','right');}),show:function(url){img=url.replace(/.*s?url(['"]?/,'').replace(/['"]?).*/,'');$(cfg_default.fullSize).empty().attr('src',img).reset();$(cfg_default.gallery).fadeIn('fast').show();$('.close').click(function(){$(cfg_default.gallery).hide()});},getSrc:function(){var err=new error();if(isinDOM(cfg_default.parent)){if(isinDOM(cfg_default.thumbs)){if(cfg_default.method=='large'){addLrg();}else{for(i=0;i<sw.count;i++){url=$(cfg_default.thumbs+':eq('+i+')');if(cfg_default.method=='small'){this.images.push(((query.match(rgx[0]))?url.attr('src'):((query.match(rgx[1]))?url.css('backgroundImage'):url.attr('src'))).replace(/.*s?url(['"]?/,'').replace(/['"]?).*/,''))}}}}else{err.check(cfg_default.thumbs);}}else{err.check(cfg_default.parent);}},index:(function(){$(function(){var getIndex=$(cfg_default.parent+' '+cfg_default.thumbs);getIndex.click(function(){return num=getIndex.index($(this));});sw.getSrc();});})(),left:function(Arr){if(num==-1){num=0;} $(cfg_default.fullSize).attr('src',Arr[num]).show();},right:function(Arr,current,last){if(num==current){num=last;} $(cfg_default.fullSize).attr('src',Arr[num]).show();},keys:$(window).keydown(function(e){if(typeof e.keyCode!=='undefined'){if(e.keyCode==37){num--;sw.left(sw.images);} if(e.keyCode==39){num++;sw.right(sw.images,sw.count,(sw.count-1));}}}),arrows:$(function(){$('.arrleft').click(function(){num--;sw.left(sw.images);});$('.arrright').click(function(){num++;sw.right(sw.images,sw.count,(sw.count-1));});}),swipe:$(function(){var mobile=window.matchMedia('(max-width: '+cfg_default.mobile+')');if(mobile.matches){$('#arrows').hide();var l_arrow=$('.arrleft');var r_arrow=$('.arrright');$('img').on('dragstart',function(event){event.preventDefault();});$(cfg_default.gallery).on('swipeleft swipeleftup swipeleftdown',function(){r_arrow.trigger('click');});$(cfg_default.gallery).on('swiperight swiperightup swiperightdown',function(){l_arrow.trigger('click');});}})}}}