-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.ddGM.min.js
12 lines (12 loc) · 15.4 KB
/
jquery.ddGM.min.js
1
2
3
4
5
6
7
8
9
10
11
12
////////////////////////////////////////////////////////////////////////////////////
/*
* Darren Doyle's Gallery M (ddGM) :: v1.0 :: 2012-10-02
* http://inventurous.net/ddgallery
*
* Copyright (c) 2012, Darren Doyle
* Free to use and abuse under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
*/
////////////////////////////////////////////////////////////////////////////////////
(function(a){if(!window.ddGM){window.ddGM={}}if(!window.ddGM.anims){a.extend(window.ddGM,{anims:{}})}if(!window.ddGM.modules){a.extend(window.ddGM,{modules:{}})}if(!window.ddGM.types){a.extend(window.ddGM,{types:{}})}if(!window.ddGM.states){a.extend(window.ddGM,{states:{}})}if(!window.ddGM.tpl){a.extend(window.ddGM,{tpl:{stage:"",item:{}}})}window.ddGM.tpl.stage+='<div class="ddGM-stage-wrapper" style="width:100%; height:100%; position:relative; overflow:hidden;">';window.ddGM.tpl.stage+='<div class="ddGM-stage" style="width:100%; height:100%;" />';window.ddGM.tpl.stage+="</div>";window.ddGM.tpl.stage+='<div class="ddGM-control" style="position:absolute;" />';window.ddGM.tpl.item={id:"",type:"",title:"",caption:"",thumb:"",content:"",extended:"",resizable:false};a.ddGM=function(d,c){var b=this,e;b.id="ddGMID"+(Math.floor(Math.random()*10000));b.opt=a.extend({},a.ddGM.defaultOptions,c);b.gal=a(d);b.obj={};b.events={loadStart:{},loadEnd:{},mouseIn:{},mouseOut:{},touchStartUp:{},touchStartDown:{},touchEndUp:{},touchEndDown:{},changeStart:{},itemLoadStart:{},itemLoadEnd:{},preAnim:{},midAnim:{},postAnim:{},changeEnd:{},resize:{},userPlayStart:{},userPlayEnd:{},userPauseStart:{},userPauseEnd:{},playStart:{},playEnd:{},pauseStart:{},pauseEnd:{},unload:{}};e=(b.gal.css("z-index")=="auto")?0:parseInt(b.gal.css("z-index"),10);b.layers={bottom:e,stage:e+1,stageBottom:e+2,stageTop:e+3,indicators:e+4,controls:e+5,top:e+6};b.items={};b.itemList=[];b.source={html:{},list:{}};b.states={hover:false,linkCapture:false,changing:false,playing:false,paused:{user:true},navSource:"",current:"",clicked:"",count:0,firstItem:true,animList:[],playTarget:0,playProgress:0,galW:b.gal.width(),galH:b.gal.height(),debugIndent:0};b.timers={};b.opt.resizeCheckInterval=500;b.deferreds={};b.keypresses={};b.initialize(b)};a.ddGM.defaultOptions={animPath:undefined,animations:{fade:{}},modulePath:undefined,modules:{arrows:{},resizer:{}},typePath:undefined,types:{img:{}},keyboard:true,pause:5000,hoverPause:true,hoverSelectors:"",contiguous:false,autoPlay:true,startItem:1,useMin:true,debug:false};a.ddGM.prototype={initialize:function(c){var b=[function(d){c.origData=c.gal.html();c.source.html=a("<div />").html(c.gal.html());c.origPos=(c.gal.css("position")=="static")?"relative":c.gal.css("position");c.gal.css({position:c.origPos}).addClass("ddGM-main");d.resolve()},function(d){c.gal.html(window.ddGM.tpl.stage).ready(d.resolve)},function(d){c.obj.control=c.gal.find(".ddGM-control");c.obj.stageWrap=c.gal.find(".ddGM-stage-wrapper");c.obj.stage=c.obj.stageWrap.children(".ddGM-stage");c.obj.stageWrap.css({"z-index":c.layers.stage});c.obj.control.css({"z-index":c.layers.controls});d.resolve()},function(d){c.loadPlugin.call(c,d,"type",c.opt.types)},function(d){c.loadPlugin.call(c,d,"module",c.opt.modules)},function(d){c.loadPlugin.call(c,d,"anim",c.opt.animations)},function(d){c.runEvent.call(c,d,"loadStart")},function(d){var e=[];c.source.html.children().each(function(){var f=a(this),g="ddGMi"+(Math.floor(Math.random()*1000000));f.addClass(g);c.source.list[g]="";e.push((function(h,j,k){return function(m){var l=false,n=a.extend({},window.ddGM.tpl.item,{id:k,dfd:m});a.each(h.opt.types,function(q,o){n.type=q;var p=window.ddGM.types[q]["selector"].call(h,j,n);if(p===true){l=true;return false}});if(!l){m.resolve()}}})(c,f,g))});c.runPipe.call(c,d,"itemParser",e)},function(d){c.opt.startItem=parseInt(c.opt.startItem,10)-1;c.opt.startItem=(c.opt.startItem>c.itemList.length-1)?c.itemList.length-1:(c.opt.startItem<0)?0:c.opt.startItem;if(c.itemList.length>0){if(c.opt.autoPlay){c.states.paused.user=false}c.changeItem.call(c,d,c.opt.startItem)}else{d.resolve()}},function(d){c.runEvent.call(c,d,"loadEnd")},function(d){c.states.firstItem=false;c.obj.hoverElements=c.gal;if(c.opt.hoverSelectors){c.obj.hoverElements.add(c.opt.hoverSelectors)}c.obj.hoverElements.on("mousemove."+c.id,function(){c.mouseHoverOn.call(c);c.obj.hoverElements.off("mousemove."+c.id)});c.hoverStart.call(c);c.timers.resizer=setInterval(function(){c.resizeTimer.call(c)},c.opt.resizeCheckInterval);a(window).on("resize."+c.id,function(){c.runEvent.call(c,null,"resize")});if(c.opt.keyboard){c.registerKeypress.call(c,37,function(){c.states.navSource="left";c.rotateItems(false)});c.registerKeypress.call(c,39,function(){c.states.navSource="right";c.rotateItems(true)})}a(document).on("keydown."+c.id,function(f){c.keyPress.call(c,f.keyCode)});d.resolve()}];c.runPipe.call(c,null,"initialize",b)},addItem:function(d){var b=this,c={};c[d.id]=d;a.extend(true,b.items,c);b.itemList.push(d.id);b.source.list[d.id]=d.type;b.debug("[+] "+d.type+": "+d.id);d.dfd.resolve();return true},changeItem:function(f,j){var o=this,b,c=true,l=o.states.current,h,n,e,g,d,k,p,m=(String(j).substr(0,5)=="ddGMi")?j:(j>o.itemList.length-1)?o.itemList[o.itemList.length-1]:(j<0)?o.itemList[0]:o.itemList[j];if(l==m||o.states.changing){return}clearTimeout(o.timers.playTimer);o.states.playDelay=o.opt.pause;k=a.inArray(l,o.itemList);p=a.inArray(m,o.itemList);o.states.changing=true;o.states.clicked=m;b=[function(q){if(!o.states.firstItem){h=o.items[l];n=o.obj.stage.find("."+l)}e=o.items[m];g=o.obj.stage.find("."+m);d=o.states.animList[(Math.ceil(Math.random()*o.states.animList.length)-1)];switch(o.states.navSource){case"right":c=true;break;case"left":c=false;break;case"timer":c=(k<p)?true:((k==o.itemList.length-1)&&(p==0))?true:false;break;case"direct":c=(k<p);break}q.resolve()},function(q){if(o.states.firstItem){q.resolve()}else{o.runEvent.call(o,q,"changeStart")}},function(q){if(g.length==0){o.gal.addClass("loading");var r=[function(s){o.runEvent.call(o,s,"itemLoadStart")},function(s){window.ddGM.types[e.type].loader.call(o,s,e,o.obj.stage)},function(s){g=o.obj.stage.find("."+m);g.css({display:"block",opacity:1});s.resolve()},function(s){o.runEvent.call(o,s,"itemLoadEnd")}];o.runPipe.call(o,q,"loadItem",r)}else{g.css({display:"block",opacity:1});q.resolve()}},function(q){if(o.states.firstItem){q.resolve()}else{o.gal.removeClass("loading");o.runEvent.call(o,q,"preAnim")}},function(q){o.deferreds.animComplete=[a.Deferred()];if(d!=undefined&&d!=""&&!o.states.firstItem){o.debug("ANIMATION START");window.ddGM.anims[d].animate.call(o,q,o.deferreds.animComplete[0],g,n,c)}else{o.deferreds.animComplete[0].resolve();q.resolve()}},function(q){if(o.states.firstItem){q.resolve()}else{o.runEvent.call(o,q,"midAnim")}},function(q){a.when(o.deferreds.animComplete[0]).then(function(){o.debug("ANIMATION COMPLETE");if(!o.states.firstItem){n.css({display:"none"}).removeClass("selected")}g.css({display:"block",opacity:1}).addClass("selected");q.resolve()})},function(q){if(o.states.firstItem){q.resolve()}else{o.runEvent.call(o,q,"postAnim")}},function(q){o.states.current=m;o.states.clicked="";if(!o.opt.hoverPause||(o.opt.hoverPause&&!o.states.hover)){o.play.call(o,"change",q)}else{q.resolve()}},function(q){o.states.changing=false;if(o.states.firstItem){q.resolve()}else{o.runEvent.call(o,q,"changeEnd")}}];o.runPipe.call(o,f,"changeItem",b)},hoverStart:function(){var b=this;b.obj.hoverElements.on("mouseenter."+b.id,function(){b.mouseHoverOn.call(b)});b.obj.hoverElements.on("mouseleave."+b.id,function(){b.mouseHoverOff.call(b)});b.obj.hoverElements.on("touchstart."+b.id,function(){b.touchHoverOn.call(b)})},mouseHoverOn:function(){var c=this,b;c.obj.hoverElements.off("mousemove."+c.id);c.states.hover=true;b=[function(d){c.runEvent.call(c,d,"mouseIn")},function(d){if(c.opt.hoverPause){c.pause.call(c,"hover")}d.resolve()}];c.runPipe.call(c,null,"mouseHoverOn",b)},mouseHoverOff:function(){var c=this,b;c.states.hover=false;b=[function(d){c.runEvent.call(c,d,"mouseOut")},function(d){if(c.opt.hoverPause){c.play.call(c,"hover")}d.resolve()}];c.runPipe.call(c,null,"mouseHoverOff",b)},touchHoverOn:function(){var c=this,b;el=c.obj.hoverElements;if(c.states.changing){return}el.off("touchstart."+c.id);el.off("mouseenter."+c.id);el.off("mouseleave."+c.id);b=[function(d){c.runEvent.call(c,d,(c.states.hover?"touchStartUp":"touchStartDown"))},function(d){el.on("touchend."+c.id,function(g){var f=(g.srcElement.nodeName=="A"||g.srcElement.parentNode.nodeName=="A");if(!f&&!c.states.hover){c.states.hover=true;c.mouseHoverOn.call(c)}else{if(!f){c.states.hover=false;c.mouseHoverOff.call(c)}}c.runEvent.call(c,null,(!c.states.hover?"touchEndUp":"touchEndDown"));el.off("touchend."+c.id);c.timers.hoverStart=setTimeout(function(){c.hoverStart.call(c)},0)});el.on("touchmove."+c.id,function(){el.off("touchend."+c.id);el.on("touchend."+c.id,function(){el.off("touchend."+c.id);c.timers.hoverStart=setTimeout(function(){c.hoverStart.call(c)},0)})});d.resolve()}];c.runPipe.call(c,null,"touch",b)},registerKeypress:function(c,d){var b=this;if(typeof b.keypresses[c]!="object"){b.keypresses[c]=[]}b.keypresses[c].push(d)},keyPress:function(c){var b=this;a.each(b.keypresses[c],function(e,d){d.call(b)})},resizeTimer:function(){var b=this,c=b.gal.width(),d=b.gal.height();if(c!=b.states.galW||d!=b.states.galH){b.runEvent.call(b,null,"resize");b.states.galW=c;b.states.galH=d}},rotateItems:function(c){var b=this,d=a.inArray(b.states.current,b.itemList);if(!b.states.changing){if(d<0){return}clearTimeout(b.timers.playTimer);if(c){d=((d+1)<b.itemList.length)?d+1:0}else{d=((d-1)>=0)?d-1:b.itemList.length-1}b.changeItem.call(b,null,d)}},play:function(e,g){var c=this,b,h=new Date(),f=h.getTime();c.states.playing=true;b=[function(d){switch(e){case"user":c.states.paused={};break;default:a.each(c.states.paused,function(k,j){if(k==e){c.states.paused[k]=false}else{if(j){c.states.playing=false}}})}d.resolve()},function(d){if(c.states.playing&&e=="user"){c.runEvent.call(c,d,"userPlayStart")}else{d.resolve()}},function(d){if(c.states.playing){c.runEvent.call(c,d,"playStart")}else{d.resolve()}},function(d){if(c.states.playing){c.states.playTarget=f+c.states.playDelay;clearTimeout(c.timers.playTimer);c.timers.playTimer=setTimeout(function(){c.states.navSource="timer";c.rotateItems.call(c,true)},c.states.playDelay)}d.resolve()},function(d){if(c.states.playing&&e=="user"){c.runEvent.call(c,d,"userPlayEnd")}else{d.resolve()}},function(d){if(c.states.playing){c.runEvent.call(c,d,"playEnd")}else{d.resolve()}}];c.runPipe.call(c,g,"play-"+e,b)},pause:function(e,g){var c=this,b,h=new Date(),f=h.getTime();clearTimeout(c.timers.playTimer);c.states.paused[e]=true;c.states.playing=false;b=[function(d){if(e=="user"){c.runEvent.call(c,d,"userPauseStart")}else{d.resolve()}},function(d){c.runEvent.call(c,d,"pauseStart")},function(d){c.states.playDelay=((c.states.playTarget-f)<0)?c.opt.pause:(c.states.playTarget-f);d.resolve()},function(d){if(e=="user"){c.runEvent.call(c,d,"userPauseEnd")}else{d.resolve()}},function(d){c.runEvent.call(c,d,"pauseEnd")}];c.runPipe.call(c,g,"pause-"+e,b)},loadPlugin:function(g,d,e){var c=this,b=[],f=c.opt[d+"Path"];a.each(e,function(j,h){b.push(function(k){if(!window.ddGM[d+"s"][j]){var l=f+"ddGM."+d+"."+j+(c.opt.useMin?".min":"")+".js";c.debug("[…] FETCHING: "+d+"("+l+")");a.ajax({type:"GET",url:l,dataType:"script",success:function(){c.initializePlugin(k,d,j,l)},error:function(o,n,m){c.debug("[X] ERROR LOADING: "+d+"("+l+") :: exception("+m+")");k.resolve()}})}else{c.initializePlugin(k,d,j,l)}})});c.runPipe.call(c,g,"load"+d,b)},initializePlugin:function(c,f,e,d){var b=this;if(f=="anim"){b.states.animList.push(e)}b.debug("[√] LOADED: "+f+"("+d+")");window.ddGM[f+"s"][e]["initialize"].call(b,c)},registerEvent:function(e,c,f,d){var b=this,g={};d=d==undefined?"modules":d=="animation"?"anims":d+"s";if(b.events[e]==undefined){b.events[e]={}}if(b.events[e][c]==undefined){b.events[e][c]=[]}if(b.events[e][c]){g={};g[d]=f;b.events[e][c].push(g)}b.debug('[e] "'+e+'" registered: '+d+"."+c+"."+f)},runEvent:function(e,d){var c=this,b=[];c.debug("*** [EVENT] "+d+" ***");a.each(c.events[d],function(f,g){b.push(function(h){var j=[];a.each(g,function(l,k){a.each(k,function(n,m){j.push(function(o){c.debug('[>] "'+d+'" run: '+n+"."+f+"."+m);if(typeof window.ddGM[n][f][m]=="function"){window.ddGM[n][f][m].call(c,o)}})})});c.runPipe.call(c,h,d+"_"+f,j)})});c.runPipe.call(c,e,d,b)},runPipe:function(e,c,d){var b=this;if(d.length<1){if(e!=null){e.resolve()}return}b.deferreds[c]=[];for(i=0;i<=d.length;i++){b.deferreds[c].push(a.Deferred())}for(i=0;i<d.length;i++){(function(f){b.deferreds[c][f].done(function(){b.debug("—— "+(f+1)+". "+c+" ——");d[f](b.deferreds[c][f+1])})})(i)}b.deferreds[c][d.length].done(function(){b.states.debugIndent--;b.debug("———————— END: "+c+" ————————");if(e!=null){e.resolve()}});b.debug("——————— START: "+c+" ———————");b.states.debugIndent++;b.deferreds[c][0].resolve()},debug:function(d,c){var b=this,e="";for(i=0;i<b.states.debugIndent;i++){e+=" "}if(this.opt.debug||c){console.log("[ddGM] "+e+d)}},destroy:function(){var b=this;a.Deferred(function(c){a.each(b.timers,function(e,d){clearTimeout(d);clearInterval(d)});a.each(b.deferreds,function(e,d){for(var g in d){try{d[g].reject()}catch(f){}}});b.deferreds=[];c.resolve()}).pipe(function(){return a.Deferred(function(c){b.runEvent.call(b,c,"unload")})}).pipe(function(){b.gal.off("."+b.id);b.obj.hoverElements.off("mouseEnter."+b.id);a(document).off("keydown."+b.id);a(window).off("resize."+b.id);b.gal.find().stop(1,0);b.gal.html(b.origData);b.gal.css({position:b.origPos})})}};a.fn.ddGM=function(c,e){var f="load",b,d=["goTo","togglePlay","play","pause","last","next","resize","load","reload","unload","destroy"];c=(c==undefined)?{}:c;if((typeof c!=="object")&&a.inArray(c,d)>=0){f=c}else{b=(typeof c==="object"||!c)?c:arguments[1];if(b.modulePath==undefined||b.animPath==undefined||b.typePath==undefined){(function(){var h=a(document).find('script[src*="ddGM"]').attr("src")||"",g=h.match(/^(.*\/).*$/i);if(b.modulePath==undefined){b.modulePath=g[1]+"modules/";if(b.debug){console.log("[ddGM] MODULE PATH: "+b.modulePath)}}if(b.animPath==undefined){b.animPath=g[1]+"animations/";if(b.debug){console.log("[ddGM] ANIMATION PATH: "+b.animPath)}}if(b.typePath==undefined){b.typePath=g[1]+"types/";if(b.debug){console.log("[ddGM] TYPE PATH: "+b.typePath)}}})()}if(b.modulePath.charAt(b.modulePath.length-1)!="/"){b.modulePath+"/"}if(b.animPath.charAt(b.animPath.length-1)!="/"){b.animPath+"/"}if(b.typePath.charAt(b.typePath.length-1)!="/"){b.typePath+"/"}}this.each(function(){var g=a(this).data("ddGM"),h=false;if(f=="load"&&b.debug){h=true;console.log("[ddGM] *** EXTERNAL COMMAND ("+f+") ***")}else{if(g){if(g.opt.debug){h=true;console.log("[ddGM] *** EXTERNAL COMMAND ("+f+") ***")}}}switch(f){case"load":case"reload":case"unload":case"destroy":if(g){g.destroy.call(g);a(this).removeData("ddGM");if(h){console.log("[ddGM] INSTANCE DESTROYED")}}if(c!="destroy"&&c!="unload"){a(this).data("ddGM",new a.ddGM(this,b));if(h){console.log("[ddGM] CREATION COMPLETED")}}break;case"goTo":e=e-1;e=(e>g.itemList.length-1)?g.itemList.length-1:(e<0)?0:e;g.changeItem.call(g,e);break;case"next":g.rotateItems.call(g,true);break;case"last":g.rotateItems.call(g,false);break;case"play":g.play.call(g,"user");break;case"pause":g.pause.call(g,"user");break;case"togglePlay":var j=false;a.each(g.states.paused,function(l,k){if(k){j=true}});if(j){g.play.call(g,"user")}else{g.pause.call(g,"user")}break;case"resize":g.runEvent.call(g,null,"resize");break}});return this}})(jQuery);