chosen.jquery.js 32 KB

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