Skip to content

Commit

Permalink
Added disable_search option when creating a chosen widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
datiecher committed Aug 28, 2012
1 parent 2e09d33 commit b6ff78e
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 65 deletions.
107 changes: 78 additions & 29 deletions chosen/chosen.jquery.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
SelectParser.prototype.add_option = function(option, group_position, group_disabled) { SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
if (option.nodeName === "OPTION") { if (option.nodeName === "OPTION") {
if (option.text !== "") { 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({ this.parsed.push({
array_index: this.parsed.length, array_index: this.parsed.length,
options_index: this.options_index, options_index: this.options_index,
Expand Down Expand Up @@ -95,6 +97,7 @@ Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest Copyright (c) 2011 by Harvest
*/ */



(function() { (function() {
var AbstractChosen, root; var AbstractChosen, root;


Expand Down Expand Up @@ -129,6 +132,7 @@ Copyright (c) 2011 by Harvest
this.result_single_selected = null; this.result_single_selected = null;
this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false; this.allow_single_deselect = (this.options.allow_single_deselect != null) && (this.form_field.options[0] != null) && this.form_field.options[0].text === "" ? this.options.allow_single_deselect : false;
this.disable_search_threshold = this.options.disable_search_threshold || 0; this.disable_search_threshold = this.options.disable_search_threshold || 0;
this.disable_search = this.options.disable_search || false;
this.search_contains = this.options.search_contains || false; this.search_contains = this.options.search_contains || false;
this.choices = 0; this.choices = 0;
this.single_backstroke_delete = this.options.single_backstroke_delete || false; this.single_backstroke_delete = this.options.single_backstroke_delete || false;
Expand Down Expand Up @@ -178,9 +182,15 @@ Copyright (c) 2011 by Harvest
if (!option.disabled) { if (!option.disabled) {
option.dom_id = this.container_id + "_o_" + option.array_index; option.dom_id = this.container_id + "_o_" + option.array_index;
classes = option.selected && this.is_multiple ? [] : ["active-result"]; classes = option.selected && this.is_multiple ? [] : ["active-result"];
if (option.selected) classes.push("result-selected"); if (option.selected) {
if (option.group_array_index != null) classes.push("group-option"); classes.push("result-selected");
if (option.classes !== "") classes.push(option.classes); }
if (option.group_array_index != null) {
classes.push("group-option");
}
if (option.classes !== "") {
classes.push(option.classes);
}
style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : ""; style = option.style.cssText !== "" ? " style=\"" + option.style + "\"" : "";
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>'; return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '"' + style + '>' + option.html + '</li>';
} else { } else {
Expand All @@ -189,7 +199,9 @@ Copyright (c) 2011 by Harvest
}; };


AbstractChosen.prototype.results_update_field = function() { 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_clear_highlight();
this.result_single_selected = null; this.result_single_selected = null;
return this.results_build(); return this.results_build();
Expand Down Expand Up @@ -226,10 +238,14 @@ Copyright (c) 2011 by Harvest
break; break;
case 13: case 13:
evt.preventDefault(); evt.preventDefault();
if (this.results_showing) return this.result_select(evt); if (this.results_showing) {
return this.result_select(evt);
}
break; break;
case 27: case 27:
if (this.results_showing) this.results_hide(); if (this.results_showing) {
this.results_hide();
}
return true; return true;
case 9: case 9:
case 38: case 38:
Expand Down Expand Up @@ -270,10 +286,11 @@ Chosen source: generate output using 'cake build'
Copyright (c) 2011 by Harvest Copyright (c) 2011 by Harvest
*/ */



(function() { (function() {
var $, Chosen, get_side_border_padding, root, var $, Chosen, get_side_border_padding, root,
__hasProp = Object.prototype.hasOwnProperty, __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; }; __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; root = this;


Expand All @@ -299,7 +316,7 @@ Copyright (c) 2011 by Harvest
__extends(Chosen, _super); __extends(Chosen, _super);


function Chosen() { function Chosen() {
Chosen.__super__.constructor.apply(this, arguments); return Chosen.__super__.constructor.apply(this, arguments);
} }


Chosen.prototype.setup = function() { Chosen.prototype.setup = function() {
Expand Down Expand Up @@ -435,7 +452,9 @@ Copyright (c) 2011 by Harvest
} }
if (!this.pending_destroy_click && !target_closelink) { if (!this.pending_destroy_click && !target_closelink) {
if (!this.active_field) { 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); $(document).click(this.click_test_action);
this.results_show(); this.results_show();
} else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) { } else if (!this.is_multiple && evt && (($(evt.target)[0] === this.selected_item[0]) || $(evt.target).parents("a.chzn-single").length)) {
Expand Down Expand Up @@ -504,7 +523,7 @@ Copyright (c) 2011 by Harvest
this.choices = 0; this.choices = 0;
} else if (!this.is_multiple) { } else if (!this.is_multiple) {
this.selected_item.addClass("chzn-default").find("span").text(this.default_text); this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
if (this.form_field.options.length <= this.disable_search_threshold) { if (this.form_field.options.length <= this.disable_search_threshold || this.disable_search === true) {
this.container.addClass("chzn-container-single-nosearch"); this.container.addClass("chzn-container-single-nosearch");
} else { } else {
this.container.removeClass("chzn-container-single-nosearch"); this.container.removeClass("chzn-container-single-nosearch");
Expand All @@ -522,7 +541,9 @@ Copyright (c) 2011 by Harvest
this.choice_build(data); this.choice_build(data);
} else if (data.selected && !this.is_multiple) { } else if (data.selected && !this.is_multiple) {
this.selected_item.removeClass("chzn-default").find("span").text(data.text); 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();
}
} }
} }
} }
Expand Down Expand Up @@ -562,7 +583,9 @@ Copyright (c) 2011 by Harvest
}; };


Chosen.prototype.result_clear_highlight = function() { 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; return this.result_highlight = null;
}; };


Expand Down Expand Up @@ -643,7 +666,9 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.search_results_mouseover = function(evt) { Chosen.prototype.search_results_mouseover = function(evt) {
var target; var target;
target = $(evt.target).hasClass("active-result") ? $(evt.target) : $(evt.target).parents(".active-result").first(); 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) { Chosen.prototype.search_results_mouseout = function(evt) {
Expand Down Expand Up @@ -700,11 +725,15 @@ Copyright (c) 2011 by Harvest
Chosen.prototype.results_reset = function() { Chosen.prototype.results_reset = function() {
this.form_field.options[0].selected = true; this.form_field.options[0].selected = true;
this.selected_item.find("span").text(this.default_text); 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.show_search_field_default();
this.results_reset_cleanup(); this.results_reset_cleanup();
this.form_field_jq.trigger("change"); 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() { Chosen.prototype.results_reset_cleanup = function() {
Expand Down Expand Up @@ -733,9 +762,13 @@ Copyright (c) 2011 by Harvest
this.choice_build(item); this.choice_build(item);
} else { } else {
this.selected_item.find("span").first().text(item.text); 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(""); this.search_field.val("");
if (this.is_multiple || this.form_field_jq.val() !== this.current_value) { if (this.is_multiple || this.form_field_jq.val() !== this.current_value) {
this.form_field_jq.trigger("change", { this.form_field_jq.trigger("change", {
Expand Down Expand Up @@ -777,7 +810,7 @@ Copyright (c) 2011 by Harvest
}; };


Chosen.prototype.winnow_results = function() { 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(); this.no_results_clear();
results = 0; results = 0;
searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html(); searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html();
Expand All @@ -800,7 +833,7 @@ Copyright (c) 2011 by Harvest
} else if (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0) { } else if (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0) {
parts = option.html.replace(/\[|\]/g, "").split(" "); parts = option.html.replace(/\[|\]/g, "").split(" ");
if (parts.length) { if (parts.length) {
for (_j = 0, _len2 = parts.length; _j < _len2; _j++) { for (_j = 0, _len1 = parts.length; _j < _len1; _j++) {
part = parts[_j]; part = parts[_j];
if (regex.test(part)) { if (regex.test(part)) {
found = true; found = true;
Expand Down Expand Up @@ -862,7 +895,9 @@ Copyright (c) 2011 by Harvest
if (!this.result_highlight) { if (!this.result_highlight) {
selected_results = !this.is_multiple ? this.search_results.find(".result-selected.active-result") : []; 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(); 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);
}
} }
}; };


Expand All @@ -881,12 +916,18 @@ Copyright (c) 2011 by Harvest
var first_active, next_sib; var first_active, next_sib;
if (!this.result_highlight) { if (!this.result_highlight) {
first_active = this.search_results.find("li.active-result").first(); 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) { } else if (this.results_showing) {
next_sib = this.result_highlight.nextAll("li.active-result").first(); 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() { Chosen.prototype.keyup_arrow = function() {
Expand All @@ -898,7 +939,9 @@ Copyright (c) 2011 by Harvest
if (prev_sibs.length) { if (prev_sibs.length) {
return this.result_do_highlight(prev_sibs.first()); return this.result_do_highlight(prev_sibs.first());
} else { } else {
if (this.choices > 0) this.results_hide(); if (this.choices > 0) {
this.results_hide();
}
return this.result_clear_highlight(); return this.result_clear_highlight();
} }
} }
Expand Down Expand Up @@ -929,13 +972,17 @@ Copyright (c) 2011 by Harvest
var stroke, _ref; var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode; stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
this.search_field_scale(); this.search_field_scale();
if (stroke !== 8 && this.pending_backstroke) this.clear_backstroke(); if (stroke !== 8 && this.pending_backstroke) {
this.clear_backstroke();
}
switch (stroke) { switch (stroke) {
case 8: case 8:
this.backstroke_length = this.search_field.val().length; this.backstroke_length = this.search_field.val().length;
break; break;
case 9: 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; this.mouse_on_container = false;
break; break;
case 13: case 13:
Expand Down Expand Up @@ -969,7 +1016,9 @@ Copyright (c) 2011 by Harvest
$('body').append(div); $('body').append(div);
w = div.width() + 25; w = div.width() + 25;
div.remove(); 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({ this.search_field.css({
'width': w + 'px' 'width': w + 'px'
}); });
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit b6ff78e

Please sign in to comment.