| 1 |
// Chosen, a Select Box Enhancer for jQuery and Protoype |
| 2 |
// by Patrick Filler for Harvest, http://getharvest.com |
| 3 |
// http://harvesthq.github.com/chosen/ |
| 4 |
// Version 0.9.7 |
| 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 `cake build`, do not edit it by hand. |
| 10 |
(function() { |
| 11 |
var SelectParser; |
| 12 |
|
| 13 |
SelectParser = (function() { |
| 14 |
|
| 15 |
function SelectParser() { |
| 16 |
this.options_index = 0; |
| 17 |
this.parsed = []; |
| 18 |
} |
| 19 |
|
| 20 |
SelectParser.prototype.add_node = function(child) { |
| 21 |
if (child.nodeName === "OPTGROUP") { |
| 22 |
return this.add_group(child); |
| 23 |
} else { |
| 24 |
return this.add_option(child); |
| 25 |
} |
| 26 |
}; |
| 27 |
|
| 28 |
SelectParser.prototype.add_group = function(group) { |
| 29 |
var group_position, option, _i, _len, _ref, _results; |
| 30 |
group_position = this.parsed.length; |
| 31 |
this.parsed.push({ |
| 32 |
array_index: group_position, |
| 33 |
group: true, |
| 34 |
label: group.label, |
| 35 |
children: 0, |
| 36 |
disabled: group.disabled |
| 37 |
}); |
| 38 |
_ref = group.childNodes; |
| 39 |
_results = []; |
| 40 |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 41 |
option = _ref[_i]; |
| 42 |
_results.push(this.add_option(option, group_position, group.disabled)); |
| 43 |
} |
| 44 |
return _results; |
| 45 |
}; |
| 46 |
|
| 47 |
SelectParser.prototype.add_option = function(option, group_position, group_disabled) { |
| 48 |
if (option.nodeName === "OPTION") { |
| 49 |
if (option.text !== "") { |
| 50 |
if (group_position != null) this.parsed[group_position].children += 1; |
| 51 |
this.parsed.push({ |
| 52 |
array_index: this.parsed.length, |
| 53 |
options_index: this.options_index, |
| 54 |
value: option.value, |
| 55 |
text: option.text, |
| 56 |
html: option.innerHTML, |
| 57 |
selected: option.selected, |
| 58 |
disabled: group_disabled === true ? group_disabled : option.disabled, |
| 59 |
group_array_index: group_position, |
| 60 |
classes: option.className, |
| 61 |
style: option.style.cssText |
| 62 |
}); |
| 63 |
} else { |
| 64 |
this.parsed.push({ |
| 65 |
array_index: this.parsed.length, |
| 66 |
options_index: this.options_index, |
| 67 |
empty: true |
| 68 |
}); |
| 69 |
} |
| 70 |
return this.options_index += 1; |
| 71 |
} |
| 72 |
}; |
| 73 |
|
| 74 |
return SelectParser; |
| 75 |
|
| 76 |
})(); |
| 77 |
|
| 78 |
SelectParser.select_to_array = function(select) { |
| 79 |
var child, parser, _i, _len, _ref; |
| 80 |
parser = new SelectParser(); |
| 81 |
_ref = select.childNodes; |
| 82 |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 83 |
child = _ref[_i]; |
| 84 |
parser.add_node(child); |
| 85 |
} |
| 86 |
return parser.parsed; |
| 87 |
}; |
| 88 |
|
| 89 |
this.SelectParser = SelectParser; |
| 90 |
|
| 91 |
}).call(this); |
| 92 |
|
| 93 |
/* |
| 94 |
Chosen source: generate output using 'cake build' |
| 95 |
Copyright (c) 2011 by Harvest |
| 96 |
*/ |
| 97 |
|
| 98 |
(function() { |
| 99 |
var AbstractChosen, root; |
| 100 |
|
| 101 |
root = this; |
| 102 |
|
| 103 |
AbstractChosen = (function() { |
| 104 |
|
| 105 |
function AbstractChosen(form_field, options) { |
| 106 |
this.form_field = form_field; |
| 107 |
this.options = options != null ? options : {}; |
| 108 |
this.set_default_values(); |
| 109 |
this.is_multiple = this.form_field.multiple; |
| 110 |
this.default_text_default = this.is_multiple ? "Select Some Options" : "Select an Option"; |
| 111 |
this.setup(); |
| 112 |
this.set_up_html(); |
| 113 |
this.register_observers(); |
| 114 |
this.finish_setup(); |
| 115 |
} |
| 116 |
|
| 117 |
AbstractChosen.prototype.set_default_values = function() { |
| 118 |
var _this = this; |
| 119 |
this.click_test_action = function(evt) { |
| 120 |
return _this.test_active_click(evt); |
| 121 |
}; |
| 122 |
this.activate_action = function(evt) { |
| 123 |
return _this.activate_field(evt); |
| 124 |
}; |
| 125 |
this.active_field = false; |
| 126 |
this.mouse_on_container = false; |
| 127 |
this.results_showing = false; |
| 128 |
this.result_highlighted = null; |
| 129 |
this.result_single_selected = null; |
| 130 |
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; |
| 131 |
this.disable_search_threshold = this.options.disable_search_threshold || 0; |
| 132 |
this.choices = 0; |
| 133 |
return this.results_none_found = this.options.no_results_text || "No results match"; |
| 134 |
}; |
| 135 |
|
| 136 |
AbstractChosen.prototype.mouse_enter = function() { |
| 137 |
return this.mouse_on_container = true; |
| 138 |
}; |
| 139 |
|
| 140 |
AbstractChosen.prototype.mouse_leave = function() { |
| 141 |
return this.mouse_on_container = false; |
| 142 |
}; |
| 143 |
|
| 144 |
AbstractChosen.prototype.input_focus = function(evt) { |
| 145 |
var _this = this; |
| 146 |
if (!this.active_field) { |
| 147 |
return setTimeout((function() { |
| 148 |
return _this.container_mousedown(); |
| 149 |
}), 50); |
| 150 |
} |
| 151 |
}; |
| 152 |
|
| 153 |
AbstractChosen.prototype.input_blur = function(evt) { |
| 154 |
var _this = this; |
| 155 |
if (!this.mouse_on_container) { |
| 156 |
this.active_field = false; |
| 157 |
return setTimeout((function() { |
| 158 |
return _this.blur_test(); |
| 159 |
}), 100); |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
AbstractChosen.prototype.result_add_option = function(option) { |
| 164 |
var classes, style; |
| 165 |
if (!option.disabled) { |
| 166 |
option.dom_id = this.container_id + "_o_" + option.array_index; |
| 167 |
classes = option.selected && this.is_multiple ? [] : ["active-result"]; |
| 168 |
if (option.selected) classes.push("result-selected"); |
| 169 |
if (option.group_array_index != null) classes.push("group-option"); |
| 170 |
if (option.classes !== "") classes.push(option.classes); |
| 171 |
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; |
| 172 |
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>'; |
| 173 |
} else { |
| 174 |
return ""; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
AbstractChosen.prototype.results_update_field = function() { |
| 179 |
this.result_clear_highlight(); |
| 180 |
this.result_single_selected = null; |
| 181 |
return this.results_build(); |
| 182 |
}; |
| 183 |
|
| 184 |
AbstractChosen.prototype.results_toggle = function() { |
| 185 |
if (this.results_showing) { |
| 186 |
return this.results_hide(); |
| 187 |
} else { |
| 188 |
return this.results_show(); |
| 189 |
} |
| 190 |
}; |
| 191 |
|
| 192 |
AbstractChosen.prototype.results_search = function(evt) { |
| 193 |
if (this.results_showing) { |
| 194 |
return this.winnow_results(); |
| 195 |
} else { |
| 196 |
return this.results_show(); |
| 197 |
} |
| 198 |
}; |
| 199 |
|
| 200 |
AbstractChosen.prototype.keyup_checker = function(evt) { |
| 201 |
var stroke, _ref; |
| 202 |
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; |
| 203 |
this.search_field_scale(); |
| 204 |
switch (stroke) { |
| 205 |
case 8: |
| 206 |
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) { |
| 207 |
return this.keydown_backstroke(); |
| 208 |
} else if (!this.pending_backstroke) { |
| 209 |
this.result_clear_highlight(); |
| 210 |
return this.results_search(); |
| 211 |
} |
| 212 |
break; |
| 213 |
case 13: |
| 214 |
evt.preventDefault(); |
| 215 |
if (this.results_showing) return this.result_select(evt); |
| 216 |
break; |
| 217 |
case 27: |
| 218 |
if (this.results_showing) this.results_hide(); |
| 219 |
return true; |
| 220 |
case 9: |
| 221 |
case 38: |
| 222 |
case 40: |
| 223 |
case 16: |
| 224 |
case 91: |
| 225 |
case 17: |
| 226 |
break; |
| 227 |
default: |
| 228 |
return this.results_search(); |
| 229 |
} |
| 230 |
}; |
| 231 |
|
| 232 |
AbstractChosen.prototype.generate_field_id = function() { |
| 233 |
var new_id; |
| 234 |
new_id = this.generate_random_id(); |
| 235 |
this.form_field.id = new_id; |
| 236 |
return new_id; |
| 237 |
}; |
| 238 |
|
| 239 |
AbstractChosen.prototype.generate_random_char = function() { |
| 240 |
var chars, newchar, rand; |
| 241 |
chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"; |
| 242 |
rand = Math.floor(Math.random() * chars.length); |
| 243 |
return newchar = chars.substring(rand, rand + 1); |
| 244 |
}; |
| 245 |
|
| 246 |
return AbstractChosen; |
| 247 |
|
| 248 |
})(); |
| 249 |
|
| 250 |
root.AbstractChosen = AbstractChosen; |
| 251 |
|
| 252 |
}).call(this); |
| 253 |
|
| 254 |
/* |
| 255 |
Chosen source: generate output using 'cake build' |
| 256 |
Copyright (c) 2011 by Harvest |
| 257 |
*/ |
| 258 |
|
| 259 |
(function() { |
| 260 |
var $, Chosen, get_side_border_padding, root, |
| 261 |
__hasProp = Object.prototype.hasOwnProperty, |
| 262 |
__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; }; |
| 263 |
|
| 264 |
root = this; |
| 265 |
|
| 266 |
$ = jQuery; |
| 267 |
|
| 268 |
$.fn.extend({ |
| 269 |
chosen: function(options) { |
| 270 |
if ($.browser.msie && ($.browser.version === "6.0" || $.browser.version === "7.0")) { |
| 271 |
return this; |
| 272 |
} |
| 273 |
return $(this).each(function(input_field) { |
| 274 |
if (!($(this)).hasClass("chzn-done")) return new Chosen(this, options); |
| 275 |
}); |
| 276 |
} |
| 277 |
}); |
| 278 |
|
| 279 |
Chosen = (function(_super) { |
| 280 |
|
| 281 |
__extends(Chosen, _super); |
| 282 |
|
| 283 |
function Chosen() { |
| 284 |
Chosen.__super__.constructor.apply(this, arguments); |
| 285 |
} |
| 286 |
|
| 287 |
Chosen.prototype.setup = function() { |
| 288 |
this.form_field_jq = $(this.form_field); |
| 289 |
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); |
| 290 |
}; |
| 291 |
|
| 292 |
Chosen.prototype.finish_setup = function() { |
| 293 |
return this.form_field_jq.addClass("chzn-done"); |
| 294 |
}; |
| 295 |
|
| 296 |
Chosen.prototype.set_up_html = function() { |
| 297 |
var container_div, dd_top, dd_width, sf_width; |
| 298 |
this.container_id = this.form_field.id.length ? this.form_field.id.replace(/(:|\.)/g, '_') : this.generate_field_id(); |
| 299 |
this.container_id += "_chzn"; |
| 300 |
this.f_width = this.form_field_jq.outerWidth(); |
| 301 |
this.default_text = this.form_field_jq.data('placeholder') ? this.form_field_jq.data('placeholder') : this.default_text_default; |
| 302 |
container_div = $("<div />", { |
| 303 |
id: this.container_id, |
| 304 |
"class": "chzn-container" + (this.is_rtl ? ' chzn-rtl' : ''), |
| 305 |
style: 'width: ' + this.f_width + 'px;' |
| 306 |
}); |
| 307 |
if (this.is_multiple) { |
| 308 |
container_div.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" style="left:-9000px;"><ul class="chzn-results"></ul></div>'); |
| 309 |
} else { |
| 310 |
container_div.html('<a href="javascript:void(0)" class="chzn-single"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>'); |
| 311 |
} |
| 312 |
this.form_field_jq.hide().after(container_div); |
| 313 |
this.container = $('#' + this.container_id); |
| 314 |
this.container.addClass("chzn-container-" + (this.is_multiple ? "multi" : "single")); |
| 315 |
this.dropdown = this.container.find('div.chzn-drop').first(); |
| 316 |
dd_top = this.container.height(); |
| 317 |
dd_width = this.f_width - get_side_border_padding(this.dropdown); |
| 318 |
this.dropdown.css({ |
| 319 |
"width": dd_width + "px", |
| 320 |
"top": dd_top + "px" |
| 321 |
}); |
| 322 |
this.search_field = this.container.find('input').first(); |
| 323 |
this.search_results = this.container.find('ul.chzn-results').first(); |
| 324 |
this.search_field_scale(); |
| 325 |
this.search_no_results = this.container.find('li.no-results').first(); |
| 326 |
if (this.is_multiple) { |
| 327 |
this.search_choices = this.container.find('ul.chzn-choices').first(); |
| 328 |
this.search_container = this.container.find('li.search-field').first(); |
| 329 |
} else { |
| 330 |
this.search_container = this.container.find('div.chzn-search').first(); |
| 331 |
this.selected_item = this.container.find('.chzn-single').first(); |
| 332 |
sf_width = dd_width - get_side_border_padding(this.search_container) - get_side_border_padding(this.search_field); |
| 333 |
this.search_field.css({ |
| 334 |
"width": sf_width + "px" |
| 335 |
}); |
| 336 |
} |
| 337 |
this.results_build(); |
| 338 |
this.set_tab_index(); |
| 339 |
return this.form_field_jq.trigger("liszt:ready", { |
| 340 |
chosen: this |
| 341 |
}); |
| 342 |
}; |
| 343 |
|
| 344 |
Chosen.prototype.register_observers = function() { |
| 345 |
var _this = this; |
| 346 |
this.container.mousedown(function(evt) { |
| 347 |
return _this.container_mousedown(evt); |
| 348 |
}); |
| 349 |
this.container.mouseup(function(evt) { |
| 350 |
return _this.container_mouseup(evt); |
| 351 |
}); |
| 352 |
this.container.mouseenter(function(evt) { |
| 353 |
return _this.mouse_enter(evt); |
| 354 |
}); |
| 355 |
this.container.mouseleave(function(evt) { |
| 356 |
return _this.mouse_leave(evt); |
| 357 |
}); |
| 358 |
this.search_results.mouseup(function(evt) { |
| 359 |
return _this.search_results_mouseup(evt); |
| 360 |
}); |
| 361 |
this.search_results.mouseover(function(evt) { |
| 362 |
return _this.search_results_mouseover(evt); |
| 363 |
}); |
| 364 |
this.search_results.mouseout(function(evt) { |
| 365 |
return _this.search_results_mouseout(evt); |
| 366 |
}); |
| 367 |
this.form_field_jq.bind("liszt:updated", function(evt) { |
| 368 |
return _this.results_update_field(evt); |
| 369 |
}); |
| 370 |
this.search_field.blur(function(evt) { |
| 371 |
return _this.input_blur(evt); |
| 372 |
}); |
| 373 |
this.search_field.keyup(function(evt) { |
| 374 |
return _this.keyup_checker(evt); |
| 375 |
}); |
| 376 |
this.search_field.keydown(function(evt) { |
| 377 |
return _this.keydown_checker(evt); |
| 378 |
}); |
| 379 |
if (this.is_multiple) { |
| 380 |
this.search_choices.click(function(evt) { |
| 381 |
return _this.choices_click(evt); |
| 382 |
}); |
| 383 |
return this.search_field.focus(function(evt) { |
| 384 |
return _this.input_focus(evt); |
| 385 |
}); |
| 386 |
} else { |
| 387 |
return this.container.click(function(evt) { |
| 388 |
return evt.preventDefault(); |
| 389 |
}); |
| 390 |
} |
| 391 |
}; |
| 392 |
|
| 393 |
Chosen.prototype.search_field_disabled = function() { |
| 394 |
this.is_disabled = this.form_field_jq[0].disabled; |
| 395 |
if (this.is_disabled) { |
| 396 |
this.container.addClass('chzn-disabled'); |
| 397 |
this.search_field[0].disabled = true; |
| 398 |
if (!this.is_multiple) { |
| 399 |
this.selected_item.unbind("focus", this.activate_action); |
| 400 |
} |
| 401 |
return this.close_field(); |
| 402 |
} else { |
| 403 |
this.container.removeClass('chzn-disabled'); |
| 404 |
this.search_field[0].disabled = false; |
| 405 |
if (!this.is_multiple) { |
| 406 |
return this.selected_item.bind("focus", this.activate_action); |
| 407 |
} |
| 408 |
} |
| 409 |
}; |
| 410 |
|
| 411 |
Chosen.prototype.container_mousedown = function(evt) { |
| 412 |
var target_closelink; |
| 413 |
if (!this.is_disabled) { |
| 414 |
target_closelink = evt != null ? ($(evt.target)).hasClass("search-choice-close") : false; |
| 415 |
if (evt && evt.type === "mousedown") evt.stopPropagation(); |
| 416 |
if (!this.pending_destroy_click && !target_closelink) { |
| 417 |
if (!this.active_field) { |
| 418 |
if (this.is_multiple) this.search_field.val(""); |
| 419 |
$(document).click(this.click_test_action); |
| 420 |
this.results_show(); |
| 421 |
} else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) { |
| 422 |
evt.preventDefault(); |
| 423 |
this.results_toggle(); |
| 424 |
} |
| 425 |
return this.activate_field(); |
| 426 |
} else { |
| 427 |
return this.pending_destroy_click = false; |
| 428 |
} |
| 429 |
} |
| 430 |
}; |
| 431 |
|
| 432 |
Chosen.prototype.container_mouseup = function(evt) { |
| 433 |
if (evt.target.nodeName === "ABBR") return this.results_reset(evt); |
| 434 |
}; |
| 435 |
|
| 436 |
Chosen.prototype.blur_test = function(evt) { |
| 437 |
if (!this.active_field && this.container.hasClass("chzn-container-active")) { |
| 438 |
return this.close_field(); |
| 439 |
} |
| 440 |
}; |
| 441 |
|
| 442 |
Chosen.prototype.close_field = function() { |
| 443 |
$(document).unbind("click", this.click_test_action); |
| 444 |
if (!this.is_multiple) { |
| 445 |
this.selected_item.attr("tabindex", this.search_field.attr("tabindex")); |
| 446 |
this.search_field.attr("tabindex", -1); |
| 447 |
} |
| 448 |
this.active_field = false; |
| 449 |
this.results_hide(); |
| 450 |
this.container.removeClass("chzn-container-active"); |
| 451 |
this.winnow_results_clear(); |
| 452 |
this.clear_backstroke(); |
| 453 |
this.show_search_field_default(); |
| 454 |
return this.search_field_scale(); |
| 455 |
}; |
| 456 |
|
| 457 |
Chosen.prototype.activate_field = function() { |
| 458 |
if (!this.is_multiple && !this.active_field) { |
| 459 |
this.search_field.attr("tabindex", this.selected_item.attr("tabindex")); |
| 460 |
this.selected_item.attr("tabindex", -1); |
| 461 |
} |
| 462 |
this.container.addClass("chzn-container-active"); |
| 463 |
this.active_field = true; |
| 464 |
this.search_field.val(this.search_field.val()); |
| 465 |
return this.search_field.focus(); |
| 466 |
}; |
| 467 |
|
| 468 |
Chosen.prototype.test_active_click = function(evt) { |
| 469 |
if ($(evt.target).parents('#' + this.container_id).length) { |
| 470 |
return this.active_field = true; |
| 471 |
} else { |
| 472 |
return this.close_field(); |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
Chosen.prototype.results_build = function() { |
| 477 |
var content, data, _i, _len, _ref; |
| 478 |
this.parsing = true; |
| 479 |
this.results_data = root.SelectParser.select_to_array(this.form_field); |
| 480 |
if (this.is_multiple && this.choices > 0) { |
| 481 |
this.search_choices.find("li.search-choice").remove(); |
| 482 |
this.choices = 0; |
| 483 |
} else if (!this.is_multiple) { |
| 484 |
this.selected_item.find("span").text(this.default_text); |
| 485 |
if (this.form_field.options.length <= this.disable_search_threshold) { |
| 486 |
this.container.addClass("chzn-container-single-nosearch"); |
| 487 |
} else { |
| 488 |
this.container.removeClass("chzn-container-single-nosearch"); |
| 489 |
} |
| 490 |
} |
| 491 |
content = ''; |
| 492 |
_ref = this.results_data; |
| 493 |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 494 |
data = _ref[_i]; |
| 495 |
if (data.group) { |
| 496 |
content += this.result_add_group(data); |
| 497 |
} else if (!data.empty) { |
| 498 |
content += this.result_add_option(data); |
| 499 |
if (data.selected && this.is_multiple) { |
| 500 |
this.choice_build(data); |
| 501 |
} else if (data.selected && !this.is_multiple) { |
| 502 |
this.selected_item.find("span").text(data.text); |
| 503 |
if (this.allow_single_deselect) this.single_deselect_control_build(); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
this.search_field_disabled(); |
| 508 |
this.show_search_field_default(); |
| 509 |
this.search_field_scale(); |
| 510 |
this.search_results.html(content); |
| 511 |
return this.parsing = false; |
| 512 |
}; |
| 513 |
|
| 514 |
Chosen.prototype.result_add_group = function(group) { |
| 515 |
if (!group.disabled) { |
| 516 |
group.dom_id = this.container_id + "_g_" + group.array_index; |
| 517 |
return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>'; |
| 518 |
} else { |
| 519 |
return ""; |
| 520 |
} |
| 521 |
}; |
| 522 |
|
| 523 |
Chosen.prototype.result_do_highlight = function(el) { |
| 524 |
var high_bottom, high_top, maxHeight, visible_bottom, visible_top; |
| 525 |
if (el.length) { |
| 526 |
this.result_clear_highlight(); |
| 527 |
this.result_highlight = el; |
| 528 |
this.result_highlight.addClass("highlighted"); |
| 529 |
maxHeight = parseInt(this.search_results.css("maxHeight"), 10); |
| 530 |
visible_top = this.search_results.scrollTop(); |
| 531 |
visible_bottom = maxHeight + visible_top; |
| 532 |
high_top = this.result_highlight.position().top + this.search_results.scrollTop(); |
| 533 |
high_bottom = high_top + this.result_highlight.outerHeight(); |
| 534 |
if (high_bottom >= visible_bottom) { |
| 535 |
return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0); |
| 536 |
} else if (high_top < visible_top) { |
| 537 |
return this.search_results.scrollTop(high_top); |
| 538 |
} |
| 539 |
} |
| 540 |
}; |
| 541 |
|
| 542 |
Chosen.prototype.result_clear_highlight = function() { |
| 543 |
if (this.result_highlight) this.result_highlight.removeClass("highlighted"); |
| 544 |
return this.result_highlight = null; |
| 545 |
}; |
| 546 |
|
| 547 |
Chosen.prototype.results_show = function() { |
| 548 |
var dd_top; |
| 549 |
if (!this.is_multiple) { |
| 550 |
this.selected_item.addClass("chzn-single-with-drop"); |
| 551 |
if (this.result_single_selected) { |
| 552 |
this.result_do_highlight(this.result_single_selected); |
| 553 |
} |
| 554 |
} |
| 555 |
dd_top = this.is_multiple ? this.container.height() : this.container.height() - 1; |
| 556 |
this.dropdown.css({ |
| 557 |
"top": dd_top + "px", |
| 558 |
"left": 0 |
| 559 |
}); |
| 560 |
this.results_showing = true; |
| 561 |
this.search_field.focus(); |
| 562 |
this.search_field.val(this.search_field.val()); |
| 563 |
return this.winnow_results(); |
| 564 |
}; |
| 565 |
|
| 566 |
Chosen.prototype.results_hide = function() { |
| 567 |
if (!this.is_multiple) { |
| 568 |
this.selected_item.removeClass("chzn-single-with-drop"); |
| 569 |
} |
| 570 |
this.result_clear_highlight(); |
| 571 |
this.dropdown.css({ |
| 572 |
"left": "-9000px" |
| 573 |
}); |
| 574 |
return this.results_showing = false; |
| 575 |
}; |
| 576 |
|
| 577 |
Chosen.prototype.set_tab_index = function(el) { |
| 578 |
var ti; |
| 579 |
if (this.form_field_jq.attr("tabindex")) { |
| 580 |
ti = this.form_field_jq.attr("tabindex"); |
| 581 |
this.form_field_jq.attr("tabindex", -1); |
| 582 |
if (this.is_multiple) { |
| 583 |
return this.search_field.attr("tabindex", ti); |
| 584 |
} else { |
| 585 |
this.selected_item.attr("tabindex", ti); |
| 586 |
return this.search_field.attr("tabindex", -1); |
| 587 |
} |
| 588 |
} |
| 589 |
}; |
| 590 |
|
| 591 |
Chosen.prototype.show_search_field_default = function() { |
| 592 |
if (this.is_multiple && this.choices < 1 && !this.active_field) { |
| 593 |
this.search_field.val(this.default_text); |
| 594 |
return this.search_field.addClass("default"); |
| 595 |
} else { |
| 596 |
this.search_field.val(""); |
| 597 |
return this.search_field.removeClass("default"); |
| 598 |
} |
| 599 |
}; |
| 600 |
|
| 601 |
Chosen.prototype.search_results_mouseup = function(evt) { |
| 602 |
var target; |
| 603 |
target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
| 604 |
if (target.length) { |
| 605 |
this.result_highlight = target; |
| 606 |
return this.result_select(evt); |
| 607 |
} |
| 608 |
}; |
| 609 |
|
| 610 |
Chosen.prototype.search_results_mouseover = function(evt) { |
| 611 |
var target; |
| 612 |
target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); |
| 613 |
if (target) return this.result_do_highlight(target); |
| 614 |
}; |
| 615 |
|
| 616 |
Chosen.prototype.search_results_mouseout = function(evt) { |
| 617 |
if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) { |
| 618 |
return this.result_clear_highlight(); |
| 619 |
} |
| 620 |
}; |
| 621 |
|
| 622 |
Chosen.prototype.choices_click = function(evt) { |
| 623 |
evt.preventDefault(); |
| 624 |
if (this.active_field && !($(evt.target).hasClass("search-choice" || $(evt.target).parents('.search-choice').first)) && !this.results_showing) { |
| 625 |
return this.results_show(); |
| 626 |
} |
| 627 |
}; |
| 628 |
|
| 629 |
Chosen.prototype.choice_build = function(item) { |
| 630 |
var choice_id, link, |
| 631 |
_this = this; |
| 632 |
choice_id = this.container_id + "_c_" + item.array_index; |
| 633 |
this.choices += 1; |
| 634 |
this.search_container.before('<li class="search-choice" id="' + choice_id + '"><span>' + item.html + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>'); |
| 635 |
link = $('#' + choice_id).find("a").first(); |
| 636 |
return link.click(function(evt) { |
| 637 |
return _this.choice_destroy_link_click(evt); |
| 638 |
}); |
| 639 |
}; |
| 640 |
|
| 641 |
Chosen.prototype.choice_destroy_link_click = function(evt) { |
| 642 |
evt.preventDefault(); |
| 643 |
if (!this.is_disabled) { |
| 644 |
this.pending_destroy_click = true; |
| 645 |
return this.choice_destroy($(evt.target)); |
| 646 |
} else { |
| 647 |
return evt.stopPropagation; |
| 648 |
} |
| 649 |
}; |
| 650 |
|
| 651 |
Chosen.prototype.choice_destroy = function(link) { |
| 652 |
this.choices -= 1; |
| 653 |
this.show_search_field_default(); |
| 654 |
if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) { |
| 655 |
this.results_hide(); |
| 656 |
} |
| 657 |
this.result_deselect(link.attr("rel")); |
| 658 |
return link.parents('li').first().remove(); |
| 659 |
}; |
| 660 |
|
| 661 |
Chosen.prototype.results_reset = function(evt) { |
| 662 |
this.form_field.options[0].selected = true; |
| 663 |
this.selected_item.find("span").text(this.default_text); |
| 664 |
this.show_search_field_default(); |
| 665 |
$(evt.target).remove(); |
| 666 |
this.form_field_jq.trigger("change"); |
| 667 |
if (this.active_field) return this.results_hide(); |
| 668 |
}; |
| 669 |
|
| 670 |
Chosen.prototype.result_select = function(evt) { |
| 671 |
var high, high_id, item, position; |
| 672 |
if (this.result_highlight) { |
| 673 |
high = this.result_highlight; |
| 674 |
high_id = high.attr("id"); |
| 675 |
this.result_clear_highlight(); |
| 676 |
if (this.is_multiple) { |
| 677 |
this.result_deactivate(high); |
| 678 |
} else { |
| 679 |
this.search_results.find(".result-selected").removeClass("result-selected"); |
| 680 |
this.result_single_selected = high; |
| 681 |
} |
| 682 |
high.addClass("result-selected"); |
| 683 |
position = high_id.substr(high_id.lastIndexOf("_") + 1); |
| 684 |
item = this.results_data[position]; |
| 685 |
item.selected = true; |
| 686 |
this.form_field.options[item.options_index].selected = true; |
| 687 |
if (this.is_multiple) { |
| 688 |
this.choice_build(item); |
| 689 |
} else { |
| 690 |
this.selected_item.find("span").first().text(item.text); |
| 691 |
if (this.allow_single_deselect) this.single_deselect_control_build(); |
| 692 |
} |
| 693 |
if (!(evt.metaKey && this.is_multiple)) this.results_hide(); |
| 694 |
this.search_field.val(""); |
| 695 |
this.form_field_jq.trigger("change"); |
| 696 |
return this.search_field_scale(); |
| 697 |
} |
| 698 |
}; |
| 699 |
|
| 700 |
Chosen.prototype.result_activate = function(el) { |
| 701 |
return el.addClass("active-result"); |
| 702 |
}; |
| 703 |
|
| 704 |
Chosen.prototype.result_deactivate = function(el) { |
| 705 |
return el.removeClass("active-result"); |
| 706 |
}; |
| 707 |
|
| 708 |
Chosen.prototype.result_deselect = function(pos) { |
| 709 |
var result, result_data; |
| 710 |
result_data = this.results_data[pos]; |
| 711 |
result_data.selected = false; |
| 712 |
this.form_field.options[result_data.options_index].selected = false; |
| 713 |
result = $("#" + this.container_id + "_o_" + pos); |
| 714 |
result.removeClass("result-selected").addClass("active-result").show(); |
| 715 |
this.result_clear_highlight(); |
| 716 |
this.winnow_results(); |
| 717 |
this.form_field_jq.trigger("change"); |
| 718 |
return this.search_field_scale(); |
| 719 |
}; |
| 720 |
|
| 721 |
Chosen.prototype.single_deselect_control_build = function() { |
| 722 |
if (this.allow_single_deselect && this.selected_item.find("abbr").length < 1) { |
| 723 |
return this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>"); |
| 724 |
} |
| 725 |
}; |
| 726 |
|
| 727 |
Chosen.prototype.winnow_results = function() { |
| 728 |
var found, option, part, parts, regex, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len2, _ref; |
| 729 |
this.no_results_clear(); |
| 730 |
results = 0; |
| 731 |
searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html(); |
| 732 |
regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); |
| 733 |
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); |
| 734 |
_ref = this.results_data; |
| 735 |
for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
| 736 |
option = _ref[_i]; |
| 737 |
if (!option.disabled && !option.empty) { |
| 738 |
if (option.group) { |
| 739 |
$('#' + option.dom_id).css('display', 'none'); |
| 740 |
} else if (!(this.is_multiple && option.selected)) { |
| 741 |
found = false; |
| 742 |
result_id = option.dom_id; |
| 743 |
result = $("#" + result_id); |
| 744 |
if (regex.test(option.html)) { |
| 745 |
found = true; |
| 746 |
results += 1; |
| 747 |
} else if (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0) { |
| 748 |
parts = option.html.replace(/\[|\]/g, "").split(" "); |
| 749 |
if (parts.length) { |
| 750 |
for (_j = 0, _len2 = parts.length; _j < _len2; _j++) { |
| 751 |
part = parts[_j]; |
| 752 |
if (regex.test(part)) { |
| 753 |
found = true; |
| 754 |
results += 1; |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
} |
| 759 |
if (found) { |
| 760 |
if (searchText.length) { |
| 761 |
startpos = option.html.search(zregex); |
| 762 |
text = option.html.substr(0, startpos + searchText.length) + '</em>' + option.html.substr(startpos + searchText.length); |
| 763 |
text = text.substr(0, startpos) + '<em>' + text.substr(startpos); |
| 764 |
} else { |
| 765 |
text = option.html; |
| 766 |
} |
| 767 |
result.html(text); |
| 768 |
this.result_activate(result); |
| 769 |
if (option.group_array_index != null) { |
| 770 |
$("#" + this.results_data[option.group_array_index].dom_id).css('display', 'list-item'); |
| 771 |
} |
| 772 |
} else { |
| 773 |
if (this.result_highlight && result_id === this.result_highlight.attr('id')) { |
| 774 |
this.result_clear_highlight(); |
| 775 |
} |
| 776 |
this.result_deactivate(result); |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
} |
| 781 |
if (results < 1 && searchText.length) { |
| 782 |
return this.no_results(searchText); |
| 783 |
} else { |
| 784 |
return this.winnow_results_set_highlight(); |
| 785 |
} |
| 786 |
}; |
| 787 |
|
| 788 |
Chosen.prototype.winnow_results_clear = function() { |
| 789 |
var li, lis, _i, _len, _results; |
| 790 |
this.search_field.val(""); |
| 791 |
lis = this.search_results.find("li"); |
| 792 |
_results = []; |
| 793 |
for (_i = 0, _len = lis.length; _i < _len; _i++) { |
| 794 |
li = lis[_i]; |
| 795 |
li = $(li); |
| 796 |
if (li.hasClass("group-result")) { |
| 797 |
_results.push(li.css('display', 'auto')); |
| 798 |
} else if (!this.is_multiple || !li.hasClass("result-selected")) { |
| 799 |
_results.push(this.result_activate(li)); |
| 800 |
} else { |
| 801 |
_results.push(void 0); |
| 802 |
} |
| 803 |
} |
| 804 |
return _results; |
| 805 |
}; |
| 806 |
|
| 807 |
Chosen.prototype.winnow_results_set_highlight = function() { |
| 808 |
var do_high, selected_results; |
| 809 |
if (!this.result_highlight) { |
| 810 |
selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : []; |
| 811 |
do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first(); |
| 812 |
if (do_high != null) return this.result_do_highlight(do_high); |
| 813 |
} |
| 814 |
}; |
| 815 |
|
| 816 |
Chosen.prototype.no_results = function(terms) { |
| 817 |
var no_results_html; |
| 818 |
no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>'); |
| 819 |
no_results_html.find("span").first().html(terms); |
| 820 |
return this.search_results.append(no_results_html); |
| 821 |
}; |
| 822 |
|
| 823 |
Chosen.prototype.no_results_clear = function() { |
| 824 |
return this.search_results.find(".no-results").remove(); |
| 825 |
}; |
| 826 |
|
| 827 |
Chosen.prototype.keydown_arrow = function() { |
| 828 |
var first_active, next_sib; |
| 829 |
if (!this.result_highlight) { |
| 830 |
first_active = this.search_results.find("li.active-result").first(); |
| 831 |
if (first_active) this.result_do_highlight($(first_active)); |
| 832 |
} else if (this.results_showing) { |
| 833 |
next_sib = this.result_highlight.nextAll("li.active-result").first(); |
| 834 |
if (next_sib) this.result_do_highlight(next_sib); |
| 835 |
} |
| 836 |
if (!this.results_showing) return this.results_show(); |
| 837 |
}; |
| 838 |
|
| 839 |
Chosen.prototype.keyup_arrow = function() { |
| 840 |
var prev_sibs; |
| 841 |
if (!this.results_showing && !this.is_multiple) { |
| 842 |
return this.results_show(); |
| 843 |
} else if (this.result_highlight) { |
| 844 |
prev_sibs = this.result_highlight.prevAll("li.active-result"); |
| 845 |
if (prev_sibs.length) { |
| 846 |
return this.result_do_highlight(prev_sibs.first()); |
| 847 |
} else { |
| 848 |
if (this.choices > 0) this.results_hide(); |
| 849 |
return this.result_clear_highlight(); |
| 850 |
} |
| 851 |
} |
| 852 |
}; |
| 853 |
|
| 854 |
Chosen.prototype.keydown_backstroke = function() { |
| 855 |
if (this.pending_backstroke) { |
| 856 |
this.choice_destroy(this.pending_backstroke.find("a").first()); |
| 857 |
return this.clear_backstroke(); |
| 858 |
} else { |
| 859 |
this.pending_backstroke = this.search_container.siblings("li.search-choice").last(); |
| 860 |
return this.pending_backstroke.addClass("search-choice-focus"); |
| 861 |
} |
| 862 |
}; |
| 863 |
|
| 864 |
Chosen.prototype.clear_backstroke = function() { |
| 865 |
if (this.pending_backstroke) { |
| 866 |
this.pending_backstroke.removeClass("search-choice-focus"); |
| 867 |
} |
| 868 |
return this.pending_backstroke = null; |
| 869 |
}; |
| 870 |
|
| 871 |
Chosen.prototype.keydown_checker = function(evt) { |
| 872 |
var stroke, _ref; |
| 873 |
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; |
| 874 |
this.search_field_scale(); |
| 875 |
if (stroke !== 8 && this.pending_backstroke) this.clear_backstroke(); |
| 876 |
switch (stroke) { |
| 877 |
case 8: |
| 878 |
this.backstroke_length = this.search_field.val().length; |
| 879 |
break; |
| 880 |
case 9: |
| 881 |
if (this.results_showing && !this.is_multiple) this.result_select(evt); |
| 882 |
this.mouse_on_container = false; |
| 883 |
break; |
| 884 |
case 13: |
| 885 |
evt.preventDefault(); |
| 886 |
break; |
| 887 |
case 38: |
| 888 |
evt.preventDefault(); |
| 889 |
this.keyup_arrow(); |
| 890 |
break; |
| 891 |
case 40: |
| 892 |
this.keydown_arrow(); |
| 893 |
break; |
| 894 |
} |
| 895 |
}; |
| 896 |
|
| 897 |
Chosen.prototype.search_field_scale = function() { |
| 898 |
var dd_top, div, h, style, style_block, styles, w, _i, _len; |
| 899 |
if (this.is_multiple) { |
| 900 |
h = 0; |
| 901 |
w = 0; |
| 902 |
style_block = "position:absolute; left: -1000px; top: -1000px; display:none;"; |
| 903 |
styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing']; |
| 904 |
for (_i = 0, _len = styles.length; _i < _len; _i++) { |
| 905 |
style = styles[_i]; |
| 906 |
style_block += style + ":" + this.search_field.css(style) + ";"; |
| 907 |
} |
| 908 |
div = $('<div />', { |
| 909 |
'style': style_block |
| 910 |
}); |
| 911 |
div.text(this.search_field.val()); |
| 912 |
$('body').append(div); |
| 913 |
w = div.width() + 25; |
| 914 |
div.remove(); |
| 915 |
if (w > this.f_width - 10) w = this.f_width - 10; |
| 916 |
this.search_field.css({ |
| 917 |
'width': w + 'px' |
| 918 |
}); |
| 919 |
dd_top = this.container.height(); |
| 920 |
return this.dropdown.css({ |
| 921 |
"top": dd_top + "px" |
| 922 |
}); |
| 923 |
} |
| 924 |
}; |
| 925 |
|
| 926 |
Chosen.prototype.generate_random_id = function() { |
| 927 |
var string; |
| 928 |
string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char(); |
| 929 |
while ($("#" + string).length > 0) { |
| 930 |
string += this.generate_random_char(); |
| 931 |
} |
| 932 |
return string; |
| 933 |
}; |
| 934 |
|
| 935 |
return Chosen; |
| 936 |
|
| 937 |
})(AbstractChosen); |
| 938 |
|
| 939 |
get_side_border_padding = function(elmt) { |
| 940 |
var side_border_padding; |
| 941 |
return side_border_padding = elmt.outerWidth() - elmt.width(); |
| 942 |
}; |
| 943 |
|
| 944 |
root.get_side_border_padding = get_side_border_padding; |
| 945 |
|
| 946 |
}).call(this); |
| 947 |
|