16,874
个编辑
LLWiki正在建设中,欢迎加入我们!
小 |
|||
* @Dependencies: ext.gadget.site-styles
* @Param {Object[]} options, 形如{text, msg, icon, href, target, click}的菜单项
* @Param {
* @Param {String} msg, mw.messages的键值
* @Return {jQuery} 菜单外层容器▼
* @Param {String} icon, FontAwesome的图标名称(仅限fas类,可选)
* @Param {String} href, 链接(可选)
* @Param {String} target(可选)
* @Param {Function} click, 单击事件(可选)
* @Param {Object} attr, 菜单外层容器属性(可选)
* @Param {Number} multiSelect, 允许的选择数(可选)
▲ * @Return {jQuery} 菜单外层容器,注意初始化时不可连锁
*/
mw.menu = function(options, attr, multiSelect) {
const hasIcon = options.some(function(ele) { return ele.icon; })
return $('<a>', {href: ele.href, target: ele.target, class: ele.selected ? 'site-menu-selected' : '', html: [
hasIcon ? $('<i>', {class: 'fa' + (ele.icon ? 'fa-' + ele.icon : '')}) : null,
ele.msg ? mw.msg( ele.msg ) : ele.text
]}).click( ele.click );
}), class: 'site-menu', tabindex: -1}, attr)).extend({open: function() { this.slideDown( 'fast' ).focus(); }})
.
setSelected: function(child) {
const $child = $(child); // child已经是jQuery了也没关系
if (this[0] != $child.parent()[0]) { return; }
if (multiSelect == 1) { this.clearSelected(); } // 单选
$child.addClass( 'site-menu-selected' );
}
}).blur(function() { $(this).slideUp( 'fast' ); }).on('click', 'a', function() {
$menu.slideUp( 'fast' );
if (multiSelect) { $menu.setSelected( this ); }
});
return $menu;
};
|