暫無描述

chosen.jquery.js 49KB

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