/** * list面板 * * Created by GUY on 2015/10/30. * @class BI.ListPane * @extends BI.Pane */ import { shortcut, extend, each, createWidget, emptyFn, nextTick, concat, get, Controller, Events, LogicFactory, Direction, isNull, removeAt, isFunction, isNotEmptyString, isEmptyArray, VerticalLayout } from "@/core"; import { Pane, ButtonGroup } from "@/base"; @shortcut() export class ListPane extends Pane { static xtype = "bi.list_pane"; static EVENT_CHANGE = "EVENT_CHANGE"; _defaultConfig() { const conf = super._defaultConfig(...arguments); return extend(conf, { baseCls: `${conf.baseCls || ""} bi-list-pane`, logic: { dynamic: true, }, lgap: 0, rgap: 0, tgap: 0, bgap: 0, vgap: 0, hgap: 0, items: [], itemsCreator: emptyFn, hasNext: emptyFn, onLoaded: emptyFn, el: { type: ButtonGroup.xtype, }, }); } _init() { super._init(...arguments); const o = this.options; this.button_group = createWidget(o.el, { type: ButtonGroup.xtype, chooseType: ButtonGroup.CHOOSE_TYPE_SINGLE, behaviors: {}, items: o.items, value: o.value, itemsCreator: (op, callback) => { if (op.times === 1) { this.empty(); nextTick(() => { this.loading(); }); } o.itemsCreator(op, (...args) => { callback(...args); o.items = concat(o.items, get(args, [0], [])); if (op.times === 1) { o.items = get(args, [0], []); nextTick(() => { this.loaded(); // callback可能在loading之前执行, check保证显示正确 this.check(); }); } }); }, hasNext: o.hasNext, layouts: [ { type: VerticalLayout.xtype, } ], }); this.button_group.on(Controller.EVENT_CHANGE, (...args) => { const [type, value, obj] = args; this.fireEvent(Controller.EVENT_CHANGE, ...args); if (type === Events.CLICK) { this.fireEvent(ListPane.EVENT_CHANGE, value, obj); } }); this.check(); createWidget(extend({ element: this, }, LogicFactory.createLogic(LogicFactory.createLogicTypeByDirection(Direction.Top), extend({ scrolly: true, lgap: o.lgap, rgap: o.rgap, tgap: o.tgap, bgap: o.bgap, vgap: o.vgap, hgap: o.hgap, }, o.logic, { items: LogicFactory.createLogicItemsByDirection(Direction.Top, this.button_group), })))); } hasPrev() { return this.button_group.hasPrev && this.button_group.hasPrev(); } hasNext() { return this.button_group.hasNext && this.button_group.hasNext(); } prependItems(items) { this.options.items = items.concat(this.options.items); this.button_group.prependItems(...arguments); this.check(); } addItems(items) { this.options.items = this.options.items.concat(items); this.button_group.addItems(...arguments); this.check(); } removeItemAt(indexes) { indexes = isNull(indexes) ? [] : indexes; removeAt(this.options.items, indexes); this.button_group.removeItemAt(...arguments); this.check(); } populate(items) { const o = this.options; if (arguments.length === 0 && (isFunction(this.button_group.attr("itemsCreator")))) {// 接管loader的populate方法 this.button_group.attr("itemsCreator").apply(this, [ { times: 1 }, (...args) => { if (args.length === 0) { throw new Error("参数不能为空"); } this.populate(...args); } ]); return; } const context = get(arguments, [2], {}); const tipText = context.tipText || ""; if (isNotEmptyString(tipText)) { super.populate.apply(this, []); this.setTipText(tipText); } else { super.populate(...arguments); this.button_group.populate(...arguments); isEmptyArray(get(arguments, [0], [])) && this.setTipText(o.tipText); } } empty() { this.button_group.empty(); } setNotSelectedValue() { this.button_group.setNotSelectedValue(...arguments); } getNotSelectedValue() { return this.button_group.getNotSelectedValue(); } setValue() { this.button_group.setValue(...arguments); } setAllSelected(v) { if (this.button_group.setAllSelected) { this.button_group.setAllSelected(v); } else { each(this.getAllButtons(), (i, btn) => { (btn.setSelected || btn.setAllSelected).apply(btn, [v]); }); } } getValue() { return this.button_group.getValue(...arguments); } getAllButtons() { return this.button_group.getAllButtons(); } getAllLeaves() { return this.button_group.getAllLeaves(); } getSelectedButtons() { return this.button_group.getSelectedButtons(); } getNotSelectedButtons() { return this.button_group.getNotSelectedButtons(); } getIndexByValue(value) { return this.button_group.getIndexByValue(value); } getNodeById(id) { return this.button_group.getNodeById(id); } getNodeByValue(value) { return this.button_group.getNodeByValue(value); } }