chosen.jquery.js 43 KB

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