MD.Input.Select.List = new Class({ Extends: MD.Input.Select ,options: { list: { } ,popover: { destroyOnHide: true ,destroyContent: false ,position: 'bottomRight' ,edge: 'topRight' ,closeButton: false ,showNipple: false } ,allowMultipleSelection: false // this setting overrides the allowUnSelect, i am not sure this is even used ,allowUnSelect: true ,activateItem: true } ,initialize: function(options) { this.setOptions(options); this._value = ''; this._text = ''; this.parent(); } ,destroy: function() { this._list.destroy(); this.parent(); } ,clear: function() { if (this._previousItem) { this._list.deactivateItem(this._previousItem); } this._previousItem = null; this._text = ""; this._value = ""; this.parent(); return this; } ,_setup: function() { this.parent(); this.options.list.dataFormat = this.options.list.dataFormat || {}; this.options.list.dataFormat = Object.merge(this.options.dataFormat, this.options.list.dataFormat); if (this.options.list.className) { this.options.list.className = 'SelectList ' + this.options.list.className; } else { this.options.list.className = 'SelectList'; } this._list = new MD.List( Object.merge( this.options.list ,{ itemAction: function(item) { this.fireEvent('select:before', item); }.bind(this) } ) ); //this._popover.setContent(this._list.toElement()); } ,_getPopoverContent: function() { return this._list.toElement(); } ,setData: function(data, append) { if ( ! append) { this._list.empty(); } this._list.append(data); return this; } ,getItemsArray: function() { if (this._list) { return this._list.getItemsArray(); } else { return []; } } ,selectItemBy: function(property, value) { this._list.getItemBy(property, value); this.fireEvent('select:before', [this._list.getItemBy(property, value)]); return this; } ,getItemBy: function(property, value) { return this._list.getItemBy(property, value); } ,selectItem: function(item, type) { // use the type to fire the select event // others can use it and determine if they need to act // upon it this.fireEvent('select:before', [item, type]); return this; } ,getNextItem: function() { var currentValue = this.getValue(); var currentIndex = this._list.getItemIndex(this.options.dataFormat.value, currentValue); if (currentValue === '') { // assume it is the placeholder return this.getItemsArray()[0]; } if (currentIndex !== false && (this.getListCount()-1) > currentIndex) { return this.getItemsArray()[currentIndex+1]; } else { return false; } } ,isFirstItem: function(item) { return this._list.getItemIndex(this.options.dataFormat.value, item[this.options.dataFormat.value]) === 0; } ,isLastItem: function(item) { return this._list.getItemIndex(this.options.dataFormat.value, item[this.options.dataFormat.value]) === (this.getListCount()-1); } ,getPreviousItem: function() { var currentValue = this.getValue(); var currentIndex = this._list.getItemIndex(this.options.dataFormat.value, currentValue); if (currentIndex !== false && currentIndex !== 0) { return this.getItemsArray()[currentIndex-1]; } else { return false; } } ,getListCount: function() { return this.getItemsArray().length; } ,_setupPopoverEvents: function() { this.getPopover().addEvent('hide', function() { if (this._list.input) { this._list.input.setValue(''); } }.bind(this)); this.parent(); } ,_setupEventHandlers: function() { this.parent(); this.addEvent('select:before', function(item, type) { type = type || 'manual'; if ( ! this.options.allowMultipleSelection && this._previousItem) { if (this.options.allowUnSelect && item === this._previousItem) { this.clear(); return; } this._list.deactivateItem(this._previousItem); } // we do not care about the "type" here, just select it in the list if (item && this.options.activateItem) { this._list.activateItem(item); this._previousItem = item; } var text = item[this._list.options.textPropertyKey]; this._text = text; if (item.hasOwnProperty(this._list.options.itemIdKey)) { this._value = item[this._list.options.itemIdKey]; } else { this._value = text; } if (this.options.autoUpdateButtonText) { this.setButtonText(text); } if (this.options.autoClosePopover && this._popover) { this._popover.hide(); } this._needsValidation = true; this.fireEvent('select', [item, type, this]); this.fireEvent('select:after', [item, this]); }.bind(this)); this._handleAfterEventsSetup(); } ,_handleAfterEventsSetup: function() { if (this.options.list.data && this.options.list.data.length) { var df = this.options.dataFormat; var selected = df.selected; var value = df.value; this.options.list.data.some(function(it) { if (it[selected]) { var item = this.getItemBy(value, it[value]); if (item) { this.selectItem(item, 'automatic'); } return true; } return false; }, this); } } // dont have a better way to use a non md.list at the moment... :( ,_setList: function(newList) { this._list = newList; // make sure new list fires the right events return this; } ,getList: function() { return this._list; } ,activateItem: function(item) { this.getList().activateItem(item); return this; } ,deactivateItem: function(item) { this.getList().deactivateItem(item); return this; } ,getValue: function() { return this._value; } ,removeValue: function() { this._value = null; return this; } ,getValueAsText: function() { return this._text; } ,getText: function() { return this.getValueAsText(); } ,setText: function(text) { this.getButton().setText(text); return this; } ,setValue: function(value, text) { var item = this.getItemBy(this._list.options.itemIdKey, value); if (item) { this.selectItem(item); } return this; } });