This repository has been archived on 2023-08-30. You can view files and clone it, but cannot push or open issues/pull-requests.
bytedo
/
wcui
Archived
1
0
Fork 0
wcui/js/lib/tree/main.js

128 lines
3.6 KiB
JavaScript
Raw Normal View History

/**
*
* @authors yutent (yutent@doui.cc)
* @date 2017-04-14 21:04:50
*
*/
"use strict";
2017-09-08 22:23:12 +08:00
define(['css!./main.css'], function(){
//储存版本信息
2017-09-08 22:23:12 +08:00
yua.ui.tree = '1.0.0'
2017-07-11 17:40:45 +08:00
var box = '<ul>{li}</ul>',
ul = '<ul :class="{open: {it}.open}">{li}</ul>',
li = '<li :class="{open: {it}.open, dir: {it}.children}">'
2017-07-11 17:40:45 +08:00
+ '<em :click="$toggle({it})"></em>'
+ '<span :click="$select({it})" :class="{active: {it}.id === currItem}" :text="{it}.name"></span>'
+ '{child}</li>';
2017-09-04 01:02:25 +08:00
var keyPath = {};
function repeat(arr, name){
var html = ''
arr.forEach(function(it, i){
var from = name + '[' + i + ']',
child = '';
html += li.replace(/\{it\}/g, from);
if(it.children){
child += repeat(it.children, from +'.children')
child = ul.replace('{li}', child).replace('{it}', from)
}
html = html.replace(/\{child\}/, child)
})
return html
}
2017-07-11 17:40:45 +08:00
function format(arr){
var tmp = {}, farr = []
arr.sort(function(a, b){
return (a.pid === b.pid) ? (a.sort - b.sort) : (a.pid - b.pid)
})
arr.forEach(function(it){
tmp[it.id] = it
2017-09-04 01:02:25 +08:00
keyPath[it.id] = ''
2017-07-11 17:40:45 +08:00
var parentItem = tmp[it.pid]
2017-07-11 17:40:45 +08:00
if(!parentItem){
return farr.push(tmp[it.id])
}
2017-09-04 01:02:25 +08:00
keyPath[it.id] += keyPath[parentItem.id] + parentItem.id + ','
2017-07-11 17:40:45 +08:00
parentItem.open = !!parentItem.open
parentItem.children = parentItem.children || []
parentItem.children.push(it)
})
return farr
}
2017-07-11 17:40:45 +08:00
return yua.component('tree', {
$template: '<div class="do-tree" :class="{{$skin}}" :html="treeHTML"></div>',
$init: function(vm){
2017-07-11 17:40:45 +08:00
vm.$select = function(obj){
vm.currItem = obj.id
if(vm.$onClick){
vm.$onClick(obj)
}
}
2017-09-04 01:02:25 +08:00
vm.$reset = function(arr){
2017-07-11 17:40:45 +08:00
vm.treeArr.clear()
2017-09-04 01:02:25 +08:00
vm.treeHTML = ''
2017-07-11 17:40:45 +08:00
vm.treeArr.pushArray(format(arr))
vm.currItem = -1
var tpl = repeat(vm.treeArr.$model, 'treeArr')
2017-09-04 01:02:25 +08:00
yua.nextTick(function(){
vm.treeHTML = box.replace('{li}', tpl)
})
}
vm.$update = function(id, obj){
var path = keyPath[id],
tmpid = null,
tmpobj = null
path += id
path = path.split(',')
while(tmpid = +path.shift()){
if(!tmpobj){
tmpobj = vm.treeArr
}else{
tmpobj = tmpobj.children
}
for(var i = 0, it; it = tmpobj[i++];){
if(it.id === tmpid){
tmpobj = it
break
}
}
}
for(var j in obj){
tmpobj[j] = obj[j]
}
2017-07-11 17:40:45 +08:00
}
},
$ready: function(vm){
vm.$onSuccess(vm)
},
2017-07-11 17:40:45 +08:00
$skin: 'skin-def',
treeHTML: '',
currItem: -1,
treeArr: [],
$select: yua.noop,
$update: yua.noop,
2017-09-04 01:02:25 +08:00
$reset: yua.noop,
2017-07-11 17:40:45 +08:00
$onSuccess: yua.noop,
$onClick: yua.noop,
$toggle: function(obj){
obj.open = !obj.open
}
})
})