From 6f1e62204b053dcc784d932d613bf577a3979c56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anderson=20Gru=CC=88dtner=20Martins?= Date: Thu, 2 Aug 2012 17:16:49 -0300 Subject: [PATCH] Add the ability to set a custom value just typing into search field Add the ability to set a custom value just typing into search field - The new texts accept custom values; - Add option to active that behavior adding a class "chzn-custom-value" to select or specifying an option "allow_custom_value"= true; - The custom values are added a new group at the end of the option list; --- Cakefile | 9 +- chosen/chosen.jquery.js | 163 +++++++++++++++++++++++------- chosen/chosen.jquery.min.js | 7 +- chosen/chosen.proto.js | 138 ++++++++++++++++++------- chosen/chosen.proto.min.js | 7 +- coffee/chosen.jquery.coffee | 28 ++++- coffee/lib/abstract-chosen.coffee | 21 +++- 7 files changed, 291 insertions(+), 82 deletions(-) diff --git a/Cakefile b/Cakefile index 8d5963938ab..27d10dfe277 100644 --- a/Cakefile +++ b/Cakefile @@ -50,10 +50,13 @@ write_chosen_javascript = (filename, body, trailing='') -> fs.writeFileSync filename, """ // Chosen, a Select Box Enhancer for jQuery and Protoype // by Patrick Filler for Harvest, http://getharvest.com -// +// // Version #{version()} // Full source at https://github.com/harvesthq/chosen // Copyright (c) 2011 Harvest http://getharvest.com +// +// Changelog: +// [2012-08-02] - Add option to specify a custom value on the fly [Anderson Grüdtner Martins] // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // This file is generated by `cake build`, do not edit it by hand. @@ -81,7 +84,7 @@ task 'build', 'build Chosen from source', build = (cb) -> cb() if typeof cb is 'function' catch e print_error e, file_name, file_contents - + task 'watch', 'watch coffee/ for changes and build Chosen', -> console.log "Watching for changes in coffee/" for file in source_files() @@ -154,7 +157,7 @@ print_error = (error, file_name, file_contents) -> else console.log """ Error compiling #{file_name}: - + #{error.message} """ diff --git a/chosen/chosen.jquery.js b/chosen/chosen.jquery.js index 8b28bc045fa..4322282ca36 100644 --- a/chosen/chosen.jquery.js +++ b/chosen/chosen.jquery.js @@ -1,9 +1,12 @@ // Chosen, a Select Box Enhancer for jQuery and Protoype // by Patrick Filler for Harvest, http://getharvest.com -// +// // Version 0.9.8 // Full source at https://github.com/harvesthq/chosen // Copyright (c) 2011 Harvest http://getharvest.com +// +// Changelog: +// [2012-08-02] - Add option to specify a custom value on the fly [Anderson Grüdtner Martins] // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // This file is generated by `cake build`, do not edit it by hand. @@ -47,7 +50,9 @@ SelectParser.prototype.add_option = function(option, group_position, group_disabled) { if (option.nodeName === "OPTION") { if (option.text !== "") { - if (group_position != null) this.parsed[group_position].children += 1; + if (group_position != null) { + this.parsed[group_position].children += 1; + } this.parsed.push({ array_index: this.parsed.length, options_index: this.options_index, @@ -95,6 +100,7 @@ Chosen source: generate output using 'cake build' Copyright (c) 2011 by Harvest */ + (function() { var AbstractChosen, root; @@ -107,8 +113,8 @@ Copyright (c) 2011 by Harvest this.options = options != null ? options : {}; this.set_default_values(); this.is_multiple = this.form_field.multiple; - this.set_default_text(); this.setup(); + this.set_default_text(); this.set_up_html(); this.register_observers(); this.finish_setup(); @@ -132,18 +138,29 @@ Copyright (c) 2011 by Harvest this.search_contains = this.options.search_contains || false; this.choices = 0; this.single_backstroke_delete = this.options.single_backstroke_delete || false; - return this.max_selected_options = this.options.max_selected_options || Infinity; + this.max_selected_options = this.options.max_selected_options || Infinity; + this.allow_custom_value = false; + return true; }; AbstractChosen.prototype.set_default_text = function() { + var results_none_found_default; if (this.form_field.getAttribute("data-placeholder")) { this.default_text = this.form_field.getAttribute("data-placeholder"); } else if (this.is_multiple) { this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || "Select Some Options"; + } else if (this.allow_custom_value) { + this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Type or Select an Option"; } else { this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Select an Option"; } - return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match"; + if (this.allow_custom_value) { + this.custom_group_text = this.form_field.getAttribute("data-custom_group_text") || this.options.custom_group_text || "Custom Value"; + results_none_found_default = "Add custom value"; + } else { + results_none_found_default = "No results match"; + } + return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || results_none_found_default; }; AbstractChosen.prototype.mouse_enter = function() { @@ -178,9 +195,15 @@ Copyright (c) 2011 by Harvest if (!option.disabled) { option.dom_id = this.container_id + "_o_" + option.array_index; classes = option.selected && this.is_multiple ? [] : ["active-result"]; - if (option.selected) classes.push("result-selected"); - if (option.group_array_index != null) classes.push("group-option"); - if (option.classes !== "") classes.push(option.classes); + if (option.selected) { + classes.push("result-selected"); + } + if (option.group_array_index != null) { + classes.push("group-option"); + } + if (option.classes !== "") { + classes.push(option.classes); + } style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; return '
  • ' + option.html + '
  • '; } else { @@ -189,7 +212,9 @@ Copyright (c) 2011 by Harvest }; AbstractChosen.prototype.results_update_field = function() { - if (!this.is_multiple) this.results_reset_cleanup(); + if (!this.is_multiple) { + this.results_reset_cleanup(); + } this.result_clear_highlight(); this.result_single_selected = null; return this.results_build(); @@ -226,10 +251,14 @@ Copyright (c) 2011 by Harvest break; case 13: evt.preventDefault(); - if (this.results_showing) return this.result_select(evt); + if (this.results_showing) { + return this.result_select(evt); + } break; case 27: - if (this.results_showing) this.results_hide(); + if (this.results_showing) { + this.results_hide(); + } return true; case 9: case 38: @@ -270,10 +299,11 @@ Chosen source: generate output using 'cake build' Copyright (c) 2011 by Harvest */ + (function() { var $, Chosen, get_side_border_padding, root, - __hasProp = Object.prototype.hasOwnProperty, - __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; }; + __hasProp = {}.hasOwnProperty, + __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; }; root = this; @@ -299,13 +329,14 @@ Copyright (c) 2011 by Harvest __extends(Chosen, _super); function Chosen() { - Chosen.__super__.constructor.apply(this, arguments); + return Chosen.__super__.constructor.apply(this, arguments); } Chosen.prototype.setup = function() { this.form_field_jq = $(this.form_field); this.current_value = this.form_field_jq.val(); - return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); + this.is_rtl = this.form_field_jq.hasClass("chzn-rtl"); + return this.allow_custom_value = this.form_field_jq.hasClass("chzn-custom-value" || this.options.allow_custom_value); }; Chosen.prototype.finish_setup = function() { @@ -435,7 +466,9 @@ Copyright (c) 2011 by Harvest } if (!this.pending_destroy_click && !target_closelink) { if (!this.active_field) { - if (this.is_multiple) this.search_field.val(""); + if (this.is_multiple) { + this.search_field.val(""); + } $(document).click(this.click_test_action); this.results_show(); } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) { @@ -522,7 +555,9 @@ Copyright (c) 2011 by Harvest this.choice_build(data); } else if (data.selected && !this.is_multiple) { this.selected_item.removeClass("chzn-default").find("span").text(data.text); - if (this.allow_single_deselect) this.single_deselect_control_build(); + if (this.allow_single_deselect) { + this.single_deselect_control_build(); + } } } } @@ -562,7 +597,9 @@ Copyright (c) 2011 by Harvest }; Chosen.prototype.result_clear_highlight = function() { - if (this.result_highlight) this.result_highlight.removeClass("highlighted"); + if (this.result_highlight) { + this.result_highlight.removeClass("highlighted"); + } return this.result_highlight = null; }; @@ -643,7 +680,9 @@ Copyright (c) 2011 by Harvest Chosen.prototype.search_results_mouseover = function(evt) { var target; target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); - if (target) return this.result_do_highlight(target); + if (target) { + return this.result_do_highlight(target); + } }; Chosen.prototype.search_results_mouseout = function(evt) { @@ -700,11 +739,15 @@ Copyright (c) 2011 by Harvest Chosen.prototype.results_reset = function() { this.form_field.options[0].selected = true; this.selected_item.find("span").text(this.default_text); - if (!this.is_multiple) this.selected_item.addClass("chzn-default"); + if (!this.is_multiple) { + this.selected_item.addClass("chzn-default"); + } this.show_search_field_default(); this.results_reset_cleanup(); this.form_field_jq.trigger("change"); - if (this.active_field) return this.results_hide(); + if (this.active_field) { + return this.results_hide(); + } }; Chosen.prototype.results_reset_cleanup = function() { @@ -712,7 +755,7 @@ Copyright (c) 2011 by Harvest }; Chosen.prototype.result_select = function(evt) { - var high, high_id, item, position; + var group, high, high_id, item, option, position, value; if (this.result_highlight) { high = this.result_highlight; high_id = high.attr("id"); @@ -733,9 +776,13 @@ Copyright (c) 2011 by Harvest this.choice_build(item); } else { this.selected_item.find("span").first().text(item.text); - if (this.allow_single_deselect) this.single_deselect_control_build(); + if (this.allow_single_deselect) { + this.single_deselect_control_build(); + } + } + if (!(evt.metaKey && this.is_multiple)) { + this.results_hide(); } - if (!(evt.metaKey && this.is_multiple)) this.results_hide(); this.search_field.val(""); if (this.is_multiple || this.form_field_jq.val() !== this.current_value) { this.form_field_jq.trigger("change", { @@ -744,7 +791,39 @@ Copyright (c) 2011 by Harvest } this.current_value = this.form_field_jq.val(); return this.search_field_scale(); + } else if (!this.is_multiple) { + value = this.search_field.val(); + group = this.add_unique_custom_group(); + option = $(''); + group.append(option); + this.form_field_jq.append(group); + this.form_field.options[this.form_field.options.length - 1].selected = true; + if (!evt.metaKey) { + this.results_hide(); + } + return this.results_build(); + } + }; + + Chosen.prototype.find_custom_group = function() { + var found, group, _i, _len, _ref; + _ref = $('optgroup', this.form_field); + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + group = _ref[_i]; + if (group.getAttribute('label') === this.custom_group_text) { + found = group; + } + } + return found; + }; + + Chosen.prototype.add_unique_custom_group = function() { + var group; + group = this.find_custom_group(); + if (!group) { + group = $(''); } + return $(group); }; Chosen.prototype.result_activate = function(el) { @@ -777,7 +856,7 @@ Copyright (c) 2011 by Harvest }; Chosen.prototype.winnow_results = function() { - var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len2, _ref; + var found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref; this.no_results_clear(); results = 0; searchText = this.search_field.val() === this.default_text ? "" : $('
    ').text($.trim(this.search_field.val())).html(); @@ -800,7 +879,7 @@ Copyright (c) 2011 by Harvest } else if (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0) { parts = option.html.replace(/\[|\]/g, "").split(" "); if (parts.length) { - for (_j = 0, _len2 = parts.length; _j < _len2; _j++) { + for (_j = 0, _len1 = parts.length; _j < _len1; _j++) { part = parts[_j]; if (regex.test(part)) { found = true; @@ -862,7 +941,9 @@ Copyright (c) 2011 by Harvest if (!this.result_highlight) { selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : []; do_high = selected_results.length ? selected_results.first() : this.search_results.find(".active-result").first(); - if (do_high != null) return this.result_do_highlight(do_high); + if (do_high != null) { + return this.result_do_highlight(do_high); + } } }; @@ -881,12 +962,18 @@ Copyright (c) 2011 by Harvest var first_active, next_sib; if (!this.result_highlight) { first_active = this.search_results.find("li.active-result").first(); - if (first_active) this.result_do_highlight($(first_active)); + if (first_active) { + this.result_do_highlight($(first_active)); + } } else if (this.results_showing) { next_sib = this.result_highlight.nextAll("li.active-result").first(); - if (next_sib) this.result_do_highlight(next_sib); + if (next_sib) { + this.result_do_highlight(next_sib); + } + } + if (!this.results_showing) { + return this.results_show(); } - if (!this.results_showing) return this.results_show(); }; Chosen.prototype.keyup_arrow = function() { @@ -898,7 +985,9 @@ Copyright (c) 2011 by Harvest if (prev_sibs.length) { return this.result_do_highlight(prev_sibs.first()); } else { - if (this.choices > 0) this.results_hide(); + if (this.choices > 0) { + this.results_hide(); + } return this.result_clear_highlight(); } } @@ -929,13 +1018,17 @@ Copyright (c) 2011 by Harvest var stroke, _ref; stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; this.search_field_scale(); - if (stroke !== 8 && this.pending_backstroke) this.clear_backstroke(); + if (stroke !== 8 && this.pending_backstroke) { + this.clear_backstroke(); + } switch (stroke) { case 8: this.backstroke_length = this.search_field.val().length; break; case 9: - if (this.results_showing && !this.is_multiple) this.result_select(evt); + if (this.results_showing && !this.is_multiple) { + this.result_select(evt); + } this.mouse_on_container = false; break; case 13: @@ -969,7 +1062,9 @@ Copyright (c) 2011 by Harvest $('body').append(div); w = div.width() + 25; div.remove(); - if (w > this.f_width - 10) w = this.f_width - 10; + if (w > this.f_width - 10) { + w = this.f_width - 10; + } this.search_field.css({ 'width': w + 'px' }); diff --git a/chosen/chosen.jquery.min.js b/chosen/chosen.jquery.min.js index 022d6620be4..2aa8c219094 100644 --- a/chosen/chosen.jquery.min.js +++ b/chosen/chosen.jquery.min.js @@ -1,10 +1,13 @@ // Chosen, a Select Box Enhancer for jQuery and Protoype // by Patrick Filler for Harvest, http://getharvest.com -// +// // Version 0.9.8 // Full source at https://github.com/harvesthq/chosen // Copyright (c) 2011 Harvest http://getharvest.com +// +// Changelog: +// [2012-08-02] - Add option to specify a custom value on the fly [Anderson Grüdtner Martins] // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // This file is generated by `cake build`, do not edit it by hand. -((function(){var a;a=function(){function a(){this.options_index=0,this.parsed=[]}return a.prototype.add_node=function(a){return a.nodeName==="OPTGROUP"?this.add_group(a):this.add_option(a)},a.prototype.add_group=function(a){var b,c,d,e,f,g;b=this.parsed.length,this.parsed.push({array_index:b,group:!0,label:a.label,children:0,disabled:a.disabled}),f=a.childNodes,g=[];for(d=0,e=f.length;d"+a.html+"")},a.prototype.results_update_field=function(){return this.is_multiple||this.results_reset_cleanup(),this.result_clear_highlight(),this.result_single_selected=null,this.results_build()},a.prototype.results_toggle=function(){return this.results_showing?this.results_hide():this.results_show()},a.prototype.results_search=function(a){return this.results_showing?this.winnow_results():this.results_show()},a.prototype.keyup_checker=function(a){var b,c;b=(c=a.which)!=null?c:a.keyCode,this.search_field_scale();switch(b){case 8:if(this.is_multiple&&this.backstroke_length<1&&this.choices>0)return this.keydown_backstroke();if(!this.pending_backstroke)return this.result_clear_highlight(),this.results_search();break;case 13:a.preventDefault();if(this.results_showing)return this.result_select(a);break;case 27:return this.results_showing&&this.results_hide(),!0;case 9:case 38:case 40:case 16:case 91:case 17:break;default:return this.results_search()}},a.prototype.generate_field_id=function(){var a;return a=this.generate_random_id(),this.form_field.id=a,a},a.prototype.generate_random_char=function(){var a,b,c;return a="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",c=Math.floor(Math.random()*a.length),b=a.substring(c,c+1)},a}(),b.AbstractChosen=a}.call(this),function(){var a,b,c,d,e=Object.prototype.hasOwnProperty,f=function(a,b){function d(){this.constructor=a}for(var c in b)e.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a};d=this,a=jQuery,a.fn.extend({chosen:function(c){return!a.browser.msie||a.browser.version!=="6.0"&&a.browser.version!=="7.0"?this.each(function(d){var e;e=a(this);if(!e.hasClass("chzn-done"))return e.data("chosen",new b(this,c))}):this}}),b=function(b){function e(){e.__super__.constructor.apply(this,arguments)}return f(e,b),e.prototype.setup=function(){return this.form_field_jq=a(this.form_field),this.current_value=this.form_field_jq.val(),this.is_rtl=this.form_field_jq.hasClass("chzn-rtl")},e.prototype.finish_setup=function(){return this.form_field_jq.addClass("chzn-done")},e.prototype.set_up_html=function(){var b,d,e,f;return this.container_id=this.form_field.id.length?this.form_field.id.replace(/[^\w]/g,"_"):this.generate_field_id(),this.container_id+="_chzn",this.f_width=this.form_field_jq.outerWidth(),b=a("
    ",{id:this.container_id,"class":"chzn-container"+(this.is_rtl?" chzn-rtl":""),style:"width: "+this.f_width+"px;"}),this.is_multiple?b.html('
      '):b.html(''+this.default_text+'
        '),this.form_field_jq.hide().after(b),this.container=a("#"+this.container_id),this.container.addClass("chzn-container-"+(this.is_multiple?"multi":"single")),this.dropdown=this.container.find("div.chzn-drop").first(),d=this.container.height(),e=this.f_width-c(this.dropdown),this.dropdown.css({width:e+"px",top:d+"px"}),this.search_field=this.container.find("input").first(),this.search_results=this.container.find("ul.chzn-results").first(),this.search_field_scale(),this.search_no_results=this.container.find("li.no-results").first(),this.is_multiple?(this.search_choices=this.container.find("ul.chzn-choices").first(),this.search_container=this.container.find("li.search-field").first()):(this.search_container=this.container.find("div.chzn-search").first(),this.selected_item=this.container.find(".chzn-single").first(),f=e-c(this.search_container)-c(this.search_field),this.search_field.css({width:f+"px"})),this.results_build(),this.set_tab_index(),this.form_field_jq.trigger("liszt:ready",{chosen:this})},e.prototype.register_observers=function(){var a=this;return this.container.mousedown(function(b){return a.container_mousedown(b)}),this.container.mouseup(function(b){return a.container_mouseup(b)}),this.container.mouseenter(function(b){return a.mouse_enter(b)}),this.container.mouseleave(function(b){return a.mouse_leave(b)}),this.search_results.mouseup(function(b){return a.search_results_mouseup(b)}),this.search_results.mouseover(function(b){return a.search_results_mouseover(b)}),this.search_results.mouseout(function(b){return a.search_results_mouseout(b)}),this.form_field_jq.bind("liszt:updated",function(b){return a.results_update_field(b)}),this.search_field.blur(function(b){return a.input_blur(b)}),this.search_field.keyup(function(b){return a.keyup_checker(b)}),this.search_field.keydown(function(b){return a.keydown_checker(b)}),this.is_multiple?(this.search_choices.click(function(b){return a.choices_click(b)}),this.search_field.focus(function(b){return a.input_focus(b)})):this.container.click(function(a){return a.preventDefault()})},e.prototype.search_field_disabled=function(){this.is_disabled=this.form_field_jq[0].disabled;if(this.is_disabled)return this.container.addClass("chzn-disabled"),this.search_field[0].disabled=!0,this.is_multiple||this.selected_item.unbind("focus",this.activate_action),this.close_field();this.container.removeClass("chzn-disabled"),this.search_field[0].disabled=!1;if(!this.is_multiple)return this.selected_item.bind("focus",this.activate_action)},e.prototype.container_mousedown=function(b){var c;if(!this.is_disabled)return c=b!=null?a(b.target).hasClass("search-choice-close"):!1,b&&b.type==="mousedown"&&!this.results_showing&&b.stopPropagation(),!this.pending_destroy_click&&!c?(this.active_field?!this.is_multiple&&b&&(a(b.target)[0]===this.selected_item[0]||a(b.target).parents("a.chzn-single").length)&&(b.preventDefault(),this.results_toggle()):(this.is_multiple&&this.search_field.val(""),a(document).click(this.click_test_action),this.results_show()),this.activate_field()):this.pending_destroy_click=!1},e.prototype.container_mouseup=function(a){if(a.target.nodeName==="ABBR"&&!this.is_disabled)return this.results_reset(a)},e.prototype.blur_test=function(a){if(!this.active_field&&this.container.hasClass("chzn-container-active"))return this.close_field()},e.prototype.close_field=function(){return a(document).unbind("click",this.click_test_action),this.is_multiple||(this.selected_item.attr("tabindex",this.search_field.attr("tabindex")),this.search_field.attr("tabindex",-1)),this.active_field=!1,this.results_hide(),this.container.removeClass("chzn-container-active"),this.winnow_results_clear(),this.clear_backstroke(),this.show_search_field_default(),this.search_field_scale()},e.prototype.activate_field=function(){return!this.is_multiple&&!this.active_field&&(this.search_field.attr("tabindex",this.selected_item.attr("tabindex")),this.selected_item.attr("tabindex",-1)),this.container.addClass("chzn-container-active"),this.active_field=!0,this.search_field.val(this.search_field.val()),this.search_field.focus()},e.prototype.test_active_click=function(b){return a(b.target).parents("#"+this.container_id).length?this.active_field=!0:this.close_field()},e.prototype.results_build=function(){var a,b,c,e,f;this.parsing=!0,this.results_data=d.SelectParser.select_to_array(this.form_field),this.is_multiple&&this.choices>0?(this.search_choices.find("li.search-choice").remove(),this.choices=0):this.is_multiple||(this.selected_item.addClass("chzn-default").find("span").text(this.default_text),this.form_field.options.length<=this.disable_search_threshold?this.container.addClass("chzn-container-single-nosearch"):this.container.removeClass("chzn-container-single-nosearch")),a="",f=this.results_data;for(c=0,e=f.length;c'+a("
        ").text(b.label).html()+"")},e.prototype.result_do_highlight=function(a){var b,c,d,e,f;if(a.length){this.result_clear_highlight(),this.result_highlight=a,this.result_highlight.addClass("highlighted"),d=parseInt(this.search_results.css("maxHeight"),10),f=this.search_results.scrollTop(),e=d+f,c=this.result_highlight.position().top+this.search_results.scrollTop(),b=c+this.result_highlight.outerHeight();if(b>=e)return this.search_results.scrollTop(b-d>0?b-d:0);if(c'+b.html+''),d=a("#"+c).find("a").first(),d.click(function(a){return e.choice_destroy_link_click(a)}))},e.prototype.choice_destroy_link_click=function(b){return b.preventDefault(),this.is_disabled?b.stopPropagation:(this.pending_destroy_click=!0,this.choice_destroy(a(b.target)))},e.prototype.choice_destroy=function(a){return this.choices-=1,this.show_search_field_default(),this.is_multiple&&this.choices>0&&this.search_field.val().length<1&&this.results_hide(),this.result_deselect(a.attr("rel")),a.parents("li").first().remove()},e.prototype.results_reset=function(){this.form_field.options[0].selected=!0,this.selected_item.find("span").text(this.default_text),this.is_multiple||this.selected_item.addClass("chzn-default"),this.show_search_field_default(),this.results_reset_cleanup(),this.form_field_jq.trigger("change");if(this.active_field)return this.results_hide()},e.prototype.results_reset_cleanup=function(){return this.selected_item.find("abbr").remove()},e.prototype.result_select=function(a){var b,c,d,e;if(this.result_highlight)return b=this.result_highlight,c=b.attr("id"),this.result_clear_highlight(),this.is_multiple?this.result_deactivate(b):(this.search_results.find(".result-selected").removeClass("result-selected"),this.result_single_selected=b,this.selected_item.removeClass("chzn-default")),b.addClass("result-selected"),e=c.substr(c.lastIndexOf("_")+1),d=this.results_data[e],d.selected=!0,this.form_field.options[d.options_index].selected=!0,this.is_multiple?this.choice_build(d):(this.selected_item.find("span").first().text(d.text),this.allow_single_deselect&&this.single_deselect_control_build()),(!a.metaKey||!this.is_multiple)&&this.results_hide(),this.search_field.val(""),(this.is_multiple||this.form_field_jq.val()!==this.current_value)&&this.form_field_jq.trigger("change",{selected:this.form_field.options[d.options_index].value}),this.current_value=this.form_field_jq.val(),this.search_field_scale()},e.prototype.result_activate=function(a){return a.addClass("active-result")},e.prototype.result_deactivate=function(a){return a.removeClass("active-result")},e.prototype.result_deselect=function(b){var c,d;return d=this.results_data[b],d.selected=!1,this.form_field.options[d.options_index].selected=!1,c=a("#"+this.container_id+"_o_"+b),c.removeClass("result-selected").addClass("active-result").show(),this.result_clear_highlight(),this.winnow_results(),this.form_field_jq.trigger("change",{deselected:this.form_field.options[d.options_index].value}),this.search_field_scale()},e.prototype.single_deselect_control_build=function(){if(this.allow_single_deselect&&this.selected_item.find("abbr").length<1)return this.selected_item.find("span").first().after('')},e.prototype.winnow_results=function(){var b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s;this.no_results_clear(),j=0,k=this.search_field.val()===this.default_text?"":a("
        ").text(a.trim(this.search_field.val())).html(),g=this.search_contains?"":"^",f=new RegExp(g+k.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),n=new RegExp(k.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),s=this.results_data;for(o=0,q=s.length;o=0||c.html.indexOf("[")===0){e=c.html.replace(/\[|\]/g,"").split(" ");if(e.length)for(p=0,r=e.length;p"+c.html.substr(l+k.length),m=m.substr(0,l)+""+m.substr(l)):m=c.html,h.html(m),this.result_activate(h),c.group_array_index!=null&&a("#"+this.results_data[c.group_array_index].dom_id).css("display","list-item")):(this.result_highlight&&i===this.result_highlight.attr("id")&&this.result_clear_highlight(),this.result_deactivate(h))}}return j<1&&k.length?this.no_results(k):this.winnow_results_set_highlight()},e.prototype.winnow_results_clear=function(){var b,c,d,e,f;this.search_field.val(""),c=this.search_results.find("li"),f=[];for(d=0,e=c.length;d'+this.results_none_found+' ""'),c.find("span").first().html(b),this.search_results.append(c)},e.prototype.no_results_clear=function(){return this.search_results.find(".no-results").remove()},e.prototype.keydown_arrow=function(){var b,c;this.result_highlight?this.results_showing&&(c=this.result_highlight.nextAll("li.active-result").first(),c&&this.result_do_highlight(c)):(b=this.search_results.find("li.active-result").first(),b&&this.result_do_highlight(a(b)));if(!this.results_showing)return this.results_show()},e.prototype.keyup_arrow=function(){var a;if(!this.results_showing&&!this.is_multiple)return this.results_show();if(this.result_highlight)return a=this.result_highlight.prevAll("li.active-result"),a.length?this.result_do_highlight(a.first()):(this.choices>0&&this.results_hide(),this.result_clear_highlight())},e.prototype.keydown_backstroke=function(){return this.pending_backstroke?(this.choice_destroy(this.pending_backstroke.find("a").first()),this.clear_backstroke()):(this.pending_backstroke=this.search_container.siblings("li.search-choice").last(),this.single_backstroke_delete?this.keydown_backstroke():this.pending_backstroke.addClass("search-choice-focus"))},e.prototype.clear_backstroke=function(){return this.pending_backstroke&&this.pending_backstroke.removeClass("search-choice-focus"),this.pending_backstroke=null},e.prototype.keydown_checker=function(a){var b,c;b=(c=a.which)!=null?c:a.keyCode,this.search_field_scale(),b!==8&&this.pending_backstroke&&this.clear_backstroke();switch(b){case 8:this.backstroke_length=this.search_field.val().length;break;case 9:this.results_showing&&!this.is_multiple&&this.result_select(a),this.mouse_on_container=!1;break;case 13:a.preventDefault();break;case 38:a.preventDefault(),this.keyup_arrow();break;case 40:this.keydown_arrow()}},e.prototype.search_field_scale=function(){var b,c,d,e,f,g,h,i,j;if(this.is_multiple){d=0,h=0,f="position:absolute; left: -1000px; top: -1000px; display:none;",g=["font-size","font-style","font-weight","font-family","line-height","text-transform","letter-spacing"];for(i=0,j=g.length;i",{style:f}),c.text(this.search_field.val()),a("body").append(c),h=c.width()+25,c.remove(),h>this.f_width-10&&(h=this.f_width-10),this.search_field.css({width:h+"px"}),b=this.container.height(),this.dropdown.css({top:b+"px"})}},e.prototype.generate_random_id=function(){var b;b="sel"+this.generate_random_char()+this.generate_random_char()+this.generate_random_char();while(a("#"+b).length>0)b+=this.generate_random_char();return b},e}(AbstractChosen),c=function(a){var b;return b=a.outerWidth()-a.width()},d.get_side_border_padding=c}.call(this); \ No newline at end of file +(function(){var e;e=function(){function e(){this.options_index=0,this.parsed=[]}return e.prototype.add_node=function(e){return e.nodeName==="OPTGROUP"?this.add_group(e):this.add_option(e)},e.prototype.add_group=function(e){var t,n,r,i,s,o;t=this.parsed.length,this.parsed.push({array_index:t,group:!0,label:e.label,children:0,disabled:e.disabled}),s=e.childNodes,o=[];for(r=0,i=s.length;r"+e.html+"")},e.prototype.results_update_field=function(){return this.is_multiple||this.results_reset_cleanup(),this.result_clear_highlight(),this.result_single_selected=null,this.results_build()},e.prototype.results_toggle=function(){return this.results_showing?this.results_hide():this.results_show()},e.prototype.results_search=function(e){return this.results_showing?this.winnow_results():this.results_show()},e.prototype.keyup_checker=function(e){var t,n;t=(n=e.which)!=null?n:e.keyCode,this.search_field_scale();switch(t){case 8:if(this.is_multiple&&this.backstroke_length<1&&this.choices>0)return this.keydown_backstroke();if(!this.pending_backstroke)return this.result_clear_highlight(),this.results_search();break;case 13:e.preventDefault();if(this.results_showing)return this.result_select(e);break;case 27:return this.results_showing&&this.results_hide(),!0;case 9:case 38:case 40:case 16:case 91:case 17:break;default:return this.results_search()}},e.prototype.generate_field_id=function(){var e;return e=this.generate_random_id(),this.form_field.id=e,e},e.prototype.generate_random_char=function(){var e,t,n;return e="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",n=Math.floor(Math.random()*e.length),t=e.substring(n,n+1)},e}(),t.AbstractChosen=e}.call(this),function(){var e,t,n,r,i={}.hasOwnProperty,s=function(e,t){function r(){this.constructor=e}for(var n in t)i.call(t,n)&&(e[n]=t[n]);return r.prototype=t.prototype,e.prototype=new r,e.__super__=t.prototype,e};r=this,e=jQuery,e.fn.extend({chosen:function(n){return!e.browser.msie||e.browser.version!=="6.0"&&e.browser.version!=="7.0"?this.each(function(r){var i;i=e(this);if(!i.hasClass("chzn-done"))return i.data("chosen",new t(this,n))}):this}}),t=function(t){function i(){return i.__super__.constructor.apply(this,arguments)}return s(i,t),i.prototype.setup=function(){return this.form_field_jq=e(this.form_field),this.current_value=this.form_field_jq.val(),this.is_rtl=this.form_field_jq.hasClass("chzn-rtl"),this.allow_custom_value=this.form_field_jq.hasClass("chzn-custom-value")},i.prototype.finish_setup=function(){return this.form_field_jq.addClass("chzn-done")},i.prototype.set_up_html=function(){var t,r,i,s;return this.container_id=this.form_field.id.length?this.form_field.id.replace(/[^\w]/g,"_"):this.generate_field_id(),this.container_id+="_chzn",this.f_width=this.form_field_jq.outerWidth(),t=e("
        ",{id:this.container_id,"class":"chzn-container"+(this.is_rtl?" chzn-rtl":""),style:"width: "+this.f_width+"px;"}),this.is_multiple?t.html('
          '):t.html(''+this.default_text+'
            '),this.form_field_jq.hide().after(t),this.container=e("#"+this.container_id),this.container.addClass("chzn-container-"+(this.is_multiple?"multi":"single")),this.dropdown=this.container.find("div.chzn-drop").first(),r=this.container.height(),i=this.f_width-n(this.dropdown),this.dropdown.css({width:i+"px",top:r+"px"}),this.search_field=this.container.find("input").first(),this.search_results=this.container.find("ul.chzn-results").first(),this.search_field_scale(),this.search_no_results=this.container.find("li.no-results").first(),this.is_multiple?(this.search_choices=this.container.find("ul.chzn-choices").first(),this.search_container=this.container.find("li.search-field").first()):(this.search_container=this.container.find("div.chzn-search").first(),this.selected_item=this.container.find(".chzn-single").first(),s=i-n(this.search_container)-n(this.search_field),this.search_field.css({width:s+"px"})),this.results_build(),this.set_tab_index(),this.form_field_jq.trigger("liszt:ready",{chosen:this})},i.prototype.register_observers=function(){var e=this;return this.container.mousedown(function(t){return e.container_mousedown(t)}),this.container.mouseup(function(t){return e.container_mouseup(t)}),this.container.mouseenter(function(t){return e.mouse_enter(t)}),this.container.mouseleave(function(t){return e.mouse_leave(t)}),this.search_results.mouseup(function(t){return e.search_results_mouseup(t)}),this.search_results.mouseover(function(t){return e.search_results_mouseover(t)}),this.search_results.mouseout(function(t){return e.search_results_mouseout(t)}),this.form_field_jq.bind("liszt:updated",function(t){return e.results_update_field(t)}),this.search_field.blur(function(t){return e.input_blur(t)}),this.search_field.keyup(function(t){return e.keyup_checker(t)}),this.search_field.keydown(function(t){return e.keydown_checker(t)}),this.is_multiple?(this.search_choices.click(function(t){return e.choices_click(t)}),this.search_field.focus(function(t){return e.input_focus(t)})):this.container.click(function(e){return e.preventDefault()})},i.prototype.search_field_disabled=function(){this.is_disabled=this.form_field_jq[0].disabled;if(this.is_disabled)return this.container.addClass("chzn-disabled"),this.search_field[0].disabled=!0,this.is_multiple||this.selected_item.unbind("focus",this.activate_action),this.close_field();this.container.removeClass("chzn-disabled"),this.search_field[0].disabled=!1;if(!this.is_multiple)return this.selected_item.bind("focus",this.activate_action)},i.prototype.container_mousedown=function(t){var n;if(!this.is_disabled)return n=t!=null?e(t.target).hasClass("search-choice-close"):!1,t&&t.type==="mousedown"&&!this.results_showing&&t.stopPropagation(),!this.pending_destroy_click&&!n?(this.active_field?!this.is_multiple&&t&&(e(t.target)[0]===this.selected_item[0]||e(t.target).parents("a.chzn-single").length)&&(t.preventDefault(),this.results_toggle()):(this.is_multiple&&this.search_field.val(""),e(document).click(this.click_test_action),this.results_show()),this.activate_field()):this.pending_destroy_click=!1},i.prototype.container_mouseup=function(e){if(e.target.nodeName==="ABBR"&&!this.is_disabled)return this.results_reset(e)},i.prototype.blur_test=function(e){if(!this.active_field&&this.container.hasClass("chzn-container-active"))return this.close_field()},i.prototype.close_field=function(){return e(document).unbind("click",this.click_test_action),this.is_multiple||(this.selected_item.attr("tabindex",this.search_field.attr("tabindex")),this.search_field.attr("tabindex",-1)),this.active_field=!1,this.results_hide(),this.container.removeClass("chzn-container-active"),this.winnow_results_clear(),this.clear_backstroke(),this.show_search_field_default(),this.search_field_scale()},i.prototype.activate_field=function(){return!this.is_multiple&&!this.active_field&&(this.search_field.attr("tabindex",this.selected_item.attr("tabindex")),this.selected_item.attr("tabindex",-1)),this.container.addClass("chzn-container-active"),this.active_field=!0,this.search_field.val(this.search_field.val()),this.search_field.focus()},i.prototype.test_active_click=function(t){return e(t.target).parents("#"+this.container_id).length?this.active_field=!0:this.close_field()},i.prototype.results_build=function(){var e,t,n,i,s;this.parsing=!0,this.results_data=r.SelectParser.select_to_array(this.form_field),this.is_multiple&&this.choices>0?(this.search_choices.find("li.search-choice").remove(),this.choices=0):this.is_multiple||(this.selected_item.addClass("chzn-default").find("span").text(this.default_text),this.form_field.options.length<=this.disable_search_threshold?this.container.addClass("chzn-container-single-nosearch"):this.container.removeClass("chzn-container-single-nosearch")),e="",s=this.results_data;for(n=0,i=s.length;n'+e("
            ").text(t.label).html()+"")},i.prototype.result_do_highlight=function(e){var t,n,r,i,s;if(e.length){this.result_clear_highlight(),this.result_highlight=e,this.result_highlight.addClass("highlighted"),r=parseInt(this.search_results.css("maxHeight"),10),s=this.search_results.scrollTop(),i=r+s,n=this.result_highlight.position().top+this.search_results.scrollTop(),t=n+this.result_highlight.outerHeight();if(t>=i)return this.search_results.scrollTop(t-r>0?t-r:0);if(n'+t.html+''),r=e("#"+n).find("a").first(),r.click(function(e){return i.choice_destroy_link_click(e)}))},i.prototype.choice_destroy_link_click=function(t){return t.preventDefault(),this.is_disabled?t.stopPropagation:(this.pending_destroy_click=!0,this.choice_destroy(e(t.target)))},i.prototype.choice_destroy=function(e){return this.choices-=1,this.show_search_field_default(),this.is_multiple&&this.choices>0&&this.search_field.val().length<1&&this.results_hide(),this.result_deselect(e.attr("rel")),e.parents("li").first().remove()},i.prototype.results_reset=function(){this.form_field.options[0].selected=!0,this.selected_item.find("span").text(this.default_text),this.is_multiple||this.selected_item.addClass("chzn-default"),this.show_search_field_default(),this.results_reset_cleanup(),this.form_field_jq.trigger("change");if(this.active_field)return this.results_hide()},i.prototype.results_reset_cleanup=function(){return this.selected_item.find("abbr").remove()},i.prototype.result_select=function(t){var n,r,i,s,o,u,a;if(this.result_highlight)return r=this.result_highlight,i=r.attr("id"),this.result_clear_highlight(),this.is_multiple?this.result_deactivate(r):(this.search_results.find(".result-selected").removeClass("result-selected"),this.result_single_selected=r,this.selected_item.removeClass("chzn-default")),r.addClass("result-selected"),u=i.substr(i.lastIndexOf("_")+1),s=this.results_data[u],s.selected=!0,this.form_field.options[s.options_index].selected=!0,this.is_multiple?this.choice_build(s):(this.selected_item.find("span").first().text(s.text),this.allow_single_deselect&&this.single_deselect_control_build()),(!t.metaKey||!this.is_multiple)&&this.results_hide(),this.search_field.val(""),(this.is_multiple||this.form_field_jq.val()!==this.current_value)&&this.form_field_jq.trigger("change",{selected:this.form_field.options[s.options_index].value}),this.current_value=this.form_field_jq.val(),this.search_field_scale();if(!this.is_multiple)return a=this.search_field.val(),n=this.add_unique_custom_group(),o=e('"),n.append(o),this.form_field_jq.append(n),this.form_field.options[this.form_field.options.length-1].selected=!0,t.metaKey||this.results_hide(),this.results_build()},i.prototype.find_custom_group=function(){var t,n,r,i,s;s=e("optgroup",this.form_field);for(r=0,i=s.length;r')),e(t)},i.prototype.result_activate=function(e){return e.addClass("active-result")},i.prototype.result_deactivate=function(e){return e.removeClass("active-result")},i.prototype.result_deselect=function(t){var n,r;return r=this.results_data[t],r.selected=!1,this.form_field.options[r.options_index].selected=!1,n=e("#"+this.container_id+"_o_"+t),n.removeClass("result-selected").addClass("active-result").show(),this.result_clear_highlight(),this.winnow_results(),this.form_field_jq.trigger("change",{deselected:this.form_field.options[r.options_index].value}),this.search_field_scale()},i.prototype.single_deselect_control_build=function(){if(this.allow_single_deselect&&this.selected_item.find("abbr").length<1)return this.selected_item.find("span").first().after('')},i.prototype.winnow_results=function(){var t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m,g,y;this.no_results_clear(),f=0,l=this.search_field.val()===this.default_text?"":e("
            ").text(e.trim(this.search_field.val())).html(),o=this.search_contains?"":"^",s=new RegExp(o+l.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),p=new RegExp(l.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),y=this.results_data;for(d=0,m=y.length;d=0||n.html.indexOf("[")===0){i=n.html.replace(/\[|\]/g,"").split(" ");if(i.length)for(v=0,g=i.length;v"+n.html.substr(c+l.length),h=h.substr(0,c)+""+h.substr(c)):h=n.html,u.html(h),this.result_activate(u),n.group_array_index!=null&&e("#"+this.results_data[n.group_array_index].dom_id).css("display","list-item")):(this.result_highlight&&a===this.result_highlight.attr("id")&&this.result_clear_highlight(),this.result_deactivate(u))}}return f<1&&l.length?this.no_results(l):this.winnow_results_set_highlight()},i.prototype.winnow_results_clear=function(){var t,n,r,i,s;this.search_field.val(""),n=this.search_results.find("li"),s=[];for(r=0,i=n.length;r'+this.results_none_found+' ""'),n.find("span").first().html(t),this.search_results.append(n)},i.prototype.no_results_clear=function(){return this.search_results.find(".no-results").remove()},i.prototype.keydown_arrow=function(){var t,n;this.result_highlight?this.results_showing&&(n=this.result_highlight.nextAll("li.active-result").first(),n&&this.result_do_highlight(n)):(t=this.search_results.find("li.active-result").first(),t&&this.result_do_highlight(e(t)));if(!this.results_showing)return this.results_show()},i.prototype.keyup_arrow=function(){var e;if(!this.results_showing&&!this.is_multiple)return this.results_show();if(this.result_highlight)return e=this.result_highlight.prevAll("li.active-result"),e.length?this.result_do_highlight(e.first()):(this.choices>0&&this.results_hide(),this.result_clear_highlight())},i.prototype.keydown_backstroke=function(){return this.pending_backstroke?(this.choice_destroy(this.pending_backstroke.find("a").first()),this.clear_backstroke()):(this.pending_backstroke=this.search_container.siblings("li.search-choice").last(),this.single_backstroke_delete?this.keydown_backstroke():this.pending_backstroke.addClass("search-choice-focus"))},i.prototype.clear_backstroke=function(){return this.pending_backstroke&&this.pending_backstroke.removeClass("search-choice-focus"),this.pending_backstroke=null},i.prototype.keydown_checker=function(e){var t,n;t=(n=e.which)!=null?n:e.keyCode,this.search_field_scale(),t!==8&&this.pending_backstroke&&this.clear_backstroke();switch(t){case 8:this.backstroke_length=this.search_field.val().length;break;case 9:this.results_showing&&!this.is_multiple&&this.result_select(e),this.mouse_on_container=!1;break;case 13:e.preventDefault();break;case 38:e.preventDefault(),this.keyup_arrow();break;case 40:this.keydown_arrow()}},i.prototype.search_field_scale=function(){var t,n,r,i,s,o,u,a,f;if(this.is_multiple){r=0,u=0,s="position:absolute; left: -1000px; top: -1000px; display:none;",o=["font-size","font-style","font-weight","font-family","line-height","text-transform","letter-spacing"];for(a=0,f=o.length;a",{style:s}),n.text(this.search_field.val()),e("body").append(n),u=n.width()+25,n.remove(),u>this.f_width-10&&(u=this.f_width-10),this.search_field.css({width:u+"px"}),t=this.container.height(),this.dropdown.css({top:t+"px"})}},i.prototype.generate_random_id=function(){var t;t="sel"+this.generate_random_char()+this.generate_random_char()+this.generate_random_char();while(e("#"+t).length>0)t+=this.generate_random_char();return t},i}(AbstractChosen),n=function(e){var t;return t=e.outerWidth()-e.width()},r.get_side_border_padding=n}.call(this); \ No newline at end of file diff --git a/chosen/chosen.proto.js b/chosen/chosen.proto.js index 66d3a42cf65..23f30d74813 100644 --- a/chosen/chosen.proto.js +++ b/chosen/chosen.proto.js @@ -1,9 +1,12 @@ // Chosen, a Select Box Enhancer for jQuery and Protoype // by Patrick Filler for Harvest, http://getharvest.com -// +// // Version 0.9.8 // Full source at https://github.com/harvesthq/chosen // Copyright (c) 2011 Harvest http://getharvest.com +// +// Changelog: +// [2012-08-02] - Add option to specify a custom value on the fly [Anderson Grüdtner Martins] // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // This file is generated by `cake build`, do not edit it by hand. @@ -47,7 +50,9 @@ SelectParser.prototype.add_option = function(option, group_position, group_disabled) { if (option.nodeName === "OPTION") { if (option.text !== "") { - if (group_position != null) this.parsed[group_position].children += 1; + if (group_position != null) { + this.parsed[group_position].children += 1; + } this.parsed.push({ array_index: this.parsed.length, options_index: this.options_index, @@ -95,6 +100,7 @@ Chosen source: generate output using 'cake build' Copyright (c) 2011 by Harvest */ + (function() { var AbstractChosen, root; @@ -107,8 +113,8 @@ Copyright (c) 2011 by Harvest this.options = options != null ? options : {}; this.set_default_values(); this.is_multiple = this.form_field.multiple; - this.set_default_text(); this.setup(); + this.set_default_text(); this.set_up_html(); this.register_observers(); this.finish_setup(); @@ -132,18 +138,29 @@ Copyright (c) 2011 by Harvest this.search_contains = this.options.search_contains || false; this.choices = 0; this.single_backstroke_delete = this.options.single_backstroke_delete || false; - return this.max_selected_options = this.options.max_selected_options || Infinity; + this.max_selected_options = this.options.max_selected_options || Infinity; + this.allow_custom_value = false; + return true; }; AbstractChosen.prototype.set_default_text = function() { + var results_none_found_default; if (this.form_field.getAttribute("data-placeholder")) { this.default_text = this.form_field.getAttribute("data-placeholder"); } else if (this.is_multiple) { this.default_text = this.options.placeholder_text_multiple || this.options.placeholder_text || "Select Some Options"; + } else if (this.allow_custom_value) { + this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Type or Select an Option"; } else { this.default_text = this.options.placeholder_text_single || this.options.placeholder_text || "Select an Option"; } - return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match"; + if (this.allow_custom_value) { + this.custom_group_text = this.form_field.getAttribute("data-custom_group_text") || this.options.custom_group_text || "Custom Value"; + results_none_found_default = "Add custom value"; + } else { + results_none_found_default = "No results match"; + } + return this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || results_none_found_default; }; AbstractChosen.prototype.mouse_enter = function() { @@ -178,9 +195,15 @@ Copyright (c) 2011 by Harvest if (!option.disabled) { option.dom_id = this.container_id + "_o_" + option.array_index; classes = option.selected && this.is_multiple ? [] : ["active-result"]; - if (option.selected) classes.push("result-selected"); - if (option.group_array_index != null) classes.push("group-option"); - if (option.classes !== "") classes.push(option.classes); + if (option.selected) { + classes.push("result-selected"); + } + if (option.group_array_index != null) { + classes.push("group-option"); + } + if (option.classes !== "") { + classes.push(option.classes); + } style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; return '
          • ' + option.html + '
          • '; } else { @@ -189,7 +212,9 @@ Copyright (c) 2011 by Harvest }; AbstractChosen.prototype.results_update_field = function() { - if (!this.is_multiple) this.results_reset_cleanup(); + if (!this.is_multiple) { + this.results_reset_cleanup(); + } this.result_clear_highlight(); this.result_single_selected = null; return this.results_build(); @@ -226,10 +251,14 @@ Copyright (c) 2011 by Harvest break; case 13: evt.preventDefault(); - if (this.results_showing) return this.result_select(evt); + if (this.results_showing) { + return this.result_select(evt); + } break; case 27: - if (this.results_showing) this.results_hide(); + if (this.results_showing) { + this.results_hide(); + } return true; case 9: case 38: @@ -270,10 +299,11 @@ Chosen source: generate output using 'cake build' Copyright (c) 2011 by Harvest */ + (function() { var Chosen, get_side_border_padding, root, - __hasProp = Object.prototype.hasOwnProperty, - __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; }; + __hasProp = {}.hasOwnProperty, + __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; }; root = this; @@ -282,7 +312,7 @@ Copyright (c) 2011 by Harvest __extends(Chosen, _super); function Chosen() { - Chosen.__super__.constructor.apply(this, arguments); + return Chosen.__super__.constructor.apply(this, arguments); } Chosen.prototype.setup = function() { @@ -421,10 +451,14 @@ Copyright (c) 2011 by Harvest var target_closelink; if (!this.is_disabled) { target_closelink = evt != null ? evt.target.hasClassName("search-choice-close") : false; - if (evt && evt.type === "mousedown" && !this.results_showing) evt.stop(); + if (evt && evt.type === "mousedown" && !this.results_showing) { + evt.stop(); + } if (!this.pending_destroy_click && !target_closelink) { if (!this.active_field) { - if (this.is_multiple) this.search_field.clear(); + if (this.is_multiple) { + this.search_field.clear(); + } document.observe("click", this.click_test_action); this.results_show(); } else if (!this.is_multiple && evt && (evt.target === this.selected_item || evt.target.up("a.chzn-single"))) { @@ -510,7 +544,9 @@ Copyright (c) 2011 by Harvest this.choice_build(data); } else if (data.selected && !this.is_multiple) { this.selected_item.removeClassName("chzn-default").down("span").update(data.html); - if (this.allow_single_deselect) this.single_deselect_control_build(); + if (this.allow_single_deselect) { + this.single_deselect_control_build(); + } } } } @@ -631,7 +667,9 @@ Copyright (c) 2011 by Harvest Chosen.prototype.search_results_mouseover = function(evt) { var target; target = evt.target.hasClassName("active-result") ? evt.target : evt.target.up(".active-result"); - if (target) return this.result_do_highlight(target); + if (target) { + return this.result_do_highlight(target); + } }; Chosen.prototype.search_results_mouseout = function(evt) { @@ -692,17 +730,25 @@ Copyright (c) 2011 by Harvest Chosen.prototype.results_reset = function() { this.form_field.options[0].selected = true; this.selected_item.down("span").update(this.default_text); - if (!this.is_multiple) this.selected_item.addClassName("chzn-default"); + if (!this.is_multiple) { + this.selected_item.addClassName("chzn-default"); + } this.show_search_field_default(); this.results_reset_cleanup(); - if (typeof Event.simulate === 'function') this.form_field.simulate("change"); - if (this.active_field) return this.results_hide(); + if (typeof Event.simulate === 'function') { + this.form_field.simulate("change"); + } + if (this.active_field) { + return this.results_hide(); + } }; Chosen.prototype.results_reset_cleanup = function() { var deselect_trigger; deselect_trigger = this.selected_item.down("abbr"); - if (deselect_trigger) return deselect_trigger.remove(); + if (deselect_trigger) { + return deselect_trigger.remove(); + } }; Chosen.prototype.result_select = function(evt) { @@ -726,9 +772,13 @@ Copyright (c) 2011 by Harvest this.choice_build(item); } else { this.selected_item.down("span").update(item.html); - if (this.allow_single_deselect) this.single_deselect_control_build(); + if (this.allow_single_deselect) { + this.single_deselect_control_build(); + } + } + if (!(evt.metaKey && this.is_multiple)) { + this.results_hide(); } - if (!(evt.metaKey && this.is_multiple)) this.results_hide(); this.search_field.value = ""; if (typeof Event.simulate === 'function' && (this.is_multiple || this.form_field.value !== this.current_value)) { this.form_field.simulate("change"); @@ -755,7 +805,9 @@ Copyright (c) 2011 by Harvest result.removeClassName("result-selected").addClassName("active-result").show(); this.result_clear_highlight(); this.winnow_results(); - if (typeof Event.simulate === 'function') this.form_field.simulate("change"); + if (typeof Event.simulate === 'function') { + this.form_field.simulate("change"); + } return this.search_field_scale(); }; @@ -768,7 +820,7 @@ Copyright (c) 2011 by Harvest }; Chosen.prototype.winnow_results = function() { - var found, option, part, parts, regex, regexAnchor, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len2, _ref; + var found, option, part, parts, regex, regexAnchor, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref; this.no_results_clear(); results = 0; searchText = this.search_field.value === this.default_text ? "" : this.search_field.value.strip().escapeHTML(); @@ -790,7 +842,7 @@ Copyright (c) 2011 by Harvest } else if (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0) { parts = option.html.replace(/\[|\]/g, "").split(" "); if (parts.length) { - for (_j = 0, _len2 = parts.length; _j < _len2; _j++) { + for (_j = 0, _len1 = parts.length; _j < _len1; _j++) { part = parts[_j]; if (regex.test(part)) { found = true; @@ -807,7 +859,9 @@ Copyright (c) 2011 by Harvest } else { text = option.html; } - if ($(result_id).innerHTML !== text) $(result_id).update(text); + if ($(result_id).innerHTML !== text) { + $(result_id).update(text); + } this.result_activate($(result_id)); if (option.group_array_index != null) { $(this.results_data[option.group_array_index].dom_id).setStyle({ @@ -857,7 +911,9 @@ Copyright (c) 2011 by Harvest if (!(do_high != null)) { do_high = this.search_results.down(".active-result"); } - if (do_high != null) return this.result_do_highlight(do_high); + if (do_high != null) { + return this.result_do_highlight(do_high); + } } }; @@ -886,9 +942,13 @@ Copyright (c) 2011 by Harvest } else if (this.results_showing) { sibs = this.result_highlight.nextSiblings(); nexts = sibs.intersect(actives); - if (nexts.length) this.result_do_highlight(nexts.first()); + if (nexts.length) { + this.result_do_highlight(nexts.first()); + } + } + if (!this.results_showing) { + return this.results_show(); } - if (!this.results_showing) return this.results_show(); } }; @@ -903,7 +963,9 @@ Copyright (c) 2011 by Harvest if (prevs.length) { return this.result_do_highlight(prevs.first()); } else { - if (this.choices > 0) this.results_hide(); + if (this.choices > 0) { + this.results_hide(); + } return this.result_clear_highlight(); } } @@ -934,13 +996,17 @@ Copyright (c) 2011 by Harvest var stroke, _ref; stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; this.search_field_scale(); - if (stroke !== 8 && this.pending_backstroke) this.clear_backstroke(); + if (stroke !== 8 && this.pending_backstroke) { + this.clear_backstroke(); + } switch (stroke) { case 8: this.backstroke_length = this.search_field.value.length; break; case 9: - if (this.results_showing && !this.is_multiple) this.result_select(evt); + if (this.results_showing && !this.is_multiple) { + this.result_select(evt); + } this.mouse_on_container = false; break; case 13: @@ -973,7 +1039,9 @@ Copyright (c) 2011 by Harvest document.body.appendChild(div); w = Element.measure(div, 'width') + 25; div.remove(); - if (w > this.f_width - 10) w = this.f_width - 10; + if (w > this.f_width - 10) { + w = this.f_width - 10; + } this.search_field.setStyle({ 'width': w + 'px' }); diff --git a/chosen/chosen.proto.min.js b/chosen/chosen.proto.min.js index c181b9d2647..f58d78d98a1 100644 --- a/chosen/chosen.proto.min.js +++ b/chosen/chosen.proto.min.js @@ -1,10 +1,13 @@ // Chosen, a Select Box Enhancer for jQuery and Protoype // by Patrick Filler for Harvest, http://getharvest.com -// +// // Version 0.9.8 // Full source at https://github.com/harvesthq/chosen // Copyright (c) 2011 Harvest http://getharvest.com +// +// Changelog: +// [2012-08-02] - Add option to specify a custom value on the fly [Anderson Grüdtner Martins] // MIT License, https://github.com/harvesthq/chosen/blob/master/LICENSE.md // This file is generated by `cake build`, do not edit it by hand. -((function(){var a;a=function(){function a(){this.options_index=0,this.parsed=[]}return a.prototype.add_node=function(a){return a.nodeName==="OPTGROUP"?this.add_group(a):this.add_option(a)},a.prototype.add_group=function(a){var b,c,d,e,f,g;b=this.parsed.length,this.parsed.push({array_index:b,group:!0,label:a.label,children:0,disabled:a.disabled}),f=a.childNodes,g=[];for(d=0,e=f.length;d"+a.html+"")},a.prototype.results_update_field=function(){return this.is_multiple||this.results_reset_cleanup(),this.result_clear_highlight(),this.result_single_selected=null,this.results_build()},a.prototype.results_toggle=function(){return this.results_showing?this.results_hide():this.results_show()},a.prototype.results_search=function(a){return this.results_showing?this.winnow_results():this.results_show()},a.prototype.keyup_checker=function(a){var b,c;b=(c=a.which)!=null?c:a.keyCode,this.search_field_scale();switch(b){case 8:if(this.is_multiple&&this.backstroke_length<1&&this.choices>0)return this.keydown_backstroke();if(!this.pending_backstroke)return this.result_clear_highlight(),this.results_search();break;case 13:a.preventDefault();if(this.results_showing)return this.result_select(a);break;case 27:return this.results_showing&&this.results_hide(),!0;case 9:case 38:case 40:case 16:case 91:case 17:break;default:return this.results_search()}},a.prototype.generate_field_id=function(){var a;return a=this.generate_random_id(),this.form_field.id=a,a},a.prototype.generate_random_char=function(){var a,b,c;return a="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",c=Math.floor(Math.random()*a.length),b=a.substring(c,c+1)},a}(),b.AbstractChosen=a}.call(this),function(){var a,b,c,d=Object.prototype.hasOwnProperty,e=function(a,b){function e(){this.constructor=a}for(var c in b)d.call(b,c)&&(a[c]=b[c]);return e.prototype=b.prototype,a.prototype=new e,a.__super__=b.prototype,a};c=this,a=function(a){function d(){d.__super__.constructor.apply(this,arguments)}return e(d,a),d.prototype.setup=function(){return this.current_value=this.form_field.value,this.is_rtl=this.form_field.hasClassName("chzn-rtl")},d.prototype.finish_setup=function(){return this.form_field.addClassName("chzn-done")},d.prototype.set_default_values=function(){return d.__super__.set_default_values.call(this),this.single_temp=new Template('#{default}
              '),this.multi_temp=new Template('
                '),this.choice_temp=new Template('
              • #{choice}
              • '),this.no_results_temp=new Template('
              • '+this.results_none_found+' "#{terms}"
              • ')},d.prototype.set_up_html=function(){var a,c,d,e,f;return this.container_id=this.form_field.identify().replace(/[^\w]/g,"_")+"_chzn",this.f_width=this.form_field.getStyle("width")?parseInt(this.form_field.getStyle("width"),10):this.form_field.getWidth(),c={id:this.container_id,"class":"chzn-container"+(this.is_rtl?" chzn-rtl":""),style:"width: "+this.f_width+"px"},a=this.is_multiple?(new Element("div",c)).update(this.multi_temp.evaluate({"default":this.default_text})):(new Element("div",c)).update(this.single_temp.evaluate({"default":this.default_text})),this.form_field.hide().insert({after:a}),this.container=$(this.container_id),this.container.addClassName("chzn-container-"+(this.is_multiple?"multi":"single")),this.dropdown=this.container.down("div.chzn-drop"),d=this.container.getHeight(),e=this.f_width-b(this.dropdown),this.dropdown.setStyle({width:e+"px",top:d+"px"}),this.search_field=this.container.down("input"),this.search_results=this.container.down("ul.chzn-results"),this.search_field_scale(),this.search_no_results=this.container.down("li.no-results"),this.is_multiple?(this.search_choices=this.container.down("ul.chzn-choices"),this.search_container=this.container.down("li.search-field")):(this.search_container=this.container.down("div.chzn-search"),this.selected_item=this.container.down(".chzn-single"),f=e-b(this.search_container)-b(this.search_field),this.search_field.setStyle({width:f+"px"})),this.results_build(),this.set_tab_index(),this.form_field.fire("liszt:ready",{chosen:this})},d.prototype.register_observers=function(){var a=this;return this.container.observe("mousedown",function(b){return a.container_mousedown(b)}),this.container.observe("mouseup",function(b){return a.container_mouseup(b)}),this.container.observe("mouseenter",function(b){return a.mouse_enter(b)}),this.container.observe("mouseleave",function(b){return a.mouse_leave(b)}),this.search_results.observe("mouseup",function(b){return a.search_results_mouseup(b)}),this.search_results.observe("mouseover",function(b){return a.search_results_mouseover(b)}),this.search_results.observe("mouseout",function(b){return a.search_results_mouseout(b)}),this.form_field.observe("liszt:updated",function(b){return a.results_update_field(b)}),this.search_field.observe("blur",function(b){return a.input_blur(b)}),this.search_field.observe("keyup",function(b){return a.keyup_checker(b)}),this.search_field.observe("keydown",function(b){return a.keydown_checker(b)}),this.is_multiple?(this.search_choices.observe("click",function(b){return a.choices_click(b)}),this.search_field.observe("focus",function(b){return a.input_focus(b)})):this.container.observe("click",function(a){return a.preventDefault()})},d.prototype.search_field_disabled=function(){this.is_disabled=this.form_field.disabled;if(this.is_disabled)return this.container.addClassName("chzn-disabled"),this.search_field.disabled=!0,this.is_multiple||this.selected_item.stopObserving("focus",this.activate_action),this.close_field();this.container.removeClassName("chzn-disabled"),this.search_field.disabled=!1;if(!this.is_multiple)return this.selected_item.observe("focus",this.activate_action)},d.prototype.container_mousedown=function(a){var b;if(!this.is_disabled)return b=a!=null?a.target.hasClassName("search-choice-close"):!1,a&&a.type==="mousedown"&&!this.results_showing&&a.stop(),!this.pending_destroy_click&&!b?(this.active_field?!this.is_multiple&&a&&(a.target===this.selected_item||a.target.up("a.chzn-single"))&&this.results_toggle():(this.is_multiple&&this.search_field.clear(),document.observe("click",this.click_test_action),this.results_show()),this.activate_field()):this.pending_destroy_click=!1},d.prototype.container_mouseup=function(a){if(a.target.nodeName==="ABBR"&&!this.is_disabled)return this.results_reset(a)},d.prototype.blur_test=function(a){if(!this.active_field&&this.container.hasClassName("chzn-container-active"))return this.close_field()},d.prototype.close_field=function(){return document.stopObserving("click",this.click_test_action),this.is_multiple||(this.selected_item.tabIndex=this.search_field.tabIndex,this.search_field.tabIndex=-1),this.active_field=!1,this.results_hide(),this.container.removeClassName("chzn-container-active"),this.winnow_results_clear(),this.clear_backstroke(),this.show_search_field_default(),this.search_field_scale()},d.prototype.activate_field=function(){return!this.is_multiple&&!this.active_field&&(this.search_field.tabIndex=this.selected_item.tabIndex,this.selected_item.tabIndex=-1),this.container.addClassName("chzn-container-active"),this.active_field=!0,this.search_field.value=this.search_field.value,this.search_field.focus()},d.prototype.test_active_click=function(a){return a.target.up("#"+this.container_id)?this.active_field=!0:this.close_field()},d.prototype.results_build=function(){var a,b,d,e,f;this.parsing=!0,this.results_data=c.SelectParser.select_to_array(this.form_field),this.is_multiple&&this.choices>0?(this.search_choices.select("li.search-choice").invoke("remove"),this.choices=0):this.is_multiple||(this.selected_item.addClassName("chzn-default").down("span").update(this.default_text),this.form_field.options.length<=this.disable_search_threshold?this.container.addClassName("chzn-container-single-nosearch"):this.container.removeClassName("chzn-container-single-nosearch")),a="",f=this.results_data;for(d=0,e=f.length;d'+a.label.escapeHTML()+"")},d.prototype.result_do_highlight=function(a){var b,c,d,e,f;this.result_clear_highlight(),this.result_highlight=a,this.result_highlight.addClassName("highlighted"),d=parseInt(this.search_results.getStyle("maxHeight"),10),f=this.search_results.scrollTop,e=d+f,c=this.result_highlight.positionedOffset().top,b=c+this.result_highlight.getHeight();if(b>=e)return this.search_results.scrollTop=b-d>0?b-d:0;if(c0&&this.search_field.value.length<1&&this.results_hide(),this.result_deselect(a.readAttribute("rel")),a.up("li").remove()},d.prototype.results_reset=function(){this.form_field.options[0].selected=!0,this.selected_item.down("span").update(this.default_text),this.is_multiple||this.selected_item.addClassName("chzn-default"),this.show_search_field_default(),this.results_reset_cleanup(),typeof Event.simulate=="function"&&this.form_field.simulate("change");if(this.active_field)return this.results_hide()},d.prototype.results_reset_cleanup=function(){var a;a=this.selected_item.down("abbr");if(a)return a.remove()},d.prototype.result_select=function(a){var b,c,d;if(this.result_highlight)return b=this.result_highlight,this.result_clear_highlight(),this.is_multiple?this.result_deactivate(b):(this.search_results.descendants(".result-selected").invoke("removeClassName","result-selected"),this.selected_item.removeClassName("chzn-default"),this.result_single_selected=b),b.addClassName("result-selected"),d=b.id.substr(b.id.lastIndexOf("_")+1),c=this.results_data[d],c.selected=!0,this.form_field.options[c.options_index].selected=!0,this.is_multiple?this.choice_build(c):(this.selected_item.down("span").update(c.html),this.allow_single_deselect&&this.single_deselect_control_build()),(!a.metaKey||!this.is_multiple)&&this.results_hide(),this.search_field.value="",typeof Event.simulate=="function"&&(this.is_multiple||this.form_field.value!==this.current_value)&&this.form_field.simulate("change"),this.current_value=this.form_field.value,this.search_field_scale()},d.prototype.result_activate=function(a){return a.addClassName("active-result")},d.prototype.result_deactivate=function(a){return a.removeClassName("active-result")},d.prototype.result_deselect=function(a){var b,c;return c=this.results_data[a],c.selected=!1,this.form_field.options[c.options_index].selected=!1,b=$(this.container_id+"_o_"+a),b.removeClassName("result-selected").addClassName("active-result").show(),this.result_clear_highlight(),this.winnow_results(),typeof Event.simulate=="function"&&this.form_field.simulate("change"),this.search_field_scale()},d.prototype.single_deselect_control_build=function(){if(this.allow_single_deselect&&!this.selected_item.down("abbr"))return this.selected_item.down("span").insert({after:''})},d.prototype.winnow_results=function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q;this.no_results_clear(),h=0,i=this.search_field.value===this.default_text?"":this.search_field.value.strip().escapeHTML(),f=this.search_contains?"":"^",e=new RegExp(f+i.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),l=new RegExp(i.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),q=this.results_data;for(m=0,o=q.length;m=0||b.html.indexOf("[")===0){d=b.html.replace(/\[|\]/g,"").split(" ");if(d.length)for(n=0,p=d.length;n"+b.html.substr(j+i.length),k=k.substr(0,j)+""+k.substr(j)):k=b.html,$(g).innerHTML!==k&&$(g).update(k),this.result_activate($(g)),b.group_array_index!=null&&$(this.results_data[b.group_array_index].dom_id).setStyle({display:"list-item"})):($(g)===this.result_highlight&&this.result_clear_highlight(),this.result_deactivate($(g)))}}return h<1&&i.length?this.no_results(i):this.winnow_results_set_highlight()},d.prototype.winnow_results_clear=function(){var a,b,c,d,e;this.search_field.clear(),b=this.search_results.select("li"),e=[];for(c=0,d=b.length;c0&&this.results_hide(),this.result_clear_highlight())},d.prototype.keydown_backstroke=function(){return this.pending_backstroke?(this.choice_destroy(this.pending_backstroke.down("a")),this.clear_backstroke()):(this.pending_backstroke=this.search_container.siblings("li.search-choice").last(),this.single_backstroke_delete?this.keydown_backstroke():this.pending_backstroke.addClassName("search-choice-focus"))},d.prototype.clear_backstroke=function(){return this.pending_backstroke&&this.pending_backstroke.removeClassName("search-choice-focus"),this.pending_backstroke=null},d.prototype.keydown_checker=function(a){var b,c;b=(c=a.which)!=null?c:a.keyCode,this.search_field_scale(),b!==8&&this.pending_backstroke&&this.clear_backstroke();switch(b){case 8:this.backstroke_length=this.search_field.value.length;break;case 9:this.results_showing&&!this.is_multiple&&this.result_select(a),this.mouse_on_container=!1;break;case 13:a.preventDefault();break;case 38:a.preventDefault(),this.keyup_arrow();break;case 40:this.keydown_arrow()}},d.prototype.search_field_scale=function(){var a,b,c,d,e,f,g,h,i;if(this.is_multiple){c=0,g=0,e="position:absolute; left: -1000px; top: -1000px; display:none;",f=["font-size","font-style","font-weight","font-family","line-height","text-transform","letter-spacing"];for(h=0,i=f.length;hthis.f_width-10&&(g=this.f_width-10),this.search_field.setStyle({width:g+"px"}),a=this.container.getHeight(),this.dropdown.setStyle({top:a+"px"})}},d}(AbstractChosen),c.Chosen=a,Prototype.Browser.IE&&/MSIE (\d+\.\d+);/.test(navigator.userAgent)&&(Prototype.BrowserFeatures.Version=new Number(RegExp.$1)),b=function(a){var b,c;return b=new Element.Layout(a),c=b.get("border-left")+b.get("border-right")+b.get("padding-left")+b.get("padding-right")},c.get_side_border_padding=b}.call(this); \ No newline at end of file +(function(){var e;e=function(){function e(){this.options_index=0,this.parsed=[]}return e.prototype.add_node=function(e){return e.nodeName==="OPTGROUP"?this.add_group(e):this.add_option(e)},e.prototype.add_group=function(e){var t,n,r,i,s,o;t=this.parsed.length,this.parsed.push({array_index:t,group:!0,label:e.label,children:0,disabled:e.disabled}),s=e.childNodes,o=[];for(r=0,i=s.length;r"+e.html+"")},e.prototype.results_update_field=function(){return this.is_multiple||this.results_reset_cleanup(),this.result_clear_highlight(),this.result_single_selected=null,this.results_build()},e.prototype.results_toggle=function(){return this.results_showing?this.results_hide():this.results_show()},e.prototype.results_search=function(e){return this.results_showing?this.winnow_results():this.results_show()},e.prototype.keyup_checker=function(e){var t,n;t=(n=e.which)!=null?n:e.keyCode,this.search_field_scale();switch(t){case 8:if(this.is_multiple&&this.backstroke_length<1&&this.choices>0)return this.keydown_backstroke();if(!this.pending_backstroke)return this.result_clear_highlight(),this.results_search();break;case 13:e.preventDefault();if(this.results_showing)return this.result_select(e);break;case 27:return this.results_showing&&this.results_hide(),!0;case 9:case 38:case 40:case 16:case 91:case 17:break;default:return this.results_search()}},e.prototype.generate_field_id=function(){var e;return e=this.generate_random_id(),this.form_field.id=e,e},e.prototype.generate_random_char=function(){var e,t,n;return e="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",n=Math.floor(Math.random()*e.length),t=e.substring(n,n+1)},e}(),t.AbstractChosen=e}.call(this),function(){var e,t,n,r={}.hasOwnProperty,i=function(e,t){function i(){this.constructor=e}for(var n in t)r.call(t,n)&&(e[n]=t[n]);return i.prototype=t.prototype,e.prototype=new i,e.__super__=t.prototype,e};n=this,e=function(e){function r(){return r.__super__.constructor.apply(this,arguments)}return i(r,e),r.prototype.setup=function(){return this.current_value=this.form_field.value,this.is_rtl=this.form_field.hasClassName("chzn-rtl")},r.prototype.finish_setup=function(){return this.form_field.addClassName("chzn-done")},r.prototype.set_default_values=function(){return r.__super__.set_default_values.call(this),this.single_temp=new Template('#{default}
                  '),this.multi_temp=new Template('
                    '),this.choice_temp=new Template('
                  • #{choice}
                  • '),this.no_results_temp=new Template('
                  • '+this.results_none_found+' "#{terms}"
                  • ')},r.prototype.set_up_html=function(){var e,n,r,i,s;return this.container_id=this.form_field.identify().replace(/[^\w]/g,"_")+"_chzn",this.f_width=this.form_field.getStyle("width")?parseInt(this.form_field.getStyle("width"),10):this.form_field.getWidth(),n={id:this.container_id,"class":"chzn-container"+(this.is_rtl?" chzn-rtl":""),style:"width: "+this.f_width+"px"},e=this.is_multiple?(new Element("div",n)).update(this.multi_temp.evaluate({"default":this.default_text})):(new Element("div",n)).update(this.single_temp.evaluate({"default":this.default_text})),this.form_field.hide().insert({after:e}),this.container=$(this.container_id),this.container.addClassName("chzn-container-"+(this.is_multiple?"multi":"single")),this.dropdown=this.container.down("div.chzn-drop"),r=this.container.getHeight(),i=this.f_width-t(this.dropdown),this.dropdown.setStyle({width:i+"px",top:r+"px"}),this.search_field=this.container.down("input"),this.search_results=this.container.down("ul.chzn-results"),this.search_field_scale(),this.search_no_results=this.container.down("li.no-results"),this.is_multiple?(this.search_choices=this.container.down("ul.chzn-choices"),this.search_container=this.container.down("li.search-field")):(this.search_container=this.container.down("div.chzn-search"),this.selected_item=this.container.down(".chzn-single"),s=i-t(this.search_container)-t(this.search_field),this.search_field.setStyle({width:s+"px"})),this.results_build(),this.set_tab_index(),this.form_field.fire("liszt:ready",{chosen:this})},r.prototype.register_observers=function(){var e=this;return this.container.observe("mousedown",function(t){return e.container_mousedown(t)}),this.container.observe("mouseup",function(t){return e.container_mouseup(t)}),this.container.observe("mouseenter",function(t){return e.mouse_enter(t)}),this.container.observe("mouseleave",function(t){return e.mouse_leave(t)}),this.search_results.observe("mouseup",function(t){return e.search_results_mouseup(t)}),this.search_results.observe("mouseover",function(t){return e.search_results_mouseover(t)}),this.search_results.observe("mouseout",function(t){return e.search_results_mouseout(t)}),this.form_field.observe("liszt:updated",function(t){return e.results_update_field(t)}),this.search_field.observe("blur",function(t){return e.input_blur(t)}),this.search_field.observe("keyup",function(t){return e.keyup_checker(t)}),this.search_field.observe("keydown",function(t){return e.keydown_checker(t)}),this.is_multiple?(this.search_choices.observe("click",function(t){return e.choices_click(t)}),this.search_field.observe("focus",function(t){return e.input_focus(t)})):this.container.observe("click",function(e){return e.preventDefault()})},r.prototype.search_field_disabled=function(){this.is_disabled=this.form_field.disabled;if(this.is_disabled)return this.container.addClassName("chzn-disabled"),this.search_field.disabled=!0,this.is_multiple||this.selected_item.stopObserving("focus",this.activate_action),this.close_field();this.container.removeClassName("chzn-disabled"),this.search_field.disabled=!1;if(!this.is_multiple)return this.selected_item.observe("focus",this.activate_action)},r.prototype.container_mousedown=function(e){var t;if(!this.is_disabled)return t=e!=null?e.target.hasClassName("search-choice-close"):!1,e&&e.type==="mousedown"&&!this.results_showing&&e.stop(),!this.pending_destroy_click&&!t?(this.active_field?!this.is_multiple&&e&&(e.target===this.selected_item||e.target.up("a.chzn-single"))&&this.results_toggle():(this.is_multiple&&this.search_field.clear(),document.observe("click",this.click_test_action),this.results_show()),this.activate_field()):this.pending_destroy_click=!1},r.prototype.container_mouseup=function(e){if(e.target.nodeName==="ABBR"&&!this.is_disabled)return this.results_reset(e)},r.prototype.blur_test=function(e){if(!this.active_field&&this.container.hasClassName("chzn-container-active"))return this.close_field()},r.prototype.close_field=function(){return document.stopObserving("click",this.click_test_action),this.is_multiple||(this.selected_item.tabIndex=this.search_field.tabIndex,this.search_field.tabIndex=-1),this.active_field=!1,this.results_hide(),this.container.removeClassName("chzn-container-active"),this.winnow_results_clear(),this.clear_backstroke(),this.show_search_field_default(),this.search_field_scale()},r.prototype.activate_field=function(){return!this.is_multiple&&!this.active_field&&(this.search_field.tabIndex=this.selected_item.tabIndex,this.selected_item.tabIndex=-1),this.container.addClassName("chzn-container-active"),this.active_field=!0,this.search_field.value=this.search_field.value,this.search_field.focus()},r.prototype.test_active_click=function(e){return e.target.up("#"+this.container_id)?this.active_field=!0:this.close_field()},r.prototype.results_build=function(){var e,t,r,i,s;this.parsing=!0,this.results_data=n.SelectParser.select_to_array(this.form_field),this.is_multiple&&this.choices>0?(this.search_choices.select("li.search-choice").invoke("remove"),this.choices=0):this.is_multiple||(this.selected_item.addClassName("chzn-default").down("span").update(this.default_text),this.form_field.options.length<=this.disable_search_threshold?this.container.addClassName("chzn-container-single-nosearch"):this.container.removeClassName("chzn-container-single-nosearch")),e="",s=this.results_data;for(r=0,i=s.length;r'+e.label.escapeHTML()+"")},r.prototype.result_do_highlight=function(e){var t,n,r,i,s;this.result_clear_highlight(),this.result_highlight=e,this.result_highlight.addClassName("highlighted"),r=parseInt(this.search_results.getStyle("maxHeight"),10),s=this.search_results.scrollTop,i=r+s,n=this.result_highlight.positionedOffset().top,t=n+this.result_highlight.getHeight();if(t>=i)return this.search_results.scrollTop=t-r>0?t-r:0;if(n0&&this.search_field.value.length<1&&this.results_hide(),this.result_deselect(e.readAttribute("rel")),e.up("li").remove()},r.prototype.results_reset=function(){this.form_field.options[0].selected=!0,this.selected_item.down("span").update(this.default_text),this.is_multiple||this.selected_item.addClassName("chzn-default"),this.show_search_field_default(),this.results_reset_cleanup(),typeof Event.simulate=="function"&&this.form_field.simulate("change");if(this.active_field)return this.results_hide()},r.prototype.results_reset_cleanup=function(){var e;e=this.selected_item.down("abbr");if(e)return e.remove()},r.prototype.result_select=function(e){var t,n,r;if(this.result_highlight)return t=this.result_highlight,this.result_clear_highlight(),this.is_multiple?this.result_deactivate(t):(this.search_results.descendants(".result-selected").invoke("removeClassName","result-selected"),this.selected_item.removeClassName("chzn-default"),this.result_single_selected=t),t.addClassName("result-selected"),r=t.id.substr(t.id.lastIndexOf("_")+1),n=this.results_data[r],n.selected=!0,this.form_field.options[n.options_index].selected=!0,this.is_multiple?this.choice_build(n):(this.selected_item.down("span").update(n.html),this.allow_single_deselect&&this.single_deselect_control_build()),(!e.metaKey||!this.is_multiple)&&this.results_hide(),this.search_field.value="",typeof Event.simulate=="function"&&(this.is_multiple||this.form_field.value!==this.current_value)&&this.form_field.simulate("change"),this.current_value=this.form_field.value,this.search_field_scale()},r.prototype.result_activate=function(e){return e.addClassName("active-result")},r.prototype.result_deactivate=function(e){return e.removeClassName("active-result")},r.prototype.result_deselect=function(e){var t,n;return n=this.results_data[e],n.selected=!1,this.form_field.options[n.options_index].selected=!1,t=$(this.container_id+"_o_"+e),t.removeClassName("result-selected").addClassName("active-result").show(),this.result_clear_highlight(),this.winnow_results(),typeof Event.simulate=="function"&&this.form_field.simulate("change"),this.search_field_scale()},r.prototype.single_deselect_control_build=function(){if(this.allow_single_deselect&&!this.selected_item.down("abbr"))return this.selected_item.down("span").insert({after:''})},r.prototype.winnow_results=function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v,m;this.no_results_clear(),u=0,a=this.search_field.value===this.default_text?"":this.search_field.value.strip().escapeHTML(),s=this.search_contains?"":"^",i=new RegExp(s+a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),c=new RegExp(a.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\$&"),"i"),m=this.results_data;for(h=0,d=m.length;h=0||t.html.indexOf("[")===0){r=t.html.replace(/\[|\]/g,"").split(" ");if(r.length)for(p=0,v=r.length;p"+t.html.substr(f+a.length),l=l.substr(0,f)+""+l.substr(f)):l=t.html,$(o).innerHTML!==l&&$(o).update(l),this.result_activate($(o)),t.group_array_index!=null&&$(this.results_data[t.group_array_index].dom_id).setStyle({display:"list-item"})):($(o)===this.result_highlight&&this.result_clear_highlight(),this.result_deactivate($(o)))}}return u<1&&a.length?this.no_results(a):this.winnow_results_set_highlight()},r.prototype.winnow_results_clear=function(){var e,t,n,r,i;this.search_field.clear(),t=this.search_results.select("li"),i=[];for(n=0,r=t.length;n0&&this.results_hide(),this.result_clear_highlight())},r.prototype.keydown_backstroke=function(){return this.pending_backstroke?(this.choice_destroy(this.pending_backstroke.down("a")),this.clear_backstroke()):(this.pending_backstroke=this.search_container.siblings("li.search-choice").last(),this.single_backstroke_delete?this.keydown_backstroke():this.pending_backstroke.addClassName("search-choice-focus"))},r.prototype.clear_backstroke=function(){return this.pending_backstroke&&this.pending_backstroke.removeClassName("search-choice-focus"),this.pending_backstroke=null},r.prototype.keydown_checker=function(e){var t,n;t=(n=e.which)!=null?n:e.keyCode,this.search_field_scale(),t!==8&&this.pending_backstroke&&this.clear_backstroke();switch(t){case 8:this.backstroke_length=this.search_field.value.length;break;case 9:this.results_showing&&!this.is_multiple&&this.result_select(e),this.mouse_on_container=!1;break;case 13:e.preventDefault();break;case 38:e.preventDefault(),this.keyup_arrow();break;case 40:this.keydown_arrow()}},r.prototype.search_field_scale=function(){var e,t,n,r,i,s,o,u,a;if(this.is_multiple){n=0,o=0,i="position:absolute; left: -1000px; top: -1000px; display:none;",s=["font-size","font-style","font-weight","font-family","line-height","text-transform","letter-spacing"];for(u=0,a=s.length;uthis.f_width-10&&(o=this.f_width-10),this.search_field.setStyle({width:o+"px"}),e=this.container.getHeight(),this.dropdown.setStyle({top:e+"px"})}},r}(AbstractChosen),n.Chosen=e,Prototype.Browser.IE&&/MSIE (\d+\.\d+);/.test(navigator.userAgent)&&(Prototype.BrowserFeatures.Version=new Number(RegExp.$1)),t=function(e){var t,n;return t=new Element.Layout(e),n=t.get("border-left")+t.get("border-right")+t.get("padding-left")+t.get("padding-right")},n.get_side_border_padding=t}.call(this); \ No newline at end of file diff --git a/coffee/chosen.jquery.coffee b/coffee/chosen.jquery.coffee index b4e6e6e7372..aae1427f777 100644 --- a/coffee/chosen.jquery.coffee +++ b/coffee/chosen.jquery.coffee @@ -21,6 +21,7 @@ class Chosen extends AbstractChosen @form_field_jq = $ @form_field @current_value = @form_field_jq.val() @is_rtl = @form_field_jq.hasClass "chzn-rtl" + @allow_custom_value = @form_field_jq.hasClass "chzn-custom-value" || @options.allow_custom_value finish_setup: -> @form_field_jq.addClass "chzn-done" @@ -42,6 +43,7 @@ class Chosen extends AbstractChosen else container_div.html '' + @default_text + '
                      ' + @form_field_jq.hide().after container_div @container = ($ '#' + @container_id) @container.addClass( "chzn-container-" + (if @is_multiple then "multi" else "single") ) @@ -328,7 +330,7 @@ class Chosen extends AbstractChosen this.results_reset_cleanup() @form_field_jq.trigger "change" this.results_hide() if @active_field - + results_reset_cleanup: -> @selected_item.find("abbr").remove() @@ -367,6 +369,30 @@ class Chosen extends AbstractChosen @form_field_jq.trigger "change", {'selected': @form_field.options[item.options_index].value} if @is_multiple || @form_field_jq.val() != @current_value @current_value = @form_field_jq.val() this.search_field_scale() + else if not @is_multiple + value = @search_field.val() + group = @add_unique_custom_group() + option = $ '' + group.append option + + @form_field_jq.append group + @form_field.options[@form_field.options.length-1].selected = true + + @results_hide() unless evt.metaKey + @results_build() + + find_custom_group: -> + found = group for group in $('optgroup', @form_field) when group.getAttribute('label') is @custom_group_text + + found + + add_unique_custom_group: -> + group = @find_custom_group() + if not group + group = $ ('') + + $ group + result_activate: (el) -> el.addClass("active-result") diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 2fc82badbbe..9e3b81cbd59 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -8,12 +8,13 @@ class AbstractChosen constructor: (@form_field, @options={}) -> this.set_default_values() - + @is_multiple = @form_field.multiple - this.set_default_text() this.setup() + this.set_default_text() + this.set_up_html() this.register_observers() @@ -33,23 +34,33 @@ class AbstractChosen @choices = 0 @single_backstroke_delete = @options.single_backstroke_delete || false @max_selected_options = @options.max_selected_options || Infinity + @allow_custom_value = false + true set_default_text: -> if @form_field.getAttribute("data-placeholder") @default_text = @form_field.getAttribute("data-placeholder") else if @is_multiple @default_text = @options.placeholder_text_multiple || @options.placeholder_text || "Select Some Options" + else if @allow_custom_value + @default_text = @options.placeholder_text_single || @options.placeholder_text || "Type or Select an Option" else @default_text = @options.placeholder_text_single || @options.placeholder_text || "Select an Option" - @results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || "No results match" + if (@allow_custom_value) + @custom_group_text = @form_field.getAttribute("data-custom_group_text") || @options.custom_group_text || "Custom Value" + results_none_found_default = "Add custom value" + else + results_none_found_default = "No results match" + + @results_none_found = @form_field.getAttribute("data-no_results_text") || @options.no_results_text || results_none_found_default mouse_enter: -> @mouse_on_container = true mouse_leave: -> @mouse_on_container = false input_focus: (evt) -> setTimeout (=> this.container_mousedown()), 50 unless @active_field - + input_blur: (evt) -> if not @mouse_on_container @active_field = false @@ -113,7 +124,7 @@ class AbstractChosen new_id = this.generate_random_id() @form_field.id = new_id new_id - + generate_random_char: -> chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" rand = Math.floor(Math.random() * chars.length)