From a8161bb08f99168806665af3e253101d251a1323 Mon Sep 17 00:00:00 2001 From: Nicolas Turlais Date: Mon, 20 Jul 2015 23:51:28 +0200 Subject: [PATCH] jsHint --- dist/js/jquery.chocolat.js | 194 +++++++++++++++++---------------- dist/js/jquery.chocolat.min.js | 2 +- gulpfile.coffee | 12 +- package.json | 1 + src/js/jquery.chocolat.js | 194 +++++++++++++++++---------------- 5 files changed, 208 insertions(+), 195 deletions(-) diff --git a/dist/js/jquery.chocolat.js b/dist/js/jquery.chocolat.js index e6de5b2..0a4ab63 100644 --- a/dist/js/jquery.chocolat.js +++ b/dist/js/jquery.chocolat.js @@ -49,12 +49,12 @@ }); }); - return this + return this; } $.extend(Chocolat.prototype, { init : function(i) { - if(!this.settings.initialized){ + if (!this.settings.initialized) { this.setDomContainer(); this.markup(); this.events(); @@ -66,48 +66,48 @@ }, preload : function(i) { - var def = $.Deferred() + var def = $.Deferred(); - if(typeof this.settings.images[i] === 'undefined'){ + if (typeof this.settings.images[i] === 'undefined') { return; } var imgLoader = new Image(); - imgLoader.onload = function() { def.resolve(imgLoader) }; + imgLoader.onload = function() { def.resolve(imgLoader); }; imgLoader.src = this.settings.images[i].src; - return def + return def; }, load : function(i) { var that = this; - if(this.settings.fullScreen){ + if (this.settings.fullScreen) { this.openFullScreen(); } - if(this.settings.currentImage === i){ + if (this.settings.currentImage === i) { return; } this.elems.overlay.fadeIn(800); this.settings.timer = setTimeout(function(){ - if(typeof that.elems != 'undefined'){ + if (typeof that.elems != 'undefined') { $.proxy(that.elems.loader.fadeIn(), that); } }, 800); var deferred = this.preload(i) .then(function (imgLoader) { - return that.place(i, imgLoader) + return that.place(i, imgLoader); }) .then(function (imgLoader) { - return that.appear(i) + return that.appear(i); }) .then(function (imgLoader) { - that.zoomable() - }) + that.zoomable(); + }); - var nextIndex = i + 1 - if(typeof this.settings.images[nextIndex] != 'undefined'){ + var nextIndex = i + 1; + if (typeof this.settings.images[nextIndex] != 'undefined') { this.preload(nextIndex); } @@ -117,6 +117,7 @@ place : function(i, imgLoader) { var that = this; + var fitting; this.settings.currentImage = i; this.description(); @@ -124,7 +125,7 @@ this.arrows(); this.storeImgSize(imgLoader, i); - fitting = this.fit(i, that.settings.container) + fitting = this.fit(i, that.settings.container); return this.center( fitting.width, @@ -132,7 +133,7 @@ fitting.left, fitting.top, 0 - ) + ); }, center : function(width, height, left, top, duration) { @@ -145,7 +146,7 @@ 'left' :left, 'top' :top }, duration) - .promise() + .promise(); }, appear : function(i) { @@ -154,11 +155,14 @@ this.elems.loader.stop().fadeOut(300, function() { that.elems.img - .attr('src', that.settings.images[i].src) + .attr('src', that.settings.images[i].src); }); }, fit : function(i, container) { + var height; + var width; + var imgHeight = this.settings.images[i].height; var imgWidth = this.settings.images[i].width; var holderHeight = $(container).height(); @@ -172,8 +176,8 @@ var holderRatio = (holderHeight / holderWidth); var imgRatio = (imgHeight / imgWidth); - if(this.settings.imageSize == 'cover') { - if(imgRatio < holderRatio) { + if (this.settings.imageSize == 'cover') { + if (imgRatio < holderRatio) { height = holderHeight; width = height / imgRatio; } @@ -182,12 +186,12 @@ height = width * imgRatio; } } - else if(this.settings.imageSize == 'native') { + else if (this.settings.imageSize == 'native') { height = imgHeight; width = imgWidth; } else { - if(imgRatio>holderGlobalRatio) { + if (imgRatio>holderGlobalRatio) { height = holderGlobalHeight; width = height / imgRatio; } @@ -195,7 +199,7 @@ width = holderGlobalWidth; height = width * imgRatio; } - if(this.settings.imageSize === 'default' && (width >= imgWidth || height >= imgHeight)) { + if (this.settings.imageSize === 'default' && (width >= imgWidth || height >= imgHeight)) { width = imgWidth; height = imgHeight; } @@ -206,21 +210,21 @@ 'width' : width, 'top' : (holderHeight - height)/2, 'left' : (holderWidth - width)/2 - } + }; }, change : function(signe) { - this.zoomOut(0) - this.zoomable() + this.zoomOut(0); + this.zoomable(); var requestedImage = this.settings.currentImage + parseInt(signe); - if(requestedImage > this.settings.lastImage) { - if(this.settings.loop){ + if (requestedImage > this.settings.lastImage) { + if (this.settings.loop) { return this.load(0); } } - else if(requestedImage < 0) { - if(this.settings.loop) { + else if (requestedImage < 0) { + if (this.settings.loop) { return this.load(this.settings.lastImage); } } @@ -230,21 +234,21 @@ }, arrows: function() { - if(this.settings.loop) { + if (this.settings.loop) { $([this.elems.left[0],this.elems.right[0]]) .addClass('active'); } - else if(this.settings.linkImages) { + else if (this.settings.linkImages) { // right - if(this.settings.currentImage == this.settings.lastImage) { - this.elems.right.removeClass('active') + if (this.settings.currentImage == this.settings.lastImage) { + this.elems.right.removeClass('active'); } else { this.elems.right.addClass('active'); } // left - if(this.settings.currentImage == 0) { - this.elems.left.removeClass('active') + if (this.settings.currentImage === 0) { + this.elems.left.removeClass('active'); } else { this.elems.left.addClass('active'); @@ -252,14 +256,14 @@ } else { $([this.elems.left[0],this.elems.right[0]]) - .removeClass('active') + .removeClass('active'); } }, description : function() { var that = this; this.elems.description - .html(that.settings.images[that.settings.currentImage].title) + .html(that.settings.images[that.settings.currentImage].title); }, pagination : function() { @@ -268,16 +272,14 @@ var position = this.settings.currentImage + 1; this.elems.pagination - .html(position + ' ' - + that.settings.separator2 - + last) + .html(position + ' ' + that.settings.separator2 + last); }, storeImgSize : function(img, i) { - if(typeof img === 'undefined') { + if (typeof img === 'undefined') { return; } - if(!this.settings.images[i].height || !this.settings.images[i].width){ + if (!this.settings.images[i].height || !this.settings.images[i].width) { this.settings.images[i].height = img.height; this.settings.images[i].width = img.width; } @@ -287,7 +289,7 @@ if (this.settings.fullscreenOpen) { this.exitFullScreen(); - return + return; } var els = [ @@ -317,13 +319,12 @@ markup : function() { this.elems.domContainer.addClass('chocolat-open ' + this.settings.className); - if(this.settings.imageSize == 'cover') { + if (this.settings.imageSize == 'cover') { this.elems.domContainer.addClass('chocolat-cover'); } - if(this.settings.container !== window) { + if (this.settings.container !== window) { this.elems.domContainer.addClass('chocolat-in-container'); } - var that = this; this.elems.wrapper = $('
', { 'class' : 'chocolat-wrapper', @@ -400,7 +401,7 @@ this.settings.fullscreenOpen = true; wrapper.webkitRequestFullscreen(); } - else if(wrapper.msRequestFullscreen) { + else if (wrapper.msRequestFullscreen) { wrapper.msRequestFullscreen(); this.settings.fullscreenOpen = true; } @@ -410,15 +411,15 @@ }, exitFullScreen : function() { - if(document.exitFullscreen) { + if (document.exitFullscreen) { document.exitFullscreen(); this.settings.fullscreenOpen = false; } - else if(document.mozCancelFullScreen) { + else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); this.settings.fullscreenOpen = false; } - else if(document.webkitExitFullscreen) { + else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); this.settings.fullscreenOpen = false; } @@ -432,13 +433,13 @@ $(document).off('keydown.chocolat').on('keydown.chocolat', function(e) { if (that.settings.initialized) { - if(e.keyCode == 37) { + if (e.keyCode == 37) { that.change(-1); } - else if(e.keyCode == 39) { + else if (e.keyCode == 39) { that.change(1); } - else if(e.keyCode == 27) { + else if (e.keyCode == 27) { that.close(); } } @@ -491,20 +492,20 @@ this.elems.wrapper.find('.chocolat-img') .off('click.chocolat') .on('click.chocolat', function(e) { - if(that.settings.initialZoomState === null && that.elems.domContainer.hasClass('chocolat-zoomable')){ - return that.zoomIn(e) + if (that.settings.initialZoomState === null && that.elems.domContainer.hasClass('chocolat-zoomable')) { + return that.zoomIn(e); } - else{ - return that.zoomOut(e) + else { + return that.zoomOut(e); } }); this.elems.wrapper.mousemove(function( e ) { - if(that.settings.initialZoomState === null) { + if (that.settings.initialZoomState === null) { return; } - if(that.elems.img.is(':animated')) { + if (that.elems.img.is(':animated')) { return; } @@ -512,30 +513,30 @@ var height = $(this).height(); var width = $(this).width(); - var currentImage = that.settings.images[that.settings.currentImage] + var currentImage = that.settings.images[that.settings.currentImage]; var imgWidth = currentImage.width; var imgHeight = currentImage.height; - var coord = [e.pageX - width/2 - pos.left, e.pageY - height/2 - pos.top] + var coord = [e.pageX - width/2 - pos.left, e.pageY - height/2 - pos.top]; - var mvtX = 0 + var mvtX = 0; if (imgWidth > width) { - mvtX = coord[0] / (width / 2) - mvtX = ((imgWidth - width + 0)/ 2) * mvtX + mvtX = coord[0] / (width / 2); + mvtX = ((imgWidth - width + 0)/ 2) * mvtX; } var mvtY = 0; if (imgHeight > height) { - mvtY = coord[1] / (height / 2) - mvtY = ((imgHeight - height + 0) / 2) * mvtY + mvtY = coord[1] / (height / 2); + mvtY = ((imgHeight - height + 0) / 2) * mvtY; } var animation = { 'margin-left': - mvtX + 'px', 'margin-top': - mvtY + 'px' - } - if(typeof e.duration !== 'undefined') { - $(that.elems.img).stop(false, true).animate(animation, e.duration) + }; + if (typeof e.duration !== 'undefined') { + $(that.elems.img).stop(false, true).animate(animation, e.duration); } else { $(that.elems.img).stop(false, true).css(animation); @@ -543,13 +544,13 @@ }); $(window).on('resize', function() { - if(!that.settings.initialized){ + if (!that.settings.initialized) { return; } that.debounce(50, function() { - fitting = that.fit(that.settings.currentImage, that.settings.container) - that.center(fitting.width, fitting.height, fitting.left, fitting.top, 0) - that.zoomable() + fitting = that.fit(that.settings.currentImage, that.settings.container); + that.center(fitting.width, fitting.height, fitting.left, fitting.top, 0); + that.zoomable(); }); }); }, @@ -559,20 +560,20 @@ var wrapperWidth = this.elems.wrapper.width(); var wrapperHeight = this.elems.wrapper.height(); - var isImageZoomable = currentImage.width > wrapperWidth || currentImage.height > wrapperHeight - var isImageStretched = this.elems.img.width() > currentImage.width || this.elems.img.height() > currentImage.height + var isImageZoomable = currentImage.width > wrapperWidth || currentImage.height > wrapperHeight; + var isImageStretched = this.elems.img.width() > currentImage.width || this.elems.img.height() > currentImage.height; - if(isImageZoomable && !isImageStretched){ - this.elems.domContainer.addClass('chocolat-zoomable') + if (isImageZoomable && !isImageStretched) { + this.elems.domContainer.addClass('chocolat-zoomable'); } else { - this.elems.domContainer.removeClass('chocolat-zoomable') + this.elems.domContainer.removeClass('chocolat-zoomable'); } }, zoomIn : function (e) { - this.settings.initialZoomState = this.settings.imageSize + this.settings.initialZoomState = this.settings.imageSize; this.settings.imageSize = 'native'; var event = $.Event('mousemove'); @@ -581,30 +582,30 @@ event.duration = this.settings.duration; this.elems.wrapper.trigger(event); - this.elems.domContainer.addClass('chocolat-zoomed') - fitting = this.fit(this.settings.currentImage, this.settings.container) + this.elems.domContainer.addClass('chocolat-zoomed'); + fitting = this.fit(this.settings.currentImage, this.settings.container); return this.center(fitting.width, fitting.height, fitting.left, fitting.top, this.settings.duration); }, zoomOut : function (e, duration) { - if(this.settings.initialZoomState === null){ + if (this.settings.initialZoomState === null) { return; } - var duration = duration || this.settings.duration + duration = duration || this.settings.duration; - this.settings.imageSize = this.settings.initialZoomState - this.settings.initialZoomState = null - this.elems.img.animate({'margin': 0}, duration) + this.settings.imageSize = this.settings.initialZoomState; + this.settings.initialZoomState = null; + this.elems.img.animate({'margin': 0}, duration); - this.elems.domContainer.removeClass('chocolat-zoomed') - fitting = this.fit(this.settings.currentImage, this.settings.container) + this.elems.domContainer.removeClass('chocolat-zoomed'); + fitting = this.fit(this.settings.currentImage, this.settings.container); return this.center(fitting.width, fitting.height, fitting.left, fitting.top, duration); }, setDomContainer : function() { // if container == window // domContainer = body - if( this.settings.container === window) { + if ( this.settings.container === window) { this.elems.domContainer = $('body'); } else { @@ -620,7 +621,7 @@ }, api: function() { - var that = this + var that = this; return { open : function(i){ i = parseInt(i) || 0; @@ -651,7 +652,8 @@ }, set : function(property, value){ - return that.settings[property] = value; + that.settings[property] = value; + return value; }, get : function(property){ @@ -661,11 +663,11 @@ getElem : function(name){ return that.elems[name]; }, - } + }; } }); - $.fn['Chocolat'] = function (options) { + $.fn.Chocolat = function (options) { return this.each(function() { calls++; @@ -678,5 +680,5 @@ ); } }); - } + }; })( jQuery, window, document ); diff --git a/dist/js/jquery.chocolat.min.js b/dist/js/jquery.chocolat.min.js index 621ef4a..e60f069 100644 --- a/dist/js/jquery.chocolat.min.js +++ b/dist/js/jquery.chocolat.min.js @@ -1 +1 @@ -!function(t,e,i,s){function n(e,i){var s=this;return this.settings=i,this._defaults=a,this.elems={},!this.settings.setTitle&&e.data("chocolat-title")&&(this.settings.setTitle=e.data("chocolat-title")),e.find(this.settings.imageSelector).each(function(){s.settings.images.push({title:t(this).attr("title"),src:t(this).attr("href"),height:!1,width:!1})}),e.find(this.settings.imageSelector).each(function(e){t(this).off("click").on("click",function(t){s.init(e),t.preventDefault()})}),this}var o=0,a={container:e,imageSelector:".chocolat-image",className:"",imageSize:"default",initialZoomState:null,fullScreen:!1,loop:!1,linkImages:!0,duration:300,setTitle:"",separator2:"/",setIndex:0,firstImage:0,lastImage:!1,currentImage:!1,initialized:!1,timer:!1,timerDebounce:!1,images:[]};t.extend(n.prototype,{init:function(t){return this.settings.initialized||(this.setDomContainer(),this.markup(),this.events(),this.settings.lastImage=this.settings.images.length-1,this.settings.initialized=!0),this.load(t)},preload:function(e){var i=t.Deferred();if("undefined"!=typeof this.settings.images[e]){var s=new Image;return s.onload=function(){i.resolve(s)},s.src=this.settings.images[e].src,i}},load:function(e){var i=this;if(this.settings.fullScreen&&this.openFullScreen(),this.settings.currentImage!==e){this.elems.overlay.fadeIn(800),this.settings.timer=setTimeout(function(){"undefined"!=typeof i.elems&&t.proxy(i.elems.loader.fadeIn(),i)},800);var s=this.preload(e).then(function(t){return i.place(e,t)}).then(function(t){return i.appear(e)}).then(function(t){i.zoomable()}),n=e+1;return"undefined"!=typeof this.settings.images[n]&&this.preload(n),s}},place:function(t,e){var i=this;return this.settings.currentImage=t,this.description(),this.pagination(),this.arrows(),this.storeImgSize(e,t),fitting=this.fit(t,i.settings.container),this.center(fitting.width,fitting.height,fitting.left,fitting.top,0)},center:function(t,e,i,s,n){return this.elems.content.css("overflow","visible").animate({width:t,height:e,left:i,top:s},n).promise()},appear:function(t){var e=this;clearTimeout(this.settings.timer),this.elems.loader.stop().fadeOut(300,function(){e.elems.img.attr("src",e.settings.images[t].src)})},fit:function(e,i){var s=this.settings.images[e].height,n=this.settings.images[e].width,o=t(i).height(),a=t(i).width(),l=this.getOutMarginH(),h=this.getOutMarginW(),r=a-h,c=o-l,g=c/r,m=o/a,u=s/n;return"cover"==this.settings.imageSize?m>u?(height=o,width=height/u):(width=a,height=width*u):"native"==this.settings.imageSize?(height=s,width=n):(u>g?(height=c,width=height/u):(width=r,height=width*u),"default"===this.settings.imageSize&&(width>=n||height>=s)&&(width=n,height=s)),{height:height,width:width,top:(o-height)/2,left:(a-width)/2}},change:function(t){this.zoomOut(0),this.zoomable();var e=this.settings.currentImage+parseInt(t);if(e>this.settings.lastImage){if(this.settings.loop)return this.load(0)}else{if(!(0>e))return this.load(e);if(this.settings.loop)return this.load(this.settings.lastImage)}},arrows:function(){this.settings.loop?t([this.elems.left[0],this.elems.right[0]]).addClass("active"):this.settings.linkImages?(this.settings.currentImage==this.settings.lastImage?this.elems.right.removeClass("active"):this.elems.right.addClass("active"),0==this.settings.currentImage?this.elems.left.removeClass("active"):this.elems.left.addClass("active")):t([this.elems.left[0],this.elems.right[0]]).removeClass("active")},description:function(){var t=this;this.elems.description.html(t.settings.images[t.settings.currentImage].title)},pagination:function(){var t=this,e=this.settings.lastImage+1,i=this.settings.currentImage+1;this.elems.pagination.html(i+" "+t.settings.separator2+e)},storeImgSize:function(t,e){"undefined"!=typeof t&&(this.settings.images[e].height&&this.settings.images[e].width||(this.settings.images[e].height=t.height,this.settings.images[e].width=t.width))},close:function(){if(this.settings.fullscreenOpen)return void this.exitFullScreen();var e=[this.elems.overlay[0],this.elems.loader[0],this.elems.wrapper[0]],i=this,s=t.when(t(e).fadeOut(200)).done(function(){i.elems.domContainer.removeClass("chocolat-open chocolat-mobile chocolat-in-container chocolat-cover")});return this.settings.currentImage=!1,this.settings.initialized=!1,s},getOutMarginW:function(){var t=this.elems.left.outerWidth(!0),e=this.elems.right.outerWidth(!0);return t+e},getOutMarginH:function(){return this.elems.top.outerHeight(!0)+this.elems.bottom.outerHeight(!0)},markup:function(){this.elems.domContainer.addClass("chocolat-open "+this.settings.className),"cover"==this.settings.imageSize&&this.elems.domContainer.addClass("chocolat-cover"),this.settings.container!==e&&this.elems.domContainer.addClass("chocolat-in-container");this.elems.wrapper=t("
",{"class":"chocolat-wrapper",id:"chocolat-content-"+this.settings.setIndex}).appendTo(this.elems.domContainer),this.elems.overlay=t("
",{"class":"chocolat-overlay"}).appendTo(this.elems.wrapper),this.elems.loader=t("
",{"class":"chocolat-loader"}).appendTo(this.elems.wrapper),this.elems.content=t("
",{"class":"chocolat-content"}).appendTo(this.elems.wrapper),this.elems.img=t("",{"class":"chocolat-img",src:""}).appendTo(this.elems.content),this.elems.top=t("
",{"class":"chocolat-top"}).appendTo(this.elems.wrapper),this.elems.left=t("
",{"class":"chocolat-left"}).appendTo(this.elems.wrapper),this.elems.right=t("
",{"class":"chocolat-right"}).appendTo(this.elems.wrapper),this.elems.bottom=t("
",{"class":"chocolat-bottom"}).appendTo(this.elems.wrapper),this.elems.fullscreen=t("",{"class":"chocolat-fullscreen"}).appendTo(this.elems.bottom),this.elems.description=t("",{"class":"chocolat-description"}).appendTo(this.elems.bottom),this.elems.pagination=t("",{"class":"chocolat-pagination"}).appendTo(this.elems.bottom),this.elems.setTitle=t("",{"class":"chocolat-set-title",html:this.settings.setTitle}).appendTo(this.elems.bottom),this.elems.close=t("",{"class":"chocolat-close"}).appendTo(this.elems.top)},openFullScreen:function(){var t=this.elems.wrapper[0];t.requestFullscreen?(this.settings.fullscreenOpen=!0,t.requestFullscreen()):t.mozRequestFullScreen?(this.settings.fullscreenOpen=!0,t.mozRequestFullScreen()):t.webkitRequestFullscreen?(this.settings.fullscreenOpen=!0,t.webkitRequestFullscreen()):t.msRequestFullscreen?(t.msRequestFullscreen(),this.settings.fullscreenOpen=!0):this.settings.fullscreenOpen=!1},exitFullScreen:function(){i.exitFullscreen?(i.exitFullscreen(),this.settings.fullscreenOpen=!1):i.mozCancelFullScreen?(i.mozCancelFullScreen(),this.settings.fullscreenOpen=!1):i.webkitExitFullscreen?(i.webkitExitFullscreen(),this.settings.fullscreenOpen=!1):this.settings.fullscreenOpen=!0},events:function(){var s=this;t(i).off("keydown.chocolat").on("keydown.chocolat",function(t){s.settings.initialized&&(37==t.keyCode?s.change(-1):39==t.keyCode?s.change(1):27==t.keyCode&&s.close())}),this.elems.wrapper.find(".chocolat-right").off("click.chocolat").on("click.chocolat",function(){s.change(1)}),this.elems.wrapper.find(".chocolat-left").off("click.chocolat").on("click.chocolat",function(){return s.change(-1)}),t([this.elems.overlay[0],this.elems.close[0]]).off("click.chocolat").on("click.chocolat",function(){return s.close()}),this.elems.fullscreen.off("click.chocolat").on("click.chocolat",function(){return s.settings.fullscreenOpen?void s.exitFullScreen():void s.openFullScreen()}),s.settings.backgroundClose&&this.elems.overlay.off("click.chocolat").on("click.chocolat",function(){return s.close()}),this.elems.wrapper.find(".chocolat-img").off("click.chocolat").on("click.chocolat",function(t){return null===s.settings.initialZoomState&&s.elems.domContainer.hasClass("chocolat-zoomable")?s.zoomIn(t):s.zoomOut(t)}),this.elems.wrapper.mousemove(function(e){if(null!==s.settings.initialZoomState&&!s.elems.img.is(":animated")){var i=t(this).offset(),n=t(this).height(),o=t(this).width(),a=s.settings.images[s.settings.currentImage],l=a.width,h=a.height,r=[e.pageX-o/2-i.left,e.pageY-n/2-i.top],c=0;l>o&&(c=r[0]/(o/2),c=(l-o+0)/2*c);var g=0;h>n&&(g=r[1]/(n/2),g=(h-n+0)/2*g);var m={"margin-left":-c+"px","margin-top":-g+"px"};"undefined"!=typeof e.duration?t(s.elems.img).stop(!1,!0).animate(m,e.duration):t(s.elems.img).stop(!1,!0).css(m)}}),t(e).on("resize",function(){s.settings.initialized&&s.debounce(50,function(){fitting=s.fit(s.settings.currentImage,s.settings.container),s.center(fitting.width,fitting.height,fitting.left,fitting.top,0),s.zoomable()})})},zoomable:function(){var t=this.settings.images[this.settings.currentImage],e=this.elems.wrapper.width(),i=this.elems.wrapper.height(),s=t.width>e||t.height>i,n=this.elems.img.width()>t.width||this.elems.img.height()>t.height;s&&!n?this.elems.domContainer.addClass("chocolat-zoomable"):this.elems.domContainer.removeClass("chocolat-zoomable")},zoomIn:function(e){this.settings.initialZoomState=this.settings.imageSize,this.settings.imageSize="native";var i=t.Event("mousemove");return i.pageX=e.pageX,i.pageY=e.pageY,i.duration=this.settings.duration,this.elems.wrapper.trigger(i),this.elems.domContainer.addClass("chocolat-zoomed"),fitting=this.fit(this.settings.currentImage,this.settings.container),this.center(fitting.width,fitting.height,fitting.left,fitting.top,this.settings.duration)},zoomOut:function(t,e){if(null!==this.settings.initialZoomState){var e=e||this.settings.duration;return this.settings.imageSize=this.settings.initialZoomState,this.settings.initialZoomState=null,this.elems.img.animate({margin:0},e),this.elems.domContainer.removeClass("chocolat-zoomed"),fitting=this.fit(this.settings.currentImage,this.settings.container),this.center(fitting.width,fitting.height,fitting.left,fitting.top,e)}},setDomContainer:function(){this.elems.domContainer=t(this.settings.container===e?"body":this.settings.container)},debounce:function(t,e){clearTimeout(this.settings.timerDebounce),this.settings.timerDebounce=setTimeout(function(){e()},t)},api:function(){var t=this;return{open:function(e){return e=parseInt(e)||0,t.init(e)},close:function(){return t.close()},next:function(){return t.change(1)},prev:function(){return t.change(-1)},"goto":function(e){return t.open(e)},current:function(){return t.settings.currentImage},place:function(){return t.place(t.settings.currentImage,t.settings.duration)},set:function(e,i){return t.settings[e]=i},get:function(e){return t.settings[e]},getElem:function(e){return t.elems[e]}}}}),t.fn.Chocolat=function(e){return this.each(function(){o++;var i=t.extend(!0,{},a,e,{setIndex:o});t.data(this,"chocolat")||t.data(this,"chocolat",new n(t(this),i))})}}(jQuery,window,document); \ No newline at end of file +!function(t,e,i,s){function n(e,i){var s=this;return this.settings=i,this._defaults=a,this.elems={},!this.settings.setTitle&&e.data("chocolat-title")&&(this.settings.setTitle=e.data("chocolat-title")),e.find(this.settings.imageSelector).each(function(){s.settings.images.push({title:t(this).attr("title"),src:t(this).attr("href"),height:!1,width:!1})}),e.find(this.settings.imageSelector).each(function(e){t(this).off("click").on("click",function(t){s.init(e),t.preventDefault()})}),this}var o=0,a={container:e,imageSelector:".chocolat-image",className:"",imageSize:"default",initialZoomState:null,fullScreen:!1,loop:!1,linkImages:!0,duration:300,setTitle:"",separator2:"/",setIndex:0,firstImage:0,lastImage:!1,currentImage:!1,initialized:!1,timer:!1,timerDebounce:!1,images:[]};t.extend(n.prototype,{init:function(t){return this.settings.initialized||(this.setDomContainer(),this.markup(),this.events(),this.settings.lastImage=this.settings.images.length-1,this.settings.initialized=!0),this.load(t)},preload:function(e){var i=t.Deferred();if("undefined"!=typeof this.settings.images[e]){var s=new Image;return s.onload=function(){i.resolve(s)},s.src=this.settings.images[e].src,i}},load:function(e){var i=this;if(this.settings.fullScreen&&this.openFullScreen(),this.settings.currentImage!==e){this.elems.overlay.fadeIn(800),this.settings.timer=setTimeout(function(){"undefined"!=typeof i.elems&&t.proxy(i.elems.loader.fadeIn(),i)},800);var s=this.preload(e).then(function(t){return i.place(e,t)}).then(function(t){return i.appear(e)}).then(function(t){i.zoomable()}),n=e+1;return"undefined"!=typeof this.settings.images[n]&&this.preload(n),s}},place:function(t,e){var i,s=this;return this.settings.currentImage=t,this.description(),this.pagination(),this.arrows(),this.storeImgSize(e,t),i=this.fit(t,s.settings.container),this.center(i.width,i.height,i.left,i.top,0)},center:function(t,e,i,s,n){return this.elems.content.css("overflow","visible").animate({width:t,height:e,left:i,top:s},n).promise()},appear:function(t){var e=this;clearTimeout(this.settings.timer),this.elems.loader.stop().fadeOut(300,function(){e.elems.img.attr("src",e.settings.images[t].src)})},fit:function(e,i){var s,n,o=this.settings.images[e].height,a=this.settings.images[e].width,l=t(i).height(),r=t(i).width(),c=this.getOutMarginH(),h=this.getOutMarginW(),g=r-h,m=l-c,u=m/g,f=l/r,d=o/a;return"cover"==this.settings.imageSize?f>d?(s=l,n=s/d):(n=r,s=n*d):"native"==this.settings.imageSize?(s=o,n=a):(d>u?(s=m,n=s/d):(n=g,s=n*d),"default"===this.settings.imageSize&&(n>=a||s>=o)&&(n=a,s=o)),{height:s,width:n,top:(l-s)/2,left:(r-n)/2}},change:function(t){this.zoomOut(0),this.zoomable();var e=this.settings.currentImage+parseInt(t);if(e>this.settings.lastImage){if(this.settings.loop)return this.load(0)}else{if(!(0>e))return this.load(e);if(this.settings.loop)return this.load(this.settings.lastImage)}},arrows:function(){this.settings.loop?t([this.elems.left[0],this.elems.right[0]]).addClass("active"):this.settings.linkImages?(this.settings.currentImage==this.settings.lastImage?this.elems.right.removeClass("active"):this.elems.right.addClass("active"),0===this.settings.currentImage?this.elems.left.removeClass("active"):this.elems.left.addClass("active")):t([this.elems.left[0],this.elems.right[0]]).removeClass("active")},description:function(){var t=this;this.elems.description.html(t.settings.images[t.settings.currentImage].title)},pagination:function(){var t=this,e=this.settings.lastImage+1,i=this.settings.currentImage+1;this.elems.pagination.html(i+" "+t.settings.separator2+e)},storeImgSize:function(t,e){"undefined"!=typeof t&&(this.settings.images[e].height&&this.settings.images[e].width||(this.settings.images[e].height=t.height,this.settings.images[e].width=t.width))},close:function(){if(this.settings.fullscreenOpen)return void this.exitFullScreen();var e=[this.elems.overlay[0],this.elems.loader[0],this.elems.wrapper[0]],i=this,s=t.when(t(e).fadeOut(200)).done(function(){i.elems.domContainer.removeClass("chocolat-open chocolat-mobile chocolat-in-container chocolat-cover")});return this.settings.currentImage=!1,this.settings.initialized=!1,s},getOutMarginW:function(){var t=this.elems.left.outerWidth(!0),e=this.elems.right.outerWidth(!0);return t+e},getOutMarginH:function(){return this.elems.top.outerHeight(!0)+this.elems.bottom.outerHeight(!0)},markup:function(){this.elems.domContainer.addClass("chocolat-open "+this.settings.className),"cover"==this.settings.imageSize&&this.elems.domContainer.addClass("chocolat-cover"),this.settings.container!==e&&this.elems.domContainer.addClass("chocolat-in-container"),this.elems.wrapper=t("
",{"class":"chocolat-wrapper",id:"chocolat-content-"+this.settings.setIndex}).appendTo(this.elems.domContainer),this.elems.overlay=t("
",{"class":"chocolat-overlay"}).appendTo(this.elems.wrapper),this.elems.loader=t("
",{"class":"chocolat-loader"}).appendTo(this.elems.wrapper),this.elems.content=t("
",{"class":"chocolat-content"}).appendTo(this.elems.wrapper),this.elems.img=t("",{"class":"chocolat-img",src:""}).appendTo(this.elems.content),this.elems.top=t("
",{"class":"chocolat-top"}).appendTo(this.elems.wrapper),this.elems.left=t("
",{"class":"chocolat-left"}).appendTo(this.elems.wrapper),this.elems.right=t("
",{"class":"chocolat-right"}).appendTo(this.elems.wrapper),this.elems.bottom=t("
",{"class":"chocolat-bottom"}).appendTo(this.elems.wrapper),this.elems.fullscreen=t("",{"class":"chocolat-fullscreen"}).appendTo(this.elems.bottom),this.elems.description=t("",{"class":"chocolat-description"}).appendTo(this.elems.bottom),this.elems.pagination=t("",{"class":"chocolat-pagination"}).appendTo(this.elems.bottom),this.elems.setTitle=t("",{"class":"chocolat-set-title",html:this.settings.setTitle}).appendTo(this.elems.bottom),this.elems.close=t("",{"class":"chocolat-close"}).appendTo(this.elems.top)},openFullScreen:function(){var t=this.elems.wrapper[0];t.requestFullscreen?(this.settings.fullscreenOpen=!0,t.requestFullscreen()):t.mozRequestFullScreen?(this.settings.fullscreenOpen=!0,t.mozRequestFullScreen()):t.webkitRequestFullscreen?(this.settings.fullscreenOpen=!0,t.webkitRequestFullscreen()):t.msRequestFullscreen?(t.msRequestFullscreen(),this.settings.fullscreenOpen=!0):this.settings.fullscreenOpen=!1},exitFullScreen:function(){i.exitFullscreen?(i.exitFullscreen(),this.settings.fullscreenOpen=!1):i.mozCancelFullScreen?(i.mozCancelFullScreen(),this.settings.fullscreenOpen=!1):i.webkitExitFullscreen?(i.webkitExitFullscreen(),this.settings.fullscreenOpen=!1):this.settings.fullscreenOpen=!0},events:function(){var s=this;t(i).off("keydown.chocolat").on("keydown.chocolat",function(t){s.settings.initialized&&(37==t.keyCode?s.change(-1):39==t.keyCode?s.change(1):27==t.keyCode&&s.close())}),this.elems.wrapper.find(".chocolat-right").off("click.chocolat").on("click.chocolat",function(){s.change(1)}),this.elems.wrapper.find(".chocolat-left").off("click.chocolat").on("click.chocolat",function(){return s.change(-1)}),t([this.elems.overlay[0],this.elems.close[0]]).off("click.chocolat").on("click.chocolat",function(){return s.close()}),this.elems.fullscreen.off("click.chocolat").on("click.chocolat",function(){return s.settings.fullscreenOpen?void s.exitFullScreen():void s.openFullScreen()}),s.settings.backgroundClose&&this.elems.overlay.off("click.chocolat").on("click.chocolat",function(){return s.close()}),this.elems.wrapper.find(".chocolat-img").off("click.chocolat").on("click.chocolat",function(t){return null===s.settings.initialZoomState&&s.elems.domContainer.hasClass("chocolat-zoomable")?s.zoomIn(t):s.zoomOut(t)}),this.elems.wrapper.mousemove(function(e){if(null!==s.settings.initialZoomState&&!s.elems.img.is(":animated")){var i=t(this).offset(),n=t(this).height(),o=t(this).width(),a=s.settings.images[s.settings.currentImage],l=a.width,r=a.height,c=[e.pageX-o/2-i.left,e.pageY-n/2-i.top],h=0;l>o&&(h=c[0]/(o/2),h=(l-o+0)/2*h);var g=0;r>n&&(g=c[1]/(n/2),g=(r-n+0)/2*g);var m={"margin-left":-h+"px","margin-top":-g+"px"};"undefined"!=typeof e.duration?t(s.elems.img).stop(!1,!0).animate(m,e.duration):t(s.elems.img).stop(!1,!0).css(m)}}),t(e).on("resize",function(){s.settings.initialized&&s.debounce(50,function(){fitting=s.fit(s.settings.currentImage,s.settings.container),s.center(fitting.width,fitting.height,fitting.left,fitting.top,0),s.zoomable()})})},zoomable:function(){var t=this.settings.images[this.settings.currentImage],e=this.elems.wrapper.width(),i=this.elems.wrapper.height(),s=t.width>e||t.height>i,n=this.elems.img.width()>t.width||this.elems.img.height()>t.height;s&&!n?this.elems.domContainer.addClass("chocolat-zoomable"):this.elems.domContainer.removeClass("chocolat-zoomable")},zoomIn:function(e){this.settings.initialZoomState=this.settings.imageSize,this.settings.imageSize="native";var i=t.Event("mousemove");return i.pageX=e.pageX,i.pageY=e.pageY,i.duration=this.settings.duration,this.elems.wrapper.trigger(i),this.elems.domContainer.addClass("chocolat-zoomed"),fitting=this.fit(this.settings.currentImage,this.settings.container),this.center(fitting.width,fitting.height,fitting.left,fitting.top,this.settings.duration)},zoomOut:function(t,e){return null!==this.settings.initialZoomState?(e=e||this.settings.duration,this.settings.imageSize=this.settings.initialZoomState,this.settings.initialZoomState=null,this.elems.img.animate({margin:0},e),this.elems.domContainer.removeClass("chocolat-zoomed"),fitting=this.fit(this.settings.currentImage,this.settings.container),this.center(fitting.width,fitting.height,fitting.left,fitting.top,e)):void 0},setDomContainer:function(){this.elems.domContainer=t(this.settings.container===e?"body":this.settings.container)},debounce:function(t,e){clearTimeout(this.settings.timerDebounce),this.settings.timerDebounce=setTimeout(function(){e()},t)},api:function(){var t=this;return{open:function(e){return e=parseInt(e)||0,t.init(e)},close:function(){return t.close()},next:function(){return t.change(1)},prev:function(){return t.change(-1)},"goto":function(e){return t.open(e)},current:function(){return t.settings.currentImage},place:function(){return t.place(t.settings.currentImage,t.settings.duration)},set:function(e,i){return t.settings[e]=i,i},get:function(e){return t.settings[e]},getElem:function(e){return t.elems[e]}}}}),t.fn.Chocolat=function(e){return this.each(function(){o++;var i=t.extend(!0,{},a,e,{setIndex:o});t.data(this,"chocolat")||t.data(this,"chocolat",new n(t(this),i))})}}(jQuery,window,document); \ No newline at end of file diff --git a/gulpfile.coffee b/gulpfile.coffee index 5780578..c38e343 100644 --- a/gulpfile.coffee +++ b/gulpfile.coffee @@ -1,10 +1,11 @@ gulp = require 'gulp' gutil = require 'gulp-util' -rename = require('gulp-rename'); +rename = require 'gulp-rename'; uglify = require 'gulp-uglify' coffee = require 'gulp-coffee' bump = require 'gulp-bump' mochaPhantomJS = require 'gulp-mocha-phantomjs' +jshint = require 'gulp-jshint' gulp.task 'compile-coffee', -> gulp.src './test/*.coffee' @@ -30,18 +31,24 @@ gulp.task 'build-images', -> gulp.src('./src/images/*') .pipe gulp.dest('./dist/images/') +gulp.task 'lint', -> + gulp.src('./src/js/jquery.chocolat.js') + .pipe(jshint()) + .pipe(jshint.reporter('default')) + gulp.task 'bump', -> gulp.src(['./package.json', './bower.json']) .pipe(bump()) .pipe gulp.dest('./') gulp.task 'watch-test',-> - gulp.watch(['./test/*.coffee', './src/js/*.js', './src/css/*.css'], ['build', 'compile-coffee', 'testing']) + gulp.watch(['./test/*.coffee', './src/js/*.js', './src/css/*.css'], ['build', 'lint', 'compile-coffee', 'testing']) gulp.task 'watch-src',-> gulp.watch(['./src/js/*.js', './src/css/*.css'], ['build-js', 'build-css']) gulp.task 'test', [ + 'lint' 'build' 'compile-coffee' 'testing' @@ -53,6 +60,7 @@ gulp.task 'build', [ 'build-images' ] gulp.task 'default', [ + 'lint' 'build' 'watch-src' ] \ No newline at end of file diff --git a/package.json b/package.json index 58a1c60..c3cc633 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "gulp": "^3.9.0", "gulp-bump": "^0.3.1", "gulp-coffee": "^2.3.1", + "gulp-jshint": "^1.11.2", "gulp-mocha-phantomjs": "^0.6.1", "gulp-rename": "^1.2.2", "gulp-uglify": "^1.2.0", diff --git a/src/js/jquery.chocolat.js b/src/js/jquery.chocolat.js index a5e8503..4a5c7c6 100644 --- a/src/js/jquery.chocolat.js +++ b/src/js/jquery.chocolat.js @@ -49,12 +49,12 @@ }); }); - return this + return this; } $.extend(Chocolat.prototype, { init : function(i) { - if(!this.settings.initialized){ + if (!this.settings.initialized) { this.setDomContainer(); this.markup(); this.events(); @@ -66,48 +66,48 @@ }, preload : function(i) { - var def = $.Deferred() + var def = $.Deferred(); - if(typeof this.settings.images[i] === 'undefined'){ + if (typeof this.settings.images[i] === 'undefined') { return; } var imgLoader = new Image(); - imgLoader.onload = function() { def.resolve(imgLoader) }; + imgLoader.onload = function() { def.resolve(imgLoader); }; imgLoader.src = this.settings.images[i].src; - return def + return def; }, load : function(i) { var that = this; - if(this.settings.fullScreen){ + if (this.settings.fullScreen) { this.openFullScreen(); } - if(this.settings.currentImage === i){ + if (this.settings.currentImage === i) { return; } this.elems.overlay.fadeIn(800); this.settings.timer = setTimeout(function(){ - if(typeof that.elems != 'undefined'){ + if (typeof that.elems != 'undefined') { $.proxy(that.elems.loader.fadeIn(), that); } }, 800); var deferred = this.preload(i) .then(function (imgLoader) { - return that.place(i, imgLoader) + return that.place(i, imgLoader); }) .then(function (imgLoader) { - return that.appear(i) + return that.appear(i); }) .then(function (imgLoader) { - that.zoomable() - }) + that.zoomable(); + }); - var nextIndex = i + 1 - if(typeof this.settings.images[nextIndex] != 'undefined'){ + var nextIndex = i + 1; + if (typeof this.settings.images[nextIndex] != 'undefined') { this.preload(nextIndex); } @@ -117,6 +117,7 @@ place : function(i, imgLoader) { var that = this; + var fitting; this.settings.currentImage = i; this.description(); @@ -124,7 +125,7 @@ this.arrows(); this.storeImgSize(imgLoader, i); - fitting = this.fit(i, that.settings.container) + fitting = this.fit(i, that.settings.container); return this.center( fitting.width, @@ -132,7 +133,7 @@ fitting.left, fitting.top, 0 - ) + ); }, center : function(width, height, left, top, duration) { @@ -145,7 +146,7 @@ 'left' :left, 'top' :top }, duration) - .promise() + .promise(); }, appear : function(i) { @@ -154,11 +155,14 @@ this.elems.loader.stop().fadeOut(300, function() { that.elems.img - .attr('src', that.settings.images[i].src) + .attr('src', that.settings.images[i].src); }); }, fit : function(i, container) { + var height; + var width; + var imgHeight = this.settings.images[i].height; var imgWidth = this.settings.images[i].width; var holderHeight = $(container).height(); @@ -172,8 +176,8 @@ var holderRatio = (holderHeight / holderWidth); var imgRatio = (imgHeight / imgWidth); - if(this.settings.imageSize == 'cover') { - if(imgRatio < holderRatio) { + if (this.settings.imageSize == 'cover') { + if (imgRatio < holderRatio) { height = holderHeight; width = height / imgRatio; } @@ -182,12 +186,12 @@ height = width * imgRatio; } } - else if(this.settings.imageSize == 'native') { + else if (this.settings.imageSize == 'native') { height = imgHeight; width = imgWidth; } else { - if(imgRatio>holderGlobalRatio) { + if (imgRatio>holderGlobalRatio) { height = holderGlobalHeight; width = height / imgRatio; } @@ -195,7 +199,7 @@ width = holderGlobalWidth; height = width * imgRatio; } - if(this.settings.imageSize === 'default' && (width >= imgWidth || height >= imgHeight)) { + if (this.settings.imageSize === 'default' && (width >= imgWidth || height >= imgHeight)) { width = imgWidth; height = imgHeight; } @@ -206,21 +210,21 @@ 'width' : width, 'top' : (holderHeight - height)/2, 'left' : (holderWidth - width)/2 - } + }; }, change : function(signe) { - this.zoomOut(0) - this.zoomable() + this.zoomOut(0); + this.zoomable(); var requestedImage = this.settings.currentImage + parseInt(signe); - if(requestedImage > this.settings.lastImage) { - if(this.settings.loop){ + if (requestedImage > this.settings.lastImage) { + if (this.settings.loop) { return this.load(0); } } - else if(requestedImage < 0) { - if(this.settings.loop) { + else if (requestedImage < 0) { + if (this.settings.loop) { return this.load(this.settings.lastImage); } } @@ -230,21 +234,21 @@ }, arrows: function() { - if(this.settings.loop) { + if (this.settings.loop) { $([this.elems.left[0],this.elems.right[0]]) .addClass('active'); } - else if(this.settings.linkImages) { + else if (this.settings.linkImages) { // right - if(this.settings.currentImage == this.settings.lastImage) { - this.elems.right.removeClass('active') + if (this.settings.currentImage == this.settings.lastImage) { + this.elems.right.removeClass('active'); } else { this.elems.right.addClass('active'); } // left - if(this.settings.currentImage == 0) { - this.elems.left.removeClass('active') + if (this.settings.currentImage === 0) { + this.elems.left.removeClass('active'); } else { this.elems.left.addClass('active'); @@ -252,14 +256,14 @@ } else { $([this.elems.left[0],this.elems.right[0]]) - .removeClass('active') + .removeClass('active'); } }, description : function() { var that = this; this.elems.description - .html(that.settings.images[that.settings.currentImage].title) + .html(that.settings.images[that.settings.currentImage].title); }, pagination : function() { @@ -268,16 +272,14 @@ var position = this.settings.currentImage + 1; this.elems.pagination - .html(position + ' ' - + that.settings.separator2 - + last) + .html(position + ' ' + that.settings.separator2 + last); }, storeImgSize : function(img, i) { - if(typeof img === 'undefined') { + if (typeof img === 'undefined') { return; } - if(!this.settings.images[i].height || !this.settings.images[i].width){ + if (!this.settings.images[i].height || !this.settings.images[i].width) { this.settings.images[i].height = img.height; this.settings.images[i].width = img.width; } @@ -287,7 +289,7 @@ if (this.settings.fullscreenOpen) { this.exitFullScreen(); - return + return; } var els = [ @@ -317,13 +319,12 @@ markup : function() { this.elems.domContainer.addClass('chocolat-open ' + this.settings.className); - if(this.settings.imageSize == 'cover') { + if (this.settings.imageSize == 'cover') { this.elems.domContainer.addClass('chocolat-cover'); } - if(this.settings.container !== window) { + if (this.settings.container !== window) { this.elems.domContainer.addClass('chocolat-in-container'); } - var that = this; this.elems.wrapper = $('
', { 'class' : 'chocolat-wrapper', @@ -400,7 +401,7 @@ this.settings.fullscreenOpen = true; wrapper.webkitRequestFullscreen(); } - else if(wrapper.msRequestFullscreen) { + else if (wrapper.msRequestFullscreen) { wrapper.msRequestFullscreen(); this.settings.fullscreenOpen = true; } @@ -410,15 +411,15 @@ }, exitFullScreen : function() { - if(document.exitFullscreen) { + if (document.exitFullscreen) { document.exitFullscreen(); this.settings.fullscreenOpen = false; } - else if(document.mozCancelFullScreen) { + else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); this.settings.fullscreenOpen = false; } - else if(document.webkitExitFullscreen) { + else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); this.settings.fullscreenOpen = false; } @@ -432,13 +433,13 @@ $(document).off('keydown.chocolat').on('keydown.chocolat', function(e) { if (that.settings.initialized) { - if(e.keyCode == 37) { + if (e.keyCode == 37) { that.change(-1); } - else if(e.keyCode == 39) { + else if (e.keyCode == 39) { that.change(1); } - else if(e.keyCode == 27) { + else if (e.keyCode == 27) { that.close(); } } @@ -491,20 +492,20 @@ this.elems.wrapper.find('.chocolat-img') .off('click.chocolat') .on('click.chocolat', function(e) { - if(that.settings.initialZoomState === null && that.elems.domContainer.hasClass('chocolat-zoomable')){ - return that.zoomIn(e) + if (that.settings.initialZoomState === null && that.elems.domContainer.hasClass('chocolat-zoomable')) { + return that.zoomIn(e); } - else{ - return that.zoomOut(e) + else { + return that.zoomOut(e); } }); this.elems.wrapper.mousemove(function( e ) { - if(that.settings.initialZoomState === null) { + if (that.settings.initialZoomState === null) { return; } - if(that.elems.img.is(':animated')) { + if (that.elems.img.is(':animated')) { return; } @@ -512,30 +513,30 @@ var height = $(this).height(); var width = $(this).width(); - var currentImage = that.settings.images[that.settings.currentImage] + var currentImage = that.settings.images[that.settings.currentImage]; var imgWidth = currentImage.width; var imgHeight = currentImage.height; - var coord = [e.pageX - width/2 - pos.left, e.pageY - height/2 - pos.top] + var coord = [e.pageX - width/2 - pos.left, e.pageY - height/2 - pos.top]; - var mvtX = 0 + var mvtX = 0; if (imgWidth > width) { - mvtX = coord[0] / (width / 2) - mvtX = ((imgWidth - width + 0)/ 2) * mvtX + mvtX = coord[0] / (width / 2); + mvtX = ((imgWidth - width + 0)/ 2) * mvtX; } var mvtY = 0; if (imgHeight > height) { - mvtY = coord[1] / (height / 2) - mvtY = ((imgHeight - height + 0) / 2) * mvtY + mvtY = coord[1] / (height / 2); + mvtY = ((imgHeight - height + 0) / 2) * mvtY; } var animation = { 'margin-left': - mvtX + 'px', 'margin-top': - mvtY + 'px' - } - if(typeof e.duration !== 'undefined') { - $(that.elems.img).stop(false, true).animate(animation, e.duration) + }; + if (typeof e.duration !== 'undefined') { + $(that.elems.img).stop(false, true).animate(animation, e.duration); } else { $(that.elems.img).stop(false, true).css(animation); @@ -543,13 +544,13 @@ }); $(window).on('resize', function() { - if(!that.settings.initialized){ + if (!that.settings.initialized) { return; } that.debounce(50, function() { - fitting = that.fit(that.settings.currentImage, that.settings.container) - that.center(fitting.width, fitting.height, fitting.left, fitting.top, 0) - that.zoomable() + fitting = that.fit(that.settings.currentImage, that.settings.container); + that.center(fitting.width, fitting.height, fitting.left, fitting.top, 0); + that.zoomable(); }); }); }, @@ -559,20 +560,20 @@ var wrapperWidth = this.elems.wrapper.width(); var wrapperHeight = this.elems.wrapper.height(); - var isImageZoomable = currentImage.width > wrapperWidth || currentImage.height > wrapperHeight - var isImageStretched = this.elems.img.width() > currentImage.width || this.elems.img.height() > currentImage.height + var isImageZoomable = currentImage.width > wrapperWidth || currentImage.height > wrapperHeight; + var isImageStretched = this.elems.img.width() > currentImage.width || this.elems.img.height() > currentImage.height; - if(isImageZoomable && !isImageStretched){ - this.elems.domContainer.addClass('chocolat-zoomable') + if (isImageZoomable && !isImageStretched) { + this.elems.domContainer.addClass('chocolat-zoomable'); } else { - this.elems.domContainer.removeClass('chocolat-zoomable') + this.elems.domContainer.removeClass('chocolat-zoomable'); } }, zoomIn : function (e) { - this.settings.initialZoomState = this.settings.imageSize + this.settings.initialZoomState = this.settings.imageSize; this.settings.imageSize = 'native'; var event = $.Event('mousemove'); @@ -581,30 +582,30 @@ event.duration = this.settings.duration; this.elems.wrapper.trigger(event); - this.elems.domContainer.addClass('chocolat-zoomed') - fitting = this.fit(this.settings.currentImage, this.settings.container) + this.elems.domContainer.addClass('chocolat-zoomed'); + fitting = this.fit(this.settings.currentImage, this.settings.container); return this.center(fitting.width, fitting.height, fitting.left, fitting.top, this.settings.duration); }, zoomOut : function (e, duration) { - if(this.settings.initialZoomState === null){ + if (this.settings.initialZoomState === null) { return; } - var duration = duration || this.settings.duration + duration = duration || this.settings.duration; - this.settings.imageSize = this.settings.initialZoomState - this.settings.initialZoomState = null - this.elems.img.animate({'margin': 0}, duration) + this.settings.imageSize = this.settings.initialZoomState; + this.settings.initialZoomState = null; + this.elems.img.animate({'margin': 0}, duration); - this.elems.domContainer.removeClass('chocolat-zoomed') - fitting = this.fit(this.settings.currentImage, this.settings.container) + this.elems.domContainer.removeClass('chocolat-zoomed'); + fitting = this.fit(this.settings.currentImage, this.settings.container); return this.center(fitting.width, fitting.height, fitting.left, fitting.top, duration); }, setDomContainer : function() { // if container == window // domContainer = body - if( this.settings.container === window) { + if ( this.settings.container === window) { this.elems.domContainer = $('body'); } else { @@ -620,7 +621,7 @@ }, api: function() { - var that = this + var that = this; return { open : function(i){ i = parseInt(i) || 0; @@ -651,7 +652,8 @@ }, set : function(property, value){ - return that.settings[property] = value; + that.settings[property] = value; + return value; }, get : function(property){ @@ -661,11 +663,11 @@ getElem : function(name){ return that.elems[name]; }, - } + }; } }); - $.fn['Chocolat'] = function (options) { + $.fn.Chocolat = function (options) { return this.each(function() { calls++; @@ -678,5 +680,5 @@ ); } }); - } + }; })( jQuery, window, document );