chosen.jquery.js 44 KB

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