16,874
个编辑
LLWiki正在建设中,欢迎加入我们!
小 (Bhsd移动页面MediaWiki:Gadget-site-lib-es6.js至MediaWiki:Gadget-SettingsDialog.js,不留重定向:更直观的命名) |
小 |
||
//<nowiki>
// 由ResourceLoader直接调用,不可使用ES6语法
/**
* @Function: 定义小工具设置对话框
* @Methods: constructor,构建mw.SettingsDialog对象
initialize,打开窗口时初始化html
getActionProcess,点击按钮时执行动作
saveOptions,将设置保存到localStorage
* @Author: [[User:Bhsd]]
*/
"use strict";
/* global OO, wgULS */
const formalize = function(
mw.SettingsDialog = function(config) {
config.classes =
mw.SettingsDialog.super.call(this, config);
this.name = config.name;
mw.gadgets = mw.gadgets || {};
const settings = mw.gadgets[ this.name ] || {}; // 这里不更新mw.gadgets[ this.name ],因为原则上小工具执行时已经更新过了
// checkbox输入:[data, label, default],输出:{data, label, widget}
this.checkboxes = (config.checkboxes || []).map(function(ele)
const option = formalize(ele);
widget: new OO.ui.CheckboxInputWidget( {selected: settings[ ele[0] ] || ele[2]} )}));▼
return $.extend(option, {widget:
// radio输入:[data, label, options],输出:{data, label, widget}▼
▲
this.radios = (config.radios || []).map(ele => ({ data: ele[0], label: ele[1],▼
widget: new OO.ui.RadioSelectInputWidget({options: formalize(ele.options),▼
value: settings[ ele[0] ] || ele[2]}) })); // 初始值不正确时会自动选取第一个选项▼
const option = formalize(ele);
return $.extend(option, {widget:
});
};
OO.inheritClass(mw.SettingsDialog, OO.ui.ProcessDialog);
mw.SettingsDialog.prototype.initialize = function() {
mw.SettingsDialog.super.prototype.initialize.apply(this, arguments);
this.$body.append(
}).concat( '<hr>' ).concat( this.radios.map(function(ele) {
return $('<div>', {class: 'multioptions-wrap', html: [$('<b>', {text: ele.label}), ele.widget.$element]})
▲ '<hr>',
}) ) );
▲ html: [$('<b>', {text: ele.label}), ele.widget.$element]}))
▲ ] );
};
mw.SettingsDialog.prototype.getActionProcess = function(action) {
};
mw.SettingsDialog.prototype.saveOptions = function() {
const settings = Object.fromEntries(
}).conat( this.radios.map(function(ele) { return [ele.data, ele.widget.getValue()]; }) ) );
mw.gadgets[ this.name ] = settings;
mw.storage.setObject(
mw.hook( 'settings.update' ).fire( this.name ); // 视情况根据新设置更新小工具,有些设置可能需要刷新页面才会生效
};
mw.SettingsDialog.static = {name: 'settingsDialog', tagName: 'div', title: wgULS('小工具设置', '小工具設置'),
{action: 'cancel', label: '取消', flags: 'safe'}]▼
▲ {action: 'save', label: '保存', flags: ['primary', 'progressive']},
▲ {action: 'cancel', label: '取消', flags: 'safe'}
▲ ]
};
//</nowiki>
// [[category:作为模块的小工具]] [[category:桌面版小工具]] [[category:手机版小工具]] [[category:系统工具]]
// {{DEFAULTSORT:SettingsDialog.js}}
|