feat(tree): 升级了tree

This commit is contained in:
smallwei
2018-07-15 21:53:25 +08:00
43 changed files with 673 additions and 399 deletions

View File

@@ -46,11 +46,11 @@ a cite{font-style: normal; *cursor:pointer;}
/** 图标字体 **/
@font-face {font-family: 'layui-icon';
src: url('../font/iconfont.eot?v=230-rc1');
src: url('../font/iconfont.eot?v=230-rc1#iefix') format('embedded-opentype'),
url('../font/iconfont.svg?v=230-rc1#iconfont') format('svg'),
url('../font/iconfont.woff?v=230-rc1') format('woff'),
url('../font/iconfont.ttf?v=230-rc1') format('truetype');
src: url('../font/iconfont.eot?v=230');
src: url('../font/iconfont.eot?v=230#iefix') format('embedded-opentype'),
url('../font/iconfont.svg?v=230#iconfont') format('svg'),
url('../font/iconfont.woff?v=230') format('woff'),
url('../font/iconfont.ttf?v=230') format('truetype');
}
.layui-icon{
@@ -205,11 +205,12 @@ a cite{font-style: normal; *cursor:pointer;}
.layui-icon-template-1:before{content:"\e656"}
.layui-icon-vercode:before{content:"\e679"}
.layui-icon-cellphone:before{content:"\e678"}
.layui-icon-screen-full:before{content:"\e622";}
.layui-icon-screen-restore:before{content:"\e758";}
.layui-icon-checkbox:before{content:"\e624"}
.layui-icon-uncheckbox:before{content:"\e68c"}
/* 基本布局 */
.layui-main{position: relative; width: 1140px; margin: 0 auto;}
.layui-header{position: relative; z-index: 1000; height: 60px;}
@@ -899,7 +900,7 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh
/** 树组件(重写中) **/
.layui-tree{line-height: 26px;}
.layui-tree li{text-overflow: ellipsis; overflow:hidden; white-space: nowrap;}
.layui-tree li{position:relative;text-overflow: ellipsis; overflow:hidden; white-space: nowrap;}
.layui-tree li a,
.layui-tree li .layui-tree-spread{display: inline-block; vertical-align: top; height: 26px; *display: inline; *zoom:1; cursor: pointer;}
.layui-tree li a{font-size: 0;}
@@ -908,11 +909,12 @@ body .layui-table-tips .layui-layer-content{background: none; padding: 0; box-sh
.layui-tree li i{padding-left: 6px; color: #333; -moz-user-select: none;}
.layui-tree li .layui-tree-check{font-size: 13px;}
.layui-tree li .layui-tree-check:hover{color: #009E94;}
.layui-tree li ul{display: none; margin-left: 20px;}
.layui-tree li ul{display: none; margin-left: 40px;}
.layui-tree li .layui-tree-enter{line-height: 24px; border: 1px dotted #000;}
.layui-tree-drag{display: none; position: absolute; left: -666px; top: -666px; background-color: #f2f2f2; padding: 5px 10px; border: 1px dotted #000; white-space: nowrap}
.layui-tree-drag i{padding-right: 5px;}
.layui-tree-menu{position: absolute;top:0;right: 0;z-index:1024;}
.layui-tree-menu span{margin-left: 10px;}
/** 导航菜单 **/
.layui-nav{position: relative; padding: 0 20px; background-color: #393D49; color: #fff; border-radius: 2px; font-size: 0; box-sizing: border-box;}
.layui-nav *{font-size: 14px;}

View File

@@ -144,6 +144,7 @@ layui.define('layer', function(exports){
index = select[0].selectedIndex; //获取最新的 selectedIndex
reElem.addClass(CLASS+'ed');
dds.removeClass(HIDE);
nearElem = null;
//初始选中样式
dds.eq(index).addClass(THIS).siblings().removeClass(THIS);
@@ -231,6 +232,22 @@ layui.define('layer', function(exports){
//标注样式
nearDd.addClass(THIS).siblings().removeClass(THIS);
//定位滚动条
var ddThis = dl.children('dd.layui-this')
,posTop = ddThis.position().top
,dlHeight = dl.height()
,ddHeight = ddThis.height();
//若选中元素在滚动条不可见底部
if(posTop > dlHeight){
dl.scrollTop(posTop + dl.scrollTop() - dlHeight + ddHeight - 5);
}
//若选择玄素在滚动条不可见顶部
if(posTop < 0){
dl.scrollTop(posTop + dl.scrollTop());
}
};

View File

@@ -377,7 +377,7 @@
,range: false //是否开启范围选择,即双控件
,format: 'yyyy-MM-dd' //默认日期格式
,value: null //默认日期支持传入new Date()或者符合format参数设定的日期格式字符
,isInitValue: false //用于控制是否自动向元素填充初始值(需配合 value 参数使用)
,isInitValue: true //用于控制是否自动向元素填充初始值(需配合 value 参数使用)
,min: '1900-1-1' //有效最小日期,年月日必须用“-”分割,时分秒必须用“:”分割。注意:它并不是遵循 format 设定的格式。
,max: '2099-12-31' //有效最大日期,同上
,trigger: 'focus' //呼出控件的事件

View File

@@ -1,215 +1,348 @@
/**
@Namelayui.tree 树组件
@Author贤心
@Namelayui.tree2.0 树组件
@Authorsmallwei
@LicenseMIT
*/
layui.define('jquery', function(exports){
"use strict";
var $ = layui.$
,hint = layui.hint();
var enterSkin = 'layui-tree-enter', Tree = function(options){
this.options = options;
};
//图标
var icon = {
arrow: ['&#xe623;', '&#xe625;'] //箭头
,checkbox: ['&#xe626;', '&#xe627;'] //复选框
,radio: ['&#xe62b;', '&#xe62a;'] //选框
,branch: ['&#xe622;', '&#xe624;'] //父节点
,leaf: '&#xe621;' //叶节点
};
//初始化
Tree.prototype.init = function(elem){
var that = this;
elem.addClass('layui-box layui-tree'); //添加tree样式
if(that.options.skin){
elem.addClass('layui-tree-skin-'+ that.options.skin);
}
that.tree(elem);
that.on(elem);
};
//树节点解析
Tree.prototype.tree = function(elem, children){
var that = this, options = that.options
var nodes = children || options.nodes;
layui.each(nodes, function(index, item){
var hasChild = item.children && item.children.length > 0;
var ul = $('<ul class="'+ (item.spread ? "layui-show" : "") +'"></ul>');
var li = $(['<li '+ (item.spread ? 'data-spread="'+ item.spread +'"' : '') +'>'
//展开箭头
,function(){
return hasChild ? '<i class="layui-icon layui-tree-spread">'+ (
item.spread ? icon.arrow[1] : icon.arrow[0]
) +'</i>' : '';
}()
//复选框/单选框
,function(){
return options.check ? (
'<i class="layui-icon layui-tree-check">'+ (
options.check === 'checkbox' ? icon.checkbox[0] : (
options.check === 'radio' ? icon.radio[0] : ''
)
) +'</i>'
) : '';
}()
//节点
,function(){
return '<a href="'+ (item.href || 'javascript:;') +'" '+ (
options.target && item.href ? 'target=\"'+ options.target +'\"' : ''
) +'>'
+ ('<i class="layui-icon layui-tree-'+ (hasChild ? "branch" : "leaf") +'">'+ (
hasChild ? (
item.spread ? icon.branch[1] : icon.branch[0]
) : icon.leaf
) +'</i>') //节点图标
+ ('<cite>'+ (item.name||'未命名') +'</cite></a>');
}()
,'</li>'].join(''));
//如果有子节点,则递归继续生成树
if(hasChild){
li.append(ul);
that.tree(ul, item.children);
}
elem.append(li);
//触发点击节点回调
typeof options.click === 'function' && that.click(li, item);
//伸展节点
that.spread(li, item);
//拖拽节点
options.drag && that.drag(li, item);
});
};
//点击节点回调
Tree.prototype.click = function(elem, item){
var that = this, options = that.options;
elem.children('a').on('click', function(e){
layui.stope(e);
options.click(item)
});
};
//伸展节点
Tree.prototype.spread = function(elem, item){
var that = this, options = that.options;
var arrow = elem.children('.layui-tree-spread')
var ul = elem.children('ul'), a = elem.children('a');
//执行伸展
var open = function(){
if(elem.data('spread')){
elem.data('spread', null)
ul.removeClass('layui-show');
arrow.html(icon.arrow[0]);
a.find('.layui-icon').html(icon.branch[0]);
} else {
elem.data('spread', true);
ul.addClass('layui-show');
arrow.html(icon.arrow[1]);
a.find('.layui-icon').html(icon.branch[1]);
}
};
//如果没有子节点,则不执行
if(!ul[0]) return;
arrow.on('click', open);
a.on('dblclick', open);
}
//通用事件
Tree.prototype.on = function(elem){
var that = this, options = that.options;
var dragStr = 'layui-tree-drag';
//屏蔽选中文字
elem.find('i').on('selectstart', function(e){
return false
});
//拖拽
if(options.drag){
$(document).on('mousemove', function(e){
var move = that.move;
if(move.from){
var to = move.to, treeMove = $('<div class="layui-box '+ dragStr +'"></div>');
e.preventDefault();
$('.' + dragStr)[0] || $('body').append(treeMove);
var dragElem = $('.' + dragStr)[0] ? $('.' + dragStr) : treeMove;
(dragElem).addClass('layui-show').html(move.from.elem.children('a').html());
dragElem.css({
left: e.pageX + 10
,top: e.pageY + 10
})
}
}).on('mouseup', function(){
var move = that.move;
if(move.from){
move.from.elem.children('a').removeClass(enterSkin);
move.to && move.to.elem.children('a').removeClass(enterSkin);
that.move = {};
$('.' + dragStr).remove();
}
});
}
};
//拖拽节点
Tree.prototype.move = {};
Tree.prototype.drag = function(elem, item){
var that = this, options = that.options;
var a = elem.children('a'), mouseenter = function(){
var othis = $(this), move = that.move;
if(move.from){
move.to = {
item: item
,elem: elem
};
othis.addClass(enterSkin);
}
};
a.on('mousedown', function(){
var move = that.move
move.from = {
item: item
,elem: elem
};
});
a.on('mouseenter', mouseenter).on('mousemove', mouseenter)
.on('mouseleave', function(){
var othis = $(this), move = that.move;
if(move.from){
delete move.to;
othis.removeClass(enterSkin);
}
});
};
//暴露接口
exports('tree', function(options){
var tree = new Tree(options = options || {});
var elem = $(options.elem);
if(!elem[0]){
return hint.error('layui.tree 没有找到'+ options.elem +'元素');
}
tree.init(elem);
});
});
layui.define('jquery', function(exports) {
"use strict";
var $ = layui.$,
hint = layui.hint();
var enterSkin = 'layui-tree-enter',
Tree = function(options) {
this.options = options;
};
//勾选集合
var changeList = [];
//图标
var icon = {
arrow: ['&#xe623;', '&#xe625;'] //箭头
,
checkbox: ['&#xe68c;', '&#xe624;'] //选框
,
leaf: '&#xe621;' //叶节点
};
//初始化
Tree.prototype.init = function(elem) {
var that = this;
elem.addClass('layui-box layui-tree'); //添加tree样式
if(that.options.skin) {
elem.addClass('layui-tree-skin-' + that.options.skin);
}
that.tree(elem);
that.on(elem);
};
//树节点解析
Tree.prototype.tree = function(elem, children) {
var that = this,
options = that.options
var nodes = children || options.nodes;
layui.each(nodes, function(index, item) {
var hasChild = item.children && item.children.length > 0;
var ul = $('<ul class="' + (item.spread ? "layui-show" : "") + '"></ul>');
var li = that.getNode(item, hasChild);
//如果有子节点,则递归继续生成树
if(hasChild) {
li.append(ul);
that.tree(ul, item.children);
}
//伸展节点
that.spread(li, item);
that.bindUlEvent(li, item);
elem.append(li);
});
};
Tree.prototype.getDom = function() {
var that = this,
options = that.options
return {
spread: function(item, hasChild) {
return hasChild ? '<i class="layui-icon layui-tree-spread">' + (
item.spread ? icon.arrow[1] : icon.arrow[0]
) + '</i>' : '';
},
checkbox: function(item) {
return options.check ? (
'<i class="layui-icon layui-tree-check">' + (
options.check === 'checkbox' ? icon.checkbox[0] : (
options.check === 'radio' ? icon.radio[0] : ''
)
) + '</i>'
) : '';
},
node: function(item) {
return '<a href="' + (item.href || 'javascript:;') + '" ' + (
options.target && item.href ? 'target=\"' + options.target + '\"' : ''
) + '>' +
('<cite>' + (item.name || '未命名') + '</cite></a>')
},
menu: function(item) {
return '<div class="layui-tree-menu">' +
'<span class="layui-tree-add">Add</span>' +
'<span class="layui-tree-delete">Delete</span>' +
'</div>'
}
}
}
//获取树节点
Tree.prototype.getNode = function(item, hasChild) {
var that = this,
options = that.options
var dom = that.getDom();
var li = $(['<li ' + (item.spread ? 'data-spread="' + item.spread + '"' : '') + '>'
//展开箭头
,
dom.spread(item, hasChild)
//复选框/单选框
,
dom.checkbox(item)
//节点
,
dom.node(item)
//菜单
,
dom.menu()
,
'</li>'
].join(''));
return li;
}
//父绑定事件
Tree.prototype.bindUlEvent = function(li, item) {
var that = this,
options = that.options
//触发点击节点回调
typeof options.click === 'function' && that.click(li, item);
//节点选择
typeof options.change === 'function' && options.check === 'checkbox' && that.checkbox(li, item);
//新增方法
typeof options.addClick === 'function' && that.add(li, item);
//删除方法
typeof options.deleteClick === 'function' && that.delete(li, item);
//拖拽节点
options.drag && that.drag(li, item);
}
//选中回调函数
Tree.prototype.change = function() {
var that = this,
options = that.options;
options.change(changeList);
},
//新增方法回调
Tree.prototype.add = function(elem, item) {
var that = this,
options = that.options;
var addBtn = elem.children('.layui-tree-menu').children('.layui-tree-add')
var arrow = elem.children('.layui-tree-spread')
var ul = elem.children('ul'),
a = elem.children('a');
var addEvent = function(e) {
layui.stope(e);
var _addEvent = {
add: function(itemAddObj) {
if(!ul[0]) {
ul = $('<ul class="layui-show"></ul>');
elem.append(ul);
}
if(!arrow[0]) {
arrow = $('<i class="layui-icon layui-tree-spread">' + icon.arrow[1] + '</i>');
elem.prepend(arrow);
that.spread(elem, item);
}
if(!elem.data('spread')) {
that.open(elem, ul, arrow)
}
var li = that.getNode(itemAddObj, false);
that.bindUlEvent(li, itemAddObj);
ul.append(li);
}
}
options.addClick(item, elem, _addEvent.add)
}
addBtn.on('click', addEvent);
}
//删除方法回调
Tree.prototype.delete = function(elem, item) {
var that = this,
options = that.options;
var deleteBtn = elem.children('.layui-tree-menu').children('.layui-tree-delete')
var ul = elem.children('ul'),
a = elem.children('a');
var deleteEvent = function(e) {
layui.stope(e);
var _deleteEvent = {
done: function() {
elem.html('');
}
}
options.deleteClick(item, elem, _deleteEvent.done)
}
deleteBtn.on('click', deleteEvent);
}
//点击节点回调
Tree.prototype.click = function(elem, item) {
var that = this,
options = that.options;
elem.children('a').on('click', function(e) {
layui.stope(e);
options.click(item)
});
};
//节点选择
Tree.prototype.checkbox = function(elem, item) {
var that = this,
options = that.options;
var checkbox = elem.children('.layui-tree-check')
var ul = elem.children('ul'),
a = elem.children('a');
var check = function() {
var index = layui.findObj(changeList, item);
if(elem.data('check')) {
elem.data('check', null)
checkbox.html(icon.checkbox[0]);
} else {
elem.data('check', true);
checkbox.html(icon.checkbox[1]);
}
if(index === -1) {
changeList.push(item);
} else {
changeList.splice(index, 1);
}
that.change();
}
checkbox.on('click', check);
};
//伸展节点
Tree.prototype.spread = function(elem, item) {
var that = this,
options = that.options;
var arrow = elem.children('.layui-tree-spread')
var ul = elem.children('ul'),
a = elem.children('a');
//如果没有子节点,则不执行
if(!ul[0]) return;
arrow.on('click', function() {
that.open(elem, ul, arrow)
});
}
//打开节点
Tree.prototype.open = function(elem, ul, arrow) {
if(elem.data('spread')) {
elem.data('spread', null)
ul.removeClass('layui-show');
arrow.html(icon.arrow[0]);
} else {
elem.data('spread', true);
ul.addClass('layui-show');
arrow.html(icon.arrow[1]);
}
};
//通用事件
Tree.prototype.on = function(elem) {
var that = this,
options = that.options;
var dragStr = 'layui-tree-drag';
//屏蔽选中文字
elem.find('i').on('selectstart', function(e) {
return false
});
//拖拽
if(options.drag) {
$(document).on('mousemove', function(e) {
var move = that.move;
if(move.from) {
var to = move.to,
treeMove = $('<div class="layui-box ' + dragStr + '"></div>');
e.preventDefault();
$('.' + dragStr)[0] || $('body').append(treeMove);
var dragElem = $('.' + dragStr)[0] ? $('.' + dragStr) : treeMove;
(dragElem).addClass('layui-show').html(move.from.elem.children('a').html());
dragElem.css({
left: e.pageX + 10,
top: e.pageY + 10
})
}
}).on('mouseup', function() {
var move = that.move;
if(move.from) {
move.from.elem.children('a').removeClass(enterSkin);
move.to && move.to.elem.children('a').removeClass(enterSkin);
that.move = {};
$('.' + dragStr).remove();
}
});
}
};
//拖拽节点
Tree.prototype.move = {};
Tree.prototype.drag = function(elem, item) {
var that = this,
options = that.options;
var a = elem.children('a'),
mouseenter = function() {
var othis = $(this),
move = that.move;
if(move.from) {
move.to = {
item: item,
elem: elem
};
othis.addClass(enterSkin);
}
};
a.on('mousedown', function() {
var move = that.move
move.from = {
item: item,
elem: elem
};
});
a.on('mouseenter', mouseenter).on('mousemove', mouseenter)
.on('mouseleave', function() {
var othis = $(this),
move = that.move;
if(move.from) {
delete move.to;
othis.removeClass(enterSkin);
}
});
};
//暴露接口
exports('tree', function(options) {
var tree = new Tree(options = options || {});
var elem = $(options.elem);
if(!elem[0]) {
return hint.error('layui.tree 没有找到' + options.elem + '元素');
}
tree.init(elem);
});
});

View File

@@ -127,11 +127,11 @@ layui.define('layer' , function(exports){
$('#'+ ELEM_IFRAME)[0] || $('body').append(iframe);
//包裹文件域
if(!options.elem.next().hasClass(ELEM_IFRAME)){
if(!options.elem.next().hasClass(ELEM_FORM)){
that.elemFile.wrap(elemForm);
//追加额外的参数
options.elem.next('.'+ ELEM_IFRAME).append(function(){
options.elem.next('.'+ ELEM_FORM).append(function(){
var arr = [];
layui.each(options.data, function(key, value){
value = typeof value === 'function' ? value() : value;
@@ -288,14 +288,17 @@ layui.define('layer' , function(exports){
//回调返回的参数
,args = {
//预览
preview: function(callback){
that.preview(callback);
}
//上传
,upload: function(index, file){
var thisFile = {};
thisFile[index] = file;
that.upload(thisFile);
}
//追加文件到队列
,pushFile: function(){
that.files = that.files || {};
layui.each(that.chooseFiles, function(index, item){
@@ -303,12 +306,22 @@ layui.define('layer' , function(exports){
});
return that.files;
}
//重置文件
,resetFile: function(index, file, filename){
var newFile = new File([file], filename);
that.files = that.files || {};
that.files[index] = newFile;
}
}
//提交上传
,send = function(){
if(type === 'choose'){
return options.choose && options.choose(args);
,send = function(){
//选择文件的回调
if(type === 'choose' || options.auto){
options.choose && options.choose(args);
if(type === 'choose'){
return;
}
}
//上传前的回调

View File

@@ -164,6 +164,13 @@ layui.define('jquery', function(exports){
.replace(/mm/g, hms[1])
.replace(/ss/g, hms[2]);
}
//防 xss 攻击
,escape: function(html){
return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
.replace(/</g, '&lt;').replace(/>/g, '&gt;')
.replace(/'/g, '&#39;').replace(/"/g, '&quot;');
}
};
exports('util', util);

View File

@@ -19,7 +19,7 @@
}
,Layui = function(){
this.v = '2.3.0-rc1'; //版本号
this.v = '2.3.0'; //版本号
}
//获取layui所在目录
@@ -430,7 +430,20 @@
error: error
}
};
//判断对象是否相等
Layui.prototype.isEqualObj = function(obj1,obj2){
return JSON.stringify(obj1)===JSON.stringify(obj2)
}
//寻找对象是否存在数组中
Layui.prototype.findObj = function(list,obj) {
var that = this,
result = -1;
that.each(list, function(index, item) {
if(that.isEqualObj(obj,item))result = index;
})
return result;
}
//遍历
Layui.prototype.each = function(obj, fn){
var key