$.fn.romoSelect = function() { return $.map(this, function(element) { return new RomoSelect(element); }); } var RomoSelect = function(element) { this.elem = $(element); this.doInit(); this.doBindSelectDropdown(); this.doRefreshUI(); if (this.elem.attr('id') !== undefined) { $('label[for="'+this.elem.attr('id')+'"]').on('click', $.proxy(function(e) { this.romoSelectDropdown.elem.focus(); }, this)); } $(window).on("pageshow", $.proxy(function(e) { var selectedVal = this.elem.find('option[selected]').attr('value'); if (selectedVal === undefined) { selectedVal = ''; } if (selectedVal !== this.elem[0].value) { this.romoSelectDropdown.doSetNewValue(selectedVal); this._setNewValue(selectedVal); } }, this)); this.elem.trigger('select:ready', [this]); } RomoSelect.prototype.doInit = function() { // override as needed } RomoSelect.prototype.doBindSelectDropdown = function() { this.romoSelectDropdown = this._buildSelectDropdownElem().romoSelectDropdown(this.elem)[0]; this.romoSelectDropdown.elem.on('selectDropdown:dropdown:toggle', $.proxy(function(e, dropdown, selectDropdown) { this.elem.trigger('select:dropdown:toggle', [dropdown, this]); }, this)); this.romoSelectDropdown.elem.on('selectDropdown:dropdown:popupOpen', $.proxy(function(e, dropdown, selectDropdown) { this.elem.trigger('select:dropdown:popupOpen', [dropdown, this]); }, this)); this.romoSelectDropdown.elem.on('selectDropdown:dropdown:popupClose', $.proxy(function(e, dropdown, selectDropdown) { this.elem.trigger('select:dropdown:popupClose', [dropdown, this]); }, this)); this.romoSelectDropdown.elem.on('selectDropdown:itemSelected', $.proxy(function(e, newValue, prevValue, selectDropdown) { this.romoSelectDropdown.elem.focus(); this.elem.trigger('select:itemSelected', [newValue, prevValue, this]); }, this)); this.romoSelectDropdown.elem.on('selectDropdown:change', $.proxy(function(e, newValue, prevValue, selectDropdown) { this._setNewValue(newValue); this.elem.trigger('change'); this.elem.trigger('select:change', [newValue, prevValue, this]); }, this)); this.elem.on('select:triggerToggle', $.proxy(function(e) { this.romoSelectDropdown.elem.trigger('selectDropdown:triggerToggle', []); }, this)); this.elem.on('select:triggerPopupOpen', $.proxy(function(e) { this.romoSelectDropdown.elem.trigger('selectDropdown:triggerPopupOpen', []); }, this)); this.elem.on('select:triggerPopupClose', $.proxy(function(e) { this.romoSelectDropdown.elem.trigger('selectDropdown:triggerPopupClose', []); }, this)); } RomoSelect.prototype.doRefreshUI = function() { var text = this.romoSelectDropdown.selectedListing().text() || ' '; this.romoSelectDropdown.elem.find('.romo-select-text').html(text); } RomoSelect.prototype.onCaretClick = function(e) { if (this.elem.prop('disabled') === false) { this.romoSelectDropdown.elem.focus(); this.elem.trigger('select:triggerPopupOpen'); } } RomoSelect.prototype._setNewValue = function(newValue) { this.elem[0].value = newValue; this.doRefreshUI(); } RomoSelect.prototype._buildSelectDropdownElem = function() { var romoSelectDropdownElem = $('