jquery.chosen.js 30 KB

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