升级.net8
This commit is contained in:
@ -3,11 +3,12 @@
|
||||
* 下拉菜单组件
|
||||
*/
|
||||
|
||||
layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
layui.define(['jquery', 'laytpl', 'lay', 'util'], function(exports){
|
||||
"use strict";
|
||||
|
||||
var $ = layui.$;
|
||||
var laytpl = layui.laytpl;
|
||||
var util = layui.util;
|
||||
var hint = layui.hint();
|
||||
var device = layui.device();
|
||||
var clickOrMousedown = (device.mobile ? 'touchstart' : 'mousedown');
|
||||
@ -15,10 +16,17 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
// 模块名
|
||||
var MOD_NAME = 'dropdown';
|
||||
var MOD_INDEX = 'layui_'+ MOD_NAME +'_index'; // 模块索引名
|
||||
var MOD_ID = 'lay-' + MOD_NAME + '-id';
|
||||
|
||||
// 外部接口
|
||||
var dropdown = {
|
||||
config: {},
|
||||
config: {
|
||||
customName: { // 自定义 data 字段名
|
||||
id: 'id',
|
||||
title: 'title',
|
||||
children: 'child'
|
||||
}
|
||||
},
|
||||
index: layui[MOD_NAME] ? (layui[MOD_NAME].index + 10000) : 0,
|
||||
|
||||
// 设置全局项
|
||||
@ -53,6 +61,9 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
},
|
||||
close: function () {
|
||||
that.remove()
|
||||
},
|
||||
open: function () {
|
||||
that.render()
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -94,8 +105,9 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
isAllowSpread: true, // 是否允许菜单组展开收缩
|
||||
isSpreadItem: true, // 是否初始展开子菜单
|
||||
data: [], // 菜单数据结构
|
||||
delay: 300, // 延迟关闭的毫秒数,若 trigger 为 hover 时才生效
|
||||
shade: 0 // 遮罩
|
||||
delay: [200, 300], // 延时显示或隐藏的毫秒数,若为 number 类型,则表示显示和隐藏的延迟时间相同,trigger 为 hover 时才生效
|
||||
shade: 0, // 遮罩
|
||||
accordion: false // 手风琴效果,仅菜单组生效。基础菜单需要在容器上追加 'lay-accordion' 属性。
|
||||
};
|
||||
|
||||
// 重载实例
|
||||
@ -125,8 +137,8 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
$.extend(options, lay.options(elem[0]));
|
||||
|
||||
// 若重复执行 render,则视为 reload 处理
|
||||
if(!rerender && elem[0] && elem.data(MOD_INDEX)){
|
||||
var newThat = thisModule.getThis(elem.data(MOD_INDEX));
|
||||
if(!rerender && elem[0] && elem.attr(MOD_ID)){
|
||||
var newThat = thisModule.getThis(elem.attr(MOD_ID));
|
||||
if(!newThat) return;
|
||||
|
||||
return newThat.reload(options, type);
|
||||
@ -139,6 +151,11 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
elem.attr('id') || that.index
|
||||
);
|
||||
|
||||
elem.attr(MOD_ID, options.id);
|
||||
|
||||
// 初始化自定义字段名
|
||||
options.customName = $.extend({}, dropdown.config.customName, options.customName);
|
||||
|
||||
if(options.show || (type === 'reloadData' && that.elemView && $('body').find(that.elemView.get(0)).length)) that.render(rerender, type); //初始即显示或者面板弹出之后执行了刷新数据
|
||||
that.events(); // 事件
|
||||
};
|
||||
@ -147,6 +164,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
Class.prototype.render = function(rerender, type){
|
||||
var that = this;
|
||||
var options = that.config;
|
||||
var customName = options.customName;
|
||||
var elemBody = $('body');
|
||||
|
||||
// 默认菜单内容
|
||||
@ -155,7 +173,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
if(options.data.length > 0 ){
|
||||
eachItemView(elemUl, options.data)
|
||||
} else {
|
||||
elemUl.html('<li class="layui-menu-item-none">No data</li>');
|
||||
elemUl.html('<li class="layui-menu-item-none">暂无数据</li>');
|
||||
}
|
||||
return elemUl;
|
||||
};
|
||||
@ -163,9 +181,10 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
// 遍历菜单项
|
||||
var eachItemView = function(views, data){
|
||||
// var views = [];
|
||||
|
||||
layui.each(data, function(index, item){
|
||||
// 是否存在子级
|
||||
var isChild = item.child && item.child.length > 0;
|
||||
var isChild = item[customName.children] && item[customName.children].length > 0;
|
||||
var isSpreadItem = ('isSpreadItem' in item) ? item.isSpreadItem : options.isSpreadItem
|
||||
var title = function(title){
|
||||
var templet = item.templet || options.templet;
|
||||
@ -175,7 +194,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
: laytpl(templet).render(item);
|
||||
}
|
||||
return title;
|
||||
}(item.title);
|
||||
}(util.escape(item[customName.title]));
|
||||
|
||||
// 初始类型
|
||||
var type = function(){
|
||||
@ -192,7 +211,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
return '';
|
||||
}();
|
||||
|
||||
if(type !== '-' && (!item.title && !item.id && !isChild)) return;
|
||||
if(type !== '-' && (!item[customName.title] && !item[customName.id] && !isChild)) return;
|
||||
|
||||
//列表元素
|
||||
var viewLi = $(['<li'+ function(){
|
||||
@ -239,14 +258,14 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
|
||||
//子级区
|
||||
if(isChild){
|
||||
var elemPanel = $('<div class="layui-panel layui-menu-body-panel"></div>')
|
||||
,elemUl = $('<ul></ul>');
|
||||
var elemPanel = $('<div class="layui-panel layui-menu-body-panel"></div>');
|
||||
var elemUl = $('<ul></ul>');
|
||||
|
||||
if(type === 'parent'){
|
||||
elemPanel.append(eachItemView(elemUl, item.child));
|
||||
elemPanel.append(eachItemView(elemUl, item[customName.children]));
|
||||
viewLi.append(elemPanel);
|
||||
} else {
|
||||
viewLi.append(eachItemView(elemUl, item.child));
|
||||
viewLi.append(eachItemView(elemUl, item[customName.children]));
|
||||
}
|
||||
}
|
||||
|
||||
@ -256,7 +275,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
};
|
||||
|
||||
// 主模板
|
||||
var TPL_MAIN = ['<div class="layui-dropdown layui-border-box layui-panel layui-anim layui-anim-downbit" lay-id="' + options.id + '">'
|
||||
var TPL_MAIN = ['<div class="layui-dropdown layui-border-box layui-panel layui-anim layui-anim-downbit" ' + MOD_ID + '="' + options.id + '">'
|
||||
,'</div>'].join('');
|
||||
|
||||
// 如果是右键事件,则每次触发事件时,将允许重新渲染
|
||||
@ -266,7 +285,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
if(!rerender && options.elem.data(MOD_INDEX +'_opened')) return;
|
||||
|
||||
// 记录模板对象
|
||||
that.elemView = $('.' + STR_ELEM + '[lay-id="' + options.id + '"]');
|
||||
that.elemView = $('.' + STR_ELEM + '[' + MOD_ID + '="' + options.id + '"]');
|
||||
if (type === 'reloadData' && that.elemView.length) {
|
||||
that.elemView.html(options.content || getDefaultView());
|
||||
} else {
|
||||
@ -313,7 +332,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
that.elemView.find('.layui-menu li').on('click', function(e){
|
||||
var othis = $(this);
|
||||
var data = othis.data('item') || {};
|
||||
var isChild = data.child && data.child.length > 0;
|
||||
var isChild = data[customName.children] && data[customName.children].length > 0;
|
||||
var isClickAllScope = options.clickScope === 'all'; // 是否所有父子菜单均触发点击事件
|
||||
|
||||
if(data.disabled) return; // 菜单项禁用状态
|
||||
@ -331,12 +350,12 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
|
||||
// 触发菜单组展开收缩
|
||||
that.elemView.find(STR_GROUP_TITLE).on('click', function(e){
|
||||
var othis = $(this)
|
||||
,elemGroup = othis.parent()
|
||||
,data = elemGroup.data('item') || {}
|
||||
var othis = $(this);
|
||||
var elemGroup = othis.parent();
|
||||
var data = elemGroup.data('item') || {};
|
||||
|
||||
if(data.type === 'group' && options.isAllowSpread){
|
||||
thisModule.spread(elemGroup);
|
||||
thisModule.spread(elemGroup, options.accordion);
|
||||
}
|
||||
});
|
||||
|
||||
@ -364,17 +383,33 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
Class.prototype.remove = function(){
|
||||
var that = this;
|
||||
var options = that.config;
|
||||
var elemPrev = thisModule.prevElem;
|
||||
var prevContentElem = thisModule.prevElem;
|
||||
|
||||
// 若存在已打开的面板元素,则移除
|
||||
if(elemPrev){
|
||||
elemPrev.data('prevElem') && (
|
||||
elemPrev.data('prevElem').data(MOD_INDEX +'_opened', false)
|
||||
);
|
||||
elemPrev.remove();
|
||||
if(prevContentElem){
|
||||
var prevId = prevContentElem.attr(MOD_ID);
|
||||
var prevTriggerElem = prevContentElem.data('prevElem');
|
||||
var prevInstance = thisModule.getThis(prevId);
|
||||
var prevOnClose = prevInstance.config.close;
|
||||
|
||||
prevTriggerElem && prevTriggerElem.data(MOD_INDEX +'_opened', false);
|
||||
prevContentElem.remove();
|
||||
delete thisModule.prevElem;
|
||||
typeof prevOnClose === 'function' && prevOnClose.call(prevInstance.config, prevTriggerElem);
|
||||
}
|
||||
lay('.' + STR_ELEM_SHADE).remove();
|
||||
};
|
||||
|
||||
Class.prototype.normalizedDelay = function(){
|
||||
var that = this;
|
||||
var options = that.config;
|
||||
var delay = [].concat(options.delay);
|
||||
|
||||
return {
|
||||
show: delay[0],
|
||||
hide: delay[1] !== undefined ? delay[1] : delay[0]
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟删除视图
|
||||
Class.prototype.delayRemove = function(){
|
||||
@ -384,7 +419,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
|
||||
thisModule.timer = setTimeout(function(){
|
||||
that.remove();
|
||||
}, options.delay);
|
||||
}, that.normalizedDelay().hide);
|
||||
};
|
||||
|
||||
// 事件
|
||||
@ -397,13 +432,23 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
|
||||
// 解除上一个事件
|
||||
if(that.prevElem) that.prevElem.off(options.trigger, that.prevElemCallback);
|
||||
|
||||
// 是否鼠标移入时触发
|
||||
var isMouseEnter = options.trigger === 'mouseenter';
|
||||
|
||||
// 记录被绑定的元素及回调
|
||||
that.prevElem = options.elem;
|
||||
that.prevElemCallback = function(e){
|
||||
clearTimeout(thisModule.timer);
|
||||
that.e = e;
|
||||
that.render();
|
||||
|
||||
// 若为鼠标移入事件,则延迟触发
|
||||
isMouseEnter ? (
|
||||
thisModule.timer = setTimeout(function(){
|
||||
that.render();
|
||||
}, that.normalizedDelay().show)
|
||||
) : that.render();
|
||||
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
@ -411,8 +456,8 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
options.elem.on(options.trigger, that.prevElemCallback);
|
||||
|
||||
// 如果是鼠标移入事件
|
||||
if(options.trigger === 'mouseenter'){
|
||||
// 直行鼠标移出事件
|
||||
if (isMouseEnter) {
|
||||
// 执行鼠标移出事件
|
||||
options.elem.on('mouseleave', function(){
|
||||
that.delayRemove();
|
||||
});
|
||||
@ -430,16 +475,33 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
};
|
||||
|
||||
// 设置菜单组展开和收缩状态
|
||||
thisModule.spread = function(othis){
|
||||
// 菜单组展开和收缩
|
||||
thisModule.spread = function(othis, isAccordion){
|
||||
var contentElem = othis.children('ul');
|
||||
var needSpread = othis.hasClass(STR_ITEM_UP);
|
||||
var elemIcon = othis.children('.'+ STR_MENU_TITLE).find('.layui-icon-' + (needSpread ? 'down' : 'up'));
|
||||
if(needSpread){
|
||||
var ANIM_MS = 200;
|
||||
|
||||
// 动画执行完成后的操作
|
||||
var complete = function() {
|
||||
$(this).css({'display': ''}); // 剔除临时 style,以适配外部样式的状态重置;
|
||||
};
|
||||
|
||||
// 动画是否正在执行
|
||||
if (contentElem.is(':animated')) return;
|
||||
|
||||
// 展开
|
||||
if (needSpread) {
|
||||
othis.removeClass(STR_ITEM_UP).addClass(STR_ITEM_DOWN);
|
||||
elemIcon.removeClass('layui-icon-down').addClass('layui-icon-up');
|
||||
} else {
|
||||
contentElem.hide().stop().slideDown(ANIM_MS, complete);
|
||||
} else { // 收缩
|
||||
contentElem.stop().slideUp(ANIM_MS, complete);
|
||||
othis.removeClass(STR_ITEM_DOWN).addClass(STR_ITEM_UP);
|
||||
elemIcon.removeClass('layui-icon-up').addClass('layui-icon-down');
|
||||
}
|
||||
|
||||
// 手风琴
|
||||
if (needSpread && isAccordion) {
|
||||
var groupSibs = othis.siblings('.' + STR_ITEM_DOWN);
|
||||
groupSibs.children('ul').stop().slideUp(ANIM_MS, complete);
|
||||
groupSibs.removeClass(STR_ITEM_DOWN).addClass(STR_ITEM_UP);
|
||||
}
|
||||
};
|
||||
|
||||
@ -454,7 +516,7 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
var that = thisModule.getThis(dropdown.thisId);
|
||||
if(!that) return;
|
||||
|
||||
if(!that.elemView[0] || !$('.'+ STR_ELEM)[0]){
|
||||
if((that.elemView && !that.elemView[0]) || !$('.'+ STR_ELEM)[0]){
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -523,9 +585,10 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
var othis = $(this);
|
||||
var elemGroup = othis.parents('.'+ STR_ITEM_GROUP +':eq(0)');
|
||||
var options = lay.options(elemGroup[0]);
|
||||
var isAccordion = typeof othis.parents('.layui-menu').eq(0).attr('lay-accordion') === 'string';
|
||||
|
||||
if(('isAllowSpread' in options) ? options.isAllowSpread : true){
|
||||
thisModule.spread(elemGroup);
|
||||
thisModule.spread(elemGroup, isAccordion);
|
||||
}
|
||||
});
|
||||
|
||||
@ -570,6 +633,15 @@ layui.define(['jquery', 'laytpl', 'lay'], function(exports){
|
||||
that.remove();
|
||||
return thisModule.call(that);
|
||||
};
|
||||
|
||||
// 打开面板
|
||||
dropdown.open = function(id){
|
||||
var that = thisModule.getThis(id);
|
||||
if(!that) return this;
|
||||
|
||||
that.render();
|
||||
return thisModule.call(that);
|
||||
}
|
||||
|
||||
// 重载实例
|
||||
dropdown.reload = function(id, options, type){
|
||||
|
Reference in New Issue
Block a user