chosen.jquery.js 42 KB

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