/** * 选择字段trigger * * Created by GUY on 2015/9/15. * @class BI.SelectTextTrigger * @extends BI.Trigger */ BI.SelectTextTrigger = BI.inherit(BI.Trigger, { _defaultConfig: function () { return BI.extend(BI.SelectTextTrigger.superclass._defaultConfig.apply(this, arguments), { baseCls: "bi-select-text-trigger", height: 24, allowClear: false, valueFormatter: BI.emptyFn, defaultText: "", }); }, _init: function () { BI.SelectTextTrigger.superclass._init.apply(this, arguments); var self = this, o = this.options; var obj = this._digest(o.value, o.items); this.trigger = BI.createWidget({ type: "bi.text_trigger", element: this, height: o.height, readonly: o.readonly, text: obj.text, defaultText: o.defaultText, textCls: obj.textCls, textHgap: o.textHgap, textVgap: o.textVgap, textLgap: o.textLgap, textRgap: o.textRgap, textTgap: o.textTgap, textBgap: o.textBgap, tipType: o.tipType, title: null, allowClear: o.allowClear, listeners: [ { eventName: BI.TextTrigger.EVENT_CLEAR, action: function () { self.setText(""); self.fireEvent(BI.SelectTextTrigger.EVENT_CLEAR); } } ] }); }, _digest: function (val, items) { var o = this.options; // 提升valueFormatter的优先级 if (o.valueFormatter !== BI.emptyFn && BI.isFunction(o.valueFormatter)) { return { text: o.valueFormatter(val), }; } val = BI.isArray(val) ? val : [val]; var result = []; var formatItems = BI.Tree.transformToArrayFormat(items); BI.each(formatItems, function (i, item) { if (BI.contains(val, item.value) && !BI.contains(result, item.text || item.value)) { result.push(item.text || item.value); BI.remove(val, item.value); } }); if (result.length > 0 && val.length === 0) { return { textCls: "", text: result.join(","), }; } else { var text = BI.isFunction(o.text) ? o.text() : o.text; if (BI.isEmptyString(text)) { return { textCls: "bi-tips", text: "" }; } return { text: o.text }; } }, setText: function (text) { this.options.text = text; this.trigger.setText(text); }, setValue: function (val) { var formatValue = this._digest(val, this.options.items); this.trigger.setTextCls(formatValue.textCls); this.trigger.setText(formatValue.text); }, setTipType: function (v) { this.options.tipType = v; this.trigger.setTipType(v); }, getTextor: function () { return this.trigger.getTextor(); }, populate: function (items) { this.options.items = items; } }); BI.SelectTextTrigger.EVENT_CLEAR = "EVENT_CLEAR"; BI.shortcut("bi.select_text_trigger", BI.SelectTextTrigger);