Browse Source

Pass event to close method

pull/243/head
Jānis Skarnelis 2 years ago
parent
commit
c39015831c
  1. 29
      src/Fancybox/Fancybox.js

29
src/Fancybox/Fancybox.js

@ -96,7 +96,7 @@ const defaults = {
}; };
// Object that contains all active instances of Fancybox // Object that contains all active instances of Fancybox
const instances = {}; const instances = new Map();
// Number of Fancybox instances created, it is used to generate new instance "id" // Number of Fancybox instances created, it is used to generate new instance "id"
let called = 0; let called = 0;
@ -139,7 +139,7 @@ class Fancybox extends Base {
this.attachEvents(); this.attachEvents();
instances[this.id] = this; instances.set(this.id, this);
// "prepare" event will trigger the creation of additional layout elements, such as thumbnails and toolbar // "prepare" event will trigger the creation of additional layout elements, such as thumbnails and toolbar
this.trigger("prepare"); this.trigger("prepare");
@ -518,7 +518,7 @@ class Fancybox extends Base {
if (eventTarget.matches("[data-fancybox-close]")) { if (eventTarget.matches("[data-fancybox-close]")) {
event.preventDefault(); event.preventDefault();
Fancybox.close(false); Fancybox.close(false, event);
return; return;
} }
@ -1199,7 +1199,7 @@ class Fancybox extends Base {
setFocusOn($trigger); setFocusOn($trigger);
} }
delete instances[this.id]; instances.delete(this.id);
const nextInstance = Fancybox.getInstance(); const nextInstance = Fancybox.getInstance();
@ -1485,10 +1485,10 @@ class Fancybox extends Base {
*/ */
static getInstance(id) { static getInstance(id) {
if (id) { if (id) {
return instances[id]; return instances.get(id);
} }
const instance = Object.values(instances) const instance = Array.from(instances.values())
.reverse() .reverse()
.find((instance) => { .find((instance) => {
if (!["closing", "customClosing", "destroy"].includes(instance.state)) { if (!["closing", "customClosing", "destroy"].includes(instance.state)) {
@ -1504,14 +1504,19 @@ class Fancybox extends Base {
/** /**
* Close all or topmost currently active instance. * Close all or topmost currently active instance.
* @param {boolean} [all] - All or only topmost active instance * @param {boolean} [all] - All or only topmost active instance
* @param {any} [arguments] - Optional data
*/ */
static close(all = true) { static close(all = true, args) {
let instance = null; if (all) {
for (const instance of instances.values()) {
while ((instance = Fancybox.getInstance())) { instance.close(args);
instance.close(); }
} else {
const instance = Fancybox.getInstance();
if (!all) return; if (instance) {
instance.close(args);
}
} }
} }

Loading…
Cancel
Save