chosen.proto.js 44 KB

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