Add live adding schemes and fix PluginApi.Settings

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

View File

@ -106,7 +106,7 @@ export default class PluginApi {
}
get Settings() {
return {
createSet: this.createSet.bind(this),
createSet: this.createSettingsSet.bind(this),
createCategory: this.createSettingsCategory.bind(this),
createSetting: this.createSetting.bind(this),
createScheme: this.createSettingsScheme.bind(this)

View File

@ -181,6 +181,50 @@ export default class SettingsSet {
await this.emit('removed-category', event);
}
/**
* Dynamically adds a scheme to this set.
* @param {SettingsScheme} scheme The scheme to add to this set
* @param {Number} index The index to add the scheme at (optional)
* @return {Promise}
*/
async addScheme(scheme, index) {
if (this.schemes.find(c => c === scheme)) return;
if (!(scheme instanceof SettingsScheme))
scheme = new SettingsScheme(scheme);
if (this.schemes.find(s => s.id === scheme.id))
throw {message: 'A scheme with this ID already exists.'};
if (index === undefined) index = this.schemes.length;
this.schemes.splice(index, 0, scheme);
await this.emit('added-scheme', {
set: this, set_id: this.id,
scheme, scheme_id: scheme.id,
at_index: index
});
return scheme;
}
/**
* Dynamically removes a scheme from this set.
* @param {SettingsScheme} scheme The scheme to remove from this set
* @return {Promise}
*/
async removeScheme(scheme) {
let index;
while ((index = this.schemes.findIndex(s => s === scheme)) > -1) {
this.schemes.splice(index, 0);
}
await this.emit('removed-scheme', {
set: this, set_id: this.id,
scheme, scheme_id: scheme.id,
from_index: index
});
}
/**
* Returns the first category where calling {function} returns true.
* @param {Function} function A function to call to filter categories