From a95c8f203d3ba43d8f8441814d92dd5556d10932 Mon Sep 17 00:00:00 2001 From: Philipp Winterle Date: Tue, 27 Dec 2016 13:13:49 +0100 Subject: [PATCH] Changed behaviour to work unlimited after first start of gallery. If gallery is destroyed it will stop working. Also if you add images BEFORE you start the gallery the first time, but created it already, it wont be able to show the valid number of images until you call refreshItems again --- dist/lg-autoupdate.js | 2 +- src/lg-autoupdate.js | 126 ++++++++++++++++++------------------------ 2 files changed, 55 insertions(+), 73 deletions(-) diff --git a/dist/lg-autoupdate.js b/dist/lg-autoupdate.js index 64aafb9..9b74630 100644 --- a/dist/lg-autoupdate.js +++ b/dist/lg-autoupdate.js @@ -1 +1 @@ -!function(e,t,i,r){"use strict";var o={itemsAutoUpdate:!0,imageCount:0},s=function(e){return this.core=jQuery(e).data("lightGallery"),this.$el=jQuery(e),this.core.s=jQuery.extend({},o,this.core.s),this.init(),this};s.prototype.init=function(){if(this.core.s.itemsAutoUpdate){if(0!==this.core.s.imageCount){var e=this;this.$el.on("onAfterOpen.lg",function(){e.setCounter(e.core.s.imageCount)})}this.$el.on("refreshItems",this.refreshItems)}},s.prototype.refreshItems=function(){var e=this;if(this.core=jQuery(this).data("lightGallery"),jQuery(".lg-outer").length>0){this.core.s.dynamic?this.core.$items=this.core.s.dynamicEl:"this"===this.core.s.selector?this.core.$items=this.core.$el:""!==this.core.s.selector?this.core.s.selectWithin?this.core.$items=jQuery(this.core.s.selectWithin).find(this.core.s.selector):this.core.$items=this.core.$el.find(jQuery(this.core.s.selector)):this.core.$items=this.core.$el.children();for(var t=this.core.$items.length-this.core.$outer.find(".lg-inner").find(".lg-item").length;t>0;){var i=jQuery('
');this.core.$outer.find(".lg-inner").append(i),this.core.$slide=this.core.$outer.find(".lg-item"),t--}this.core.$items.on("click.lgcustom",function(t){try{t.preventDefault()}catch(e){t.returnValue=!1}e.core.$el.trigger("onBeforeOpen.lg"),e.core.index=e.core.s.index||e.core.$items.index(this),jQuery("body").hasClass("lg-on")||(e.core.build(e.core.index),jQuery("body").addClass("lg-on"))}),this.core.enableSwipe(),this.core.enableDrag(),0===this.core.s.imageCount&&this.core.modules.autoUpdate.setCounter(this.core.$items.length)}},s.prototype.overwriteCloseEvent=function(){var e=this.core;this.core.$outer.off("mouseup.lg"),this.core.$outer.on("mouseup.lg",function(t){jQuery(t.target).is(".lg-close")&&(e.$outer.hasClass("lg-dragging")||e.destroy())})},s.prototype.setCounter=function(e){this.core.$outer.find("#lg-counter-all").html(e)},s.prototype.destroy=function(){},jQuery.fn.lightGallery.modules.autoUpdate=s}(jQuery,window,document); \ No newline at end of file +!function(e,t,i,s){"use strict";var o={autoUpdate_itemsAutoUpdate:!0,autoUpdate_imageCount:0,autoUpdate_hasRefreshEvent:!1},r=function(e){return this.core=jQuery(e).data("lightGallery"),this.$el=jQuery(e),this.core.s=jQuery.extend({},o,this.core.s),this.init(),this};r.prototype.init=function(){if(this.core.s.autoUpdate_itemsAutoUpdate){if(0!==this.core.s.autoUpdate_imageCount){var e=this;this.$el.on("onAfterOpen.lg",function(){e.setCounter(e.core.s.autoUpdate_imageCount)})}this.core.s.autoUpdate_hasRefreshEvent||(this.$el.on("refreshItems",this.refreshItems),this.core.s.autoUpdate_hasRefreshEvent=!0)}},r.prototype.refreshItems=function(){var e=this;this.core=jQuery(this).data("lightGallery"),this.core.s.dynamic?this.core.$items=this.core.s.dynamicEl:"this"===this.core.s.selector?this.core.$items=this.core.$el:""!==this.core.s.selector?this.core.s.selectWithin?this.core.$items=jQuery(this.core.s.selectWithin).find(this.core.s.selector):this.core.$items=this.core.$el.find(jQuery(this.core.s.selector)):this.core.$items=this.core.$el.children();for(var t=this.core.$items.length-this.core.$outer.find(".lg-inner").find(".lg-item").length;t>0;){var i=jQuery('
');this.core.$outer.find(".lg-inner").append(i),this.core.$slide=this.core.$outer.find(".lg-item"),t--}this.core.$items.on("click.lgcustom",function(t){try{t.preventDefault()}catch(e){t.returnValue=!1}e.core.$el.trigger("onBeforeOpen.lg"),e.core.index=e.core.s.index||e.core.$items.index(this),jQuery("body").hasClass("lg-on")||(e.core.build(e.core.index),jQuery("body").addClass("lg-on"))}),this.core.enableSwipe(),this.core.enableDrag(),0===this.core.s.autoUpdate_imageCount&&this.core.modules.autoUpdate.setCounter(this.core.$items.length)},r.prototype.setCounter=function(e){this.core.$outer.find("#lg-counter-all").html(e)},r.prototype.destroy=function(){},jQuery.fn.lightGallery.modules.autoUpdate=r}(jQuery,window,document); \ No newline at end of file diff --git a/src/lg-autoupdate.js b/src/lg-autoupdate.js index 90fce6d..6f56667 100644 --- a/src/lg-autoupdate.js +++ b/src/lg-autoupdate.js @@ -7,8 +7,9 @@ 'use strict'; var defaults = { - itemsAutoUpdate: true, - imageCount: 0 + autoUpdate_itemsAutoUpdate: true, + autoUpdate_imageCount: 0, + autoUpdate_hasRefreshEvent: false }; var AutoUpdate = function (element) { @@ -25,16 +26,18 @@ }; AutoUpdate.prototype.init = function () { - if(this.core.s.itemsAutoUpdate) { - if (this.core.s.imageCount !== 0) { + if(this.core.s.autoUpdate_itemsAutoUpdate) { + if (this.core.s.autoUpdate_imageCount !== 0) { var self = this; this.$el.on("onAfterOpen.lg", function() { - self.setCounter(self.core.s.imageCount); + self.setCounter(self.core.s.autoUpdate_imageCount); }); } - // Add custom events for triggering - this.$el.on("refreshItems", this.refreshItems); - + if (!this.core.s.autoUpdate_hasRefreshEvent) { + // Add custom events for triggering + this.$el.on("refreshItems", this.refreshItems); + this.core.s.autoUpdate_hasRefreshEvent = true; // prevent adding event more than once + } } }; @@ -44,82 +47,61 @@ // Get core data this.core = jQuery(this).data('lightGallery'); - // Check if gallery is opened - if (jQuery(".lg-outer").length > 0) { - - // Add gallery items - if (this.core.s.dynamic) { - this.core.$items = this.core.s.dynamicEl; - } else { - if (this.core.s.selector === 'this') { - this.core.$items = this.core.$el; - } else if (this.core.s.selector !== '') { - if (this.core.s.selectWithin) { - this.core.$items = jQuery(this.core.s.selectWithin).find(this.core.s.selector); - } else { - this.core.$items = this.core.$el.find(jQuery(this.core.s.selector)); - } + // Add gallery items + if (this.core.s.dynamic) { + this.core.$items = this.core.s.dynamicEl; + } else { + if (this.core.s.selector === 'this') { + this.core.$items = this.core.$el; + } else if (this.core.s.selector !== '') { + if (this.core.s.selectWithin) { + this.core.$items = jQuery(this.core.s.selectWithin).find(this.core.s.selector); } else { - this.core.$items = this.core.$el.children(); + this.core.$items = this.core.$el.find(jQuery(this.core.s.selector)); } + } else { + this.core.$items = this.core.$el.children(); } + } - // Append new elements to outer html - var elementsToAdd = this.core.$items.length - this.core.$outer.find(".lg-inner").find(".lg-item").length; - while (elementsToAdd > 0) { - var newSlide = jQuery('
'); - this.core.$outer.find(".lg-inner").append(newSlide); - this.core.$slide = this.core.$outer.find(".lg-item"); - elementsToAdd--; - } - - // Add removed events again - this.core.$items.on('click.lgcustom', function(event) { - - // For IE8 - try { - event.preventDefault(); - } catch (er) { - event.returnValue = false; - } - - self.core.$el.trigger('onBeforeOpen.lg'); + // Append new elements to outer html + var elementsToAdd = this.core.$items.length - this.core.$outer.find(".lg-inner").find(".lg-item").length; + while (elementsToAdd > 0) { + var newSlide = jQuery('
'); + this.core.$outer.find(".lg-inner").append(newSlide); + this.core.$slide = this.core.$outer.find(".lg-item"); + elementsToAdd--; + } - self.core.index = self.core.s.index || self.core.$items.index(this); + // Add removed events again + this.core.$items.on('click.lgcustom', function(event) { - // prevent accidental double execution - if (!jQuery('body').hasClass('lg-on')) { - self.core.build(self.core.index); - jQuery('body').addClass('lg-on'); - } - }); + // For IE8 + try { + event.preventDefault(); + } catch (er) { + event.returnValue = false; + } - this.core.enableSwipe(); - this.core.enableDrag(); + self.core.$el.trigger('onBeforeOpen.lg'); - // this.core.modules.autoUpdate.overwriteCloseEvent(); + self.core.index = self.core.s.index || self.core.$items.index(this); - // Write the counter state - if (this.core.s.imageCount === 0) { - this.core.modules.autoUpdate.setCounter(this.core.$items.length); + // prevent accidental double execution + if (!jQuery('body').hasClass('lg-on')) { + self.core.build(self.core.index); + jQuery('body').addClass('lg-on'); } - } - }; - - AutoUpdate.prototype.overwriteCloseEvent = function() { - var core = this.core; - // Overwrites of close behaviour - this.core.$outer.off("mouseup.lg"); + }); - this.core.$outer.on('mouseup.lg', function(e) { + this.core.enableSwipe(); + this.core.enableDrag(); - if (jQuery(e.target).is('.lg-close') ) { - if (!core.$outer.hasClass('lg-dragging')) { - core.destroy(); - } - } + // Write the counter state + if (this.core.s.autoUpdate_imageCount === 0) { + this.core.modules.autoUpdate.setCounter(this.core.$items.length); + } - }); }; AutoUpdate.prototype.setCounter = function(count) { @@ -140,4 +122,4 @@ jQuery.fn.lightGallery.modules.autoUpdate = AutoUpdate; -})(jQuery, window, document); \ No newline at end of file +})(jQuery, window, document);