1
|
|
|
// Chosen, a Select Box Enhancer for jQuery and Prototype |
2
|
|
|
// by Patrick Filler for Harvest, http://getharvest.com |
3
|
|
|
// |
4
|
|
|
// Version 0.14.0 |
5
|
|
|
// Full source at https://github.com/harvesthq/chosen |
6
|
|
|
// Copyright (c) 2011 Harvest http://getharvest.com |
7
|
|
|
|
8
|
|
|
// MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md |
9
|
|
|
// This file is generated by `grunt build`, do not edit it by hand. |
10
|
|
|
|
11
|
|
|
// |
12
|
|
|
// Modified for Joomla! UI: |
13
|
|
|
// - fix zero width, based on https://github.com/harvesthq/chosen/pull/1439 |
14
|
|
|
// - allow to add a custom value on fly, based on https://github.com/harvesthq/chosen/pull/749 |
15
|
|
|
// |
16
|
|
|
|
17
|
|
|
(function() { |
18
|
|
|
var $, AbstractChosen, Chosen, SelectParser, _ref, |
19
|
|
|
__hasProp = {}.hasOwnProperty, |
20
|
|
|
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; |
|
|
|
|
21
|
|
|
|
22
|
|
|
SelectParser = (function() { |
23
|
|
|
function SelectParser() { |
24
|
|
|
this.options_index = 0; |
25
|
|
|
this.parsed = []; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
SelectParser.prototype.add_node = function(child) { |
29
|
|
|
if (child.nodeName.toUpperCase() === "OPTGROUP") { |
30
|
|
|
return this.add_group(child); |
31
|
|
|
} else { |
32
|
|
|
return this.add_option(child); |
33
|
|
|
} |
34
|
|
|
}; |
35
|
|
|
|
36
|
|
|
SelectParser.prototype.add_group = function(group) { |
37
|
|
|
var group_position, option, _i, _len, _ref, _results; |
38
|
|
|
|
39
|
|
|
group_position = this.parsed.length; |
40
|
|
|
this.parsed.push({ |
41
|
|
|
array_index: group_position, |
42
|
|
|
group: true, |
43
|
|
|
label: this.escapeExpression(group.label), |
44
|
|
|
children: 0, |
45
|
|
|
disabled: group.disabled |
46
|
|
|
}); |
47
|
|
|
_ref = group.childNodes; |
48
|
|
|
_results = []; |
49
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
50
|
|
|
option = _ref[_i]; |
51
|
|
|
_results.push(this.add_option(option, group_position, group.disabled)); |
52
|
|
|
} |
53
|
|
|
return _results; |
54
|
|
|
}; |
55
|
|
|
|
56
|
|
|
SelectParser.prototype.add_option = function(option, group_position, group_disabled) { |
57
|
|
|
if (option.nodeName.toUpperCase() === "OPTION") { |
|
|
|
|
58
|
|
|
if (option.text !== "") { |
59
|
|
|
if (group_position != null) { |
60
|
|
|
this.parsed[group_position].children += 1; |
61
|
|
|
} |
62
|
|
|
this.parsed.push({ |
63
|
|
|
array_index: this.parsed.length, |
64
|
|
|
options_index: this.options_index, |
65
|
|
|
value: option.value, |
66
|
|
|
text: option.text, |
67
|
|
|
html: option.innerHTML, |
68
|
|
|
selected: option.selected, |
69
|
|
|
disabled: group_disabled === true ? group_disabled : option.disabled, |
70
|
|
|
group_array_index: group_position, |
71
|
|
|
classes: option.className, |
72
|
|
|
style: option.style.cssText |
73
|
|
|
}); |
74
|
|
|
} else { |
75
|
|
|
this.parsed.push({ |
76
|
|
|
array_index: this.parsed.length, |
77
|
|
|
options_index: this.options_index, |
78
|
|
|
empty: true |
79
|
|
|
}); |
80
|
|
|
} |
81
|
|
|
return this.options_index += 1; |
82
|
|
|
} |
83
|
|
|
}; |
84
|
|
|
|
85
|
|
|
SelectParser.prototype.escapeExpression = function(text) { |
86
|
|
|
var map, unsafe_chars; |
87
|
|
|
|
88
|
|
|
if ((text == null) || text === false) { |
89
|
|
|
return ""; |
90
|
|
|
} |
91
|
|
|
if (!/[\&\<\>\"\'\`]/.test(text)) { |
92
|
|
|
return text; |
93
|
|
|
} |
94
|
|
|
map = { |
95
|
|
|
"<": "<", |
96
|
|
|
">": ">", |
97
|
|
|
'"': """, |
98
|
|
|
"'": "'", |
99
|
|
|
"`": "`" |
100
|
|
|
}; |
101
|
|
|
unsafe_chars = /&(?!\w+;)|[\<\>\"\'\`]/g; |
102
|
|
|
return text.replace(unsafe_chars, function(chr) { |
103
|
|
|
return map[chr] || "&"; |
104
|
|
|
}); |
105
|
|
|
}; |
106
|
|
|
|
107
|
|
|
return SelectParser; |
108
|
|
|
|
109
|
|
|
})(); |
110
|
|
|
|
111
|
|
|
SelectParser.select_to_array = function(select) { |
112
|
|
|
var child, parser, _i, _len, _ref; |
113
|
|
|
|
114
|
|
|
parser = new SelectParser(); |
115
|
|
|
_ref = select.childNodes; |
116
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
117
|
|
|
child = _ref[_i]; |
118
|
|
|
parser.add_node(child); |
119
|
|
|
} |
120
|
|
|
return parser.parsed; |
121
|
|
|
}; |
122
|
|
|
|
123
|
|
|
AbstractChosen = (function() { |
124
|
|
|
function AbstractChosen(form_field, options) { |
125
|
|
|
this.form_field = form_field; |
126
|
|
|
this.options = options != null ? options : {}; |
127
|
|
|
if (!AbstractChosen.browser_is_supported()) { |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
this.is_multiple = this.form_field.multiple; |
131
|
|
|
this.set_default_text(); |
132
|
|
|
this.set_default_values(); |
133
|
|
|
this.setup(); |
134
|
|
|
this.set_up_html(); |
135
|
|
|
this.register_observers(); |
136
|
|
|
this.finish_setup(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
AbstractChosen.prototype.set_default_values = function() { |
140
|
|
|
var _this = this; |
141
|
|
|
|
142
|
|
|
this.click_test_action = function(evt) { |
143
|
|
|
return _this.test_active_click(evt); |
144
|
|
|
}; |
145
|
|
|
this.activate_action = function(evt) { |
146
|
|
|
return _this.activate_field(evt); |
147
|
|
|
}; |
148
|
|
|
this.active_field = false; |
149
|
|
|
this.mouse_on_container = false; |
150
|
|
|
this.results_showing = false; |
151
|
|
|
this.result_highlighted = null; |
152
|
|
|
this.result_single_selected = null; |
153
|
|
|
/*<JUI>*/ |
154
|
|
|
/* Original: not exist */ |
155
|
|
|
this.allow_custom_value = false; |
156
|
|
|
/*</JUI>*/ |
157
|
|
|
this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false; |
158
|
|
|
this.disable_search_threshold = this.options.disable_search_threshold || 0; |
159
|
|
|
this.disable_search = this.options.disable_search || false; |
160
|
|
|
this.enable_split_word_search = this.options.enable_split_word_search != null ? this.options.enable_split_word_search : true; |
161
|
|
|
this.group_search = this.options.group_search != null ? this.options.group_search : true; |
162
|
|
|
this.search_contains = this.options.search_contains || false; |
163
|
|
|
this.single_backstroke_delete = this.options.single_backstroke_delete != null ? this.options.single_backstroke_delete : true; |
164
|
|
|
this.max_selected_options = this.options.max_selected_options || Infinity; |
165
|
|
|
this.inherit_select_classes = this.options.inherit_select_classes || false; |
166
|
|
|
this.display_selected_options = this.options.display_selected_options != null ? this.options.display_selected_options : true; |
167
|
|
|
return this.display_disabled_options = this.options.display_disabled_options != null ? this.options.display_disabled_options : true; |
168
|
|
|
}; |
169
|
|
|
|
170
|
|
|
AbstractChosen.prototype.set_default_text = function() { |
171
|
|
|
if (this.form_field.getAttribute("data-placeholder")) { |
172
|
|
|
this.default_text = this.form_field.getAttribute("data-placeholder"); |
173
|
|
|
} else if (this.is_multiple) { |
174
|
|
|
this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || AbstractChosen.default_multiple_text; |
175
|
|
|
} else { |
176
|
|
|
this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || AbstractChosen.default_single_text; |
177
|
|
|
} |
178
|
|
|
/*<JUI>*/ |
179
|
|
|
/* Original: not exist */ |
180
|
|
|
this.custom_group_text = this.form_field.getAttribute("data-custom_group_text") || this.options.custom_group_text || "Custom Value"; |
181
|
|
|
/*</JUI>*/ |
182
|
|
|
return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || AbstractChosen.default_no_result_text; |
183
|
|
|
}; |
184
|
|
|
|
185
|
|
|
AbstractChosen.prototype.mouse_enter = function() { |
186
|
|
|
return this.mouse_on_container = true; |
187
|
|
|
}; |
188
|
|
|
|
189
|
|
|
AbstractChosen.prototype.mouse_leave = function() { |
190
|
|
|
return this.mouse_on_container = false; |
191
|
|
|
}; |
192
|
|
|
|
193
|
|
|
AbstractChosen.prototype.input_focus = function(evt) { |
|
|
|
|
194
|
|
|
var _this = this; |
195
|
|
|
|
196
|
|
|
if (this.is_multiple) { |
197
|
|
|
if (!this.active_field) { |
|
|
|
|
198
|
|
|
return setTimeout((function() { |
199
|
|
|
return _this.container_mousedown(); |
200
|
|
|
}), 50); |
201
|
|
|
} |
202
|
|
|
} else { |
203
|
|
|
if (!this.active_field) { |
|
|
|
|
204
|
|
|
return this.activate_field(); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
}; |
208
|
|
|
|
209
|
|
|
AbstractChosen.prototype.input_blur = function(evt) { |
|
|
|
|
210
|
|
|
var _this = this; |
211
|
|
|
|
212
|
|
|
if (!this.mouse_on_container) { |
|
|
|
|
213
|
|
|
this.active_field = false; |
214
|
|
|
return setTimeout((function() { |
215
|
|
|
return _this.blur_test(); |
216
|
|
|
}), 100); |
217
|
|
|
} |
218
|
|
|
}; |
219
|
|
|
|
220
|
|
|
AbstractChosen.prototype.results_option_build = function(options) { |
221
|
|
|
var content, data, _i, _len, _ref; |
222
|
|
|
|
223
|
|
|
content = ''; |
224
|
|
|
_ref = this.results_data; |
225
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
226
|
|
|
data = _ref[_i]; |
227
|
|
|
if (data.group) { |
228
|
|
|
content += this.result_add_group(data); |
229
|
|
|
} else { |
230
|
|
|
content += this.result_add_option(data); |
231
|
|
|
} |
232
|
|
|
if (options != null ? options.first : void 0) { |
|
|
|
|
233
|
|
|
if (data.selected && this.is_multiple) { |
234
|
|
|
this.choice_build(data); |
235
|
|
|
} else if (data.selected && !this.is_multiple) { |
236
|
|
|
this.single_set_selected_text(data.text); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
return content; |
241
|
|
|
}; |
242
|
|
|
|
243
|
|
|
AbstractChosen.prototype.result_add_option = function(option) { |
244
|
|
|
var classes, style; |
245
|
|
|
|
246
|
|
|
if (!option.search_match) { |
247
|
|
|
return ''; |
248
|
|
|
} |
249
|
|
|
if (!this.include_option_in_results(option)) { |
250
|
|
|
return ''; |
251
|
|
|
} |
252
|
|
|
classes = []; |
253
|
|
|
if (!option.disabled && !(option.selected && this.is_multiple)) { |
254
|
|
|
classes.push("active-result"); |
255
|
|
|
} |
256
|
|
|
if (option.disabled && !(option.selected && this.is_multiple)) { |
257
|
|
|
classes.push("disabled-result"); |
258
|
|
|
} |
259
|
|
|
if (option.selected) { |
260
|
|
|
classes.push("result-selected"); |
261
|
|
|
} |
262
|
|
|
if (option.group_array_index != null) { |
263
|
|
|
classes.push("group-option"); |
264
|
|
|
} |
265
|
|
|
if (option.classes !== "") { |
266
|
|
|
classes.push(option.classes); |
267
|
|
|
} |
268
|
|
|
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; |
269
|
|
|
return "<li class=\"" + (classes.join(' ')) + "\"" + style + " data-option-array-index=\"" + option.array_index + "\">" + option.search_text + "</li>"; |
270
|
|
|
}; |
271
|
|
|
|
272
|
|
|
AbstractChosen.prototype.result_add_group = function(group) { |
273
|
|
|
if (!(group.search_match || group.group_match)) { |
274
|
|
|
return ''; |
275
|
|
|
} |
276
|
|
|
if (!(group.active_options > 0)) { |
277
|
|
|
return ''; |
278
|
|
|
} |
279
|
|
|
return "<li class=\"group-result\">" + group.search_text + "</li>"; |
280
|
|
|
}; |
281
|
|
|
|
282
|
|
|
AbstractChosen.prototype.results_update_field = function() { |
283
|
|
|
this.set_default_text(); |
284
|
|
|
if (!this.is_multiple) { |
285
|
|
|
this.results_reset_cleanup(); |
286
|
|
|
} |
287
|
|
|
this.result_clear_highlight(); |
288
|
|
|
this.result_single_selected = null; |
289
|
|
|
this.results_build(); |
290
|
|
|
if (this.results_showing) { |
|
|
|
|
291
|
|
|
return this.winnow_results(); |
292
|
|
|
} |
293
|
|
|
}; |
294
|
|
|
|
295
|
|
|
AbstractChosen.prototype.results_toggle = function() { |
296
|
|
|
if (this.results_showing) { |
297
|
|
|
return this.results_hide(); |
298
|
|
|
} else { |
299
|
|
|
return this.results_show(); |
300
|
|
|
} |
301
|
|
|
}; |
302
|
|
|
|
303
|
|
|
AbstractChosen.prototype.results_search = function(evt) { |
|
|
|
|
304
|
|
|
if (this.results_showing) { |
305
|
|
|
return this.winnow_results(); |
306
|
|
|
} else { |
307
|
|
|
return this.results_show(); |
308
|
|
|
} |
309
|
|
|
}; |
310
|
|
|
|
311
|
|
|
AbstractChosen.prototype.winnow_results = function() { |
312
|
|
|
var escapedSearchText, option, regex, regexAnchor, results, results_group, searchText, startpos, text, zregex, _i, _len, _ref; |
313
|
|
|
|
314
|
|
|
this.no_results_clear(); |
315
|
|
|
results = 0; |
316
|
|
|
searchText = this.get_search_text(); |
317
|
|
|
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); |
318
|
|
|
regexAnchor = this.search_contains ? "" : "^"; |
319
|
|
|
regex = new RegExp(regexAnchor + escapedSearchText, 'i'); |
320
|
|
|
zregex = new RegExp(escapedSearchText, 'i'); |
321
|
|
|
_ref = this.results_data; |
322
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
323
|
|
|
option = _ref[_i]; |
324
|
|
|
option.search_match = false; |
325
|
|
|
results_group = null; |
326
|
|
|
if (this.include_option_in_results(option)) { |
327
|
|
|
if (option.group) { |
328
|
|
|
option.group_match = false; |
329
|
|
|
option.active_options = 0; |
330
|
|
|
} |
331
|
|
|
if ((option.group_array_index != null) && this.results_data[option.group_array_index]) { |
332
|
|
|
results_group = this.results_data[option.group_array_index]; |
333
|
|
|
if (results_group.active_options === 0 && results_group.search_match) { |
334
|
|
|
results += 1; |
335
|
|
|
} |
336
|
|
|
results_group.active_options += 1; |
337
|
|
|
} |
338
|
|
|
if (!(option.group && !this.group_search)) { |
339
|
|
|
option.search_text = option.group ? option.label : option.html; |
340
|
|
|
option.search_match = this.search_string_match(option.search_text, regex); |
341
|
|
|
if (option.search_match && !option.group) { |
342
|
|
|
results += 1; |
343
|
|
|
} |
344
|
|
|
if (option.search_match) { |
345
|
|
|
if (searchText.length) { |
346
|
|
|
startpos = option.search_text.search(zregex); |
347
|
|
|
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length); |
348
|
|
|
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos); |
349
|
|
|
} |
350
|
|
|
if (results_group != null) { |
351
|
|
|
results_group.group_match = true; |
352
|
|
|
} |
353
|
|
|
} else if ((option.group_array_index != null) && this.results_data[option.group_array_index].search_match) { |
354
|
|
|
option.search_match = true; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
this.result_clear_highlight(); |
360
|
|
|
if (results < 1 && searchText.length) { |
361
|
|
|
this.update_results_content(""); |
362
|
|
|
return this.no_results(searchText); |
363
|
|
|
} else { |
364
|
|
|
this.update_results_content(this.results_option_build()); |
365
|
|
|
return this.winnow_results_set_highlight(); |
366
|
|
|
} |
367
|
|
|
}; |
368
|
|
|
|
369
|
|
|
AbstractChosen.prototype.search_string_match = function(search_string, regex) { |
370
|
|
|
var part, parts, _i, _len; |
371
|
|
|
|
372
|
|
|
if (regex.test(search_string)) { |
373
|
|
|
return true; |
374
|
|
|
} else if (this.enable_split_word_search && (search_string.indexOf(" ") >= 0 || search_string.indexOf("[") === 0)) { |
|
|
|
|
375
|
|
|
parts = search_string.replace(/\[|\]/g, "").split(" "); |
376
|
|
|
if (parts.length) { |
|
|
|
|
377
|
|
|
for (_i = 0, _len = parts.length; _i < _len; _i++) { |
378
|
|
|
part = parts[_i]; |
379
|
|
|
if (regex.test(part)) { |
380
|
|
|
return true; |
381
|
|
|
} |
382
|
|
|
} |
|
|
|
|
383
|
|
|
} |
384
|
|
|
} |
385
|
|
|
}; |
386
|
|
|
|
387
|
|
|
AbstractChosen.prototype.choices_count = function() { |
388
|
|
|
var option, _i, _len, _ref; |
389
|
|
|
|
390
|
|
|
if (this.selected_option_count != null) { |
391
|
|
|
return this.selected_option_count; |
392
|
|
|
} |
393
|
|
|
this.selected_option_count = 0; |
394
|
|
|
_ref = this.form_field.options; |
395
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
396
|
|
|
option = _ref[_i]; |
397
|
|
|
if (option.selected) { |
398
|
|
|
this.selected_option_count += 1; |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
return this.selected_option_count; |
402
|
|
|
}; |
403
|
|
|
|
404
|
|
|
AbstractChosen.prototype.choices_click = function(evt) { |
405
|
|
|
evt.preventDefault(); |
406
|
|
|
if (!(this.results_showing || this.is_disabled)) { |
|
|
|
|
407
|
|
|
return this.results_show(); |
408
|
|
|
} |
409
|
|
|
}; |
410
|
|
|
|
411
|
|
|
AbstractChosen.prototype.keyup_checker = function(evt) { |
412
|
|
|
var stroke, _ref; |
413
|
|
|
|
414
|
|
|
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; |
415
|
|
|
this.search_field_scale(); |
416
|
|
|
switch (stroke) { |
417
|
|
|
case 8: |
418
|
|
|
if (this.is_multiple && this.backstroke_length < 1 && this.choices_count() > 0) { |
419
|
|
|
return this.keydown_backstroke(); |
420
|
|
|
} else if (!this.pending_backstroke) { |
421
|
|
|
this.result_clear_highlight(); |
422
|
|
|
return this.results_search(); |
423
|
|
|
} |
424
|
|
|
break; |
|
|
|
|
425
|
|
|
case 13: |
426
|
|
|
evt.preventDefault(); |
427
|
|
|
if (this.results_showing) { |
428
|
|
|
return this.result_select(evt); |
429
|
|
|
} |
430
|
|
|
break; |
|
|
|
|
431
|
|
|
case 27: |
432
|
|
|
if (this.results_showing) { |
433
|
|
|
this.results_hide(); |
434
|
|
|
} |
435
|
|
|
return true; |
436
|
|
|
case 9: |
437
|
|
|
case 38: |
438
|
|
|
case 40: |
439
|
|
|
case 16: |
440
|
|
|
case 91: |
441
|
|
|
case 17: |
442
|
|
|
break; |
|
|
|
|
443
|
|
|
default: |
444
|
|
|
return this.results_search(); |
445
|
|
|
} |
446
|
|
|
}; |
447
|
|
|
|
448
|
|
|
AbstractChosen.prototype.container_width = function() { |
449
|
|
|
if (this.options.width != null) { |
450
|
|
|
return this.options.width; |
451
|
|
|
} else { |
452
|
|
|
/*<JUI>*/ |
453
|
|
|
/* Original: |
454
|
|
|
return "" + this.form_field.offsetWidth + "px"; |
455
|
|
|
*/ |
456
|
|
|
return this.form_field_jq.css("width") || "" + this.form_field.offsetWidth + "px"; |
457
|
|
|
/*</JUI>*/ |
458
|
|
|
} |
459
|
|
|
}; |
460
|
|
|
|
461
|
|
|
AbstractChosen.prototype.include_option_in_results = function(option) { |
462
|
|
|
if (this.is_multiple && (!this.display_selected_options && option.selected)) { |
463
|
|
|
return false; |
464
|
|
|
} |
465
|
|
|
if (!this.display_disabled_options && option.disabled) { |
466
|
|
|
return false; |
467
|
|
|
} |
468
|
|
|
if (option.empty) { |
469
|
|
|
return false; |
470
|
|
|
} |
471
|
|
|
return true; |
472
|
|
|
}; |
473
|
|
|
|
474
|
|
|
AbstractChosen.browser_is_supported = function() { |
475
|
|
|
if (window.navigator.appName === "Microsoft Internet Explorer") { |
476
|
|
|
return document.documentMode >= 8; |
477
|
|
|
} |
478
|
|
|
if (/iP(od|hone)/i.test(window.navigator.userAgent)) { |
479
|
|
|
return false; |
480
|
|
|
} |
481
|
|
|
if (/Android/i.test(window.navigator.userAgent)) { |
482
|
|
|
if (/Mobile/i.test(window.navigator.userAgent)) { |
483
|
|
|
return false; |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
return true; |
487
|
|
|
}; |
488
|
|
|
|
489
|
|
|
AbstractChosen.default_multiple_text = "Select Some Options"; |
490
|
|
|
|
491
|
|
|
AbstractChosen.default_single_text = "Select an Option"; |
492
|
|
|
|
493
|
|
|
AbstractChosen.default_no_result_text = "No results match"; |
494
|
|
|
|
495
|
|
|
return AbstractChosen; |
496
|
|
|
|
497
|
|
|
})(); |
498
|
|
|
|
499
|
|
|
$ = jQuery; |
500
|
|
|
|
501
|
|
|
$.fn.extend({ |
502
|
|
|
chosen: function(options) { |
503
|
|
|
if (!AbstractChosen.browser_is_supported()) { |
504
|
|
|
return this; |
505
|
|
|
} |
506
|
|
|
return this.each(function(input_field) { |
|
|
|
|
507
|
|
|
var $this, chosen; |
508
|
|
|
|
509
|
|
|
$this = $(this); |
510
|
|
|
chosen = $this.data('chosen'); |
511
|
|
|
if (options === 'destroy' && chosen) { |
512
|
|
|
chosen.destroy(); |
513
|
|
|
} else if (!chosen) { |
514
|
|
|
$this.data('chosen', new Chosen(this, options)); |
515
|
|
|
} |
516
|
|
|
}); |
517
|
|
|
} |
518
|
|
|
}); |
519
|
|
|
|
520
|
|
|
Chosen = (function(_super) { |
521
|
|
|
__extends(Chosen, _super); |
522
|
|
|
|
523
|
|
|
function Chosen() { |
524
|
|
|
_ref = Chosen.__super__.constructor.apply(this, arguments); |
525
|
|
|
return _ref; |
526
|
|
|
} |
527
|
|
|
|
528
|
|
|
Chosen.prototype.setup = function() { |
529
|
|
|
this.form_field_jq = $(this.form_field); |
530
|
|
|
this.current_selectedIndex = this.form_field.selectedIndex; |
531
|
|
|
/*<JUI>*/ |
532
|
|
|
/* Original: not exist */ |
533
|
|
|
this.allow_custom_value = this.form_field_jq.hasClass("chzn-custom-value") || this.options.allow_custom_value; |
534
|
|
|
/*</JUI>*/ |
535
|
|
|
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); |
536
|
|
|
}; |
537
|
|
|
|
538
|
|
|
Chosen.prototype.finish_setup = function() { |
539
|
|
|
return this.form_field_jq.addClass("chzn-done"); |
540
|
|
|
}; |
541
|
|
|
|
542
|
|
|
Chosen.prototype.set_up_html = function() { |
543
|
|
|
var container_classes, container_props; |
544
|
|
|
|
545
|
|
|
container_classes = ["chzn-container"]; |
546
|
|
|
container_classes.push("chzn-container-" + (this.is_multiple ? "multi" : "single")); |
547
|
|
|
if (this.inherit_select_classes && this.form_field.className) { |
548
|
|
|
container_classes.push(this.form_field.className); |
549
|
|
|
} |
550
|
|
|
if (this.is_rtl) { |
551
|
|
|
container_classes.push("chzn-rtl"); |
552
|
|
|
} |
553
|
|
|
container_props = { |
554
|
|
|
'class': container_classes.join(' '), |
555
|
|
|
'style': "width: " + (this.container_width()) + ";", |
556
|
|
|
'title': this.form_field.title |
557
|
|
|
}; |
558
|
|
|
if (this.form_field.id.length) { |
559
|
|
|
container_props.id = this.form_field.id.replace(/[^\w]/g, '_') + "_chzn"; |
560
|
|
|
} |
561
|
|
|
this.container = $("<div />", container_props); |
562
|
|
|
if (this.is_multiple) { |
563
|
|
|
this.container.html('<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + this.default_text + '" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop"><ul class="chzn-results"></ul></div>'); |
564
|
|
|
} else { |
565
|
|
|
this.container.html('<a class="chzn-single chzn-default" tabindex="-1"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>'); |
566
|
|
|
} |
567
|
|
|
this.form_field_jq.hide().after(this.container); |
568
|
|
|
this.dropdown = this.container.find('div.chzn-drop').first(); |
569
|
|
|
this.search_field = this.container.find('input').first(); |
570
|
|
|
this.search_results = this.container.find('ul.chzn-results').first(); |
571
|
|
|
this.search_field_scale(); |
572
|
|
|
this.search_no_results = this.container.find('li.no-results').first(); |
573
|
|
|
if (this.is_multiple) { |
574
|
|
|
this.search_choices = this.container.find('ul.chzn-choices').first(); |
575
|
|
|
this.search_container = this.container.find('li.search-field').first(); |
576
|
|
|
} else { |
577
|
|
|
this.search_container = this.container.find('div.chzn-search').first(); |
578
|
|
|
this.selected_item = this.container.find('.chzn-single').first(); |
579
|
|
|
} |
580
|
|
|
this.results_build(); |
581
|
|
|
this.set_tab_index(); |
582
|
|
|
this.set_label_behavior(); |
583
|
|
|
return this.form_field_jq.trigger("liszt:ready", { |
584
|
|
|
chosen: this |
585
|
|
|
}); |
586
|
|
|
}; |
587
|
|
|
|
588
|
|
|
Chosen.prototype.register_observers = function() { |
589
|
|
|
var _this = this; |
590
|
|
|
|
591
|
|
|
this.container.bind('mousedown.chosen', function(evt) { |
592
|
|
|
_this.container_mousedown(evt); |
593
|
|
|
}); |
594
|
|
|
this.container.bind('mouseup.chosen', function(evt) { |
595
|
|
|
_this.container_mouseup(evt); |
596
|
|
|
}); |
597
|
|
|
this.container.bind('mouseenter.chosen', function(evt) { |
598
|
|
|
_this.mouse_enter(evt); |
599
|
|
|
}); |
600
|
|
|
this.container.bind('mouseleave.chosen', function(evt) { |
601
|
|
|
_this.mouse_leave(evt); |
602
|
|
|
}); |
603
|
|
|
this.search_results.bind('mouseup.chosen', function(evt) { |
604
|
|
|
_this.search_results_mouseup(evt); |
605
|
|
|
}); |
606
|
|
|
this.search_results.bind('mouseover.chosen', function(evt) { |
607
|
|
|
_this.search_results_mouseover(evt); |
608
|
|
|
}); |
609
|
|
|
this.search_results.bind('mouseout.chosen', function(evt) { |
610
|
|
|
_this.search_results_mouseout(evt); |
611
|
|
|
}); |
612
|
|
|
this.search_results.bind('mousewheel.chosen DOMMouseScroll.chosen', function(evt) { |
613
|
|
|
_this.search_results_mousewheel(evt); |
614
|
|
|
}); |
615
|
|
|
this.form_field_jq.bind("liszt:updated.chosen", function(evt) { |
616
|
|
|
_this.results_update_field(evt); |
617
|
|
|
}); |
618
|
|
|
this.form_field_jq.bind("liszt:activate.chosen", function(evt) { |
619
|
|
|
_this.activate_field(evt); |
620
|
|
|
}); |
621
|
|
|
this.form_field_jq.bind("liszt:open.chosen", function(evt) { |
622
|
|
|
_this.container_mousedown(evt); |
623
|
|
|
}); |
624
|
|
|
this.search_field.bind('blur.chosen', function(evt) { |
625
|
|
|
_this.input_blur(evt); |
626
|
|
|
}); |
627
|
|
|
this.search_field.bind('keyup.chosen', function(evt) { |
628
|
|
|
_this.keyup_checker(evt); |
629
|
|
|
}); |
630
|
|
|
this.search_field.bind('keydown.chosen', function(evt) { |
631
|
|
|
_this.keydown_checker(evt); |
632
|
|
|
}); |
633
|
|
|
this.search_field.bind('focus.chosen', function(evt) { |
634
|
|
|
_this.input_focus(evt); |
635
|
|
|
}); |
636
|
|
|
if (this.is_multiple) { |
637
|
|
|
return this.search_choices.bind('click.chosen', function(evt) { |
638
|
|
|
_this.choices_click(evt); |
639
|
|
|
}); |
640
|
|
|
} else { |
641
|
|
|
return this.container.bind('click.chosen', function(evt) { |
642
|
|
|
evt.preventDefault(); |
643
|
|
|
}); |
644
|
|
|
} |
645
|
|
|
}; |
646
|
|
|
|
647
|
|
|
Chosen.prototype.destroy = function() { |
648
|
|
|
$(document).unbind("click.chosen", this.click_test_action); |
649
|
|
|
if (this.search_field[0].tabIndex) { |
650
|
|
|
this.form_field_jq[0].tabIndex = this.search_field[0].tabIndex; |
651
|
|
|
} |
652
|
|
|
this.container.remove(); |
653
|
|
|
this.form_field_jq.removeData('chosen'); |
654
|
|
|
return this.form_field_jq.show(); |
655
|
|
|
}; |
656
|
|
|
|
657
|
|
|
Chosen.prototype.search_field_disabled = function() { |
658
|
|
|
this.is_disabled = this.form_field_jq[0].disabled; |
659
|
|
|
if (this.is_disabled) { |
660
|
|
|
this.container.addClass('chzn-disabled'); |
661
|
|
|
this.search_field[0].disabled = true; |
662
|
|
|
if (!this.is_multiple) { |
663
|
|
|
this.selected_item.unbind("focus.chosen", this.activate_action); |
664
|
|
|
} |
665
|
|
|
return this.close_field(); |
666
|
|
|
} else { |
667
|
|
|
this.container.removeClass('chzn-disabled'); |
668
|
|
|
this.search_field[0].disabled = false; |
669
|
|
|
if (!this.is_multiple) { |
|
|
|
|
670
|
|
|
return this.selected_item.bind("focus.chosen", this.activate_action); |
671
|
|
|
} |
672
|
|
|
} |
673
|
|
|
}; |
674
|
|
|
|
675
|
|
|
Chosen.prototype.container_mousedown = function(evt) { |
676
|
|
|
if (!this.is_disabled) { |
|
|
|
|
677
|
|
|
if (evt && evt.type === "mousedown" && !this.results_showing) { |
678
|
|
|
evt.preventDefault(); |
679
|
|
|
} |
680
|
|
|
if (!((evt != null) && ($(evt.target)).hasClass("search-choice-close"))) { |
|
|
|
|
681
|
|
|
if (!this.active_field) { |
682
|
|
|
if (this.is_multiple) { |
683
|
|
|
this.search_field.val(""); |
684
|
|
|
} |
685
|
|
|
$(document).bind('click.chosen', this.click_test_action); |
686
|
|
|
this.results_show(); |
687
|
|
|
} else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) { |
688
|
|
|
evt.preventDefault(); |
689
|
|
|
this.results_toggle(); |
690
|
|
|
} |
691
|
|
|
return this.activate_field(); |
692
|
|
|
} |
693
|
|
|
} |
694
|
|
|
}; |
695
|
|
|
|
696
|
|
|
Chosen.prototype.container_mouseup = function(evt) { |
697
|
|
|
if (evt.target.nodeName === "ABBR" && !this.is_disabled) { |
|
|
|
|
698
|
|
|
return this.results_reset(evt); |
699
|
|
|
} |
700
|
|
|
}; |
701
|
|
|
|
702
|
|
|
Chosen.prototype.search_results_mousewheel = function(evt) { |
703
|
|
|
var delta, _ref1, _ref2; |
704
|
|
|
|
705
|
|
|
delta = -((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || ((_ref2 = evt.originialEvent) != null ? _ref2.detail : void 0); |
|
|
|
|
706
|
|
|
if (delta != null) { |
|
|
|
|
707
|
|
|
evt.preventDefault(); |
708
|
|
|
if (evt.type === 'DOMMouseScroll') { |
709
|
|
|
delta = delta * 40; |
710
|
|
|
} |
711
|
|
|
return this.search_results.scrollTop(delta + this.search_results.scrollTop()); |
712
|
|
|
} |
713
|
|
|
}; |
714
|
|
|
|
715
|
|
|
Chosen.prototype.blur_test = function(evt) { |
|
|
|
|
716
|
|
|
if (!this.active_field && this.container.hasClass("chzn-container-active")) { |
|
|
|
|
717
|
|
|
return this.close_field(); |
718
|
|
|
} |
719
|
|
|
}; |
720
|
|
|
|
721
|
|
|
Chosen.prototype.close_field = function() { |
722
|
|
|
$(document).unbind("click.chosen", this.click_test_action); |
723
|
|
|
this.active_field = false; |
724
|
|
|
this.results_hide(); |
725
|
|
|
this.container.removeClass("chzn-container-active"); |
726
|
|
|
this.clear_backstroke(); |
727
|
|
|
this.show_search_field_default(); |
728
|
|
|
return this.search_field_scale(); |
729
|
|
|
}; |
730
|
|
|
|
731
|
|
|
Chosen.prototype.activate_field = function() { |
732
|
|
|
this.container.addClass("chzn-container-active"); |
733
|
|
|
this.active_field = true; |
734
|
|
|
this.search_field.val(this.search_field.val()); |
735
|
|
|
return this.search_field.focus(); |
736
|
|
|
}; |
737
|
|
|
|
738
|
|
|
Chosen.prototype.test_active_click = function(evt) { |
739
|
|
|
if (this.container.is($(evt.target).closest('.chzn-container'))) { |
740
|
|
|
return this.active_field = true; |
741
|
|
|
} else { |
742
|
|
|
return this.close_field(); |
743
|
|
|
} |
744
|
|
|
}; |
745
|
|
|
|
746
|
|
|
Chosen.prototype.results_build = function() { |
747
|
|
|
this.parsing = true; |
748
|
|
|
this.selected_option_count = null; |
749
|
|
|
this.results_data = SelectParser.select_to_array(this.form_field); |
750
|
|
|
if (this.is_multiple) { |
751
|
|
|
this.search_choices.find("li.search-choice").remove(); |
752
|
|
|
} else if (!this.is_multiple) { |
753
|
|
|
this.single_set_selected_text(); |
754
|
|
|
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) { |
755
|
|
|
this.search_field[0].readOnly = true; |
756
|
|
|
this.container.addClass("chzn-container-single-nosearch"); |
757
|
|
|
} else { |
758
|
|
|
this.search_field[0].readOnly = false; |
759
|
|
|
this.container.removeClass("chzn-container-single-nosearch"); |
760
|
|
|
} |
761
|
|
|
} |
762
|
|
|
this.update_results_content(this.results_option_build({ |
763
|
|
|
first: true |
764
|
|
|
})); |
765
|
|
|
this.search_field_disabled(); |
766
|
|
|
this.show_search_field_default(); |
767
|
|
|
this.search_field_scale(); |
768
|
|
|
return this.parsing = false; |
769
|
|
|
}; |
770
|
|
|
|
771
|
|
|
Chosen.prototype.result_do_highlight = function(el) { |
772
|
|
|
var high_bottom, high_top, maxHeight, visible_bottom, visible_top; |
773
|
|
|
|
774
|
|
|
if (el.length) { |
|
|
|
|
775
|
|
|
this.result_clear_highlight(); |
776
|
|
|
this.result_highlight = el; |
777
|
|
|
this.result_highlight.addClass("highlighted"); |
778
|
|
|
maxHeight = parseInt(this.search_results.css("maxHeight"), 10); |
779
|
|
|
visible_top = this.search_results.scrollTop(); |
780
|
|
|
visible_bottom = maxHeight + visible_top; |
781
|
|
|
high_top = this.result_highlight.position().top + this.search_results.scrollTop(); |
782
|
|
|
high_bottom = high_top + this.result_highlight.outerHeight(); |
783
|
|
|
if (high_bottom >= visible_bottom) { |
784
|
|
|
return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0); |
785
|
|
|
} else if (high_top < visible_top) { |
|
|
|
|
786
|
|
|
return this.search_results.scrollTop(high_top); |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
}; |
790
|
|
|
|
791
|
|
|
Chosen.prototype.result_clear_highlight = function() { |
792
|
|
|
if (this.result_highlight) { |
793
|
|
|
this.result_highlight.removeClass("highlighted"); |
794
|
|
|
} |
795
|
|
|
return this.result_highlight = null; |
796
|
|
|
}; |
797
|
|
|
|
798
|
|
|
Chosen.prototype.results_show = function() { |
799
|
|
|
if (this.is_multiple && this.max_selected_options <= this.choices_count()) { |
800
|
|
|
this.form_field_jq.trigger("liszt:maxselected", { |
801
|
|
|
chosen: this |
802
|
|
|
}); |
803
|
|
|
return false; |
804
|
|
|
} |
805
|
|
|
this.container.addClass("chzn-with-drop"); |
806
|
|
|
this.form_field_jq.trigger("liszt:showing_dropdown", { |
807
|
|
|
chosen: this |
808
|
|
|
}); |
809
|
|
|
this.results_showing = true; |
810
|
|
|
this.search_field.focus(); |
811
|
|
|
this.search_field.val(this.search_field.val()); |
812
|
|
|
return this.winnow_results(); |
813
|
|
|
}; |
814
|
|
|
|
815
|
|
|
Chosen.prototype.update_results_content = function(content) { |
816
|
|
|
return this.search_results.html(content); |
817
|
|
|
}; |
818
|
|
|
|
819
|
|
|
Chosen.prototype.results_hide = function() { |
820
|
|
|
if (this.results_showing) { |
821
|
|
|
this.result_clear_highlight(); |
822
|
|
|
this.container.removeClass("chzn-with-drop"); |
823
|
|
|
this.form_field_jq.trigger("liszt:hiding_dropdown", { |
824
|
|
|
chosen: this |
825
|
|
|
}); |
826
|
|
|
} |
827
|
|
|
return this.results_showing = false; |
828
|
|
|
}; |
829
|
|
|
|
830
|
|
|
Chosen.prototype.set_tab_index = function(el) { |
|
|
|
|
831
|
|
|
var ti; |
832
|
|
|
|
833
|
|
|
if (this.form_field.tabIndex) { |
|
|
|
|
834
|
|
|
ti = this.form_field.tabIndex; |
835
|
|
|
this.form_field.tabIndex = -1; |
836
|
|
|
return this.search_field[0].tabIndex = ti; |
837
|
|
|
} |
838
|
|
|
}; |
839
|
|
|
|
840
|
|
|
Chosen.prototype.set_label_behavior = function() { |
841
|
|
|
var _this = this; |
842
|
|
|
|
843
|
|
|
this.form_field_label = this.form_field_jq.parents("label"); |
844
|
|
|
if (!this.form_field_label.length && this.form_field.id.length) { |
845
|
|
|
this.form_field_label = $("label[for='" + this.form_field.id + "']"); |
846
|
|
|
} |
847
|
|
|
if (this.form_field_label.length > 0) { |
|
|
|
|
848
|
|
|
return this.form_field_label.bind('click.chosen', function(evt) { |
849
|
|
|
if (_this.is_multiple) { |
850
|
|
|
return _this.container_mousedown(evt); |
851
|
|
|
} else { |
852
|
|
|
return _this.activate_field(); |
853
|
|
|
} |
854
|
|
|
}); |
855
|
|
|
} |
856
|
|
|
}; |
857
|
|
|
|
858
|
|
|
Chosen.prototype.show_search_field_default = function() { |
859
|
|
|
if (this.is_multiple && this.choices_count() < 1 && !this.active_field) { |
860
|
|
|
this.search_field.val(this.default_text); |
861
|
|
|
return this.search_field.addClass("default"); |
862
|
|
|
} else { |
863
|
|
|
this.search_field.val(""); |
864
|
|
|
return this.search_field.removeClass("default"); |
865
|
|
|
} |
866
|
|
|
}; |
867
|
|
|
|
868
|
|
|
Chosen.prototype.search_results_mouseup = function(evt) { |
869
|
|
|
var target; |
870
|
|
|
|
871
|
|
|
target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
872
|
|
|
if (target.length) { |
|
|
|
|
873
|
|
|
this.result_highlight = target; |
874
|
|
|
this.result_select(evt); |
875
|
|
|
return this.search_field.focus(); |
876
|
|
|
} |
877
|
|
|
}; |
878
|
|
|
|
879
|
|
|
Chosen.prototype.search_results_mouseover = function(evt) { |
880
|
|
|
var target; |
881
|
|
|
|
882
|
|
|
target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
883
|
|
|
if (target) { |
|
|
|
|
884
|
|
|
return this.result_do_highlight(target); |
885
|
|
|
} |
886
|
|
|
}; |
887
|
|
|
|
888
|
|
|
Chosen.prototype.search_results_mouseout = function(evt) { |
889
|
|
|
if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) { |
|
|
|
|
890
|
|
|
return this.result_clear_highlight(); |
891
|
|
|
} |
892
|
|
|
}; |
893
|
|
|
|
894
|
|
|
Chosen.prototype.choice_build = function(item) { |
895
|
|
|
var choice, close_link, |
896
|
|
|
_this = this; |
897
|
|
|
|
898
|
|
|
choice = $('<li />', { |
899
|
|
|
"class": "search-choice" |
900
|
|
|
}).html("<span>" + item.html + "</span>"); |
901
|
|
|
if (item.disabled) { |
902
|
|
|
choice.addClass('search-choice-disabled'); |
903
|
|
|
} else { |
904
|
|
|
close_link = $('<a />', { |
905
|
|
|
"class": 'search-choice-close', |
906
|
|
|
'data-option-array-index': item.array_index |
907
|
|
|
}); |
908
|
|
|
close_link.bind('click.chosen', function(evt) { |
909
|
|
|
return _this.choice_destroy_link_click(evt); |
910
|
|
|
}); |
911
|
|
|
choice.append(close_link); |
912
|
|
|
} |
913
|
|
|
return this.search_container.before(choice); |
914
|
|
|
}; |
915
|
|
|
|
916
|
|
|
Chosen.prototype.choice_destroy_link_click = function(evt) { |
917
|
|
|
evt.preventDefault(); |
918
|
|
|
evt.stopPropagation(); |
919
|
|
|
if (!this.is_disabled) { |
|
|
|
|
920
|
|
|
return this.choice_destroy($(evt.target)); |
921
|
|
|
} |
922
|
|
|
}; |
923
|
|
|
|
924
|
|
|
Chosen.prototype.choice_destroy = function(link) { |
925
|
|
|
if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) { |
|
|
|
|
926
|
|
|
this.show_search_field_default(); |
927
|
|
|
if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) { |
928
|
|
|
this.results_hide(); |
929
|
|
|
} |
930
|
|
|
link.parents('li').first().remove(); |
931
|
|
|
return this.search_field_scale(); |
932
|
|
|
} |
933
|
|
|
}; |
934
|
|
|
|
935
|
|
|
Chosen.prototype.results_reset = function() { |
936
|
|
|
this.form_field.options[0].selected = true; |
937
|
|
|
this.selected_option_count = null; |
938
|
|
|
this.single_set_selected_text(); |
939
|
|
|
this.show_search_field_default(); |
940
|
|
|
this.results_reset_cleanup(); |
941
|
|
|
this.form_field_jq.trigger("change"); |
942
|
|
|
if (this.active_field) { |
|
|
|
|
943
|
|
|
return this.results_hide(); |
944
|
|
|
} |
945
|
|
|
}; |
946
|
|
|
|
947
|
|
|
Chosen.prototype.results_reset_cleanup = function() { |
948
|
|
|
this.current_selectedIndex = this.form_field.selectedIndex; |
949
|
|
|
return this.selected_item.find("abbr").remove(); |
950
|
|
|
}; |
951
|
|
|
|
952
|
|
|
Chosen.prototype.result_select = function(evt) { |
953
|
|
|
/*<JUI>*/ |
954
|
|
|
/* Original: |
955
|
|
|
var high, item, selected_index; |
956
|
|
|
*/ |
957
|
|
|
var group, high, high_id, item, option, position, value; |
|
|
|
|
958
|
|
|
/*</JUI>*/ |
959
|
|
|
|
960
|
|
|
if (this.result_highlight) { |
961
|
|
|
high = this.result_highlight; |
962
|
|
|
this.result_clear_highlight(); |
963
|
|
|
if (this.is_multiple && this.max_selected_options <= this.choices_count()) { |
964
|
|
|
this.form_field_jq.trigger("liszt:maxselected", { |
965
|
|
|
chosen: this |
966
|
|
|
}); |
967
|
|
|
return false; |
968
|
|
|
} |
969
|
|
|
if (this.is_multiple) { |
970
|
|
|
high.removeClass("active-result"); |
971
|
|
|
} else { |
972
|
|
|
if (this.result_single_selected) { |
973
|
|
|
this.result_single_selected.removeClass("result-selected"); |
974
|
|
|
selected_index = this.result_single_selected[0].getAttribute('data-option-array-index'); |
|
|
|
|
975
|
|
|
this.results_data[selected_index].selected = false; |
976
|
|
|
} |
977
|
|
|
this.result_single_selected = high; |
978
|
|
|
} |
979
|
|
|
high.addClass("result-selected"); |
980
|
|
|
item = this.results_data[high[0].getAttribute("data-option-array-index")]; |
981
|
|
|
item.selected = true; |
982
|
|
|
this.form_field.options[item.options_index].selected = true; |
983
|
|
|
this.selected_option_count = null; |
984
|
|
|
if (this.is_multiple) { |
985
|
|
|
this.choice_build(item); |
986
|
|
|
} else { |
987
|
|
|
this.single_set_selected_text(item.text); |
988
|
|
|
} |
989
|
|
|
if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) { |
990
|
|
|
this.results_hide(); |
991
|
|
|
} |
992
|
|
|
this.search_field.val(""); |
993
|
|
|
if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) { |
994
|
|
|
this.form_field_jq.trigger("change", { |
995
|
|
|
'selected': this.form_field.options[item.options_index].value |
996
|
|
|
}); |
997
|
|
|
} |
998
|
|
|
this.current_selectedIndex = this.form_field.selectedIndex; |
999
|
|
|
return this.search_field_scale(); |
1000
|
|
|
} |
1001
|
|
|
/*<JUI>*/ |
1002
|
|
|
/* Original: not exist */ |
1003
|
|
|
else if ((!this.is_multiple) && this.allow_custom_value) { |
|
|
|
|
1004
|
|
|
value = this.search_field.val(); |
1005
|
|
|
group = this.add_unique_custom_group(); |
1006
|
|
|
option = $('<option value="' + value + '">' + value + '</option>'); |
1007
|
|
|
group.append(option); |
1008
|
|
|
this.form_field_jq.append(group); |
1009
|
|
|
this.form_field.options[this.form_field.options.length - 1].selected = true; |
1010
|
|
|
if (!evt.metaKey) { |
1011
|
|
|
this.results_hide(); |
1012
|
|
|
} |
1013
|
|
|
return this.results_build(); |
1014
|
|
|
} |
1015
|
|
|
/*</JUI>*/ |
1016
|
|
|
}; |
1017
|
|
|
|
1018
|
|
|
/*<JUI>*/ |
1019
|
|
|
/* Original: not exist */ |
1020
|
|
|
Chosen.prototype.find_custom_group = function() { |
1021
|
|
|
var found, group, _i, _len, _ref; |
1022
|
|
|
_ref = $('optgroup', this.form_field); |
1023
|
|
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
1024
|
|
|
group = _ref[_i]; |
1025
|
|
|
if (group.getAttribute('label') === this.custom_group_text) { |
1026
|
|
|
found = group; |
1027
|
|
|
} |
1028
|
|
|
} |
1029
|
|
|
return found; |
|
|
|
|
1030
|
|
|
}; |
1031
|
|
|
|
1032
|
|
|
Chosen.prototype.add_unique_custom_group = function() { |
1033
|
|
|
var group; |
1034
|
|
|
group = this.find_custom_group(); |
1035
|
|
|
if (!group) { |
1036
|
|
|
group = $('<optgroup label="' + this.custom_group_text + '"></optgroup>'); |
1037
|
|
|
} |
1038
|
|
|
return $(group); |
1039
|
|
|
}; |
1040
|
|
|
/*</JUI>*/ |
1041
|
|
|
|
1042
|
|
|
Chosen.prototype.single_set_selected_text = function(text) { |
1043
|
|
|
if (text == null) { |
1044
|
|
|
text = this.default_text; |
1045
|
|
|
} |
1046
|
|
|
if (text === this.default_text) { |
1047
|
|
|
this.selected_item.addClass("chzn-default"); |
1048
|
|
|
} else { |
1049
|
|
|
this.single_deselect_control_build(); |
1050
|
|
|
this.selected_item.removeClass("chzn-default"); |
1051
|
|
|
} |
1052
|
|
|
return this.selected_item.find("span").text(text); |
1053
|
|
|
}; |
1054
|
|
|
|
1055
|
|
|
Chosen.prototype.result_deselect = function(pos) { |
1056
|
|
|
var result_data; |
1057
|
|
|
|
1058
|
|
|
result_data = this.results_data[pos]; |
1059
|
|
|
if (!this.form_field.options[result_data.options_index].disabled) { |
1060
|
|
|
result_data.selected = false; |
1061
|
|
|
this.form_field.options[result_data.options_index].selected = false; |
1062
|
|
|
this.selected_option_count = null; |
1063
|
|
|
this.result_clear_highlight(); |
1064
|
|
|
if (this.results_showing) { |
1065
|
|
|
this.winnow_results(); |
1066
|
|
|
} |
1067
|
|
|
this.form_field_jq.trigger("change", { |
1068
|
|
|
deselected: this.form_field.options[result_data.options_index].value |
1069
|
|
|
}); |
1070
|
|
|
this.search_field_scale(); |
1071
|
|
|
return true; |
1072
|
|
|
} else { |
1073
|
|
|
return false; |
1074
|
|
|
} |
1075
|
|
|
}; |
1076
|
|
|
|
1077
|
|
|
Chosen.prototype.single_deselect_control_build = function() { |
1078
|
|
|
if (!this.allow_single_deselect) { |
1079
|
|
|
return; |
1080
|
|
|
} |
1081
|
|
|
if (!this.selected_item.find("abbr").length) { |
1082
|
|
|
this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>"); |
1083
|
|
|
} |
1084
|
|
|
return this.selected_item.addClass("chzn-single-with-deselect"); |
1085
|
|
|
}; |
1086
|
|
|
|
1087
|
|
|
Chosen.prototype.get_search_text = function() { |
1088
|
|
|
if (this.search_field.val() === this.default_text) { |
1089
|
|
|
return ""; |
1090
|
|
|
} else { |
1091
|
|
|
return $('<div/>').text($.trim(this.search_field.val())).html(); |
1092
|
|
|
} |
1093
|
|
|
}; |
1094
|
|
|
|
1095
|
|
|
Chosen.prototype.winnow_results_set_highlight = function() { |
1096
|
|
|
var do_high, selected_results; |
1097
|
|
|
|
1098
|
|
|
selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : []; |
1099
|
|
|
do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first(); |
1100
|
|
|
if (do_high != null) { |
|
|
|
|
1101
|
|
|
return this.result_do_highlight(do_high); |
1102
|
|
|
} |
1103
|
|
|
}; |
1104
|
|
|
|
1105
|
|
|
Chosen.prototype.no_results = function(terms) { |
1106
|
|
|
var no_results_html; |
1107
|
|
|
|
1108
|
|
|
no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>'); |
1109
|
|
|
no_results_html.find("span").first().html(terms); |
1110
|
|
|
return this.search_results.append(no_results_html); |
1111
|
|
|
}; |
1112
|
|
|
|
1113
|
|
|
Chosen.prototype.no_results_clear = function() { |
1114
|
|
|
return this.search_results.find(".no-results").remove(); |
1115
|
|
|
}; |
1116
|
|
|
|
1117
|
|
|
Chosen.prototype.keydown_arrow = function() { |
1118
|
|
|
var next_sib; |
1119
|
|
|
|
1120
|
|
|
if (this.results_showing && this.result_highlight) { |
1121
|
|
|
next_sib = this.result_highlight.nextAll("li.active-result").first(); |
1122
|
|
|
if (next_sib) { |
|
|
|
|
1123
|
|
|
return this.result_do_highlight(next_sib); |
1124
|
|
|
} |
1125
|
|
|
} else { |
1126
|
|
|
return this.results_show(); |
1127
|
|
|
} |
1128
|
|
|
}; |
1129
|
|
|
|
1130
|
|
|
Chosen.prototype.keyup_arrow = function() { |
1131
|
|
|
var prev_sibs; |
1132
|
|
|
|
1133
|
|
|
if (!this.results_showing && !this.is_multiple) { |
1134
|
|
|
return this.results_show(); |
1135
|
|
|
} else if (this.result_highlight) { |
|
|
|
|
1136
|
|
|
prev_sibs = this.result_highlight.prevAll("li.active-result"); |
1137
|
|
|
if (prev_sibs.length) { |
1138
|
|
|
return this.result_do_highlight(prev_sibs.first()); |
1139
|
|
|
} else { |
1140
|
|
|
if (this.choices_count() > 0) { |
1141
|
|
|
this.results_hide(); |
1142
|
|
|
} |
1143
|
|
|
return this.result_clear_highlight(); |
1144
|
|
|
} |
1145
|
|
|
} |
1146
|
|
|
}; |
1147
|
|
|
|
1148
|
|
|
Chosen.prototype.keydown_backstroke = function() { |
1149
|
|
|
var next_available_destroy; |
1150
|
|
|
|
1151
|
|
|
if (this.pending_backstroke) { |
1152
|
|
|
this.choice_destroy(this.pending_backstroke.find("a").first()); |
1153
|
|
|
return this.clear_backstroke(); |
1154
|
|
|
} else { |
1155
|
|
|
next_available_destroy = this.search_container.siblings("li.search-choice").last(); |
1156
|
|
|
if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) { |
|
|
|
|
1157
|
|
|
this.pending_backstroke = next_available_destroy; |
1158
|
|
|
if (this.single_backstroke_delete) { |
1159
|
|
|
return this.keydown_backstroke(); |
1160
|
|
|
} else { |
1161
|
|
|
return this.pending_backstroke.addClass("search-choice-focus"); |
1162
|
|
|
} |
1163
|
|
|
} |
1164
|
|
|
} |
1165
|
|
|
}; |
1166
|
|
|
|
1167
|
|
|
Chosen.prototype.clear_backstroke = function() { |
1168
|
|
|
if (this.pending_backstroke) { |
1169
|
|
|
this.pending_backstroke.removeClass("search-choice-focus"); |
1170
|
|
|
} |
1171
|
|
|
return this.pending_backstroke = null; |
1172
|
|
|
}; |
1173
|
|
|
|
1174
|
|
|
Chosen.prototype.keydown_checker = function(evt) { |
1175
|
|
|
var stroke, _ref1; |
1176
|
|
|
|
1177
|
|
|
stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode; |
1178
|
|
|
this.search_field_scale(); |
1179
|
|
|
if (stroke !== 8 && this.pending_backstroke) { |
1180
|
|
|
this.clear_backstroke(); |
1181
|
|
|
} |
1182
|
|
|
switch (stroke) { |
|
|
|
|
1183
|
|
|
case 8: |
1184
|
|
|
this.backstroke_length = this.search_field.val().length; |
1185
|
|
|
break; |
1186
|
|
|
case 9: |
1187
|
|
|
if (this.results_showing && !this.is_multiple) { |
1188
|
|
|
this.result_select(evt); |
1189
|
|
|
} |
1190
|
|
|
this.mouse_on_container = false; |
1191
|
|
|
break; |
1192
|
|
|
case 13: |
1193
|
|
|
evt.preventDefault(); |
1194
|
|
|
break; |
1195
|
|
|
case 38: |
1196
|
|
|
evt.preventDefault(); |
1197
|
|
|
this.keyup_arrow(); |
1198
|
|
|
break; |
1199
|
|
|
case 40: |
1200
|
|
|
evt.preventDefault(); |
1201
|
|
|
this.keydown_arrow(); |
1202
|
|
|
break; |
1203
|
|
|
} |
1204
|
|
|
}; |
1205
|
|
|
|
1206
|
|
|
Chosen.prototype.search_field_scale = function() { |
1207
|
|
|
var div, f_width, h, style, style_block, styles, w, _i, _len; |
1208
|
|
|
|
1209
|
|
|
if (this.is_multiple) { |
|
|
|
|
1210
|
|
|
h = 0; |
|
|
|
|
1211
|
|
|
w = 0; |
|
|
|
|
1212
|
|
|
style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"; |
1213
|
|
|
styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing']; |
1214
|
|
|
for (_i = 0, _len = styles.length; _i < _len; _i++) { |
1215
|
|
|
style = styles[_i]; |
1216
|
|
|
style_block += style + ":" + this.search_field.css(style) + ";"; |
1217
|
|
|
} |
1218
|
|
|
div = $('<div />', { |
1219
|
|
|
'style': style_block |
1220
|
|
|
}); |
1221
|
|
|
div.text(this.search_field.val()); |
1222
|
|
|
$('body').append(div); |
1223
|
|
|
w = div.width() + 25; |
1224
|
|
|
div.remove(); |
1225
|
|
|
f_width = this.container.outerWidth(); |
1226
|
|
|
if (w > f_width - 10) { |
1227
|
|
|
w = f_width - 10; |
1228
|
|
|
} |
1229
|
|
|
return this.search_field.css({ |
1230
|
|
|
'width': w + 'px' |
1231
|
|
|
}); |
1232
|
|
|
} |
1233
|
|
|
}; |
1234
|
|
|
|
1235
|
|
|
return Chosen; |
1236
|
|
|
|
1237
|
|
|
})(AbstractChosen); |
1238
|
|
|
|
1239
|
|
|
}).call(this); |
1240
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.