/Docs/07-Implementacion/Source/trunk/EDUAR_Regular/EDUAR/EDUAR_UI/Scripts/chosen/chosen.jquery.js

http://blpm.googlecode.com/ · JavaScript · 794 lines · 782 code · 1 blank · 11 comment · 161 complexity · 39e22bc2b83961c21e05b591faa7a741 MD5 · raw file

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