import { shortcut, Widget, extend, emptyFn, createWidget, isKey, map, contains, remove, Controller, delay, isNotNull, Selection, Direction, LogicFactory, pushDistinct } from "@/core"; import { SelectList, MultiSelectBar, MultiSelectItem } from "@/case"; import { MultiSelectInnerLoader } from "./loader"; @shortcut() export class MultiSelectLoader extends Widget { static xtype = "bi.multi_select_loader"; static EVENT_CHANGE = "EVENT_CHANGE"; _defaultConfig() { return extend(super._defaultConfig(...arguments), { baseCls: "bi-multi-select-loader", logic: { dynamic: true, }, el: { height: 400, }, valueFormatter: emptyFn, itemsCreator: emptyFn, itemFormatter: emptyFn, onLoaded: emptyFn, itemHeight: BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT, isDefaultInit: false, }); } _init() { super._init(...arguments); const self = this, opts = this.options; let hasNext = false; this.storeValue = opts.value || {}; this._assertValue(this.storeValue); this.button_group = createWidget({ type: SelectList.xtype, logic: opts.logic, toolbar: { type: MultiSelectBar.xtype, cls: "bi-list-item-active", height: this.options.itemHeight || BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT, iconWrapperWidth: 36, }, el: extend( { onLoaded: opts.onLoaded, el: { type: MultiSelectInnerLoader.xtype, isDefaultInit: opts.isDefaultInit, }, }, opts.el ), itemsCreator(op, callback) { const startValue = self._startValue; self.storeValue && (op = extend(op || {}, { selectedValues: isKey(startValue) && self.storeValue.type === Selection.Multi ? self.storeValue.value.concat(startValue) : self.storeValue.value, })); opts.itemsCreator(op, ob => { hasNext = ob.hasNext; let firstItems = []; if (op.times === 1 && self.storeValue) { const json = map(self.storeValue.value, (i, v) => { const txt = opts.valueFormatter(v) || v; return { text: txt, value: v, title: txt, selected: self.storeValue.type === Selection.Multi, }; }); if (isKey(self._startValue) && !contains(self.storeValue.value, self._startValue)) { const txt = opts.valueFormatter(startValue) || startValue; json.unshift({ text: txt, value: startValue, title: txt, selected: true, }); } firstItems = self._createItems(json); } callback(firstItems.concat(self._createItems(ob.items)), ob.keyword || ""); if (op.times === 1 && self.storeValue) { isKey(startValue) && (self.storeValue.type === Selection.All ? remove(self.storeValue.value, startValue) : pushDistinct(self.storeValue.value, startValue)); self.setValue(self.storeValue); } op.times === 1 && self._scrollToTop(); }); }, hasNext() { return hasNext; }, value: this.storeValue, }); createWidget( extend( { element: this, }, LogicFactory.createLogic( LogicFactory.createLogicTypeByDirection(Direction.Top), extend( { scrolly: true, vgap: 5, }, opts.logic, { items: LogicFactory.createLogicItemsByDirection(Direction.Top, this.button_group), } ) ) ) ); this.button_group.on(Controller.EVENT_CHANGE, function () { self.fireEvent(Controller.EVENT_CHANGE, arguments); }); this.button_group.on(SelectList.EVENT_CHANGE, function () { self.fireEvent(MultiSelectLoader.EVENT_CHANGE, arguments); }); } _createItems(items) { const allSelected = this.isAllSelected(); const itemFormatter = this.options.itemFormatter; return map(items, (i, item) => { return { type: MultiSelectItem.xtype, logic: this.options.logic, cls: "bi-list-item-active", height: this.options.itemHeight || BI.SIZE_CONSANTS.LIST_ITEM_HEIGHT, selected: allSelected, iconWrapperWidth: 36, ...item, ...itemFormatter(item), }; }); } _scrollToTop() { const self = this; delay(() => { self.button_group.element.scrollTop(0); }, 30); } isAllSelected() { return this.button_group.isAllSelected(); } _assertValue(val) { val || (val = {}); val.type || (val.type = Selection.Multi); val.value || (val.value = []); } setStartValue(v) { this._startValue = v; } setValue(v) { this.storeValue = v || {}; this._assertValue(this.storeValue); this.button_group.setValue(this.storeValue); } getValue() { return this.button_group.getValue(); } getAllButtons() { return this.button_group.getAllButtons(); } empty() { this.button_group.empty(); } populate(...args) { const items = args[0]; if (isNotNull(items)) { args[0] = this._createItems(items); } this.button_group.populate(...args); } resetHeight(h) { this.button_group.resetHeight(h - 10); } resetWidth(w) { this.button_group.resetWidth(w); } }