Skip to content
This repository has been archived by the owner on May 4, 2020. It is now read-only.

Commit

Permalink
Changed behaviour to work unlimited after first start of gallery.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philipp-winterle committed Dec 27, 2016
1 parent 824b470 commit a95c8f2
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 73 deletions.
2 changes: 1 addition & 1 deletion dist/lg-autoupdate.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

126 changes: 54 additions & 72 deletions src/lg-autoupdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
}

};
Expand All @@ -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('<div class="lg-item"></div>');
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('<div class="lg-item"></div>');
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) {
Expand All @@ -140,4 +122,4 @@
jQuery.fn.lightGallery.modules.autoUpdate = AutoUpdate;


})(jQuery, window, document);
})(jQuery, window, document);

0 comments on commit a95c8f2

Please sign in to comment.