chosen.proto.js 42 KB

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