import { shortcut, Widget, extend, createWidget, toPix, Controller, isKey, isNull, isEmptyArray, isEmptyString, isArray, contains, find } from "@/core"; import { Combo } from "@/base"; import { SingleTreeTrigger } from "./singletree.trigger"; import { SingleTreePopup } from "./singletree.popup"; @shortcut() export class SingleTreeCombo extends Widget { static xtype = "bi.single_tree_combo"; static EVENT_CHANGE = "EVENT_CHANGE"; static EVENT_BEFORE_POPUPVIEW = "EVENT_BEFORE_POPUPVIEW"; _defaultConfig(config) { return extend(super._defaultConfig(...arguments), { baseCls: `bi-single-tree-combo ${config.simple ? "bi-border-bottom" : "bi-border bi-border-radius"}`, trigger: {}, height: 24, text: "", items: [], value: "", allowClear: false, }); } _init() { const self = this, o = this.options; super._init(...arguments); this.trigger = createWidget( extend( { type: SingleTreeTrigger.xtype, text: o.text, defaultText: o.defaultText, height: toPix(o.height, 2), items: o.items, value: o.value, allowClear: o.allowClear, warningTitle: o.warningTitle, }, o.trigger ) ); this.trigger.on(SingleTreeTrigger.EVENT_CLEAR, () => { self._clear(); }); this.popup = createWidget({ type: SingleTreePopup.xtype, items: o.items, value: o.value, }); this.combo = createWidget({ type: Combo.xtype, width: toPix(o.width, 2), height: toPix(o.height, 2), container: o.container, element: this, adjustLength: 2, el: this.trigger, popup: { el: this.popup, }, }); this.combo.on(Controller.EVENT_CHANGE, function () { self.fireEvent(Controller.EVENT_CHANGE, arguments); }); this.combo.on(Combo.EVENT_BEFORE_POPUPVIEW, function () { self.fireEvent(SingleTreeCombo.EVENT_BEFORE_POPUPVIEW, arguments); }); this.popup.on(SingleTreePopup.EVENT_CHANGE, () => { self.setValue(self.popup.getValue()); self.combo.hideView(); self.fireEvent(SingleTreeCombo.EVENT_CHANGE); }); if (isKey(o.value)) { this._checkError(o.value); } } _checkError(v) { if (isNull(v) || isEmptyArray(v) || isEmptyString(v)) { this.trigger.options.tipType = "success"; this.trigger.element.removeClass("error"); this.element.removeClass("error"); } else { v = isArray(v) ? v : [v]; const result = find(this.options.items, (idx, item) => contains(v, item.value)); if (isNull(result)) { this.trigger.setTipType("warning"); this.element.removeClass("error").addClass("error"); this.trigger.element.removeClass("error").addClass("error"); } else { this.trigger.setTipType("success"); this.trigger.element.removeClass("error"); this.element.removeClass("error"); } } } _clear() { this.setValue([]); } populate(items) { this.combo.populate(items); } setValue(v) { v = isArray(v) ? v : [v]; this.trigger.setValue(v); this.popup.setValue(v); this._checkError(v); } getValue() { return this.popup.getValue(); } }