LLWiki正在建设中,欢迎加入我们

“MediaWiki:Gadget-SettingsDialog.js”的版本间差异

跳转到导航 跳转到搜索
添加1,086字节 、​ 2021年2月5日 (五) 04:21
无编辑摘要
* getObject:获取小工具对象
* addTab:添加小工具
* buildForm:生成表单
* removeTab:移除小工具
* saveOptions:将设置保存到localStorage
}) );
var ready = false;
const buildForm = function(dialog, params) { // 生成表单,只需要执行一次,所有不用写成SettingsDialog的内置方法
// constructor只添加一个CSS类,剩下的交给addTab方法逐一添加小工具
if (params.ready) { return; }
const Help = params.help,
$element = dialog.content.getTabPanel( params.name ).$element.append(
$('<div>', {html: [mw.msg('gadget-sd-help')].concat( Help ? [mw.msg('gadget-sd-helppage'),
$('<a>', {href: mw.util.getUrl('Help:小工具/' + Help), target: '_blank', text: mw.msg('gadget-sd-helptext')}),
'。'] : [] )}) );
(params.items || []).forEach(function(ele) {
ele.widget = new OO.ui[ele.type + 'InputWidget']( ele.config );
$element.append( new OO.ui.FieldLayout(ele.widget, {label: mw.msg( ele.label ), help: ele.help}).$element );
delete ele.config;
});
(params.fields || []).forEach(function(ele) {
const field = new OO.ui.FieldsetLayout({label: mw.msg( ele.label ), help: ele.help, helpInline: true});
field.addItems( (ele.items || []).map(function(e) {
e.widget = new OO.ui[e.type + 'InputWidget']( e.config );
delete e.config;
return new OO.ui.FieldLayout(e.widget, {label: mw.msg( e.label ), help: e.help});
}) );
$element.append( field.$element );
});
$('<div>', {class: 'panel-btns', html: new OO.ui.ButtonWidget({label: mw.msg('gadget-sd-back'),
flags: 'destructive'}).on('click', function() { dialog.clearOptions( params.name ); }).$element})
.appendTo( $element );
params.ready = true;
mw.hook( 'settings.dialog' ).fire( params ); // 生成一个Hook
};
/**
* @Description: constructor只添加一个CSS类,剩下的交给addTab方法逐一添加小工具
*/
function SettingsDialog() {
SettingsDialog.super.call(this, {classes: ['settingsDialog']});
}
OO.inheritClass(SettingsDialog, OO.ui.ProcessDialog);
/**
// initialize只创建一个OO.ui.IndexLayout对象,剩下的交给addTab方法填入内容
* @Description: initialize只创建一个OO.ui.IndexLayout对象,剩下的交给addTab方法填入内容
*/
SettingsDialog.prototype.initialize = function() {
SettingsDialog.super.prototype.initialize.apply(this, arguments);
this.$body.append( this.content.$element );
};
/**
* @Description: 处理动作
* @Param {String} 动作名
*/
SettingsDialog.prototype.getActionProcess = function(action) {
const dialog = this,
return new OO.ui.Process();
};
/**
// 需要同时添加数据和HTML
* @Description: 同时添加数据和HTML,其中HTML会延后
* @Param {Object} 数据对象
*/
SettingsDialog.prototype.addTab = function(params) {
const dialog = this,
this.gadgets.push( params );
// 必要时才开始加载表单
panel.on('active', function() { dialog.buildForm(dialog, params ); });
if (ready) { return; }
// 添加按钮,注意手机版的执行时机
ready = true;
};
/**
SettingsDialog.prototype.buildForm = function(params) {
* @Description: 获取小工具序号
if (params.ready) { return; }
* @Param {String} 小工具名称
const dialog = this,
* @Param {Object} 小工具数据
Help = params.help,
* @Return {Number} 小工具序号
$element = this.content.getTabPanel( params.name ).$element.append(
*/
$('<div>', {html: [mw.msg('gadget-sd-help')].concat( Help ? [mw.msg('gadget-sd-helppage'),
$('<a>', {href: mw.util.getUrl('Help:小工具/' + Help), target: '_blank', text: mw.msg('gadget-sd-helptext')}),
'。'] : [] )}) );
(params.items || []).forEach(function(ele) {
ele.widget = new OO.ui[ele.type + 'InputWidget']( ele.config );
$element.append( new OO.ui.FieldLayout(ele.widget, {label: mw.msg( ele.label ), help: ele.help}).$element );
});
(params.fields || []).forEach(function(ele) {
const field = new OO.ui.FieldsetLayout({label: mw.msg( ele.label ), help: ele.help, helpInline: true});
field.addItems( (ele.items || []).map(function(e) {
e.widget = new OO.ui[e.type + 'InputWidget']( e.config );
return new OO.ui.FieldLayout(e.widget, {label: mw.msg( e.label ), help: e.help});
}) );
$element.append( field.$element );
});
$('<div>', {class: 'panel-btns', html: [
new OO.ui.ButtonWidget({label: mw.msg('gadget-sd-back'), flags: 'destructive'}).on('click', function() {
dialog.clearOptions( params.name );
}), new OO.ui.ButtonWidget({label: mw.msg('gadget-sd-save'), flags: 'progressive'}).on('click', function() {
dialog.saveOptions( params.name );
})].map(function(ele) { return ele.$element; })}).appendTo( $element );
params.ready = true;
mw.hook( 'settings.dialog' ).fire( params ); // 生成一个Hook
};
SettingsDialog.prototype.getIndex = function(arg) {
if (typeof arg == 'string') { return this.gadgets.findIndex(function(ele) { return ele.name == arg; }); }
else { return this.gadgets.indexOf( arg ); }
};
/**
* @Description: 获取小工具名称
* @Param {Number} 小工具序号
* @Param {Object} 小工具数据
* @Return {String} 小工具名称
*/
SettingsDialog.prototype.getName = function(arg) {
if (typeof arg == 'string') { return arg; }
else { return arg.name; }
};
/**
* @Description: 获取小工具数据
* @Param {String} 小工具名称
* @Param {Number} 小工具序号
* @Return {Object} 小工具数据
*/
SettingsDialog.prototype.getObject = function(arg) {
if (typeof arg == 'string') { return this.gadgets.find(function(ele) { return ele.name == arg; }); }
else { return arg; }
};
/**
* @Description: 移除小工具
* @Param {String} 小工具名称
* @Param {Number} 小工具序号
* @Param {Object} 小工具数据
*/
SettingsDialog.prototype.removeTab = function(arg) {
const name = this.getName(arg),
this.content.removeTabPanels( this.content.getTabPanel( name ) );
};
/**
* @Description: 还原选项
* @Param {String} 小工具名称
* @Param {Number} 小工具序号
* @Param {Object} 小工具数据
*/
SettingsDialog.prototype.clearOptions = function(arg) {
const gadget = this.getObject(arg);
const name = gadget.name,
settings = mw.gadgets[ name ];
(gadget.items || []).forEach(function(ele) { ele.widget.setValue( settings[ ele.key ] ); });
(gadget.fields || []).forEach(function(ele) {
const obj = settings[ ele.key ] || {};
ele.items.forEach(function(e) { e.widget.setValue( obj[ e.key ] ); });
});
}) ();
};
/**
* @Description: 保存设置
* @Param {String} 小工具名称
* @Param {Number} 小工具序号
* @Param {Object} 小工具数据
*/
SettingsDialog.prototype.saveOptions = function(arg) {
const gadget = this.getObject(arg);
const name = gadget.name,
settings = mw.gadgets[ name ];
(gadget.items || []).filter(function(ele) { return !ele.widget.isDisabled(); })
.forEach(function(ele) { settings[ ele.key ] = ele.widget.getValue() || undefined; });
(gadget.fields || []).forEach(function(ele) {
settings[ ele.key ] = settings[ ele.key ] || {};
const obj = settings[ ele.key ]; // 已经是一个对象了
ele.items.filter(function(e) { return !e.widget.isDisabled(); })
.forEach(function(e) { settings[ ele.key ]obj[ e.key ] = e.widget.getValue() || undefined; });
});
mw.storage.setObject('gadget-' + name, settings);
mw.notify(mw.msg( 'gadget-sd-notify' ), {type: 'success', tag: 'gadget-settings'});
}) ();
};
SettingsDialog.static = {name: 'settingsDialog', tagName: 'div', title: mw.msg('gadget-sd-title'),
};
mw.settingsDialog = new SettingsDialog();
const $body = $(document.body);
if (!mw.windowManager) {
mw.windowManager = mw.windowmanager || new OO.ui.WindowManager();
mw.windowManager.$element.appendTo( '$body' );
}
mw.windowManager.addWindows( [mw.settingsDialog] ); // 此时已经初始化
$(document.body).on('click', '#ca-settingsDialog', function(e) {
e.preventDefault();
const dialog = mw.settingsDialog,
dialog.open().opening.then(function() {
if (params.ready) { return; }
dialog.buildForm(dialog, params );
});
});
16,874

个编辑

导航菜单