chosen.jquery.js 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  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. addRoleId(item.value); //添加副职角色
  871. close_link = $('<a />', {
  872. "class": 'search-choice-close',
  873. 'data-option-array-index': item.array_index
  874. });
  875. close_link.bind('click.chosen', function(evt) {
  876. removeRoleId(item.value); //移除副职角色
  877. return _this.choice_destroy_link_click(evt);
  878. });
  879. choice.append(close_link);
  880. }
  881. return this.search_container.before(choice);
  882. };
  883. Chosen.prototype.choice_destroy_link_click = function(evt) {
  884. evt.preventDefault();
  885. evt.stopPropagation();
  886. if (!this.is_disabled) {
  887. return this.choice_destroy($(evt.target));
  888. }
  889. };
  890. Chosen.prototype.choice_destroy = function(link) {
  891. if (this.result_deselect(link[0].getAttribute("data-option-array-index"))) {
  892. this.show_search_field_default();
  893. if (this.is_multiple && this.choices_count() > 0 && this.search_field.val().length < 1) {
  894. this.results_hide();
  895. }
  896. link.parents('li').first().remove();
  897. return this.search_field_scale();
  898. }
  899. };
  900. Chosen.prototype.results_reset = function() {
  901. this.reset_single_select_options();
  902. this.form_field.options[0].selected = true;
  903. this.single_set_selected_text();
  904. this.show_search_field_default();
  905. this.results_reset_cleanup();
  906. this.form_field_jq.trigger("change");
  907. if (this.active_field) {
  908. return this.results_hide();
  909. }
  910. };
  911. Chosen.prototype.results_reset_cleanup = function() {
  912. this.current_selectedIndex = this.form_field.selectedIndex;
  913. return this.selected_item.find("abbr").remove();
  914. };
  915. Chosen.prototype.result_select = function(evt) {
  916. var high, item;
  917. if (this.result_highlight) {
  918. high = this.result_highlight;
  919. this.result_clear_highlight();
  920. if (this.is_multiple && this.max_selected_options <= this.choices_count()) {
  921. this.form_field_jq.trigger("chosen:maxselected", {
  922. chosen: this
  923. });
  924. return false;
  925. }
  926. if (this.is_multiple) {
  927. high.removeClass("active-result");
  928. } else {
  929. this.reset_single_select_options();
  930. }
  931. item = this.results_data[high[0].getAttribute("data-option-array-index")];
  932. item.selected = true;
  933. this.form_field.options[item.options_index].selected = true;
  934. this.selected_option_count = null;
  935. if (this.is_multiple) {
  936. this.choice_build(item);
  937. } else {
  938. this.single_set_selected_text(item.text);
  939. }
  940. if (!((evt.metaKey || evt.ctrlKey) && this.is_multiple)) {
  941. this.results_hide();
  942. }
  943. this.search_field.val("");
  944. if (this.is_multiple || this.form_field.selectedIndex !== this.current_selectedIndex) {
  945. this.form_field_jq.trigger("change", {
  946. 'selected': this.form_field.options[item.options_index].value
  947. });
  948. }
  949. this.current_selectedIndex = this.form_field.selectedIndex;
  950. return this.search_field_scale();
  951. }
  952. };
  953. Chosen.prototype.single_set_selected_text = function(text) {
  954. if (text == null) {
  955. text = this.default_text;
  956. }
  957. if (text === this.default_text) {
  958. this.selected_item.addClass("chosen-default");
  959. } else {
  960. this.single_deselect_control_build();
  961. this.selected_item.removeClass("chosen-default");
  962. }
  963. return this.selected_item.find("span").text(text);
  964. };
  965. Chosen.prototype.result_deselect = function(pos) {
  966. var result_data;
  967. result_data = this.results_data[pos];
  968. if (!this.form_field.options[result_data.options_index].disabled) {
  969. result_data.selected = false;
  970. this.form_field.options[result_data.options_index].selected = false;
  971. this.selected_option_count = null;
  972. this.result_clear_highlight();
  973. if (this.results_showing) {
  974. this.winnow_results();
  975. }
  976. this.form_field_jq.trigger("change", {
  977. deselected: this.form_field.options[result_data.options_index].value
  978. });
  979. this.search_field_scale();
  980. return true;
  981. } else {
  982. return false;
  983. }
  984. };
  985. Chosen.prototype.single_deselect_control_build = function() {
  986. if (!this.allow_single_deselect) {
  987. return;
  988. }
  989. if (!this.selected_item.find("abbr").length) {
  990. this.selected_item.find("span").first().after("<abbr class=\"search-choice-close\"></abbr>");
  991. }
  992. return this.selected_item.addClass("chosen-single-with-deselect");
  993. };
  994. Chosen.prototype.get_search_text = function() {
  995. if (this.search_field.val() === this.default_text) {
  996. return "";
  997. } else {
  998. return $('<div/>').text($.trim(this.search_field.val())).html();
  999. }
  1000. };
  1001. Chosen.prototype.winnow_results_set_highlight = function() {
  1002. var do_high, selected_results;
  1003. selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : [];
  1004. do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first();
  1005. if (do_high != null) {
  1006. return this.result_do_highlight(do_high);
  1007. }
  1008. };
  1009. Chosen.prototype.no_results = function(terms) {
  1010. var no_results_html;
  1011. no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
  1012. no_results_html.find("span").first().html(terms);
  1013. this.search_results.append(no_results_html);
  1014. return this.form_field_jq.trigger("chosen:no_results", {
  1015. chosen: this
  1016. });
  1017. };
  1018. Chosen.prototype.no_results_clear = function() {
  1019. return this.search_results.find(".no-results").remove();
  1020. };
  1021. Chosen.prototype.keydown_arrow = function() {
  1022. var next_sib;
  1023. if (this.results_showing && this.result_highlight) {
  1024. next_sib = this.result_highlight.nextAll("li.active-result").first();
  1025. if (next_sib) {
  1026. return this.result_do_highlight(next_sib);
  1027. }
  1028. } else {
  1029. return this.results_show();
  1030. }
  1031. };
  1032. Chosen.prototype.keyup_arrow = function() {
  1033. var prev_sibs;
  1034. if (!this.results_showing && !this.is_multiple) {
  1035. return this.results_show();
  1036. } else if (this.result_highlight) {
  1037. prev_sibs = this.result_highlight.prevAll("li.active-result");
  1038. if (prev_sibs.length) {
  1039. return this.result_do_highlight(prev_sibs.first());
  1040. } else {
  1041. if (this.choices_count() > 0) {
  1042. this.results_hide();
  1043. }
  1044. return this.result_clear_highlight();
  1045. }
  1046. }
  1047. };
  1048. Chosen.prototype.keydown_backstroke = function() {
  1049. var next_available_destroy;
  1050. if (this.pending_backstroke) {
  1051. this.choice_destroy(this.pending_backstroke.find("a").first());
  1052. return this.clear_backstroke();
  1053. } else {
  1054. next_available_destroy = this.search_container.siblings("li.search-choice").last();
  1055. if (next_available_destroy.length && !next_available_destroy.hasClass("search-choice-disabled")) {
  1056. this.pending_backstroke = next_available_destroy;
  1057. if (this.single_backstroke_delete) {
  1058. return this.keydown_backstroke();
  1059. } else {
  1060. return this.pending_backstroke.addClass("search-choice-focus");
  1061. }
  1062. }
  1063. }
  1064. };
  1065. Chosen.prototype.clear_backstroke = function() {
  1066. if (this.pending_backstroke) {
  1067. this.pending_backstroke.removeClass("search-choice-focus");
  1068. }
  1069. return this.pending_backstroke = null;
  1070. };
  1071. Chosen.prototype.keydown_checker = function(evt) {
  1072. var stroke, _ref1;
  1073. stroke = (_ref1 = evt.which) != null ? _ref1 : evt.keyCode;
  1074. this.search_field_scale();
  1075. if (stroke !== 8 && this.pending_backstroke) {
  1076. this.clear_backstroke();
  1077. }
  1078. switch (stroke) {
  1079. case 8:
  1080. this.backstroke_length = this.search_field.val().length;
  1081. break;
  1082. case 9:
  1083. if (this.results_showing && !this.is_multiple) {
  1084. this.result_select(evt);
  1085. }
  1086. this.mouse_on_container = false;
  1087. break;
  1088. case 13:
  1089. if (this.results_showing) {
  1090. evt.preventDefault();
  1091. }
  1092. break;
  1093. case 32:
  1094. if (this.disable_search) {
  1095. evt.preventDefault();
  1096. }
  1097. break;
  1098. case 38:
  1099. evt.preventDefault();
  1100. this.keyup_arrow();
  1101. break;
  1102. case 40:
  1103. evt.preventDefault();
  1104. this.keydown_arrow();
  1105. break;
  1106. }
  1107. };
  1108. Chosen.prototype.search_field_scale = function() {
  1109. var div, f_width, h, style, style_block, styles, w, _i, _len;
  1110. if (this.is_multiple) {
  1111. h = 0;
  1112. w = 0;
  1113. style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
  1114. styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
  1115. for (_i = 0, _len = styles.length; _i < _len; _i++) {
  1116. style = styles[_i];
  1117. style_block += style + ":" + this.search_field.css(style) + ";";
  1118. }
  1119. div = $('<div />', {
  1120. 'style': style_block
  1121. });
  1122. div.text(this.search_field.val());
  1123. $('body').append(div);
  1124. w = div.width() + 25;
  1125. div.remove();
  1126. f_width = this.container.outerWidth();
  1127. if (w > f_width - 10) {
  1128. w = f_width - 10;
  1129. }
  1130. return this.search_field.css({
  1131. 'width': w + 'px'
  1132. });
  1133. }
  1134. };
  1135. return Chosen;
  1136. })(AbstractChosen);
  1137. }).call(this);