fineui是帆软报表和BI产品线所使用的前端框架。
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.

221 lines
6.0 KiB

2 years ago
import {
shortcut,
extend,
emptyFn,
each,
isKey,
UUID,
isNotEmptyArray,
defaults,
createWidget,
Tree,
nextTick,
Selection,
Controller,
Events,
VerticalLayout,
AdaptiveLayout,
isNull,
isArray
} from "@/core";
import { Pane, CustomTree, Loader, ButtonTree } from "@/base";
import { BasicTreeNode, BasicTreeItem, TreeExpander } from "@/case";
@shortcut()
export class MultiLayerSelectLevelTree extends Pane {
static xtype = "bi.multilayer_select_level_tree";
static EVENT_CHANGE = "EVENT_CHANGE";
_defaultConfig() {
return extend(super._defaultConfig(...arguments), {
baseCls: "bi-multilayer-select-level-tree",
isDefaultInit: false,
items: [],
2 years ago
itemsCreator: emptyFn,
keywordGetter: emptyFn,
6 years ago
value: "",
2 years ago
scrollable: true,
7 years ago
});
2 years ago
}
2 years ago
_init() {
const o = this.options;
super._init(...arguments);
6 years ago
this.storeValue = o.value;
this.initTree(this.options.items);
6 years ago
this.check();
2 years ago
}
2 years ago
_formatItems(nodes, layer, pNode) {
const self = this,
o = this.options;
const keyword = o.keywordGetter();
each(nodes, (i, node) => {
const extend = {
isFirstNode: i === 0,
isLastNode: i === nodes.length - 1,
2 years ago
height: BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT,
};
node.layer = layer;
2 years ago
if (!isKey(node.id)) {
node.id = UUID();
}
6 years ago
node.keyword = node.keyword || keyword;
extend.pNode = pNode;
2 years ago
if (
node.isParent === true ||
node.parent === true ||
isNotEmptyArray(node.children)
) {
extend.type = BasicTreeNode.xtype;
extend.selectable = true;
2 years ago
defaults(node, extend);
self._formatItems(node.children, layer + 1, node);
} else {
2 years ago
extend.type = BasicTreeItem.xtype;
defaults(node, extend);
}
});
2 years ago
return nodes;
2 years ago
}
2 years ago
_assertId(sNodes) {
each(sNodes, (i, node) => {
node.id = node.id || UUID();
});
2 years ago
}
2 years ago
initTree(nodes) {
const self = this,
o = this.options;
let hasNext = false;
this.empty();
this._assertId(nodes);
2 years ago
this.tree = createWidget({
type: CustomTree.xtype,
7 years ago
cls: "tree-view display-table",
expander: {
2 years ago
type: TreeExpander.xtype,
selectable: true,
isDefaultInit: o.isDefaultInit,
el: {},
popup: {
2 years ago
type: CustomTree.xtype,
},
},
2 years ago
items: this._formatItems(Tree.transformToTreeFormat(nodes), 0),
itemsCreator(op, callback) {
op.times === 1 &&
!op.node &&
nextTick(() => {
6 years ago
self.loading();
});
2 years ago
o.itemsCreator(op, ob => {
hasNext = ob.hasNext;
2 years ago
op.times === 1 && !op.node && self._populate(ob.items);
callback(
self._formatItems(
Tree.transformToTreeFormat(ob.items),
op.node ? op.node.layer + 1 : 0,
op.node
)
);
6 years ago
self.setValue(self.storeValue);
2 years ago
op.times === 1 &&
!op.node &&
nextTick(() => {
6 years ago
self.loaded();
});
});
},
value: o.value,
el: {
2 years ago
type: Loader.xtype,
isDefaultInit: o.itemsCreator !== emptyFn,
el: {
2 years ago
type: ButtonTree.xtype,
chooseType:
o.chooseType === Selection.None
? Selection.None
: Selection.Default, // 不使用buttontree内部getValue逻辑
behaviors: o.behaviors,
2 years ago
layouts: [
{
type: VerticalLayout.xtype,
}
],
},
2 years ago
hasNext() {
return hasNext;
2 years ago
},
},
});
2 years ago
this.tree.on(Controller.EVENT_CHANGE, function (type, value) {
self.fireEvent(Controller.EVENT_CHANGE, arguments);
if (type === Events.CLICK) {
6 years ago
self.setValue(value);
2 years ago
self.fireEvent(
MultiLayerSelectLevelTree.EVENT_CHANGE,
arguments
);
}
7 years ago
});
7 years ago
2 years ago
createWidget({
type: AdaptiveLayout.xtype,
7 years ago
element: this,
6 years ago
scrollable: o.scrollable,
2 years ago
items: [this.tree],
6 years ago
});
2 years ago
}
2 years ago
_populate() {
super.populate(...arguments);
}
6 years ago
2 years ago
populate(nodes) {
6 years ago
this._populate(nodes);
2 years ago
isNull(nodes)
? this.tree.populate()
: this.tree.populate(
this._formatItems(Tree.transformToTreeFormat(nodes), 0)
);
}
2 years ago
setValue(v) {
// getValue依赖于storeValue, 那么不选的时候就不要更新storeValue了
2 years ago
if (this.options.chooseType === Selection.None) {
} else {
this.storeValue = v;
this.tree.setValue(v);
}
2 years ago
}
2 years ago
getValue() {
return isArray(this.storeValue)
? this.storeValue
: isNull(this.storeValue)
? []
: [this.storeValue];
}
2 years ago
getAllLeaves() {
return this.tree.getAllLeaves();
2 years ago
}
2 years ago
getNodeById(id) {
return this.tree.getNodeById(id);
2 years ago
}
2 years ago
getNodeByValue(id) {
return this.tree.getNodeByValue(id);
}
2 years ago
}