chosen.jquery.js 41 KB

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