diff --git a/client/src/modules/pluginapi.js b/client/src/modules/pluginapi.js index 2e882f68..083386b6 100644 --- a/client/src/modules/pluginapi.js +++ b/client/src/modules/pluginapi.js @@ -210,33 +210,41 @@ export default class PluginApi { get modalStack() { return this._modalStack || (this._modalStack = []); } + get baseModalComponent() { + return Modals.baseComponent; + } addModal(_modal, component) { const modal = Modals.add(_modal, component); modal.close = force => this.closeModal(modal, force); + modal.on('close', () => { + let index; + while ((index = this.modalStack.findIndex(m => m === modal)) > -1) + this.modalStack.splice(index, 1); + }); this.modalStack.push(modal); return modal; } - async closeModal(modal, force) { - await Modals.close(modal, force); - this._modalStack = this.modalStack.filter(m => m !== modal); + closeModal(modal, force) { + return Modals.close(modal, force); } - closeAllModals() { + closeAllModals(force) { + const promises = []; for (let modal of this.modalStack) - modal.close(); + promises.push(modal.close(force)); + return Promise.all(promises); } - closeLastModal() { + closeLastModal(force) { if (!this.modalStack.length) return; - this.modalStack[this.modalStack.length - 1].close(); + return this.modalStack[this.modalStack.length - 1].close(force); + } + basicModal(title, text) { + return this.addModal(Modals.basic(title, text)); } settingsModal(settingsset, headertext, options) { - return this.addModal(Object.assign({ - headertext: headertext ? headertext : settingsset.headertext, - settings: settingsset, - schemes: settingsset.schemes - }, options), SettingsModal); + return this.addModal(Modals.settings(settingsset, headertext, options)); } get Modals() { - return Object.defineProperty({ + return Object.defineProperty(Object.defineProperty({ add: this.addModal.bind(this), close: this.closeModal.bind(this), closeAll: this.closeAllModals.bind(this), @@ -244,6 +252,8 @@ export default class PluginApi { settings: this.settingsModal.bind(this) }, 'stack', { get: () => this.modalStack + }), 'baseComponent', { + get: () => this.baseModalComponent }); } diff --git a/client/src/ui/modals.js b/client/src/ui/modals.js index 2715e430..6ebbaa0c 100644 --- a/client/src/ui/modals.js +++ b/client/src/ui/modals.js @@ -40,6 +40,7 @@ class Modal extends AsyncEventEmitter { this.vueInstance = undefined; this.vue = undefined; + this.close = this.close.bind(this); this.closed = this.once('closed'); } @@ -90,7 +91,10 @@ export default class Modals { modal.closing = true; await new Promise(resolve => setTimeout(resolve, 200)); - this._stack = this.stack.filter(m => m !== modal); + let index; + while ((index = this.stack.findIndex(m => m === modal)) > -1) + this.stack.splice(index, 1); + Events.emit('bd-refresh-modals'); try { @@ -250,7 +254,7 @@ export default class Modals { * An array of open modals. */ static get stack() { - return this._stack ? this._stack : (this._stack = []); + return this._stack || (this._stack = []); } /**