/* global jQuery:true */ /* * Fuel UX Selectlist * https://github.com/ExactTarget/fuelux * * Copyright (c) 2014 ExactTarget * Licensed under the BSD New license. */ // -- BEGIN UMD WRAPPER PREFACE -- // For more information on UMD visit: // https://github.com/umdjs/umd/blob/master/jqueryPlugin.js (function umdFactory (factory) { if (typeof define === 'function' && define.amd) { // if AMD loader is available, register as an anonymous module. define(['jquery'], factory); } else if (typeof exports === 'object') { // Node/CommonJS module.exports = factory(require('jquery')); } else { // OR use browser globals if AMD is not present factory(jQuery); } }(function SelectlistWrapper ($) { // -- END UMD WRAPPER PREFACE -- // -- BEGIN MODULE CODE HERE -- var old = $.fn.selectlist; // SELECT CONSTRUCTOR AND PROTOTYPE var Selectlist = function (element, options) { this.$element = $(element); this.options = $.extend({}, $.fn.selectlist.defaults, options); this.$button = this.$element.find('.btn.dropdown-toggle'); this.$hiddenField = this.$element.find('.hidden-field'); this.$label = this.$element.find('.selected-label'); this.$dropdownMenu = this.$element.find('.dropdown-menu'); this.$element.on('click.fu.selectlist', '.dropdown-menu a', $.proxy(this.itemClicked, this)); this.setDefaultSelection(); if (options.resize === 'auto' || this.$element.attr('data-resize') === 'auto') { this.resize(); } // if selectlist is empty or is one item, disable it var items = this.$dropdownMenu.children('li'); if( items.length === 0) { this.disable(); this.doSelect( $(this.options.emptyLabelHTML)); } // support jumping focus to first letter in dropdown when key is pressed this.$element.on('shown.bs.dropdown', function () { var $this = $(this); // attach key listener when dropdown is shown $(document).on('keypress.fu.selectlist', function(e){ // get the key that was pressed var key = String.fromCharCode(e.which); // look the items to find the first item with the first character match and set focus $this.find("li").each(function(idx,item){ if ($(item).text().charAt(0).toLowerCase() === key) { $(item).children('a').focus(); return false; } }); }); }); // unbind key event when dropdown is hidden this.$element.on('hide.bs.dropdown', function () { $(document).off('keypress.fu.selectlist'); }); }; Selectlist.prototype = { constructor: Selectlist, destroy: function () { this.$element.remove(); // any external bindings // [none] // empty elements to return to original markup // [none] // returns string of markup return this.$element[0].outerHTML; }, doSelect: function ($item) { var $selectedItem; this.$selectedItem = $selectedItem = $item; this.$hiddenField.val(this.$selectedItem.attr('data-value')); this.$label.html($(this.$selectedItem.children()[0]).html()); // clear and set selected item to allow declarative init state // unlike other controls, selectlist's value is stored internal, not in an input this.$element.find('li').each(function () { if ($selectedItem.is($(this))) { $(this).attr('data-selected', true); } else { $(this).removeData('selected').removeAttr('data-selected'); } }); }, itemClicked: function (e) { this.$element.trigger('clicked.fu.selectlist', this.$selectedItem); e.preventDefault(); // ignore if a disabled item is clicked if ($(e.currentTarget).parent('li').is('.disabled, :disabled')) { return; } // is clicked element different from currently selected element? if (!($(e.target).parent().is(this.$selectedItem))) { this.itemChanged(e); } // return focus to control after selecting an option this.$element.find('.dropdown-toggle').focus(); }, itemChanged: function (e) { //selectedItem needs to be