BetterDiscordApp-v2/client/src/ui/modals.js

148 lines
5.1 KiB
JavaScript
Raw Normal View History

2018-02-13 17:44:07 +01:00
/**
* BetterDiscord Modals
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Utils, FileUtils } from 'common';
import { Settings, Events, PluginManager, ThemeManager } from 'modules';
2018-02-13 17:44:07 +01:00
import BasicModal from './components/bd/modals/BasicModal.vue';
2018-02-19 19:18:55 +01:00
import ConfirmModal from './components/bd/modals/ConfirmModal.vue';
2018-02-13 17:44:07 +01:00
import ErrorModal from './components/bd/modals/ErrorModal.vue';
import SettingsModal from './components/bd/modals/SettingsModal.vue';
2018-02-28 20:34:12 +01:00
import PermissionModal from './components/bd/modals/PermissionModal.vue';
2018-02-13 17:44:07 +01:00
2018-02-13 17:57:05 +01:00
export default class {
2018-02-13 17:44:07 +01:00
static add(modal, component) {
modal.component = modal.component || {
template: '<custom-modal :modal="modal" />',
components: { 'custom-modal': component },
data() { return { modal }; },
2018-03-01 20:26:44 +01:00
created() {
modal.vueInstance = this;
modal.vue = this.$children[0];
}
2018-02-13 17:44:07 +01:00
};
modal.closing = false;
2018-02-14 19:19:20 +01:00
modal.close = force => this.close(modal, force);
2018-02-19 19:17:49 +01:00
modal.id = Date.now();
2018-02-13 17:44:07 +01:00
this.stack.push(modal);
Events.emit('bd-refresh-modals');
return modal;
}
2018-02-14 19:19:20 +01:00
static close(modal, force) {
return new Promise(async (resolve, reject) => {
if (modal.beforeClose) {
2018-02-14 19:19:20 +01:00
try {
2018-03-01 20:26:44 +01:00
const beforeCloseResult = await modal.beforeClose(force);
2018-02-14 19:19:20 +01:00
if (beforeCloseResult && !force) return reject(beforeCloseResult);
} catch (err) {
if (!force) return reject(err);
}
}
2018-02-13 17:44:07 +01:00
modal.closing = true;
setTimeout(() => {
this._stack = this.stack.filter(m => m !== modal);
Events.emit('bd-refresh-modals');
resolve();
}, 200);
});
}
static closeAll() {
for (let modal of this.stack)
modal.close();
}
static closeLast() {
if (!this.stack.length) return;
this.stack[this.stack.length - 1].close();
}
static basic(title, text) {
return this.add({ title, text }, BasicModal);
}
2018-02-19 19:18:55 +01:00
static confirm(title, text) {
const modal = { title, text };
2018-03-01 20:26:44 +01:00
modal.promise = new Promise((resolve, reject) => {
2018-02-19 19:18:55 +01:00
modal.confirm = () => resolve(true);
modal.beforeClose = () => reject();
this.add(modal, ConfirmModal);
});
return modal;
}
2018-02-28 20:34:12 +01:00
static permissions(title, name, perms) {
2018-03-01 20:26:44 +01:00
const modal = { title, name, perms };
2018-02-28 20:34:12 +01:00
modal.promise = new Promise((resolve, reject) => {
modal.confirm = () => resolve(true);
modal.beforeClose = () => reject();
this.add(modal, PermissionModal);
});
return modal;
}
2018-02-13 17:44:07 +01:00
static error(event) {
return this.add({ event }, ErrorModal);
}
static showContentManagerErrors(clear = true) {
2018-02-13 17:57:05 +01:00
// Get any errors from PluginManager and ThemeManager
const errors = ([]).concat(PluginManager.errors).concat(ThemeManager.errors);
if (errors.length) {
const modal = this.error({
header:
(PluginManager.errors.length && ThemeManager.errors.length ? '' :
(PluginManager.errors.length ? PluginManager.moduleName : ThemeManager.moduleName) + ' - ') +
(PluginManager.errors.length ? `${PluginManager.errors.length} ${PluginManager.contentType}${PluginManager.errors.length !== 1 ? 's' : ''}` : '') +
(PluginManager.errors.length && ThemeManager.errors.length ? ' and ' : '') +
(ThemeManager.errors.length ? `${ThemeManager.errors.length} ${ThemeManager.contentType}${ThemeManager.errors.length !== 1 ? 's' : ''}` : '') +
' failed to load',
module: (PluginManager.errors.length && ThemeManager.errors.length ? 'Content Manager' :
(PluginManager.errors.length ? PluginManager.moduleName : ThemeManager.moduleName)),
type: 'err',
content: errors
});
if (clear) {
PluginManager._errors = [];
ThemeManager._errors = [];
}
return modal;
}
2018-02-13 17:57:05 +01:00
}
2018-03-02 21:19:59 +01:00
static settings(settingsset, headertext, options) {
return this.add(Object.assign({
headertext: headertext ? headertext : settingsset.headertext,
settings: settingsset,
2018-03-02 21:48:29 +01:00
schemes: settingsset.schemes
2018-03-02 21:19:59 +01:00
}, options), SettingsModal);
}
static internalSettings(set_id) {
const set = Settings.getSet(set_id);
if (!set) return;
2018-03-01 20:00:24 +01:00
return this.settings(set, set.headertext);
}
static contentSettings(content) {
2018-03-01 20:00:24 +01:00
return this.settings(content.settings, content.name + ' Settings');
}
2018-02-13 17:44:07 +01:00
static get stack() {
return this._stack ? this._stack : (this._stack = []);
}
}