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

优化组件子父关系的定义;移除组件:if指令的支持;完成tree组件的移植

old
宇天 2018-07-21 22:40:04 +08:00
parent e5b4f7e739
commit 2aaa7ad832
6 changed files with 172 additions and 201 deletions

View File

@ -227,14 +227,11 @@ const _Anot = (function() {
} else { } else {
var _parent = $elem var _parent = $elem
while ((_parent = _parent.parentNode)) { while ((_parent = _parent.parentNode)) {
if (_parent.anotctrl) { if (_parent.__VM__) {
break break
} }
} }
scanTag( scanTag($elem.parentNode, _parent ? [_parent.__VM__] : [])
$elem.parentNode,
_parent ? [VMODELS[_parent.anotctrl]] : []
)
} }
} }
}) })
@ -3283,10 +3280,6 @@ const _Anot = (function() {
}) })
} }
function createSignalTower(elem, vmodel) {
elem.anotctrl = elem.anotctrl || vmodel.$id
}
function getBindingCallback(elem, name, vmodels) { function getBindingCallback(elem, name, vmodels) {
var callback = elem.getAttribute(name) var callback = elem.getAttribute(name)
if (callback) { if (callback) {
@ -3559,7 +3552,8 @@ const _Anot = (function() {
vm = [newVmodel] vm = [newVmodel]
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则 elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
createSignalTower(elem, newVmodel) // 挂载VM对象到相应的元素上
elem.__VM__ = newVmodel
hideProperty(newVmodel, '$elem', elem) hideProperty(newVmodel, '$elem', elem)
if (vmodels.length) { if (vmodels.length) {
@ -3827,19 +3821,12 @@ const _Anot = (function() {
var state = {} var state = {}
var props = getOptionsFromTag(elem, host.vmodels) var props = getOptionsFromTag(elem, host.vmodels)
var $id = props.uuid || generateID(widget) var $id = props.uuid || generateID(widget)
var __willpush__ = null
var slots = null var slots = null
if (elem.firstElementChild) { if (elem.firstElementChild) {
slots = parseSlot(elem.children) slots = parseSlot(elem.children)
} }
if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push')
__willpush__ = props.hostPush
props.hostPush = parseVmValue(parentVm, __willpush__)
}
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
var disabledKeyReverse = false var disabledKeyReverse = false
@ -3891,7 +3878,7 @@ const _Anot = (function() {
var childValueWatcher = function() { var childValueWatcher = function() {
var val = this.value var val = this.value
if (val && typeof val === 'object') { if (val && typeof val === 'object') {
val = val.$model || val val = val.$model
} }
parseVmValue(parentVm, valueKey, val) parseVmValue(parentVm, valueKey, val)
} }
@ -3978,19 +3965,21 @@ const _Anot = (function() {
var vmodel = Anot(hooks) var vmodel = Anot(hooks)
hideProperty(vmodel, '__WIDGET__', name) hideProperty(vmodel, '__WIDGET__', name)
delete vmodel.$mounted delete vmodel.$mounted
parentVm.$components.push(vmodel)
if (parentVm.__WIDGET__ === name) { // 对象组件的子父vm关系, 只存最顶层的$components对象中,
vmodel.$up = parentVm // 而子vm, 无论向下多少级, 他们的$up对象也只存最顶层的组件vm
var __pvm__ = parentVm
while (__pvm__.$up && __pvm__.$up.__WIDGET__ === name) {
__pvm__ = __pvm__.$up
} }
__pvm__.$components.push(vmodel)
if (__pvm__.__WIDGET__ === name) {
vmodel.$up = __pvm__
}
__pvm__ = undefined
elem.msResolved = 1 //防止二进扫描此元素 elem.msResolved = 1 //防止二进扫描此元素
if (__willpush__) {
hideProperty(vmodel, '$push', function(val) {
parseVmValue(parentVm, __willpush__, val)
})
}
componentWillMount.call(vmodel) componentWillMount.call(vmodel)
globalHooks.componentWillMount.call(null, vmodel) globalHooks.componentWillMount.call(null, vmodel)
@ -4051,6 +4040,7 @@ const _Anot = (function() {
} }
} }
}) })
elem.__VM__ = vmodel
scanTag(elem, [vmodel]) scanTag(elem, [vmodel])
Anot.vmodels[vmodel.$id] = vmodel Anot.vmodels[vmodel.$id] = vmodel
if (!elem.childNodes.length) { if (!elem.childNodes.length) {

View File

@ -242,14 +242,11 @@
} else { } else {
var _parent = $elem var _parent = $elem
while ((_parent = _parent.parentNode)) { while ((_parent = _parent.parentNode)) {
if (_parent.anotctrl) { if (_parent.__VM__) {
break break
} }
} }
scanTag( scanTag($elem.parentNode, _parent ? [_parent.__VM__] : [])
$elem.parentNode,
_parent ? [VMODELS[_parent.anotctrl]] : []
)
} }
} }
}) })
@ -3298,10 +3295,6 @@
}) })
} }
function createSignalTower(elem, vmodel) {
elem.anotctrl = elem.anotctrl || vmodel.$id
}
function getBindingCallback(elem, name, vmodels) { function getBindingCallback(elem, name, vmodels) {
var callback = elem.getAttribute(name) var callback = elem.getAttribute(name)
if (callback) { if (callback) {
@ -3574,7 +3567,8 @@
vm = [newVmodel] vm = [newVmodel]
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则 elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
createSignalTower(elem, newVmodel) // 挂载VM对象到相应的元素上
elem.__VM__ = newVmodel
hideProperty(newVmodel, '$elem', elem) hideProperty(newVmodel, '$elem', elem)
if (vmodels.length) { if (vmodels.length) {
@ -3842,19 +3836,12 @@
var state = {} var state = {}
var props = getOptionsFromTag(elem, host.vmodels) var props = getOptionsFromTag(elem, host.vmodels)
var $id = props.uuid || generateID(widget) var $id = props.uuid || generateID(widget)
var __willpush__ = null
var slots = null var slots = null
if (elem.firstElementChild) { if (elem.firstElementChild) {
slots = parseSlot(elem.children) slots = parseSlot(elem.children)
} }
if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push')
__willpush__ = props.hostPush
props.hostPush = parseVmValue(parentVm, __willpush__)
}
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
var disabledKeyReverse = false var disabledKeyReverse = false
@ -3906,7 +3893,7 @@
var childValueWatcher = function() { var childValueWatcher = function() {
var val = this.value var val = this.value
if (val && typeof val === 'object') { if (val && typeof val === 'object') {
val = val.$model || val val = val.$model
} }
parseVmValue(parentVm, valueKey, val) parseVmValue(parentVm, valueKey, val)
} }
@ -3993,19 +3980,21 @@
var vmodel = Anot(hooks) var vmodel = Anot(hooks)
hideProperty(vmodel, '__WIDGET__', name) hideProperty(vmodel, '__WIDGET__', name)
delete vmodel.$mounted delete vmodel.$mounted
parentVm.$components.push(vmodel)
if (parentVm.__WIDGET__ === name) { // 对象组件的子父vm关系, 只存最顶层的$components对象中,
vmodel.$up = parentVm // 而子vm, 无论向下多少级, 他们的$up对象也只存最顶层的组件vm
var __pvm__ = parentVm
while (__pvm__.$up && __pvm__.$up.__WIDGET__ === name) {
__pvm__ = __pvm__.$up
} }
__pvm__.$components.push(vmodel)
if (__pvm__.__WIDGET__ === name) {
vmodel.$up = __pvm__
}
__pvm__ = undefined
elem.msResolved = 1 //防止二进扫描此元素 elem.msResolved = 1 //防止二进扫描此元素
if (__willpush__) {
hideProperty(vmodel, '$push', function(val) {
parseVmValue(parentVm, __willpush__, val)
})
}
componentWillMount.call(vmodel) componentWillMount.call(vmodel)
globalHooks.componentWillMount.call(null, vmodel) globalHooks.componentWillMount.call(null, vmodel)
@ -4066,6 +4055,7 @@
} }
} }
}) })
elem.__VM__ = vmodel
scanTag(elem, [vmodel]) scanTag(elem, [vmodel])
Anot.vmodels[vmodel.$id] = vmodel Anot.vmodels[vmodel.$id] = vmodel
if (!elem.childNodes.length) { if (!elem.childNodes.length) {

View File

@ -227,14 +227,11 @@ const _Anot = (function() {
} else { } else {
var _parent = $elem var _parent = $elem
while ((_parent = _parent.parentNode)) { while ((_parent = _parent.parentNode)) {
if (_parent.anotctrl) { if (_parent.__VM__) {
break break
} }
} }
scanTag( scanTag($elem.parentNode, _parent ? [_parent.__VM__] : [])
$elem.parentNode,
_parent ? [VMODELS[_parent.anotctrl]] : []
)
} }
} }
}) })
@ -3283,10 +3280,6 @@ const _Anot = (function() {
}) })
} }
function createSignalTower(elem, vmodel) {
elem.anotctrl = elem.anotctrl || vmodel.$id
}
function getBindingCallback(elem, name, vmodels) { function getBindingCallback(elem, name, vmodels) {
var callback = elem.getAttribute(name) var callback = elem.getAttribute(name)
if (callback) { if (callback) {
@ -3559,7 +3552,8 @@ const _Anot = (function() {
vm = [newVmodel] vm = [newVmodel]
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则 elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
createSignalTower(elem, newVmodel) // 挂载VM对象到相应的元素上
elem.__VM__ = newVmodel
hideProperty(newVmodel, '$elem', elem) hideProperty(newVmodel, '$elem', elem)
if (vmodels.length) { if (vmodels.length) {
@ -3827,19 +3821,12 @@ const _Anot = (function() {
var state = {} var state = {}
var props = getOptionsFromTag(elem, host.vmodels) var props = getOptionsFromTag(elem, host.vmodels)
var $id = props.uuid || generateID(widget) var $id = props.uuid || generateID(widget)
var __willpush__ = null
var slots = null var slots = null
if (elem.firstElementChild) { if (elem.firstElementChild) {
slots = parseSlot(elem.children) slots = parseSlot(elem.children)
} }
if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push')
__willpush__ = props.hostPush
props.hostPush = parseVmValue(parentVm, __willpush__)
}
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
var disabledKeyReverse = false var disabledKeyReverse = false
@ -3891,7 +3878,7 @@ const _Anot = (function() {
var childValueWatcher = function() { var childValueWatcher = function() {
var val = this.value var val = this.value
if (val && typeof val === 'object') { if (val && typeof val === 'object') {
val = val.$model || val val = val.$model
} }
parseVmValue(parentVm, valueKey, val) parseVmValue(parentVm, valueKey, val)
} }
@ -3978,19 +3965,21 @@ const _Anot = (function() {
var vmodel = Anot(hooks) var vmodel = Anot(hooks)
hideProperty(vmodel, '__WIDGET__', name) hideProperty(vmodel, '__WIDGET__', name)
delete vmodel.$mounted delete vmodel.$mounted
parentVm.$components.push(vmodel)
if (parentVm.__WIDGET__ === name) { // 对象组件的子父vm关系, 只存最顶层的$components对象中,
vmodel.$up = parentVm // 而子vm, 无论向下多少级, 他们的$up对象也只存最顶层的组件vm
var __pvm__ = parentVm
while (__pvm__.$up && __pvm__.$up.__WIDGET__ === name) {
__pvm__ = __pvm__.$up
} }
__pvm__.$components.push(vmodel)
if (__pvm__.__WIDGET__ === name) {
vmodel.$up = __pvm__
}
__pvm__ = undefined
elem.msResolved = 1 //防止二进扫描此元素 elem.msResolved = 1 //防止二进扫描此元素
if (__willpush__) {
hideProperty(vmodel, '$push', function(val) {
parseVmValue(parentVm, __willpush__, val)
})
}
componentWillMount.call(vmodel) componentWillMount.call(vmodel)
globalHooks.componentWillMount.call(null, vmodel) globalHooks.componentWillMount.call(null, vmodel)
@ -4051,6 +4040,7 @@ const _Anot = (function() {
} }
} }
}) })
elem.__VM__ = vmodel
scanTag(elem, [vmodel]) scanTag(elem, [vmodel])
Anot.vmodels[vmodel.$id] = vmodel Anot.vmodels[vmodel.$id] = vmodel
if (!elem.childNodes.length) { if (!elem.childNodes.length) {

View File

@ -242,14 +242,11 @@
} else { } else {
var _parent = $elem var _parent = $elem
while ((_parent = _parent.parentNode)) { while ((_parent = _parent.parentNode)) {
if (_parent.anotctrl) { if (_parent.__VM__) {
break break
} }
} }
scanTag( scanTag($elem.parentNode, _parent ? [_parent.__VM__] : [])
$elem.parentNode,
_parent ? [VMODELS[_parent.anotctrl]] : []
)
} }
} }
}) })
@ -3298,10 +3295,6 @@
}) })
} }
function createSignalTower(elem, vmodel) {
elem.anotctrl = elem.anotctrl || vmodel.$id
}
function getBindingCallback(elem, name, vmodels) { function getBindingCallback(elem, name, vmodels) {
var callback = elem.getAttribute(name) var callback = elem.getAttribute(name)
if (callback) { if (callback) {
@ -3574,7 +3567,8 @@
vm = [newVmodel] vm = [newVmodel]
elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则 elem.removeAttribute(node.name) //removeAttributeNode不会刷新xx[anot]样式规则
createSignalTower(elem, newVmodel) // 挂载VM对象到相应的元素上
elem.__VM__ = newVmodel
hideProperty(newVmodel, '$elem', elem) hideProperty(newVmodel, '$elem', elem)
if (vmodels.length) { if (vmodels.length) {
@ -3842,19 +3836,12 @@
var state = {} var state = {}
var props = getOptionsFromTag(elem, host.vmodels) var props = getOptionsFromTag(elem, host.vmodels)
var $id = props.uuid || generateID(widget) var $id = props.uuid || generateID(widget)
var __willpush__ = null
var slots = null var slots = null
if (elem.firstElementChild) { if (elem.firstElementChild) {
slots = parseSlot(elem.children) slots = parseSlot(elem.children)
} }
if (props.hasOwnProperty('hostPush')) {
elem.removeAttribute('host-push')
__willpush__ = props.hostPush
props.hostPush = parseVmValue(parentVm, __willpush__)
}
if (props.hasOwnProperty(':disabled')) { if (props.hasOwnProperty(':disabled')) {
var disabledKey = props[':disabled'] var disabledKey = props[':disabled']
var disabledKeyReverse = false var disabledKeyReverse = false
@ -3906,7 +3893,7 @@
var childValueWatcher = function() { var childValueWatcher = function() {
var val = this.value var val = this.value
if (val && typeof val === 'object') { if (val && typeof val === 'object') {
val = val.$model || val val = val.$model
} }
parseVmValue(parentVm, valueKey, val) parseVmValue(parentVm, valueKey, val)
} }
@ -3993,19 +3980,21 @@
var vmodel = Anot(hooks) var vmodel = Anot(hooks)
hideProperty(vmodel, '__WIDGET__', name) hideProperty(vmodel, '__WIDGET__', name)
delete vmodel.$mounted delete vmodel.$mounted
parentVm.$components.push(vmodel)
if (parentVm.__WIDGET__ === name) { // 对象组件的子父vm关系, 只存最顶层的$components对象中,
vmodel.$up = parentVm // 而子vm, 无论向下多少级, 他们的$up对象也只存最顶层的组件vm
var __pvm__ = parentVm
while (__pvm__.$up && __pvm__.$up.__WIDGET__ === name) {
__pvm__ = __pvm__.$up
} }
__pvm__.$components.push(vmodel)
if (__pvm__.__WIDGET__ === name) {
vmodel.$up = __pvm__
}
__pvm__ = undefined
elem.msResolved = 1 //防止二进扫描此元素 elem.msResolved = 1 //防止二进扫描此元素
if (__willpush__) {
hideProperty(vmodel, '$push', function(val) {
parseVmValue(parentVm, __willpush__, val)
})
}
componentWillMount.call(vmodel) componentWillMount.call(vmodel)
globalHooks.componentWillMount.call(null, vmodel) globalHooks.componentWillMount.call(null, vmodel)
@ -4066,6 +4055,7 @@
} }
} }
}) })
elem.__VM__ = vmodel
scanTag(elem, [vmodel]) scanTag(elem, [vmodel])
Anot.vmodels[vmodel.$id] = vmodel Anot.vmodels[vmodel.$id] = vmodel
if (!elem.childNodes.length) { if (!elem.childNodes.length) {

View File

@ -11,26 +11,26 @@ import './main.scss'
//储存版本信息 //储存版本信息
Anot.ui.tree = '1.0.0' Anot.ui.tree = '1.0.0'
const log = console.log
function format(arr, { id, parent, label }) { function format(arr, expand, { id, parent, label } = {}) {
let tmp = {} let tmp = {}
let farr = [] let farr = []
this.__path__ = {}
arr = Anot.deepCopy(arr) arr = Anot.deepCopy(arr)
arr.sort(function(a, b) { arr.sort(function(a, b) {
return a[parent] === b[parent] ? a.sort - b.sort : a[parent] - b[parent] return a[parent] === b[parent] ? a.sort - b.sort : a[parent] - b[parent]
}) })
arr.forEach(function(it) { arr.forEach(it => {
// Anot.hideProperty(it, '__checked__', !!it.__checked__) this.__path__[it[id]] = []
it.__checked__ = !!it.__checked__
it.open = !!it.open
// console.log(it.hasOwnProperty('__checked__'), it.__checked__)
tmp[it[id]] = it tmp[it[id]] = it
var parentItem = tmp[it[parent]] let parentItem = tmp[it[parent]]
if (!parentItem) { if (!parentItem) {
return farr.push(tmp[it[id]]) return farr.push(tmp[it[id]])
} }
this.__path__[it[id]] = this.__path__[parentItem[id]].concat(parentItem[id])
parentItem.__open__ =
typeof expand === 'boolean' ? expand : expand.includes(parentItem[id])
parentItem.children = parentItem.children || [] parentItem.children = parentItem.children || []
parentItem.children.push(it) parentItem.children.push(it)
}) })
@ -40,41 +40,49 @@ function format(arr, { id, parent, label }) {
export default Anot.component('tree', { export default Anot.component('tree', {
__init__: function(props, state, next) { __init__: function(props, state, next) {
this.classList.add('do-tree') this.classList.add('do-tree')
// this.setAttribute(':visible', 'list.size()') this.setAttribute(':visible', 'list.size()')
if (props.hasOwnProperty('expand')) {
if (Array.isArray(props.expand)) {
state.expand = props.expand
} else {
state.expand = true
}
}
if (props.list) { if (props.list) {
for (let it of props.list) { for (let it of props.list) {
state.__LIST__.push(it)
state.__LIST_DICT__[it[props.id]] = it state.__LIST_DICT__[it[props.id]] = it
} }
} }
state.value = state.value || [] state.list = format.call(this, props.list || [], state.expand, props)
state.list = format(props.list || [], props)
state.multiCheck = props.hasOwnProperty('multiCheck') state.multiCheck = props.hasOwnProperty('multiCheck')
delete props.list delete props.list
delete props.multiCheck delete props.multiCheck
delete props.expand
next() next()
}, },
render: function() { render: function() {
let { multiCheck } = this let { multiCheck } = this
return ` return `
<section class="do-tree__item" :repeat="list" :class="{open: el.open, dir: el.children}"> <section class="do-tree__item" :repeat="list" :class="{open: el.__open__, dir: el.children}">
<em <em
:class="{ :class="{
'do-icon-txt': !el.children, 'do-icon-txt': !el.children,
'do-icon-folder-close': el.children && !el.open, 'do-icon-folder-close': el.children && !el.__open__,
'do-icon-folder-open': el.children && el.open, 'do-icon-folder-open': el.children && el.__open__,
}" }"
:click="__toggle(el)"></em> :click="__toggle(el)"></em>
<span <span
class="checkbox" class="checkbox"
:class="{'do-icon-get': value.includes(el[props.id])}" :class="{'do-icon-get': __isChecked(el)}"
:if="multiCheck" :if="multiCheck"
:click="__check(el, null, $event)"></span> :click="__check(el, $event)"></span>
<span <span class="label"
:click="__select(el)" :click="__pick(el)"
:class="{active: el[props.id] === currItem}" :class="{active: el[props.id] === value}"
:text="el[props.label]"></span> :text="el[props.label]"></span>
<div class="sub-tree" :if-loop="el.children"> <div class="sub-tree" :if-loop="el.children">
@ -82,8 +90,7 @@ export default Anot.component('tree', {
:value="value" :value="value"
:attr="{ :attr="{
list: el.children, list: el.children,
'@itemClick': props.itemClick, expand: expand,
'@itemPicked': __check,
id: props.id, id: props.id,
label: props.label, label: props.label,
parent: props.parent, parent: props.parent,
@ -94,108 +101,111 @@ export default Anot.component('tree', {
}, },
componentDidMount: function() { componentDidMount: function() {
this.list.forEach(it => {
if (it.__checked__) {
this.checkedItems[it[this.props.id]] = it.$model
}
})
if (typeof this.props.created === 'function') { if (typeof this.props.created === 'function') {
this.props.created(this) this.props.created(this)
} }
}, },
state: { state: {
__LIST__: [],
__LIST_DICT__: {}, __LIST_DICT__: {},
list: [], list: [],
value: [], value: null,
multiCheck: false, multiCheck: false
currItem: -1,
checkedItems: {}
}, },
skip: ['checkedItems', '__LIST__', '__LIST_DICT__'], skip: ['__LIST_DICT__'],
props: { props: {
id: 'id', id: 'id',
label: 'label', label: 'label',
parent: 'parent', parent: 'parent',
checked: [],
created: Anot.PropsTypes.isFunction(), created: Anot.PropsTypes.isFunction(),
itemClick: Anot.PropsTypes.isFunction(), itemClick: Anot.PropsTypes.isFunction(),
itemPicked: Anot.PropsTypes.isFunction() itemCheck: Anot.PropsTypes.isFunction()
}, },
methods: { methods: {
// 子目录的开关 // 子目录的开关
__toggle: function(obj) { __toggle: function(item) {
if (!obj.children) { if (!item.children) {
return return
} }
obj.open = !obj.open item.__open__ = !item.__open__
},
__isChecked(item) {
let vm = this
if (vm.$up) {
vm = vm.$up
}
let { checked, id } = vm.props
return checked.includes(item[id])
}, },
// 选中条目, 并将选中的列表向上冒泡, 直到最外层将结果通过回调返回给外部 // 选中条目, 并将选中的列表向上冒泡, 直到最外层将结果通过回调返回给外部
__check: function(el, itemsFromChild, ev) { __check: function(item, ev) {
if (ev) { Anot(ev.target).toggleClass('do-icon-get')
Anot(ev.target).toggleClass('do-icon-get')
}
// return
let item = null
let arr = []
let vm, id, itemPicked, checkedItems
if (this.props) {
vm = this
id = this.props.id
itemPicked = this.props.itemPicked
checkedItems = this.checkedItems
} else {
vm = this.$up
id = vm.props.id
itemPicked = vm.props.itemPicked
checkedItems = vm.checkedItems
}
if (itemsFromChild) {
item = el
Object.assign(checkedItems, itemsFromChild)
} else {
el.__checked__ = !el.__checked__
// Anot.hideProperty(el, '__checked__', !el.__checked__)
item = el.$model
checkedItems[item[id]] = el.__checked__ ? item : null
}
let vm = this
if (vm.$up) { if (vm.$up) {
arr = checkedItems vm = vm.$up
} else {
// 冒泡到最高一层时, 将对象转为数组
for (let i in checkedItems) {
if (checkedItems[i]) {
arr.push(checkedItems[i])
} else {
delete checkedItems[i]
}
}
} }
if (typeof itemPicked === 'function') { let {
itemPicked(item, arr) props: { checked, id, itemCheck },
__LIST_DICT__
} = vm
if (checked.includes(item[id])) {
Anot.Array.remove(checked, item[id])
} else {
checked.push(item[id])
}
if (typeof itemCheck === 'function') {
itemCheck(__LIST_DICT__[item[id]], checked)
} }
}, },
// 单个条目的点击选择 // 单个条目的点击选择
__select: function(el) { __pick: function(el) {
let { id, itemClick } = this.props let vm = this
this.currItem = el[id] let id = this.props.id
// 只取最顶层的VM的字典, 因为子VM的字典已经被污染了
if (vm.$up) {
vm = vm.$up
}
let {
__LIST_DICT__,
props: { itemClick }
} = vm
this.value = el[id]
if (typeof itemClick === 'function') { if (typeof itemClick === 'function') {
itemClick(this.__LIST_DICT__[el[id]]) itemClick(__LIST_DICT__[el[id]])
// itemClick(el.$model) }
},
update(itemId, data) {
let vm = this
let path = this.$elem.__path__[itemId]
let { id } = this.props
// 主键ID不可修改
delete data[id]
if (path.length) {
for (let tmp of vm.$components) {
if (tmp.__LIST_DICT__[itemId]) {
vm = tmp
break
}
}
}
for (let it of vm.list) {
if (it[id] === itemId) {
Object.assign(it, data)
}
} }
}, },
// 以给定的列表重置组件渲染 // 以给定的列表重置组件渲染
resetWith: function(arr = []) { resetWith: function(arr = []) {
this.checked = {}
this.list.clear() this.list.clear()
for (let it of arr) { for (let it of arr) {
this.__LIST__.push(it)
this.__LIST_DICT__[it[this.props.id]] = it this.__LIST_DICT__[it[this.props.id]] = it
} }
this.list.pushArray(format(arr || [], this.props)) this.list.pushArray(
format.call(this.$elem, arr || [], this.expand, this.props)
)
} }
} }
}) })

View File

@ -9,20 +9,21 @@
@import '../../css/var.scss'; @import '../../css/var.scss';
.do-tree {overflow:hidden;overflow-y:auto;position:relative;display:inline-block;width:100%;height:100%;line-height:36px;font-size:15px;color:nth($cd, 2); .do-tree {overflow:hidden;overflow-y:auto;position:relative;display:block;width:100%;height:100%;line-height:34px;font-size:15px;color:nth($cd, 2);
&__item {overflow:hidden; min-height:35px;margin:1px 0; white-space:nowrap; text-overflow:ellipsis; &__item {overflow:hidden; min-height:34px;
.sub-tree {display:none;margin-left:20px;} .sub-tree {display:none;width:100%;padding-left:20px;}
em,span {display:inline-block;cursor:pointer;color:nth($cd, 1);} em,span {display:block;cursor:pointer;}
em {float:left;padding:0 5px;font-size:20px;} em {float:left;padding:0 5px;font-size:20px;color:nth($cd, 1);}
span { span {
&:hover {color:nth($cd, 1);} &:hover {color:nth($cd, 1);}
&.active {color:nth($cd, 3);font-weight:bold;} &.active {color:nth($cd, 3);font-weight:bold;}
&.checkbox {float:left;position:relative;width:18px;height:18px;margin:8px 5px 8px 0;line-height:16px;border:1px solid nth($cd, 2);border-radius:3px; font-size:16px;text-align:center;} &.checkbox {float:left;position:relative;width:18px;height:18px;margin:8px 5px 8px 0;line-height:16px;border:1px solid nth($cd, 2);border-radius:3px; font-size:16px;text-align:center;}
&.label { white-space:nowrap; text-overflow:ellipsis;}
} }
} }
&__item.open>.sub-tree {display:block;} &__item.open>.sub-tree {display:block;}