Skip to content

Commit

Permalink
Added options to add translated strings
Browse files Browse the repository at this point in the history
Defaults are:
  default_options = {
    addOptionText: 'Add this item',
    noResultsText: 'No results match',
    selectOptionText: 'Select an Option',
    selectOptionsText: 'Select Some Options',
  }

Updated examples accordingly
  • Loading branch information
koenpunt committed Aug 7, 2011
1 parent b171c43 commit b2b2c92
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
15 changes: 11 additions & 4 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@
}
});
Chosen = (function() {
var default_options;
default_options = {
addOptionText: 'Add this item',
noResultsText: 'No results match',
selectOptionText: 'Select an Option',
selectOptionsText: 'Select Some Options'
};
function Chosen(elmn, options) {
this.options = $.extend({}, options);
this.options = $.extend({}, default_options, options);
this.set_default_values();
this.form_field = elmn;
this.form_field_jq = $(this.form_field);
this.is_multiple = this.form_field.multiple;
this.is_rtl = this.form_field_jq.hasClass("chzn-rtl");
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
this.default_text_default = this.form_field.multiple ? this.options.selectOptionsText : this.options.selectOptionText;
this.set_up_html();
this.register_observers();
this.form_field_jq.addClass("chzn-done");
Expand Down Expand Up @@ -547,10 +554,10 @@
};
Chosen.prototype.no_results = function(terms, selected) {
var no_results_html;
no_results_html = $('<li class="no-results">No results match "<span></span>".</li>');
no_results_html = $('<li class="no-results">' + this.options.noResultsText + ' "<span></span>".</li>');
no_results_html.find("span").first().html(terms);
if (this.options.addOption && !selected) {
no_results_html.append(' <a href="javascript:void(0);" class="option-add">Add this item</a>');
no_results_html.append(' <a href="javascript:void(0);" class="option-add">' + this.options.addOptionText + '</a>');
no_results_html.find("a.option-add").bind("click", __bind(function(evt) {
return this.select_add_option(terms);
}, this));
Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

27 changes: 20 additions & 7 deletions chosen/chosen.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = this;
Chosen = (function() {
var default_options;
default_options = {
addOptionText: 'Add this item',
noResultsText: 'No results match',
selectOptionText: 'Select an Option',
selectOptionsText: 'Select Some Options'
};
function Chosen(elmn, options) {
this.set_default_values();
this.form_field = elmn;
this.options = Object.extend({}, options);
this.options = Object.extend(default_options, options);
this.is_multiple = this.form_field.multiple;
this.is_rtl = this.form_field.hasClassName("chzn-rtl");
this.default_text_default = this.form_field.multiple ? "Select Some Options" : "Select an Option";
this.default_text_default = this.form_field.multiple ? this.options.selectOptionsText : this.options.selectOptionText;
this.set_up_html();
this.register_observers();
this.form_field.addClassName("chzn-done");
Expand All @@ -39,8 +46,9 @@
this.single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>');
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.no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>".#{add_item_link}</li>');
return this.new_option_html = new Template('<option value="#{terms}">#{terms}</option>');
this.no_results_temp = new Template('<li class="no-results">#{text} "<span>#{terms}</span>".#{add_item_link}</li>');
this.new_option_temp = new Template('<option value="#{terms}">#{terms}</option>');
return this.add_link_temp = new Template(' <a href="javascript:void(0);" class="option-add">#{text}</a>');
};
Chosen.prototype.set_up_html = function() {
var base_template, container_props, dd_top, dd_width, sf_width;
Expand Down Expand Up @@ -549,9 +557,12 @@
var add_item_link;
add_item_link = '';
if (this.options.addOption && !selected) {
add_item_link = ' <a href="javascript:void(0);" class="option-add">Add this item</a>';
add_item_link = this.add_link_temp.evaluate({
text: this.options.addOptionText
});
}
this.search_results.insert(this.no_results_temp.evaluate({
text: this.options.noResultsText,
terms: terms,
add_item_link: add_item_link
}));
Expand All @@ -571,8 +582,10 @@
}
};
Chosen.prototype.select_append_option = function(terms) {
var option;
option = this.new_option_html.evaluate({
/*
TODO Close options after adding
*/ var option;
option = this.new_option_temp.evaluate({
terms: terms
});
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.

15 changes: 11 additions & 4 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@ $.fn.extend({

class Chosen

default_options = {
addOptionText: 'Add this item',
noResultsText: 'No results match',
selectOptionText: 'Select an Option',
selectOptionsText: 'Select Some Options',
}

constructor: (elmn, options) ->
@options = $.extend({}, options)
@options = $.extend({}, default_options, options)
this.set_default_values()

@form_field = elmn
@form_field_jq = $ @form_field
@is_multiple = @form_field.multiple
@is_rtl = @form_field_jq.hasClass "chzn-rtl"

@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option"
@default_text_default = if @form_field.multiple then @options.selectOptionsText else @options.selectOptionText

this.set_up_html()
this.register_observers()
Expand Down Expand Up @@ -468,11 +475,11 @@ class Chosen
this.result_do_highlight do_high if do_high?

no_results: (terms, selected) ->
no_results_html = $('<li class="no-results">No results match "<span></span>".</li>')
no_results_html = $('<li class="no-results">' + @options.noResultsText + ' "<span></span>".</li>')
no_results_html.find("span").first().html(terms)

if @options.addOption and not selected
no_results_html.append(' <a href="javascript:void(0);" class="option-add">Add this item</a>')
no_results_html.append(' <a href="javascript:void(0);" class="option-add">' + @options.addOptionText + '</a>')
no_results_html.find("a.option-add").bind "click", (evt) => this.select_add_option(terms)
@search_results.append no_results_html

Expand Down
30 changes: 22 additions & 8 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ root = this

class Chosen

default_options = {
addOptionText: 'Add this item',
noResultsText: 'No results match',
selectOptionText: 'Select an Option',
selectOptionsText: 'Select Some Options',
}

constructor: (elmn, options) ->
this.set_default_values()

@form_field = elmn
@options = Object.extend({}, options)
@options = Object.extend(default_options, options)

@is_multiple = @form_field.multiple
@is_rtl = @form_field.hasClassName "chzn-rtl"

@default_text_default = if @form_field.multiple then "Select Some Options" else "Select an Option"
@default_text_default = if @form_field.multiple then @options.selectOptionsText else @options.selectOptionText

this.set_up_html()
this.register_observers()
Expand All @@ -35,8 +43,9 @@ class Chosen
@single_temp = new Template('<a href="javascript:void(0)" class="chzn-single"><span>#{default}</span><div><b></b></div></a><div class="chzn-drop" style="left:-9000px;"><div class="chzn-search"><input type="text" autocomplete="off" /></div><ul class="chzn-results"></ul></div>')
@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>')
@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>')
@no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>".#{add_item_link}</li>')
@new_option_html = new Template('<option value="#{terms}">#{terms}</option>')
@no_results_temp = new Template('<li class="no-results">#{text} "<span>#{terms}</span>".#{add_item_link}</li>')
@new_option_temp = new Template('<option value="#{terms}">#{terms}</option>')
@add_link_temp = new Template(' <a href="javascript:void(0);" class="option-add">#{text}</a>')


set_up_html: ->
Expand Down Expand Up @@ -467,13 +476,14 @@ class Chosen

no_results: (terms, selected) ->
add_item_link = ''

if @options.addOption and not selected
add_item_link = ' <a href="javascript:void(0);" class="option-add">Add this item</a>'
add_item_link = @add_link_temp.evaluate( text: @options.addOptionText )

@search_results.insert @no_results_temp.evaluate( terms: terms, add_item_link: add_item_link )
@search_results.insert @no_results_temp.evaluate( text: @options.noResultsText, terms: terms, add_item_link: add_item_link )

if @options.addOption and not selected
@search_results.down("a.option-add").observe "click", (evt) => this.select_add_option(terms) unless selected
@search_results.down("a.option-add").observe "click", (evt) => this.select_add_option(terms) unless selected

select_add_option: ( terms ) ->
if Object.isFunction(@options.addOption)
Expand All @@ -483,7 +493,11 @@ class Chosen


select_append_option: ( terms ) ->
option = @new_option_html.evaluate( terms: terms )
###
TODO Close options after adding
###

option = @new_option_temp.evaluate( terms: terms )
@form_field.insert option
Event.fire @form_field, "liszt:updated"
this.result_select()
Expand Down
12 changes: 9 additions & 3 deletions example.html
Original file line number Diff line number Diff line change
Expand Up @@ -1275,9 +1275,15 @@ <h2>Setup (for Prototype)</h2>
var selects, _i, _len, _results;
selects = $$(".chzn-select");
for (_i = 0, _len = selects.length; _i < _len; _i++) {
new Chosen(selects[_i], {addOption: function(term, callback){
callback.call(this, term);
}});
new Chosen(selects[_i], {
noResultsText: 'Geen resultaten voor',
addOptionText: 'Toevoegen',
selectOptionText: 'Selecteer een optie',
selectOptionsText: 'Selecteer opties',
addOption: function(term, callback){
callback.call(this, term);
}
});
}
});
</script>
Expand Down
4 changes: 4 additions & 0 deletions example.jquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,10 @@ <h2>Setup (for jQuery)</h2>
<script src="chosen/chosen.jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(".chzn-select").chosen({
noResultsText: 'Geen resultaten voor',
addOptionText: 'Toevoegen',
selectOptionText: 'Selecteer een optie',
selectOptionsText: 'Selecteer opties',
addOption: function(term, callback){
callback.call(this, term);
}
Expand Down

0 comments on commit b2b2c92

Please sign in to comment.