/** @name Edo.controls.SuperSelect @class @typeName superselect @description 超级选择框 @extend Edo.controls.ComboBox */ Edo.controls.SuperSelect = function(){ Edo.controls.SuperSelect.superclass.constructor.call(this); }; Edo.controls.SuperSelect.extend(Edo.controls.ComboBox,{ /** @name Edo.controls.SuperSelect#multiSelect @property @type Boolean @default true */ multiSelect: true, /** @name Edo.controls.SuperSelect#autoHeight @property @type Boolean @default true */ autoHeight: true, popupReset: false, /** @name Edo.controls.SuperSelect#triggerVisible @property @type Boolean @default false */ triggerVisible: false, elCls: 'e-superselect', delimiter: ',', /** @name Edo.controls.SuperSelect#valueField @property @type String @default id */ valueField: 'id', /** @name Edo.controls.SuperSelect#textField @property @type String @default text @description 文本显示字段 */ textField: 'text', itemRenderer: function(r, rowIndex){ return ''+r[this.textField]+''; }, setValue: function(vv){ if(!Edo.isValue(vv)) vv = []; if(typeof vv == 'string') vv = vv.split(this.delimiter); if(!(vv instanceof Array)) vv = [vv]; this.selecteds = []; vv.each(function(v){ var o = {}; o[this.valueField] = v; //{id: 1} var index = this.data.findIndex(o); if(index != -1){ this.selecteds.add(this.data.getAt(index)); } }, this); this.view.data.load(this.selecteds); this.fixAutoSize(); this.relayout(); }, getValue: function(){ var rs = []; this.selecteds.each(function(o){ if(this.valueField == '*') rs.add(o); else rs.add(o[this.valueField]); }, this); return rs.join(this.delimiter); }, initEvents: function(){ this.on('keydown', this._onkeydown, this); this.on('click', this._onclick, this); this.on('beforeselectionchange', this._onSuperSelectBeforeSelectionChange, this, 0); Edo.controls.SuperSelect.superclass.initEvents.call(this); }, _onclick: function(e){ if(Edo.util.Dom.hasClass(e.target, 'e-superselect-item-close')){ var rowIndex = parseInt(e.target.id); var row = this.view.data.getAt(rowIndex); this.selecteds.remove(row); this.view.data.load(this.selecteds); //调整尺寸 this.fixAutoSize(); this.fixInput(this.itemIndex); this.relayout('select'); } }, _onkeydown: function(e){ var inField = this.field == e.target; if(inField && this.field.value != '') return; if(this.popupDisplayed) { if(e.keyCode == 8 && this.field.value == ''){ this.hidePopup.defer(100, this); } return; } var autoSize = true; switch(e.keyCode){ case 8: e.stopDefault(); if(inField){ this.view.data.removeAt(this.itemIndex); }else{ var sels = this.view.getSelecteds(); this.view.data.removeRange(sels); } this.itemIndex -= 1; var item = this.view.getByIndex(this.itemIndex); if(!item){ this.itemIndex -= 1; item = this.view.getByIndex(this.itemIndex); } break; case 127: if(inField){ this.view.data.removeAt(this.itemIndex+1); }else{ var sels = this.view.getSelecteds(); this.view.data.removeRange(sels); } break; case 37: case 38: this.itemIndex -= 1; this.view.clearSelect(); break; case 39: this.itemIndex += 1; // this.view.clearSelect(); break; case 40: return; break; case 36: //home this.itemIndex = -1; this.view.clearSelect(); break; case 35: //end this.itemIndex = this.view.data.getCount()-1; this.view.clearSelect(); break; default: // this.view.fixAutoSize(); // this.view.relayout('input'); // this.fixAutoSize(); // this.relayout('input'); return; } if(this.itemIndex < -1){ this.itemIndex = -1; } if(this.itemIndex > this.view.data.getCount()-1){ this.itemIndex = this.view.data.getCount()-1; } this.fixInput(this.itemIndex); this.fixAutoSize(); this.relayout(); }, _onSuperSelectBeforeSelectionChange: function(e){ var sel = e.selectedItem; if(!sel) return false; if(!this.multiSelect){ this.selecteds = []; } this.itemIndex += 1; this.selecteds.insert(this.itemIndex, sel); if(this.itemIndex < -1){ this.itemIndex = -1; } if(this.itemIndex > this.view.data.getCount()-1){ this.itemIndex = this.view.data.getCount()-1; } //调整尺寸 this.view.data.load(this.selecteds); this.field.value = ''; //this.fixInput(this.itemIndex); this.fixInput.defer(10, this, [this.itemIndex]); //### this.fixAutoSize(); this.relayout('select'); return false; }, createChildren: function(el){ Edo.controls.SuperSelect.superclass.createChildren.call(this, el); //this.selecteds.addRange(this.data.source); this.view = Edo.create({ type: 'dataview', render: this.el, itemSelector: 'e-superselect-item', itemCls: 'e-superselect-item', selectedCls: 'e-superselect-item-checked', cls: 'e-superselect-inner', width: (this.realWidth || this.defaultWidth) - (this.triggerVisible ? 20 : 0), //height: this.realHeight || this.defaultHeight, autoHeight: true, data: this.selecteds, emptyText: '请选择', tpl: '<%=this.createView()%>', valueField: this.valueField, textField: this.textField, itemRenderer: this.itemRenderer, enableDeferRefresh: false, createView: function(){ var data = this.data; if(!data || data.getCount() == 0) return ''; data = this.data.view; var text = ''; var sb = ['']; for(var i=0,l=data.length; i