-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-ddselect.js
More file actions
208 lines (171 loc) · 6.22 KB
/
Copy pathbootstrap-ddselect.js
File metadata and controls
208 lines (171 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* bootstrap-ddselect.js
* v1.0.1
*
* By Martin Eriksson
*/
(function ($) {
"use strict";
var DDSelect = (function () {
// Constructor
//
function DDSelect(element, options) {
var that = this;
// Keep references to our jQuery elements
//
this.$select = $(element);
this.$btnGroup = $(DDSelect.template);
// Apply our options
//
if (options.parentClass) {
this.$btnGroup.addClass(options.parentClass);
}
if (options.buttonClass) {
this.$btnGroup.find('.btn').addClass(options.buttonClass);
}
if (options.sizeClass) {
this.$btnGroup.find('.btn').addClass(options.sizeClass);
}
if (options.dropup) {
this.$btnGroup.addClass('dropup');
}
// Initialize our DropDown and hide the Select element.
//
this.$select.hide().after(this.$btnGroup);
// Disable click on the button
//
this.$btnGroup.find('.btn').on('click', function (e) {
e.preventDefault();
});
// Capture the change event of the select to update our display value
//
this.$select.on('change', function (e) {
that.$btnGroup.find('.btn').first().text($(this).find(':selected').text());
});
// Add the button group HTML
//
this.refresh();
}
DDSelect.prototype.update = function () {
var currentSelected = this.$select.find(':selected');
// Set the text of the DropDown as of the currently selected option text
//
this.$btnGroup.find('.btn .text').text(currentSelected.text());
// Check if the Select element is disabled
//
this.$btnGroup.find('.btn').toggleClass('disabled', this.$select.is(':disabled'));
};
DDSelect.prototype.refeshOptions = function () {
var that = this;
// Remove all Options from the DropDown
//
this.$btnGroup.find('.dropdown-menu').empty();
// Adds a single option to the DropDown
//
function addOption($option, $parent) {
var $link = $('<a href="" />').attr('data-value', $option.attr('value')).text($option.text()),
$listItem = $('<li />').append($link);
if ($option.is(':disabled')) {
$listItem.addClass('disabled');
$link.attr('tabindex', '-1').attr('href', '#');
} else {
$link.on('click', function (e) {
e.preventDefault();
$(this).parents('.btn-group').prev('select').val($(this).attr('data-value'));
that.refresh();
});
}
$parent.append($listItem);
}
// Adds an option group to the DropDown
//
function addOptionGroup($optgroup, $parent) {
var $submenu = $('<li class="dropdown-submenu"><a tabindex="-1" href="#">' + $optgroup.attr('label') + '</a><ul class="dropdown-menu"></ul></li>');
$optgroup.children('option').each(function () {
addOption($(this), $submenu.children('.dropdown-menu'));
});
$parent.append($submenu);
}
// Add all items to the DropDown
//
this.$select.children().each(function () {
var $this = $(this);
if ($this.is('optgroup')) {
addOptionGroup($this, that.$btnGroup.children('.dropdown-menu'));
} else if ($this.is('option')) {
addOption($this, that.$btnGroup.children('.dropdown-menu'));
}
});
};
DDSelect.prototype.refresh = function () {
this.update();
this.refeshOptions();
};
DDSelect.prototype.getApi = function () {
var that = this;
return {
refresh: function () {
that.refresh();
},
show: function () {
that.$btnGroup.show();
},
hide: function () {
that.$btnGroup.hide();
}
};
};
DDSelect.template = '<div class="btn-group">' +
'<button class="btn" data-toggle="dropdown"><span class="text" /></button>' +
'<button class="btn dropdown-toggle" data-toggle="dropdown">' +
'<span class="caret" />' +
'</button>' +
'<ul class="dropdown-menu" />' +
'</div>';
return DDSelect;
})();
// The jQuery Plugin API
//
$.fn.ddselect = function () {
var options = {},
method = '',
dataKey = 'ddselect';
// Parse the arguments
//
if (typeof arguments[0] === 'string') {
action = arguments[0];
} else {
options = arguments[0];
}
// Merge the options with the default ones
//
options = $.extend({}, $.fn.ddselect.defaultOptions, options);
// Return the jQuery chain
//
return this.each(function () {
var ddSelect = $(this).data(dataKey),
api = null;
if (ddSelect) {
api = ddSelect.getApi();
method = 'refresh';
}
if (method) {
if (api[method]) {
api[method]();
} else {
$.error('Method ' + method + ' does not exist on jQuery.dropdownselect');
}
} else {
$(this).data(dataKey, new DDSelect(this, options));
}
});
};
// The default options
//
$.fn.ddselect.defaultOptions = {
dropup: false,
parentClass: 'ddselect',
buttonClass: '',
sizeClass: ''
};
})(jQuery);