chosen.jquery.js 44 KB

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