chosen.proto.js 40 KB

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