chosen.jquery.js 42 KB

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