chosen.jquery.js 39 KB

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