chosen.jquery.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  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);