chosen.proto.js 44 KB

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