From 382401d527314b270e1c67b4f88cd3bfbca81d46 Mon Sep 17 00:00:00 2001 From: Victor Häggqvist Date: Thu, 14 Jan 2016 22:54:43 +0100 Subject: create plugin architecture --- src/LightBox.js | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) (limited to 'src/LightBox.js') diff --git a/src/LightBox.js b/src/LightBox.js index b9f6b03..5479819 100644 --- a/src/LightBox.js +++ b/src/LightBox.js @@ -26,10 +26,6 @@ export class LightBox { quitOnEnd: false, quitOnImgClick: false, quitOnDocClick: true, - onStart: false, - onEnd: false, - onLoadStart: false, - onLoadEnd: false, }; this.options = Object.assign(options, defaultOptions); @@ -46,6 +42,12 @@ export class LightBox { this.swipeEnd = 0; this.imagePosLeft = 0; + + this.onStartListeners = []; + this.onEndListeners = []; + this.onLoadStartListeners = []; + this.onLoadEndListeners = []; + this.bindEvents(); } @@ -112,8 +114,9 @@ export class LightBox { setTimeout(() => { this.removeImage(); this.inProgress = false; - if (this.options.onEnd !== false) - this.options.onEnd(); + + this.onEndListeners.forEach(l => l()); + }, this.options.animationSpeed); } @@ -129,7 +132,7 @@ export class LightBox { this.inProgress = false; - if (this.options.onStart !== false ) this.options.onStart(); + this.onStartListeners.forEach(l => l()); this.target = element; @@ -183,7 +186,7 @@ export class LightBox { } this.inProgress = true; - if (this.options.onLoadStart !== false) this.options.onLoadStart(); + this.onLoadStartListeners.forEach(l => l()); setTimeout(() => { log.debug('loadImage in'); @@ -209,6 +212,7 @@ export class LightBox { setTimeout(() => { this.inProgress = false; + this.onLoadEndListeners.forEach(l => l()); }, this.options.animationSpeed); if (this.options.preloadNext) { @@ -375,6 +379,26 @@ export class LightBox { log.debug('resized'); this.setImage(); } + + addOnStartListener(listener) { + this.onStartListeners.push(listener); + } + + addOnEndListener(listener) { + this.onEndListeners.push(listener); + } + + addOnLoadStartListener(listener) { + this.onLoadStartListeners.push(listener); + } + + addOnLoadEndListener(listener) { + this.onLoadEndListeners.push(listener); + } + + registerPlugin(plugin) { + plugin.register(this); + } } LightBox.HAS_TOUCH = ('ontouchstart' in window); -- cgit v1.2.3