3 * https://github.com/ExactTarget/fuelux
5 * Copyright (c) 2012 ExactTarget
6 * Licensed under the MIT license.
9 define(['require','jquery','./util'],function(require) {
11 var $ = require('jquery');
14 // SELECT CONSTRUCTOR AND PROTOTYPE
16 var Select = function (element, options) {
17 this.$element = $(element);
18 this.options = $.extend({}, $.fn.select.defaults, options);
19 this.$element.on('click', 'a', $.proxy(this.itemclicked, this));
20 this.$button = this.$element.find('.btn');
21 this.$label = this.$element.find('.dropdown-label');
22 this.setDefaultSelection();
24 if (options.resize === 'auto') {
33 itemclicked: function (e) {
34 this.$selectedItem = $(e.target).parent();
35 this.$label.text(this.$selectedItem.text());
37 // pass object including text and any data-attributes
39 var data = this.selectedItem();
41 // trigger changed event
42 this.$element.trigger('changed', data);
48 var el = $('#selectTextSize')[0];
50 // create element if it doesn't exist
51 // used to calculate the length of the longest string
53 $('<div/>').attr({id:'selectTextSize'}).appendTo('body');
59 // iterate through each item to find longest string
60 this.$element.find('a').each(function () {
62 var txt = $this.text();
63 var $txtSize = $('#selectTextSize');
65 newWidth = $txtSize.outerWidth();
66 if(newWidth > width) {
71 this.$label.width(width);
74 selectedItem: function() {
75 var txt = this.$selectedItem.text();
76 return $.extend({ text: txt }, this.$selectedItem.data());
79 selectByText: function(text) {
80 var selector = 'li a:fuelTextExactCI(' + text + ')';
81 this.selectBySelector(selector);
84 selectByValue: function(value) {
85 var selector = 'li[data-value="' + value + '"]';
86 this.selectBySelector(selector);
89 selectByIndex: function(index) {
91 var selector = 'li:eq(' + index + ')';
92 this.selectBySelector(selector);
95 selectBySelector: function(selector) {
96 var item = this.$element.find(selector);
98 this.$selectedItem = item;
99 this.$label.text(this.$selectedItem.text());
102 setDefaultSelection: function() {
103 var selector = 'li[data-selected=true]:first';
104 var item = this.$element.find(selector);
105 if(item.length === 0) {
107 this.selectByIndex(0);
110 // select by data-attribute
111 this.selectBySelector(selector);
112 item.removeData('selected');
113 item.removeAttr('data-selected');
118 this.$button.removeClass('disabled');
121 disable: function() {
122 this.$button.addClass('disabled');
128 // SELECT PLUGIN DEFINITION
130 $.fn.select = function (option,value) {
133 var $set = this.each(function () {
135 var data = $this.data('select');
136 var options = typeof option === 'object' && option;
138 if (!data) $this.data('select', (data = new Select(this, options)));
139 if (typeof option === 'string') methodReturn = data[option](value);
142 return (methodReturn === undefined) ? $set : methodReturn;
145 $.fn.select.defaults = {};
147 $.fn.select.Constructor = Select;
154 $(window).on('load', function () {
155 $('.select').each(function () {
157 if ($this.data('select')) return;
158 $this.select($this.data());
162 $('body').on('mousedown.select.data-api', '.select', function (e) {
164 if ($this.data('select')) return;
165 $this.select($this.data());