forked from fanruan/fineui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.6 KiB
130 lines
3.6 KiB
/** |
|
* guy |
|
* 二级树 |
|
* @class BI.LevelTree |
|
* @extends BI.Single |
|
*/ |
|
BI.LevelTree = BI.inherit(BI.Widget, { |
|
_defaultConfig: function () { |
|
return BI.extend(BI.LevelTree.superclass._defaultConfig.apply(this, arguments), { |
|
baseCls: "bi-level-tree", |
|
el: { |
|
chooseType: 0 |
|
}, |
|
expander: {}, |
|
items: [], |
|
value: "" |
|
}); |
|
}, |
|
|
|
_init: function () { |
|
BI.LevelTree.superclass._init.apply(this, arguments); |
|
|
|
this.initTree(this.options.items); |
|
}, |
|
|
|
_formatItems: function (nodes, layer, pNode) { |
|
var self = this; |
|
BI.each(nodes, function (i, node) { |
|
var extend = { |
|
layer: layer, |
|
height: BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT, |
|
isFirstNode: i === 0, |
|
isLastNode: i === nodes.length - 1, |
|
}; |
|
if (!BI.isKey(node.id)) { |
|
node.id = BI.UUID(); |
|
} |
|
extend.pNode = pNode; |
|
if (node.isParent === true || node.parent === true || BI.isNotEmptyArray(node.children)) { |
|
extend.type = "bi.tree_node"; |
|
extend.selectable = false; |
|
|
|
BI.defaults(node, extend); |
|
self._formatItems(node.children, layer + 1, node); |
|
} else { |
|
extend.type = "bi.tree_item"; |
|
BI.defaults(node, extend); |
|
} |
|
}); |
|
return nodes; |
|
}, |
|
|
|
_assertId: function (sNodes) { |
|
BI.each(sNodes, function (i, node) { |
|
if (!BI.isKey(node.id)) { |
|
node.id = BI.UUID(); |
|
} |
|
}); |
|
}, |
|
|
|
// 构造树结构, |
|
initTree: function (nodes) { |
|
var self = this, o = this.options; |
|
this.empty(); |
|
this._assertId(nodes); |
|
this.tree = BI.createWidget({ |
|
type: "bi.custom_tree", |
|
element: this, |
|
expander: BI.extend({ |
|
type: "bi.tree_expander", |
|
el: {}, |
|
isDefaultInit: false, |
|
selectable: false, |
|
popup: { |
|
type: "bi.custom_tree" |
|
} |
|
}, o.expander), |
|
items: this._formatItems(BI.Tree.transformToTreeFormat(nodes), 0), |
|
value: o.value, |
|
|
|
el: BI.extend({ |
|
type: "bi.button_tree", |
|
chooseType: 0, |
|
layouts: [{ |
|
type: "bi.vertical" |
|
}] |
|
}, o.el) |
|
}); |
|
this.tree.on(BI.Controller.EVENT_CHANGE, function (type, value, ob) { |
|
self.fireEvent(BI.Controller.EVENT_CHANGE, arguments); |
|
if (type === BI.Events.CLICK) { |
|
self.fireEvent(BI.LevelTree.EVENT_CHANGE, value, ob); |
|
self.setValue(value); |
|
} |
|
}); |
|
}, |
|
|
|
// 生成树方法 |
|
stroke: function (nodes) { |
|
this.tree.stroke.apply(this.tree, arguments); |
|
}, |
|
|
|
populate: function (items, keyword) { |
|
items = this._formatItems(BI.Tree.transformToTreeFormat(items), 0); |
|
this.tree.populate(items, keyword); |
|
}, |
|
|
|
setValue: function (v) { |
|
this.tree.setValue(v); |
|
}, |
|
|
|
getValue: function () { |
|
return this.tree.getValue(); |
|
}, |
|
|
|
getAllLeaves: function () { |
|
return this.tree.getAllLeaves(); |
|
}, |
|
|
|
getNodeById: function (id) { |
|
return this.tree.getNodeById(id); |
|
}, |
|
|
|
getNodeByValue: function (id) { |
|
return this.tree.getNodeByValue(id); |
|
} |
|
}); |
|
BI.LevelTree.EVENT_CHANGE = "EVENT_CHANGE"; |
|
|
|
BI.shortcut("bi.level_tree", BI.LevelTree);
|
|
|