You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

777 regels
29 KiB

  1. (function() {
  2. /*
  3. Chosen, a Select Box Enhancer for jQuery and Protoype
  4. by Patrick Filler for Harvest, http://getharvest.com
  5. Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License
  6. Copyright (c) 2011 by Harvest
  7. */ var $, Chosen, SelectParser, get_side_border_padding, root;
  8. var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  9. root = typeof exports !== "undefined" && exports !== null ? exports : this;
  10. $ = jQuery;
  11. $.fn.extend({
  12. chosen: function(data, options) {
  13. return $(this).each(function(input_field) {
  14. if (!($(this)).hasClass("chzn-done")) {
  15. return new Chosen(this, data, options);
  16. }
  17. });
  18. }
  19. });
  20. Chosen = (function() {
  21. function Chosen(elmn) {
  22. this.set_default_values();
  23. this.form_field = elmn;
  24. this.form_field_jq = $(this.form_field);
  25. this.is_multiple = this.form_field.multiple;
  26. this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
  27. this.set_up_html();
  28. this.register_observers();
  29. this.form_field_jq.addClass("chzn-done");
  30. }
  31. Chosen.prototype.set_default_values = function() {
  32. this.click_test_action = __bind(function(evt) {
  33. return this.test_active_click(evt);
  34. }, this);
  35. this.active_field = false;
  36. this.mouse_on_container = false;
  37. this.results_showing = false;
  38. this.result_highlighted = null;
  39. this.result_single_selected = null;
  40. return this.choices = 0;
  41. };
  42. Chosen.prototype.set_up_html = function() {
  43. var container_div, dd_top, dd_width, sf_width;
  44. this.container_id = this.form_field.id.length ? this.form_field.id.replace('.', '_') : this.generate_field_id();
  45. this.container_id += "_chzn";
  46. this.f_width = this.form_field_jq.width();
  47. this.default_text = this.form_field_jq.attr('title') ? this.form_field_jq.attr('title') : this.default_text_default;
  48. container_div = $("<div />", {
  49. id: this.container_id,
  50. "class": 'chzn-container',
  51. style: 'width: ' + this.f_width + 'px;'
  52. });
  53. if (this.is_multiple) {
  54. container_div.html('<ul class="chzn-choices"><li class="search-field"><input type="text" value="' + this.default_text + '" class="default" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
  55. } else {
  56. container_div.html('<a href="javascript:void(0)" class="chzn-single"><span>' + this.default_text + '</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" /></div><ul class="chzn-results"></ul></div>');
  57. }
  58. this.form_field_jq.hide().after(container_div);
  59. this.container = $('#' + this.container_id);
  60. this.container.addClass("chzn-container-" + (this.is_multiple ? "multi" : "single"));
  61. this.dropdown = this.container.find('div.chzn-drop').first();
  62. dd_top = this.container.height();
  63. dd_width = this.f_width - get_side_border_padding(this.dropdown);
  64. this.dropdown.css({
  65. "width": dd_width + "px",
  66. "top": dd_top + "px"
  67. });
  68. this.search_field = this.container.find('input').first();
  69. this.search_results = this.container.find('ul.chzn-results').first();
  70. this.search_field_scale();
  71. this.search_no_results = this.container.find('li.no-results').first();
  72. if (this.is_multiple) {
  73. this.search_choices = this.container.find('ul.chzn-choices').first();
  74. this.search_container = this.container.find('li.search-field').first();
  75. } else {
  76. this.search_container = this.container.find('div.chzn-search').first();
  77. this.selected_item = this.container.find('.chzn-single').first();
  78. sf_width = dd_width - get_side_border_padding(this.search_container) - get_side_border_padding(this.search_field);
  79. this.search_field.css({
  80. "width": sf_width + "px"
  81. });
  82. }
  83. this.results_build();
  84. return this.set_tab_index();
  85. };
  86. Chosen.prototype.register_observers = function() {
  87. this.container.click(__bind(function(evt) {
  88. return this.container_click(evt);
  89. }, this));
  90. this.container.mouseenter(__bind(function(evt) {
  91. return this.mouse_enter(evt);
  92. }, this));
  93. this.container.mouseleave(__bind(function(evt) {
  94. return this.mouse_leave(evt);
  95. }, this));
  96. this.search_results.click(__bind(function(evt) {
  97. return this.search_results_click(evt);
  98. }, this));
  99. this.search_results.mouseover(__bind(function(evt) {
  100. return this.search_results_mouseover(evt);
  101. }, this));
  102. this.search_results.mouseout(__bind(function(evt) {
  103. return this.search_results_mouseout(evt);
  104. }, this));
  105. this.form_field_jq.bind("liszt:updated", __bind(function(evt) {
  106. return this.results_update_field(evt);
  107. }, this));
  108. this.search_field.blur(__bind(function(evt) {
  109. return this.input_blur(evt);
  110. }, this));
  111. this.search_field.keyup(__bind(function(evt) {
  112. return this.keyup_checker(evt);
  113. }, this));
  114. this.search_field.keydown(__bind(function(evt) {
  115. return this.keydown_checker(evt);
  116. }, this));
  117. if (this.is_multiple) {
  118. this.search_choices.click(__bind(function(evt) {
  119. return this.choices_click(evt);
  120. }, this));
  121. return this.search_field.focus(__bind(function(evt) {
  122. return this.input_focus(evt);
  123. }, this));
  124. } else {
  125. return this.selected_item.focus(__bind(function(evt) {
  126. return this.activate_field(evt);
  127. }, this));
  128. }
  129. };
  130. Chosen.prototype.container_click = function(evt) {
  131. if (evt && evt.type === "click") {
  132. evt.stopPropagation();
  133. }
  134. if (!this.pending_destroy_click) {
  135. if (!this.active_field) {
  136. if (this.is_multiple) {
  137. this.search_field.val("");
  138. }
  139. $(document).click(this.click_test_action);
  140. this.results_show();
  141. } else if (!this.is_multiple && evt && ($(evt.target) === this.selected_item || $(evt.target).parents("a.chzn-single").length)) {
  142. evt.preventDefault();
  143. this.results_toggle();
  144. }
  145. return this.activate_field();
  146. } else {
  147. return this.pending_destroy_click = false;
  148. }
  149. };
  150. Chosen.prototype.mouse_enter = function() {
  151. return this.mouse_on_container = true;
  152. };
  153. Chosen.prototype.mouse_leave = function() {
  154. return this.mouse_on_container = false;
  155. };
  156. Chosen.prototype.input_focus = function(evt) {
  157. if (!this.active_field) {
  158. return setTimeout((__bind(function() {
  159. return this.container_click();
  160. }, this)), 50);
  161. }
  162. };
  163. Chosen.prototype.input_blur = function(evt) {
  164. if (!this.mouse_on_container) {
  165. this.active_field = false;
  166. return setTimeout((__bind(function() {
  167. return this.blur_test();
  168. }, this)), 100);
  169. }
  170. };
  171. Chosen.prototype.blur_test = function(evt) {
  172. if (!this.active_field && this.container.hasClass("chzn-container-active")) {
  173. return this.close_field();
  174. }
  175. };
  176. Chosen.prototype.close_field = function() {
  177. $(document).unbind("click", this.click_test_action);
  178. if (!this.is_multiple) {
  179. this.selected_item.attr("tabindex", this.search_field.attr("tabindex"));
  180. this.search_field.attr("tabindex", -1);
  181. }
  182. this.active_field = false;
  183. this.results_hide();
  184. this.container.removeClass("chzn-container-active");
  185. this.winnow_results_clear();
  186. this.clear_backstroke();
  187. this.show_search_field_default();
  188. return this.search_field_scale();
  189. };
  190. Chosen.prototype.activate_field = function() {
  191. if (!this.is_multiple && !this.active_field) {
  192. this.search_field.attr("tabindex", this.selected_item.attr("tabindex"));
  193. this.selected_item.attr("tabindex", -1);
  194. }
  195. this.container.addClass("chzn-container-active");
  196. this.active_field = true;
  197. this.search_field.val(this.search_field.val());
  198. return this.search_field.focus();
  199. };
  200. Chosen.prototype.test_active_click = function(evt) {
  201. if ($(evt.target).parents('#' + this.container_id).length) {
  202. return this.active_field = true;
  203. } else {
  204. return this.close_field();
  205. }
  206. };
  207. Chosen.prototype.results_build = function() {
  208. var content, data, startTime, _i, _len, _ref;
  209. startTime = new Date();
  210. this.parsing = true;
  211. this.results_data = SelectParser.select_to_array(this.form_field);
  212. if (this.is_multiple && this.choices > 0) {
  213. this.search_choices.find("li.search-choice").remove();
  214. this.choices = 0;
  215. } else if (!this.is_multiple) {
  216. this.selected_item.find("span").text(this.default_text);
  217. }
  218. content = '';
  219. _ref = this.results_data;
  220. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  221. data = _ref[_i];
  222. if (data.group) {
  223. content += this.result_add_group(data);
  224. } else if (!data.empty) {
  225. content += this.result_add_option(data);
  226. if (data.selected && this.is_multiple) {
  227. this.choice_build(data);
  228. } else if (data.selected && !this.is_multiple) {
  229. this.selected_item.find("span").text(data.text);
  230. }
  231. }
  232. }
  233. this.show_search_field_default();
  234. this.search_field_scale();
  235. this.search_results.html(content);
  236. return this.parsing = false;
  237. };
  238. Chosen.prototype.result_add_group = function(group) {
  239. if (!group.disabled) {
  240. group.dom_id = this.container_id + "_g_" + group.array_index;
  241. return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>';
  242. } else {
  243. return "";
  244. }
  245. };
  246. Chosen.prototype.result_add_option = function(option) {
  247. var classes;
  248. if (!option.disabled) {
  249. option.dom_id = this.container_id + "_o_" + option.array_index;
  250. classes = option.selected && this.is_multiple ? [] : ["active-result"];
  251. if (option.selected) {
  252. classes.push("result-selected");
  253. }
  254. if (option.group_array_index != null) {
  255. classes.push("group-option");
  256. }
  257. return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + $("<div />").text(option.text).html() + '</li>';
  258. } else {
  259. return "";
  260. }
  261. };
  262. Chosen.prototype.results_update_field = function() {
  263. this.result_clear_highlight();
  264. this.result_single_selected = null;
  265. return this.results_build();
  266. };
  267. Chosen.prototype.result_do_highlight = function(el) {
  268. var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
  269. if (el.length) {
  270. this.result_clear_highlight();
  271. this.result_highlight = el;
  272. this.result_highlight.addClass("highlighted");
  273. maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
  274. visible_top = this.search_results.scrollTop();
  275. visible_bottom = maxHeight + visible_top;
  276. high_top = this.result_highlight.position().top + this.search_results.scrollTop();
  277. high_bottom = high_top + this.result_highlight.outerHeight();
  278. if (high_bottom >= visible_bottom) {
  279. return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
  280. } else if (high_top < visible_top) {
  281. return this.search_results.scrollTop(high_top);
  282. }
  283. }
  284. };
  285. Chosen.prototype.result_clear_highlight = function() {
  286. if (this.result_highlight) {
  287. this.result_highlight.removeClass("highlighted");
  288. }
  289. return this.result_highlight = null;
  290. };
  291. Chosen.prototype.results_toggle = function() {
  292. if (this.results_showing) {
  293. return this.results_hide();
  294. } else {
  295. return this.results_show();
  296. }
  297. };
  298. Chosen.prototype.results_show = function() {
  299. var dd_top;
  300. if (!this.is_multiple) {
  301. this.selected_item.addClass("chzn-single-with-drop");
  302. if (this.result_single_selected) {
  303. this.result_do_highlight(this.result_single_selected);
  304. }
  305. }
  306. dd_top = this.is_multiple ? this.container.height() : this.container.height() - 1;
  307. this.dropdown.css({
  308. "top": dd_top + "px",
  309. "left": 0
  310. });
  311. this.results_showing = true;
  312. this.search_field.focus();
  313. this.search_field.val(this.search_field.val());
  314. return this.winnow_results();
  315. };
  316. Chosen.prototype.results_hide = function() {
  317. if (!this.is_multiple) {
  318. this.selected_item.removeClass("chzn-single-with-drop");
  319. }
  320. this.result_clear_highlight();
  321. this.dropdown.css({
  322. "left": "-9000px"
  323. });
  324. return this.results_showing = false;
  325. };
  326. Chosen.prototype.set_tab_index = function(el) {
  327. var ti;
  328. if (this.form_field_jq.attr("tabindex")) {
  329. ti = this.form_field_jq.attr("tabindex");
  330. this.form_field_jq.attr("tabindex", -1);
  331. if (this.is_multiple) {
  332. return this.search_field.attr("tabindex", ti);
  333. } else {
  334. this.selected_item.attr("tabindex", ti);
  335. return this.search_field.attr("tabindex", -1);
  336. }
  337. }
  338. };
  339. Chosen.prototype.show_search_field_default = function() {
  340. if (this.is_multiple && this.choices < 1 && !this.active_field) {
  341. this.search_field.val(this.default_text);
  342. return this.search_field.addClass("default");
  343. } else {
  344. this.search_field.val("");
  345. return this.search_field.removeClass("default");
  346. }
  347. };
  348. Chosen.prototype.search_results_click = function(evt) {
  349. var target;
  350. target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
  351. if (target.length) {
  352. this.result_highlight = target;
  353. return this.result_select();
  354. }
  355. };
  356. Chosen.prototype.search_results_mouseover = function(evt) {
  357. var target;
  358. target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first();
  359. if (target) {
  360. return this.result_do_highlight(target);
  361. }
  362. };
  363. Chosen.prototype.search_results_mouseout = function(evt) {
  364. if ($(evt.target).hasClass("active-result" || $(evt.target).parents('.active-result').first())) {
  365. return this.result_clear_highlight();
  366. }
  367. };
  368. Chosen.prototype.choices_click = function(evt) {
  369. evt.preventDefault();
  370. if (this.active_field && !($(evt.target).hasClass("search-choice" || $(evt.target).parents('.search-choice').first)) && !this.results_showing) {
  371. return this.results_show();
  372. }
  373. };
  374. Chosen.prototype.choice_build = function(item) {
  375. var choice_id, link;
  376. choice_id = this.container_id + "_c_" + item.array_index;
  377. this.choices += 1;
  378. this.search_container.before('<li class="search-choice" id="' + choice_id + '"><span>' + item.text + '</span><a href="javascript:void(0)" class="search-choice-close" rel="' + item.array_index + '"></a></li>');
  379. link = $('#' + choice_id).find("a").first();
  380. return link.click(__bind(function(evt) {
  381. return this.choice_destroy_link_click(evt);
  382. }, this));
  383. };
  384. Chosen.prototype.choice_destroy_link_click = function(evt) {
  385. evt.preventDefault();
  386. this.pending_destroy_click = true;
  387. return this.choice_destroy($(evt.target));
  388. };
  389. Chosen.prototype.choice_destroy = function(link) {
  390. this.choices -= 1;
  391. this.show_search_field_default();
  392. if (this.is_multiple && this.choices > 0 && this.search_field.val().length < 1) {
  393. this.results_hide();
  394. }
  395. this.result_deselect(link.attr("rel"));
  396. return link.parents('li').first().remove();
  397. };
  398. Chosen.prototype.result_select = function() {
  399. var high, high_id, item, position;
  400. if (this.result_highlight) {
  401. high = this.result_highlight;
  402. high_id = high.attr("id");
  403. this.result_clear_highlight();
  404. high.addClass("result-selected");
  405. if (this.is_multiple) {
  406. this.result_deactivate(high);
  407. } else {
  408. this.result_single_selected = high;
  409. }
  410. position = high_id.substr(high_id.lastIndexOf("_") + 1);
  411. item = this.results_data[position];
  412. item.selected = true;
  413. this.form_field.options[item.options_index].selected = true;
  414. if (this.is_multiple) {
  415. this.choice_build(item);
  416. } else {
  417. this.selected_item.find("span").first().text(item.text);
  418. }
  419. this.results_hide();
  420. this.search_field.val("");
  421. this.form_field_jq.trigger("change");
  422. return this.search_field_scale();
  423. }
  424. };
  425. Chosen.prototype.result_activate = function(el) {
  426. return el.addClass("active-result").show();
  427. };
  428. Chosen.prototype.result_deactivate = function(el) {
  429. return el.removeClass("active-result").hide();
  430. };
  431. Chosen.prototype.result_deselect = function(pos) {
  432. var result, result_data;
  433. result_data = this.results_data[pos];
  434. result_data.selected = false;
  435. this.form_field.options[result_data.options_index].selected = false;
  436. result = $("#" + this.container_id + "_o_" + pos);
  437. result.removeClass("result-selected").addClass("active-result").show();
  438. this.result_clear_highlight();
  439. this.winnow_results();
  440. this.form_field_jq.trigger("change");
  441. return this.search_field_scale();
  442. };
  443. Chosen.prototype.results_search = function(evt) {
  444. if (this.results_showing) {
  445. return this.winnow_results();
  446. } else {
  447. return this.results_show();
  448. }
  449. };
  450. Chosen.prototype.winnow_results = function() {
  451. var found, option, part, parts, regex, result_id, results, searchText, startTime, startpos, text, zregex, _i, _j, _len, _len2, _ref;
  452. startTime = new Date();
  453. this.no_results_clear();
  454. results = 0;
  455. searchText = this.search_field.val() === this.default_text ? "" : $.trim(this.search_field.val());
  456. regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
  457. zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
  458. _ref = this.results_data;
  459. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  460. option = _ref[_i];
  461. if (!option.disabled && !option.empty) {
  462. if (option.group) {
  463. $('#' + option.dom_id).hide();
  464. } else if (!(this.is_multiple && option.selected)) {
  465. found = false;
  466. result_id = option.dom_id;
  467. if (regex.test(option.text)) {
  468. found = true;
  469. results += 1;
  470. } else if (option.text.indexOf(" ") >= 0 || option.text.indexOf("[") === 0) {
  471. parts = option.text.replace(/\[|\]/g, "").split(" ");
  472. if (parts.length) {
  473. for (_j = 0, _len2 = parts.length; _j < _len2; _j++) {
  474. part = parts[_j];
  475. if (regex.test(part)) {
  476. found = true;
  477. results += 1;
  478. }
  479. }
  480. }
  481. }
  482. if (found) {
  483. if (searchText.length) {
  484. startpos = option.text.search(zregex);
  485. text = option.text.substr(0, startpos + searchText.length) + '</em>' + option.text.substr(startpos + searchText.length);
  486. text = text.substr(0, startpos) + '<em>' + text.substr(startpos);
  487. } else {
  488. text = option.text;
  489. }
  490. if ($("#" + result_id).html !== text) {
  491. $("#" + result_id).html(text);
  492. }
  493. this.result_activate($("#" + result_id));
  494. if (option.group_array_index != null) {
  495. $("#" + this.results_data[option.group_array_index].dom_id).show();
  496. }
  497. } else {
  498. if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
  499. this.result_clear_highlight();
  500. }
  501. this.result_deactivate($("#" + result_id));
  502. }
  503. }
  504. }
  505. }
  506. if (results < 1 && searchText.length) {
  507. return this.no_results(searchText);
  508. } else {
  509. return this.winnow_results_set_highlight();
  510. }
  511. };
  512. Chosen.prototype.winnow_results_clear = function() {
  513. var li, lis, _i, _len, _results;
  514. this.search_field.val("");
  515. lis = this.search_results.find("li");
  516. _results = [];
  517. for (_i = 0, _len = lis.length; _i < _len; _i++) {
  518. li = lis[_i];
  519. li = $(li);
  520. _results.push(li.hasClass("group-result") ? li.show() : !this.is_multiple || !li.hasClass("result-selected") ? this.result_activate(li) : void 0);
  521. }
  522. return _results;
  523. };
  524. Chosen.prototype.winnow_results_set_highlight = function() {
  525. var do_high;
  526. if (!this.result_highlight) {
  527. do_high = this.search_results.find(".active-result").first();
  528. if (do_high) {
  529. return this.result_do_highlight(do_high);
  530. }
  531. }
  532. };
  533. Chosen.prototype.no_results = function(terms) {
  534. var no_results_html;
  535. no_results_html = $('<li class="no-results">No results match "<span></span>"</li>');
  536. no_results_html.find("span").first().text(terms);
  537. return this.search_results.append(no_results_html);
  538. };
  539. Chosen.prototype.no_results_clear = function() {
  540. return this.search_results.find(".no-results").remove();
  541. };
  542. Chosen.prototype.keydown_arrow = function() {
  543. var first_active, next_sib;
  544. if (!this.result_highlight) {
  545. first_active = this.search_results.find("li.active-result").first();
  546. if (first_active) {
  547. this.result_do_highlight($(first_active));
  548. }
  549. } else if (this.results_showing) {
  550. next_sib = this.result_highlight.nextAll("li.active-result").first();
  551. if (next_sib) {
  552. this.result_do_highlight(next_sib);
  553. }
  554. }
  555. if (!this.results_showing) {
  556. return this.results_show();
  557. }
  558. };
  559. Chosen.prototype.keyup_arrow = function() {
  560. var prev_sibs;
  561. if (!this.results_showing && !this.is_multiple) {
  562. return this.results_show();
  563. } else if (this.result_highlight) {
  564. prev_sibs = this.result_highlight.prevAll("li.active-result");
  565. if (prev_sibs.length) {
  566. return this.result_do_highlight(prev_sibs.first());
  567. } else {
  568. if (this.choices > 0) {
  569. this.results_hide();
  570. }
  571. return this.result_clear_highlight();
  572. }
  573. }
  574. };
  575. Chosen.prototype.keydown_backstroke = function() {
  576. if (this.pending_backstroke) {
  577. this.choice_destroy(this.pending_backstroke.find("a").first());
  578. return this.clear_backstroke();
  579. } else {
  580. this.pending_backstroke = this.search_container.siblings("li.search-choice").last();
  581. return this.pending_backstroke.addClass("search-choice-focus");
  582. }
  583. };
  584. Chosen.prototype.clear_backstroke = function() {
  585. if (this.pending_backstroke) {
  586. this.pending_backstroke.removeClass("search-choice-focus");
  587. }
  588. return this.pending_backstroke = null;
  589. };
  590. Chosen.prototype.keyup_checker = function(evt) {
  591. var stroke, _ref;
  592. stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
  593. this.search_field_scale();
  594. switch (stroke) {
  595. case 8:
  596. if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
  597. return this.keydown_backstroke();
  598. } else if (!this.pending_backstroke) {
  599. this.result_clear_highlight();
  600. return this.results_search();
  601. }
  602. break;
  603. case 13:
  604. evt.preventDefault();
  605. if (this.results_showing) {
  606. return this.result_select();
  607. }
  608. break;
  609. case 27:
  610. if (this.results_showing) {
  611. return this.results_hide();
  612. }
  613. break;
  614. case 9:
  615. case 38:
  616. case 40:
  617. case 16:
  618. break;
  619. default:
  620. return this.results_search();
  621. }
  622. };
  623. Chosen.prototype.keydown_checker = function(evt) {
  624. var stroke, _ref;
  625. stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
  626. this.search_field_scale();
  627. if (stroke !== 8 && this.pending_backstroke) {
  628. this.clear_backstroke();
  629. }
  630. switch (stroke) {
  631. case 8:
  632. this.backstroke_length = this.search_field.val().length;
  633. break;
  634. case 9:
  635. this.mouse_on_container = false;
  636. break;
  637. case 13:
  638. evt.preventDefault();
  639. break;
  640. case 38:
  641. evt.preventDefault();
  642. this.keyup_arrow();
  643. break;
  644. case 40:
  645. this.keydown_arrow();
  646. break;
  647. }
  648. };
  649. Chosen.prototype.search_field_scale = function() {
  650. var dd_top, div, h, style, style_block, styles, w, _i, _len;
  651. if (this.is_multiple) {
  652. h = 0;
  653. w = 0;
  654. style_block = "position:absolute; left: -1000px; top: -1000px; display:none;";
  655. styles = ['font-size', 'font-style', 'font-weight', 'font-family', 'line-height', 'text-transform', 'letter-spacing'];
  656. for (_i = 0, _len = styles.length; _i < _len; _i++) {
  657. style = styles[_i];
  658. style_block += style + ":" + this.search_field.css(style) + ";";
  659. }
  660. div = $('<div />', {
  661. 'style': style_block
  662. });
  663. div.text(this.search_field.val());
  664. $('body').append(div);
  665. w = div.width() + 25;
  666. div.remove();
  667. if (w > this.f_width - 10) {
  668. w = this.f_width - 10;
  669. }
  670. this.search_field.css({
  671. 'width': w + 'px'
  672. });
  673. dd_top = this.container.height();
  674. return this.dropdown.css({
  675. "top": dd_top + "px"
  676. });
  677. }
  678. };
  679. Chosen.prototype.generate_field_id = function() {
  680. var new_id;
  681. new_id = this.generate_random_id();
  682. this.form_field.id = new_id;
  683. return new_id;
  684. };
  685. Chosen.prototype.generate_random_id = function() {
  686. var string;
  687. string = "sel" + this.generate_random_char() + this.generate_random_char() + this.generate_random_char();
  688. while ($("#" + string).length > 0) {
  689. string += this.generate_random_char();
  690. }
  691. return string;
  692. };
  693. Chosen.prototype.generate_random_char = function() {
  694. var chars, newchar, rand;
  695. chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
  696. rand = Math.floor(Math.random() * chars.length);
  697. return newchar = chars.substring(rand, rand + 1);
  698. };
  699. return Chosen;
  700. })();
  701. get_side_border_padding = function(elmt) {
  702. var side_border_padding;
  703. return side_border_padding = elmt.outerWidth() - elmt.width();
  704. };
  705. root.get_side_border_padding = get_side_border_padding;
  706. SelectParser = (function() {
  707. function SelectParser() {
  708. this.options_index = 0;
  709. this.parsed = [];
  710. }
  711. SelectParser.prototype.add_node = function(child) {
  712. if (child.nodeName === "OPTGROUP") {
  713. return this.add_group(child);
  714. } else {
  715. return this.add_option(child);
  716. }
  717. };
  718. SelectParser.prototype.add_group = function(group) {
  719. var group_position, option, _i, _len, _ref, _results;
  720. group_position = this.parsed.length;
  721. this.parsed.push({
  722. array_index: group_position,
  723. group: true,
  724. label: group.label,
  725. children: 0,
  726. disabled: group.disabled
  727. });
  728. _ref = group.childNodes;
  729. _results = [];
  730. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  731. option = _ref[_i];
  732. _results.push(this.add_option(option, group_position, group.disabled));
  733. }
  734. return _results;
  735. };
  736. SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
  737. if (option.nodeName === "OPTION") {
  738. if (option.text !== "") {
  739. if (group_position != null) {
  740. this.parsed[group_position].children += 1;
  741. }
  742. this.parsed.push({
  743. array_index: this.parsed.length,
  744. options_index: this.options_index,
  745. value: option.value,
  746. text: option.text,
  747. selected: option.selected,
  748. disabled: group_disabled === true ? group_disabled : option.disabled,
  749. group_array_index: group_position
  750. });
  751. } else {
  752. this.parsed.push({
  753. array_index: this.parsed.length,
  754. options_index: this.options_index,
  755. empty: true
  756. });
  757. }
  758. return this.options_index += 1;
  759. }
  760. };
  761. return SelectParser;
  762. })();
  763. SelectParser.select_to_array = function(select) {
  764. var child, parser, _i, _len, _ref;
  765. parser = new SelectParser();
  766. _ref = select.childNodes;
  767. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  768. child = _ref[_i];
  769. parser.add_node(child);
  770. }
  771. return parser.parsed;
  772. };
  773. root.SelectParser = SelectParser;
  774. }).call(this);