From 5e2b5975ed109496f90dae1bd5f6133e95549033 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Thu, 1 Mar 2018 21:10:30 +0000 Subject: [PATCH] Allow custom settings to extend the CustomSetting class --- .../src/structs/settings/settingscategory.js | 1 + .../src/structs/settings/types/basesetting.js | 8 ++--- client/src/structs/settings/types/custom.js | 33 ++++++++++++------- tests/plugins/Example/component.js | 17 ++++++++++ tests/plugins/Example/config.json | 7 ++++ 5 files changed, 51 insertions(+), 15 deletions(-) diff --git a/client/src/structs/settings/settingscategory.js b/client/src/structs/settings/settingscategory.js index 88c96ff7..6497c11a 100644 --- a/client/src/structs/settings/settingscategory.js +++ b/client/src/structs/settings/settingscategory.js @@ -10,6 +10,7 @@ import Setting from './setting'; import EventEmitter from 'events'; +import { ClientLogger as Logger } from 'common'; import { SettingUpdatedEvent, SettingsUpdatedEvent } from 'structs'; export default class SettingsCategory { diff --git a/client/src/structs/settings/types/basesetting.js b/client/src/structs/settings/types/basesetting.js index f8cc90d2..e638400b 100644 --- a/client/src/structs/settings/types/basesetting.js +++ b/client/src/structs/settings/types/basesetting.js @@ -47,10 +47,6 @@ export default class Setting { return undefined; } - // get changed() { - // return this.args.changed; - // } - get text() { return this.args.text; } @@ -59,6 +55,10 @@ export default class Setting { return this.args.hint; } + get path() { + return this.args.path; + } + get disabled() { return this.args.disabled || false; } diff --git a/client/src/structs/settings/types/custom.js b/client/src/structs/settings/types/custom.js index b8cb5b79..eb95de93 100644 --- a/client/src/structs/settings/types/custom.js +++ b/client/src/structs/settings/types/custom.js @@ -11,19 +11,15 @@ import Setting from './basesetting'; import SettingsCategory from '../settingscategory'; import SettingsScheme from '../settingsscheme'; +import path from 'path'; export default class CustomSetting extends Setting { constructor(args) { super(args); - if (this.args.class_file) { - const component = window.require(path.join(this.path, this.args.class_file)); - const setting_class = this.args.class ? component[this.args.class](CustomSetting) : component.default ? component.default(CustomSetting) : component(CustomSetting); - - const setting = new setting_class(this.args); - if (setting instanceof CustomSetting) return setting; - } + if (this.args.class_file && this.path) + this.setClass(this.args.class_file, this.args.class); } get file() { @@ -38,12 +34,27 @@ export default class CustomSetting extends Setting { return this.args.component; } - get path() { - return this.args.path; - } - get debug() { return this.args.debug || false; } + setContentPath(_path) { + this.args.path = _path; + + if (this.args.class_file) + this.setClass(this.args.class_file, this.args.class); + + console.log(`Custom setting ${this.id}:`, this); + } + + setClass(class_file, class_export) { + const component = window.require(path.join(this.path, this.args.class_file)); + const setting_class = class_export ? component[class_export](CustomSetting) : component.default ? component.default(CustomSetting) : component(CustomSetting); + + if (!(setting_class.prototype instanceof CustomSetting)) + throw {message: 'Custom setting class function returned a class that doesn\'t extend from CustomSetting.'}; + + this.__proto__ = setting_class.prototype; + } + } diff --git a/tests/plugins/Example/component.js b/tests/plugins/Example/component.js index c82c1359..f7fd7fe3 100644 --- a/tests/plugins/Example/component.js +++ b/tests/plugins/Example/component.js @@ -2,3 +2,20 @@ module.exports.default = { template: "
Test custom setting {{ setting.id }}. This is from component.js in the plugin/theme's directory. (It can use functions.)
", props: ['setting', 'change'] }; + +const component = { + template: "
Test custom setting {{ setting.id }}. This is included inline with the plugin/theme's config. (Which means it can't use any functions, but can still bind functions to events.)
", + props: ['setting', 'change'] +}; + +module.exports.CustomSetting = function (CustomSetting) { + return class extends CustomSetting { + get component() { + return component; + } + + get debug() { + return true; + } + } +}; diff --git a/tests/plugins/Example/config.json b/tests/plugins/Example/config.json index e2a64b95..92f6730c 100644 --- a/tests/plugins/Example/config.json +++ b/tests/plugins/Example/config.json @@ -237,6 +237,13 @@ "value": false, "function": "getSettingsComponentHTMLElement", "debug": true + }, + { + "id": "custom-6", + "type": "custom", + "value": false, + "class_file": "component.js", + "class": "CustomSetting" } ] },