chosen.jquery.js 38 KB

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