-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmultiple-select-big.js
More file actions
252 lines (237 loc) · 10.8 KB
/
multiple-select-big.js
File metadata and controls
252 lines (237 loc) · 10.8 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
(function ($, window, document, undefined) {
function Selectmultiple(element, opts) {
this.options = $.extend({}, Selectmultiple.DEFAULTS, opts);
this.$element = $(element);
this.init();
this.$element.data('value', null);
this.$element.data('count', this.length);
this.$element.data('options', this.options.data);
}
var pluginName = 'selectmultiple';
// default options
Selectmultiple.DEFAULTS = {
data: [],
size: 100,
text: 'selectmultiple',
placeholder: 'search'
};
// draw the html widget and load the first options.size items
Selectmultiple.prototype = {
init: function () {
this.$element.empty(); // clear element content
this.optionPage = 1;
this.page = 1;
this.scrollAmount = 0;
// make the multipe select fit in the parent container is no width specified
this.length = this.options.data.length;
this.select = []; // array of boolean, size of data.length. Store wether an option is selected (true) or not
for (var i = 0; i < this.length; i++) {
this.select[i] = false;
}
// CREATE HTML COMPONENTS
this.$element.addClass('btn-group');
this.$element.addClass('show-tick');
this.$element.css('width', '100%');
this.$buttonsearch = $('<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">')
.css({'width': '100%', 'padding': '5px 6px 2px 6px'})
.appendTo(this.$element);
this.$buttontext = $('<span>')
.text(this.options.text)
.css({'width': '120px', 'display': 'inline-block', 'overflow': 'hidden', 'white-space': 'nowrap', 'text-overflow': 'ellipsis'})
.appendTo(this.$buttonsearch);
this.$buttoniconspan = $('<span class="caret"> ')
.css({'margin-left': '10px', 'margin-top': '-10px'})
.appendTo(this.$buttonsearch);
this.$menu = $('<div class="dropdown-menu" >')
.css({'width': '100%', 'padding': '10px'})
.appendTo(this.$element);
this.$input = $('<input type="text" class="input form-control" >')
.css({'margin-top': '6px', 'width': (this.width - 30) + 'px'})
.prop('placeholder', this.options.placeholder)
.appendTo(this.$menu);
this.$select = $('<select multiple size="15" >')
.css({'width': '100%', 'margin-top': '6px'})
.appendTo(this.$menu);
if (this.length > this.options.size) {
this.$buttonloadall = $('<button type="button" class="btn btn-default btn-xs btn-secondary">')
.css({'width': '100%', 'margin-top': '10px'})
.text('deselect all')
.appendTo(this.$menu);
// load all the list at once
this.$buttonloadall.off('click').on('click', $.proxy(function () {
this.deselectAll();
this.$buttontext.html(this.options.text);
}, this));
}
// LOAD ITEMS
this.load(this.options.size);
// EVENT LISTENER
var ctrlPressed = false;
$(window).keydown(function (evt) {
if (evt.which === 17) {
ctrlPressed = true;
}
}).keyup(function (evt) {
if (evt.which === 17) {
ctrlPressed = false;
}
});
// on text input, search for options mathcing the text entered
// use str.indexOf() for the moment, should use a regex? ==> regex is slower
this.$input.off('change paste keyup').on('change paste keyup', $.proxy(function () {
if (this.$input.val() === "") {
this.page = 1;
this.load(this.options.size);
} else {
this.page = 1;
this.optionPage = 1;
this.search(this.$input.val());
}
}, this));
// when values are selected, refill the this.select array to store new selected options
this.$select.off('change').on('change', $.proxy(function () {
var id = [];
var val = [];
var count = 0;
this.$select.children().filter(":selected").each(function (i, selected) {
id[i] = parseInt($(selected).prop('id'));
});
// if ctrl key is press, keep previous item in selection
if (ctrlPressed) {
for (var j = 0; j < this.length; j++) {
if (id.indexOf(j) !== -1) {
this.select[j] = true;
val.push(this.options.data[j]);
}
}
} else {
for (var j = 0; j < this.length; j++) {
if (id.indexOf(j) !== -1) {
this.select[j] = true;
val.push(this.options.data[j]);
} else {
this.select[j] = false;
}
}
}
for (var k = 0; k < this.length; k++) {
if (this.select[k]) {
count++;
}
}
val = val.length === 0 ? null : val;
this.$element.data('value', val);
this.$element.data('count', count);
this.$buttontext.html(val.join(";"));
this.$buttonsearch.trigger('multiple_select_change'); // custom event to detect when selected values changed
}, this));
// when select bottom is reached after scrolling, load size more items
// only do this if the search field is empty
this.$select.on('scroll', $.proxy(function () {
if ((this.$select.scrollTop() + this.$select.innerHeight() >= this.$select[0].scrollHeight) && (this.page * this.options.size < this.length)) {
this.scrollAmount = this.$select.scrollTop();
if (this.$input.val() === "") {
this.appendItems();
} else {
this.optionPage++;
this.search(this.$input.val());
}
}
}, this));
// when the menu is cliked, it doesn't hide
this.$menu.click(function (event) {
event.stopPropagation();
});
},
// load the first ln items in the select
load: function (ln) {
if (ln === this.length) {
this.page = (this.length / this.options.size) + 1;
} else {
this.page = 1;
}
ln = ln > this.length ? this.length : ln;
var html = '';
for (var i = 0; i < ln; i++) {
if (this.select[i]) {
html += '<option id="' + i + '" selected>' + this.options.data[i] + '</option>';
} else {
html += '<option id="' + i + '" >' + this.options.data[i] + '</option>';
}
}
this.$select.html(html);
},
// append options.size items to the select
appendItems: function () {
var html = '';
var start = this.page * this.options.size;
var end = (start + this.options.size) >= this.length ? this.length : (start + this.options.size);
for (var i = start; i < end; i++) {
if (this.select[i]) {
html += '<option id="' + i + '" selected>' + this.options.data[i] + '</option>';
} else {
html += '<option id="' + i + '" >' + this.options.data[i] + '</option>';
}
}
this.page++;
this.$select.append(html);
this.$select.scrollTop(this.scrollAmount);
},
// redraw the list to display only options matching user input
search: function (input) {
var html = '';
var size = this.optionPage * this.options.size;
var nb_opt = 0;
for (var i = 0; i < this.length; i++) {
if ((this.options.data[i].indexOf(input) !== -1) && (nb_opt <= size)) {
nb_opt++;
if (this.select[i]) {
html += '<option id="' + i + '" selected>' + this.options.data[i] + '</option>';
} else {
html += '<option id="' + i + '" >' + this.options.data[i] + '</option>';
}
}
}
this.$select.html(html);
this.$select.scrollTop(this.scrollAmount);
},
// select or deselect all according to bool value ( select all if true)
selectAll: function (bool) {
if (bool === null) {
bool = true;
}
for (var i = 0; i < this.length; i++) {
this.select[i] = bool;
}
this.load(this.options.size);
this.$element.data('count', 0);
this.$element.data('value', bool ? this.options.data : null);
this.$buttonsearch.trigger('multiple_select_change');
},
deselectAll: function () {
this.selectAll(false);
}
};
// eihter trigger a public fonction or call constructor
$.fn.selectmultiple = function (methodOrOptions) {
var args = arguments;
if (methodOrOptions === 'value') {
return this.data('value');
} else if (methodOrOptions === 'count') {
return this.data('count');
} else if (methodOrOptions === 'option') {
return this.data('options');
}
return this.each(function () {
if (typeof methodOrOptions === 'object') {
$.data(this, 'plugin_' + pluginName, new Selectmultiple(this, methodOrOptions));
} else if (Selectmultiple.prototype[methodOrOptions]) {
if (typeof $.data(this, 'plugin_' + pluginName) != 'undefined') {
$.data(this, 'plugin_' + pluginName)[methodOrOptions].apply($.data(this, 'plugin_' + pluginName), args);
}
} else {
$.error('Method ' + methodOrOptions + ' does not exist on multiselect');
}
});
};
})(jQuery, window, document);