Update plugin API modals

This commit is contained in:
Samuel Elliott 2018-03-07 01:10:06 +00:00
parent a89d186730
commit a8d60582d0
No known key found for this signature in database
GPG Key ID: 8420C7CDE43DC4D6
2 changed files with 29 additions and 15 deletions

View File

@ -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
});
}

View File

@ -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 = []);
}
/**