Fix ready event (closes #117)

This commit is contained in:
Thomas Brouard 2022-05-24 10:05:04 +02:00
parent d911073418
commit d0df158fcf
6 changed files with 36 additions and 5 deletions

View file

@ -11,11 +11,13 @@
<script src="../dist/electron-tabs.sortable.js"></script> <script src="../dist/electron-tabs.sortable.js"></script>
<script> <script>
const tabGroup = document.querySelector("tab-group"); const tabGroup = document.querySelector("tab-group");
tabGroup.on("ready", () => console.info("TabGroup is ready"));
tabGroup.setDefaultTab({ tabGroup.setDefaultTab({
title: "Wikipedia", title: "Wikipedia",
src: "https://www.wikipedia.org/", src: "https://www.wikipedia.org/",
active: true active: true,
ready: () => console.info("New Tab is ready")
}); });
tabGroup.addTab({ tabGroup.addTab({

View file

@ -61,6 +61,7 @@ class $4fa36e821943b400$var$TabGroup extends HTMLElement {
if (window.Sortable) initSortable(); if (window.Sortable) initSortable();
else document.addEventListener("DOMContentLoaded", initSortable); else document.addEventListener("DOMContentLoaded", initSortable);
} }
this.emit("ready", this);
} }
setDefaultTab(tab) { setDefaultTab(tab) {
this.options.defaultTab = tab; this.options.defaultTab = tab;
@ -167,6 +168,7 @@ class $4fa36e821943b400$var$Tab extends EventTarget {
$4fa36e821943b400$var$TabPrivate.initWebview.bind(this)(); $4fa36e821943b400$var$TabPrivate.initWebview.bind(this)();
if (args.visible !== false) this.show(); if (args.visible !== false) this.show();
if (typeof args.ready === "function") args.ready(this); if (typeof args.ready === "function") args.ready(this);
else this.emit("ready", this);
} }
setTitle(title) { setTitle(title) {
if (this.isClosed) return; if (this.isClosed) return;
@ -380,15 +382,22 @@ const $4fa36e821943b400$var$TabPrivate = {
* This makes the browser EventTarget API work similar to EventEmitter * This makes the browser EventTarget API work similar to EventEmitter
*/ const $4fa36e821943b400$var$eventEmitterMixin = { */ const $4fa36e821943b400$var$eventEmitterMixin = {
emit (type, ...args) { emit (type, ...args) {
if (type === "ready") this.isReady = true;
this.dispatchEvent(new CustomEvent(type, { this.dispatchEvent(new CustomEvent(type, {
detail: args detail: args
})); }));
}, },
on (type, fn) { on (type, fn) {
if (type === "ready" && this.isReady === true) fn.apply(this, [
this
]);
this.addEventListener(type, ({ detail: detail })=>fn.apply(this, detail) this.addEventListener(type, ({ detail: detail })=>fn.apply(this, detail)
); );
}, },
once (type, fn) { once (type, fn) {
if (type === "ready" && this.isReady === true) fn.apply(this, [
this
]);
this.addEventListener(type, ({ detail: detail })=>fn.apply(this, detail) this.addEventListener(type, ({ detail: detail })=>fn.apply(this, detail)
, { , {
once: true once: true

File diff suppressed because one or more lines are too long

View file

@ -3131,6 +3131,7 @@ class TabGroup extends HTMLElement {
if (window.Sortable) initSortable(); if (window.Sortable) initSortable();
else document.addEventListener("DOMContentLoaded", initSortable); else document.addEventListener("DOMContentLoaded", initSortable);
} }
this.emit("ready", this);
} }
setDefaultTab(tab) { setDefaultTab(tab) {
this.options.defaultTab = tab; this.options.defaultTab = tab;
@ -3237,6 +3238,7 @@ class Tab extends EventTarget {
TabPrivate.initWebview.bind(this)(); TabPrivate.initWebview.bind(this)();
if (args.visible !== false) this.show(); if (args.visible !== false) this.show();
if (typeof args.ready === "function") args.ready(this); if (typeof args.ready === "function") args.ready(this);
else this.emit("ready", this);
} }
setTitle(title) { setTitle(title) {
if (this.isClosed) return; if (this.isClosed) return;
@ -3450,15 +3452,22 @@ const TabPrivate = {
* This makes the browser EventTarget API work similar to EventEmitter * This makes the browser EventTarget API work similar to EventEmitter
*/ const eventEmitterMixin = { */ const eventEmitterMixin = {
emit (type, ...args) { emit (type, ...args) {
if (type === "ready") this.isReady = true;
this.dispatchEvent(new CustomEvent(type, { this.dispatchEvent(new CustomEvent(type, {
detail: args detail: args
})); }));
}, },
on (type, fn) { on (type, fn) {
if (type === "ready" && this.isReady === true) fn.apply(this, [
this
]);
this.addEventListener(type, ({ detail })=>fn.apply(this, detail) this.addEventListener(type, ({ detail })=>fn.apply(this, detail)
); );
}, },
once (type, fn) { once (type, fn) {
if (type === "ready" && this.isReady === true) fn.apply(this, [
this
]);
this.addEventListener(type, ({ detail })=>fn.apply(this, detail) this.addEventListener(type, ({ detail })=>fn.apply(this, detail)
, { , {
once: true once: true

File diff suppressed because one or more lines are too long

View file

@ -18,8 +18,6 @@ class TabGroup extends HTMLElement {
newTabButton: this.getAttribute("new-tab-button") || false, newTabButton: this.getAttribute("new-tab-button") || false,
defaultTab: { title: "New Tab", active: true }, defaultTab: { title: "New Tab", active: true },
sortable: this.getAttribute("sortable") || false sortable: this.getAttribute("sortable") || false
// TODO: replace this callback
// ready: args.ready
}; };
// Create custom element // Create custom element
@ -75,6 +73,8 @@ class TabGroup extends HTMLElement {
document.addEventListener("DOMContentLoaded", initSortable); document.addEventListener("DOMContentLoaded", initSortable);
} }
} }
this.emit("ready", this);
} }
setDefaultTab (tab) { setDefaultTab (tab) {
@ -220,6 +220,8 @@ class Tab extends EventTarget {
} }
if (typeof args.ready === "function") { if (typeof args.ready === "function") {
args.ready(this); args.ready(this);
} else {
this.emit("ready", this);
} }
} }
@ -490,14 +492,23 @@ const TabPrivate = {
*/ */
const eventEmitterMixin = { const eventEmitterMixin = {
emit (type, ...args) { emit (type, ...args) {
if (type === "ready") {
this.isReady = true;
}
this.dispatchEvent(new CustomEvent(type, { detail: args })); this.dispatchEvent(new CustomEvent(type, { detail: args }));
}, },
on (type, fn) { on (type, fn) {
if (type === "ready" && this.isReady === true) {
fn.apply(this, [this]);
}
this.addEventListener(type, ({ detail }) => fn.apply(this, detail)); this.addEventListener(type, ({ detail }) => fn.apply(this, detail));
}, },
once (type, fn) { once (type, fn) {
if (type === "ready" && this.isReady === true) {
fn.apply(this, [this]);
}
this.addEventListener(type, ({ detail }) => fn.apply(this, detail), { once: true }); this.addEventListener(type, ({ detail }) => fn.apply(this, detail), { once: true });
} }
}; };