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