Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 7de0007b855c464f9a26425afb85e3fe4daf0df5
Merge: 10fb211 80f3985
Author: Koen Punt <koen@koenpunt.nl>
Date:   Thu Nov 1 11:32:06 2012 +0100

    Merge branch 'gh7_empty_select_should_show_input' into rebase

commit 10fb2119954a7129e3214985d679f3e957f1bd0a
Author: Koen Punt <koen@koenpunt.nl>
Date:   Thu Nov 1 11:30:04 2012 +0100

    Merged koenpunt/master onto harvesthq/master

commit 80f3985
Author: Koen Punt <koen@koenpunt.nl>
Date:   Thu Nov 1 11:02:57 2012 +0100

    fixes #7, select without options will now show input on click

commit c481fed
Author: Patrick Filler <pf@patrickfiller.com>
Date:   Tue Oct 9 14:20:10 2012 -0700

    Make sure single text link gets negative tab index. Fixes issue with tabbing backwards.

commit 9085750
Author: Patrick Filler <pf@patrickfiller.com>
Date:   Tue Oct 9 10:21:04 2012 -0700

    Fixes tab order issues with single Chosen.
  • Loading branch information
koenpunt committed Mar 10, 2013
1 parent 5f6ef66 commit 3cc7287
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 288 deletions.
104 changes: 34 additions & 70 deletions chosen/chosen.jquery.js
Expand Up @@ -138,7 +138,9 @@ Copyright (c) 2011 by Harvest
this.choices = 0;
this.single_backstroke_delete = this.options.single_backstroke_delete || false;
this.max_selected_options = this.options.max_selected_options || Infinity;
return this.inherit_select_classes = this.options.inherit_select_classes || false;
this.inherit_select_classes = this.options.inherit_select_classes || false;
this.create_option = this.options.create_option || false;
return this.persistent_create_option = this.options.persistent_create_option || false;
};

AbstractChosen.prototype.set_default_text = function() {
Expand All @@ -149,7 +151,8 @@ Copyright (c) 2011 by Harvest
} 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";
this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match";
return this.create_option_text = this.form_field.getAttribute("data-create_option_text") || this.options.create_option_text || "Add option";
};

AbstractChosen.prototype.mouse_enter = function() {
Expand Down Expand Up @@ -334,14 +337,15 @@ Copyright (c) 2011 by Harvest

__extends(Chosen, _super);

function Chosen(elmn, data, options) {
this.options = $.extend({}, options);
this.set_default_values();
this.form_field = elmn;
function Chosen() {
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();
this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
}
return this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
};

Chosen.prototype.finish_setup = function() {
return this.form_field_jq.addClass("chzn-done");
Expand Down Expand Up @@ -548,10 +552,10 @@ Copyright (c) 2011 by Harvest
this.choices = 0;
} else if (!this.is_multiple) {
this.selected_item.addClass("chzn-default").find("span").text(this.default_text);
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
this.container.addClass("chzn-container-single-nosearch");
} else {
if (this.create_option && !this.disable_search) {
this.container.removeClass("chzn-container-single-nosearch");
} else if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
this.container.addClass("chzn-container-single-nosearch");
}
}
content = '';
Expand Down Expand Up @@ -588,12 +592,6 @@ Copyright (c) 2011 by Harvest
}
};

Chosen.prototype.results_update_field = function() {
this.result_clear_highlight();
this.result_single_selected = null;
return this.results_build();
};

Chosen.prototype.result_do_highlight = function(el) {
var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
if (el.length) {
Expand Down Expand Up @@ -854,15 +852,16 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.winnow_results = function() {
var exactRegex, found, option, part, parts, regex, regexAnchor, result_id, results, searchText, selected, startpos, text, zregex, _i, _j, _len, _len1, _ref;
var eregex, exact_result, found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref;
this.no_results_clear();
this.create_option_clear();
results = 0;
selected = false;
searchText = this.search_field.val() === this.default_text ? "" : $('<div/>').text($.trim(this.search_field.val())).html();
regexAnchor = this.search_contains ? "" : "^";
regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
exactRegex = new RegExp('^' + searchText + '$', 'i');
eregex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + '$', 'i');
exact_result = false;
_ref = this.results_data;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i];
Expand All @@ -872,9 +871,13 @@ Copyright (c) 2011 by Harvest
} else if (!(this.is_multiple && option.selected)) {
found = false;
result_id = option.dom_id;
result = $("#" + result_id);
if (regex.test(option.html)) {
found = true;
results += 1;
if (eregex.test(option.html)) {
exact_result = true;
}
} else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
parts = option.html.replace(/\[|\]/g, "").split(" ");
if (parts.length) {
Expand Down Expand Up @@ -904,18 +907,17 @@ Copyright (c) 2011 by Harvest
if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
this.result_clear_highlight();
}
this.result_deactivate($("#" + result_id));
}
} else if (this.is_multiple && option.selected) {
if (exactRegex.test(option.html)) {
selected = true;
this.result_deactivate(result);
}
}
}
}
if (results < 1 && searchText.length) {
return this.no_results(searchText, selected);
return this.no_results(searchText);
} else {
if (this.create_option && !exact_result && this.persistent_create_option && searchText.length) {
this.show_create_option(searchText);
}
return this.winnow_results_set_highlight();
}
};
Expand Down Expand Up @@ -950,18 +952,14 @@ Copyright (c) 2011 by Harvest
}
};

Chosen.prototype.no_results = function(terms, selected) {
var no_results_html,
_this = this;
no_results_html = $('<li class="no-results">No results match "<span></span>".</li>');
Chosen.prototype.no_results = function(terms) {
var no_results_html;
no_results_html = $('<li class="no-results">' + this.results_none_found + ' "<span></span>"</li>');
no_results_html.find("span").first().html(terms);
if (!selected) {
no_results_html.append(' <a href="javascript:void(0);" class="option-add">Add this item</a>');
no_results_html.find("a.option-add").bind("click", function(evt) {
return _this.select_add_option(terms);
});
this.search_results.append(no_results_html);
if (this.create_option) {
return this.show_create_option(terms);
}
return this.search_results.append(no_results_html);
};

Chosen.prototype.show_create_option = function(terms) {
Expand Down Expand Up @@ -1057,40 +1055,6 @@ Copyright (c) 2011 by Harvest
return this.pending_backstroke = null;
};

Chosen.prototype.keyup_checker = function(evt) {
var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
this.search_field_scale();
switch (stroke) {
case 8:
if (this.is_multiple && this.backstroke_length < 1 && this.choices > 0) {
return this.keydown_backstroke();
} else if (!this.pending_backstroke) {
this.result_clear_highlight();
return this.results_search();
}
break;
case 13:
evt.preventDefault();
if (this.results_showing) {
return this.result_select();
}
break;
case 27:
if (this.results_showing) {
return this.results_hide();
}
break;
case 9:
case 38:
case 40:
case 16:
break;
default:
return this.results_search();
}
};

Chosen.prototype.keydown_checker = function(evt) {
var stroke, _ref;
stroke = (_ref = evt.which) != null ? _ref : evt.keyCode;
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

86 changes: 41 additions & 45 deletions chosen/chosen.proto.js
Expand Up @@ -138,7 +138,9 @@ Copyright (c) 2011 by Harvest
this.choices = 0;
this.single_backstroke_delete = this.options.single_backstroke_delete || false;
this.max_selected_options = this.options.max_selected_options || Infinity;
return this.inherit_select_classes = this.options.inherit_select_classes || false;
this.inherit_select_classes = this.options.inherit_select_classes || false;
this.create_option = this.options.create_option || false;
return this.persistent_create_option = this.options.persistent_create_option || false;
};

AbstractChosen.prototype.set_default_text = function() {
Expand All @@ -149,7 +151,8 @@ Copyright (c) 2011 by Harvest
} 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";
this.results_none_found = this.form_field.getAttribute("data-no_results_text") || this.options.no_results_text || "No results match";
return this.create_option_text = this.form_field.getAttribute("data-create_option_text") || this.options.create_option_text || "Add option";
};

AbstractChosen.prototype.mouse_enter = function() {
Expand Down Expand Up @@ -329,9 +332,9 @@ Copyright (c) 2011 by Harvest
this.multi_temp = new Template('<ul class="chzn-choices"><li class="search-field"><input type="text" value="#{default}" class="default" autocomplete="off" style="width:25px;" /></li></ul><div class="chzn-drop" style="left:-9000px;"><ul class="chzn-results"></ul></div>');
this.choice_temp = new Template('<li class="search-choice" id="#{id}"><span>#{choice}</span><a href="javascript:void(0)" class="search-choice-close" rel="#{position}"></a></li>');
this.choice_noclose_temp = new Template('<li class="search-choice search-choice-disabled" id="#{id}"><span>#{choice}</span></li>');
this.no_results_temp = new Template('<li class="no-results">' + this.results_none_found + ' "<span>#{terms}</span>".#{add_item_link}</li>');
this.no_results_temp = new Template('<li class="no-results">' + this.results_none_found + ' "<span>#{terms}</span>"</li>');
this.new_option_temp = new Template('<option value="#{value}">#{text}</option>');
return this.add_link_temp = new Template(' <a href="javascript:void(0);" class="option-add">' + this.create_option_text + '</a>');
return this.create_option_temp = new Template('<li class="create-option active-result"><a href="javascript:void(0);">#{text}</a>: "#{terms}"</li>');
};

Chosen.prototype.set_up_html = function() {
Expand Down Expand Up @@ -534,10 +537,10 @@ Copyright (c) 2011 by Harvest
this.choices = 0;
} else if (!this.is_multiple) {
this.selected_item.addClassName("chzn-default").down("span").update(this.default_text);
if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
this.container.addClassName("chzn-container-single-nosearch");
} else {
if (this.create_option && !this.disable_search) {
this.container.removeClassName("chzn-container-single-nosearch");
} else if (this.disable_search || this.form_field.options.length <= this.disable_search_threshold) {
this.container.addClassName("chzn-container-single-nosearch");
}
}
content = '';
Expand Down Expand Up @@ -764,6 +767,10 @@ Copyright (c) 2011 by Harvest
var high, item, position;
if (this.result_highlight) {
high = this.result_highlight;
if (high.hasClassName('create-option')) {
this.select_create_option(this.search_field.value);
return this.results_hide();
}
this.result_clear_highlight();
if (this.is_multiple) {
this.result_deactivate(high);
Expand Down Expand Up @@ -834,15 +841,16 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.winnow_results = function() {
var exactRegex, found, option, part, parts, regex, regexAnchor, result_id, results, searchText, selected, startpos, text, zregex, _i, _j, _len, _len1, _ref;
var eregex, exact_result, found, option, part, parts, regex, regexAnchor, result, result_id, results, searchText, startpos, text, zregex, _i, _j, _len, _len1, _ref;
this.no_results_clear();
this.create_option_clear();
results = 0;
selected = false;
searchText = this.search_field.value === this.default_text ? "" : this.search_field.value.strip().escapeHTML();
regexAnchor = this.search_contains ? "" : "^";
regex = new RegExp(regexAnchor + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
exactRegex = new RegExp('^' + searchText + '$', 'i');
eregex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + '$', 'i');
exact_result = false;
_ref = this.results_data;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i];
Expand All @@ -852,9 +860,13 @@ Copyright (c) 2011 by Harvest
} else if (!(this.is_multiple && option.selected)) {
found = false;
result_id = option.dom_id;
result = $(option.dom_id);
if (regex.test(option.html)) {
found = true;
results += 1;
if (eregex.test(option.html)) {
exact_result = true;
}
} else if (this.enable_split_word_search && (option.html.indexOf(" ") >= 0 || option.html.indexOf("[") === 0)) {
parts = option.html.replace(/\[|\]/g, "").split(" ");
if (parts.length) {
Expand Down Expand Up @@ -890,15 +902,11 @@ Copyright (c) 2011 by Harvest
}
this.result_deactivate($(result_id));
}
} else if (this.is_multiple && option.selected) {
if (exactRegex.test(option.html)) {
selected = true;
}
}
}
}
if (results < 1 && searchText.length) {
return this.no_results(searchText, selected);
return this.no_results(searchText);
} else {
if (this.create_option && !exact_result && this.persistent_create_option && searchText.length) {
this.show_create_option(searchText);
Expand Down Expand Up @@ -940,36 +948,28 @@ Copyright (c) 2011 by Harvest
}
};

Chosen.prototype.no_results = function(terms, selected) {
var add_item_link,
_this = this;
add_item_link = '';
if (this.create_option && !selected) {
add_item_link = this.add_link_temp.evaluate();
}
Chosen.prototype.no_results = function(terms) {
this.search_results.insert(this.no_results_temp.evaluate({
text: this.options.noResultsText,
terms: terms,
add_item_link: add_item_link
terms: terms
}));
if (this.create_option && !selected) {
return this.search_results.down("a.option-add").observe("click", function(evt) {
if (!selected) {
return _this.select_create_option(terms);
}
});
if (this.create_option) {
return this.show_create_option(terms);
}
/*
no_results_html = $('<li class="no-results">' + @results_none_found + ' "<span></span>"</li>')
no_results_html.find("span").first().html(terms)
@search_results.append no_results_html
if @create_option #and not selected
this.show_create_option( terms )
*/
};

Chosen.prototype.show_create_option = function(terms) {
return this.search_results.insert(this.create_option_temp.evaluate({
text: this.create_option_text,
terms: terms
}));
};

Chosen.prototype.create_option_clear = function() {
var create_option;
create_option = this.search_results.select(".create-option");
return create_option.each(function(el) {
return el.remove();
});
};

Chosen.prototype.select_create_option = function(terms) {
Expand All @@ -984,10 +984,6 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.select_append_option = function(options) {
/*
TODO Close options after adding
*/

var option;
option = this.new_option_temp.evaluate(options);
this.form_field.insert(option);
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

0 comments on commit 3cc7287

Please sign in to comment.