Update to use latest version of Select Parser (https://github.com/pfiller/Select-Parser). This fixes issues with group positioning.

This commit is contained in:
Patrick Filler 2011-07-19 15:46:40 -04:00
parent d0ef447dce
commit f5967bd265
4 changed files with 126 additions and 126 deletions

View file

@ -6,7 +6,7 @@
Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License
Copyright (c) 2011 by Harvest Copyright (c) 2011 by Harvest
*/ var $, Chosen, OptionsParser, get_side_border_padding, root; */ var $, Chosen, SelectParser, get_side_border_padding, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = typeof exports !== "undefined" && exports !== null ? exports : this; root = typeof exports !== "undefined" && exports !== null ? exports : this;
$ = jQuery; $ = jQuery;
@ -201,7 +201,7 @@
var content, data, startTime, _i, _len, _ref; var content, data, startTime, _i, _len, _ref;
startTime = new Date(); startTime = new Date();
this.parsing = true; this.parsing = true;
this.results_data = OptionsParser.select_to_array(this.form_field); this.results_data = SelectParser.select_to_array(this.form_field);
if (this.is_multiple && this.choices > 0) { if (this.is_multiple && this.choices > 0) {
this.search_choices.find("li.search-choice").remove(); this.search_choices.find("li.search-choice").remove();
this.choices = 0; this.choices = 0;
@ -229,7 +229,7 @@
}; };
Chosen.prototype.result_add_group = function(group) { Chosen.prototype.result_add_group = function(group) {
if (!group.disabled) { if (!group.disabled) {
group.dom_id = this.form_field.id + "chzn_g_" + group.id; group.dom_id = this.form_field.id + "chzn_g_" + group.array_index;
return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>'; return '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>';
} else { } else {
return ""; return "";
@ -238,12 +238,12 @@
Chosen.prototype.result_add_option = function(option) { Chosen.prototype.result_add_option = function(option) {
var classes; var classes;
if (!option.disabled) { if (!option.disabled) {
option.dom_id = this.form_field.id + "chzn_o_" + option.id; option.dom_id = this.form_field.id + "chzn_o_" + option.array_index;
classes = option.selected && this.is_multiple ? [] : ["active-result"]; classes = option.selected && this.is_multiple ? [] : ["active-result"];
if (option.selected) { if (option.selected) {
classes.push("result-selected"); classes.push("result-selected");
} }
if (option.group_id >= 0) { if (option.group_array_index != null) {
classes.push("group-option"); classes.push("group-option");
} }
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + $("<div />").text(option.text).html() + '</li>'; return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + $("<div />").text(option.text).html() + '</li>';
@ -365,9 +365,9 @@
}; };
Chosen.prototype.choice_build = function(item) { Chosen.prototype.choice_build = function(item) {
var choice_id, link; var choice_id, link;
choice_id = this.form_field.id + "_chzn_c_" + item.id; choice_id = this.form_field.id + "_chzn_c_" + item.array_index;
this.choices += 1; this.choices += 1;
this.search_container.before('<li class="search-choice" id="' + choice_id + '"><span>' + item.text + '</span><a href="#" class="search-choice-close" rel="' + item.id + '"></a></li>'); this.search_container.before('<li class="search-choice" id="' + choice_id + '"><span>' + item.text + '</span><a href="#" class="search-choice-close" rel="' + item.array_index + '"></a></li>');
link = $('#' + choice_id).find("a").first(); link = $('#' + choice_id).find("a").first();
return link.click(__bind(function(evt) { return link.click(__bind(function(evt) {
return this.choice_destroy_link_click(evt); return this.choice_destroy_link_click(evt);
@ -402,7 +402,7 @@
position = high_id.substr(high_id.lastIndexOf("_") + 1); position = high_id.substr(high_id.lastIndexOf("_") + 1);
item = this.results_data[position]; item = this.results_data[position];
item.selected = true; item.selected = true;
this.form_field.options[item.select_index].selected = true; this.form_field.options[item.options_index].selected = true;
if (this.is_multiple) { if (this.is_multiple) {
this.choice_build(item); this.choice_build(item);
} else { } else {
@ -424,7 +424,7 @@
var result, result_data; var result, result_data;
result_data = this.results_data[pos]; result_data = this.results_data[pos];
result_data.selected = false; result_data.selected = false;
this.form_field.options[result_data.select_index].selected = false; this.form_field.options[result_data.options_index].selected = false;
result = $(this.form_field.id + "chzn_o_" + pos); result = $(this.form_field.id + "chzn_o_" + pos);
result.removeClass("result-selected").addClass("active-result").show(); result.removeClass("result-selected").addClass("active-result").show();
this.result_clear_highlight(); this.result_clear_highlight();
@ -455,7 +455,7 @@
$(option.dom_id).hide(); $(option.dom_id).hide();
} else if (!(this.is_multiple && option.selected)) { } else if (!(this.is_multiple && option.selected)) {
found = false; found = false;
result_id = this.form_field.id + "chzn_o_" + option.id; result_id = option.dom_id;
if (regex.test(option.text)) { if (regex.test(option.text)) {
found = true; found = true;
results += 1; results += 1;
@ -483,8 +483,8 @@
$("#" + result_id).html(text); $("#" + result_id).html(text);
} }
this.result_activate($("#" + result_id)); this.result_activate($("#" + result_id));
if (option.group_id != null) { if (option.group_array_index != null) {
$("#" + this.results_data[option.group_id].dom_id).show(); $("#" + this.results_data[option.group_array_index].dom_id).show();
} }
} else { } else {
if (this.result_highlight && result_id === this.result_highlight.attr('id')) { if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
@ -670,68 +670,69 @@
return side_border_padding = elmt.outerWidth() - elmt.width(); return side_border_padding = elmt.outerWidth() - elmt.width();
}; };
root.get_side_border_padding = get_side_border_padding; root.get_side_border_padding = get_side_border_padding;
OptionsParser = (function() { SelectParser = (function() {
function OptionsParser() { function SelectParser() {
this.group_index = 0; this.options_index = 0;
this.sel_index = 0;
this.parsed = []; this.parsed = [];
} }
OptionsParser.prototype.add_node = function(child) { SelectParser.prototype.add_node = function(child) {
if (child.nodeName === "OPTGROUP") { if (child.nodeName === "OPTGROUP") {
return this.add_group(child); return this.add_group(child);
} else { } else {
return this.add_option(child); return this.add_option(child);
} }
}; };
OptionsParser.prototype.add_group = function(group) { SelectParser.prototype.add_group = function(group) {
var group_id, option, _i, _len, _ref; var group_position, option, _i, _len, _ref, _results;
group_id = this.sel_index + this.group_index; group_position = this.parsed.length;
this.parsed.push({ this.parsed.push({
id: group_id, array_index: group_position,
group: true, group: true,
label: group.label, label: group.label,
position: this.group_index,
children: 0, children: 0,
disabled: group.disabled disabled: group.disabled
}); });
_ref = group.childNodes; _ref = group.childNodes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i]; option = _ref[_i];
this.add_option(option, group_id, group.disabled); _results.push(this.add_option(option, group_position, group.disabled));
} }
return this.group_index += 1; return _results;
}; };
OptionsParser.prototype.add_option = function(option, group_id, group_disabled) { SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
var _ref; var _ref;
if (option.nodeName === "OPTION") { if (option.nodeName === "OPTION") {
if (option.text !== "") { if (option.text !== "") {
if (group_id || group_id === 0) { if (group_position != null) {
this.parsed[group_id].children += 1; this.parsed[group_position].children += 1;
} }
this.parsed.push({ this.parsed.push({
id: this.sel_index + this.group_index, array_index: this.parsed.length,
select_index: this.sel_index, options_index: this.options_index,
value: option.value, value: option.value,
text: option.text, text: option.text,
selected: option.selected, selected: option.selected,
disabled: (_ref = group_disabled === true) != null ? _ref : { disabled: (_ref = group_disabled === true) != null ? _ref : {
group_disabled: option.disabled group_disabled: option.disabled
}, },
group_id: group_id group_array_index: group_position
}); });
} else { } else {
this.parsed.push({ this.parsed.push({
array_index: this.parsed.length,
options_index: this.options_index,
empty: true empty: true
}); });
} }
return this.sel_index += 1; return this.options_index += 1;
} }
}; };
return OptionsParser; return SelectParser;
})(); })();
OptionsParser.select_to_array = function(select) { SelectParser.select_to_array = function(select) {
var child, parser, _i, _len, _ref; var child, parser, _i, _len, _ref;
parser = new OptionsParser(); parser = new SelectParser();
_ref = select.childNodes; _ref = select.childNodes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i]; child = _ref[_i];
@ -739,5 +740,5 @@
} }
return parser.parsed; return parser.parsed;
}; };
root.OptionsParser = OptionsParser; root.SelectParser = SelectParser;
}).call(this); }).call(this);

View file

@ -6,7 +6,7 @@
Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License Available for use under the MIT License, http://en.wikipedia.org/wiki/MIT_License
Copyright (c) 2011 by Harvest Copyright (c) 2011 by Harvest
*/ var Chosen, OptionsParser, get_side_border_padding, root; */ var Chosen, SelectParser, get_side_border_padding, root;
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
root = typeof exports !== "undefined" && exports !== null ? exports : this; root = typeof exports !== "undefined" && exports !== null ? exports : this;
Chosen = (function() { Chosen = (function() {
@ -198,7 +198,7 @@
var content, data, startTime, _i, _len, _ref; var content, data, startTime, _i, _len, _ref;
startTime = new Date(); startTime = new Date();
this.parsing = true; this.parsing = true;
this.results_data = OptionsParser.select_to_array(this.form_field); this.results_data = SelectParser.select_to_array(this.form_field);
if (this.is_multiple && this.choices > 0) { if (this.is_multiple && this.choices > 0) {
this.search_choices.select("li.search-choice").invoke("remove"); this.search_choices.select("li.search-choice").invoke("remove");
this.choices = 0; this.choices = 0;
@ -226,7 +226,7 @@
}; };
Chosen.prototype.result_add_group = function(group) { Chosen.prototype.result_add_group = function(group) {
if (!group.disabled) { if (!group.disabled) {
group.dom_id = this.form_field.id + "chzn_g_" + group.id; group.dom_id = this.form_field.id + "chzn_g_" + group.array_index;
return '<li id="' + group.dom_id + '" class="group-result">' + group.label.escapeHTML() + '</li>'; return '<li id="' + group.dom_id + '" class="group-result">' + group.label.escapeHTML() + '</li>';
} else { } else {
return ""; return "";
@ -235,12 +235,12 @@
Chosen.prototype.result_add_option = function(option) { Chosen.prototype.result_add_option = function(option) {
var classes; var classes;
if (!option.disabled) { if (!option.disabled) {
option.dom_id = this.form_field.id + "chzn_o_" + option.id; option.dom_id = this.form_field.id + "chzn_o_" + option.array_index;
classes = option.selected && this.is_multiple ? [] : ["active-result"]; classes = option.selected && this.is_multiple ? [] : ["active-result"];
if (option.selected) { if (option.selected) {
classes.push("result-selected"); classes.push("result-selected");
} }
if (option.group_id >= 0) { if (option.group_array_index != null) {
classes.push("group-option"); classes.push("group-option");
} }
return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.text.escapeHTML() + '</li>'; return '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.text.escapeHTML() + '</li>';
@ -360,13 +360,13 @@
}; };
Chosen.prototype.choice_build = function(item) { Chosen.prototype.choice_build = function(item) {
var choice_id, link; var choice_id, link;
choice_id = this.form_field.id + "_chzn_c_" + item.id; choice_id = this.form_field.id + "_chzn_c_" + item.array_index;
this.choices += 1; this.choices += 1;
this.search_container.insert({ this.search_container.insert({
before: this.choice_temp.evaluate({ before: this.choice_temp.evaluate({
"id": choice_id, "id": choice_id,
"choice": item.text, "choice": item.text,
"position": item.id "position": item.array_index
}) })
}); });
link = $(choice_id).down('a'); link = $(choice_id).down('a');
@ -402,7 +402,7 @@
position = high.id.substr(high.id.lastIndexOf("_") + 1); position = high.id.substr(high.id.lastIndexOf("_") + 1);
item = this.results_data[position]; item = this.results_data[position];
item.selected = true; item.selected = true;
this.form_field.options[item.select_index].selected = true; this.form_field.options[item.options_index].selected = true;
if (this.is_multiple) { if (this.is_multiple) {
this.choice_build(item); this.choice_build(item);
} else { } else {
@ -426,7 +426,7 @@
var result, result_data; var result, result_data;
result_data = this.results_data[pos]; result_data = this.results_data[pos];
result_data.selected = false; result_data.selected = false;
this.form_field.options[result_data.select_index].selected = false; this.form_field.options[result_data.options_index].selected = false;
result = $(this.form_field.id + "chzn_o_" + pos); result = $(this.form_field.id + "chzn_o_" + pos);
result.removeClassName("result-selected").addClassName("active-result").show(); result.removeClassName("result-selected").addClassName("active-result").show();
this.result_clear_highlight(); this.result_clear_highlight();
@ -459,7 +459,7 @@
$(option.dom_id).hide(); $(option.dom_id).hide();
} else if (!(this.is_multiple && option.selected)) { } else if (!(this.is_multiple && option.selected)) {
found = false; found = false;
result_id = this.form_field.id + "chzn_o_" + option.id; result_id = option.dom_id;
if (regex.test(option.text)) { if (regex.test(option.text)) {
found = true; found = true;
results += 1; results += 1;
@ -487,8 +487,8 @@
$(result_id).update(text); $(result_id).update(text);
} }
this.result_activate($(result_id)); this.result_activate($(result_id));
if (option.group_id != null) { if (option.group_array_index != null) {
$(this.results_data[option.group_id].dom_id).show(); $(this.results_data[option.group_array_index].dom_id).show();
} }
} else { } else {
if ($(result_id) === this.result_highlight) { if ($(result_id) === this.result_highlight) {
@ -689,68 +689,69 @@
}; };
root.get_side_border_padding = get_side_border_padding; root.get_side_border_padding = get_side_border_padding;
root = typeof exports !== "undefined" && exports !== null ? exports : this; root = typeof exports !== "undefined" && exports !== null ? exports : this;
OptionsParser = (function() { SelectParser = (function() {
function OptionsParser() { function SelectParser() {
this.group_index = 0; this.options_index = 0;
this.sel_index = 0;
this.parsed = []; this.parsed = [];
} }
OptionsParser.prototype.add_node = function(child) { SelectParser.prototype.add_node = function(child) {
if (child.nodeName === "OPTGROUP") { if (child.nodeName === "OPTGROUP") {
return this.add_group(child); return this.add_group(child);
} else { } else {
return this.add_option(child); return this.add_option(child);
} }
}; };
OptionsParser.prototype.add_group = function(group) { SelectParser.prototype.add_group = function(group) {
var group_id, option, _i, _len, _ref; var group_position, option, _i, _len, _ref, _results;
group_id = this.sel_index + this.group_index; group_position = this.parsed.length;
this.parsed.push({ this.parsed.push({
id: group_id, array_index: group_position,
group: true, group: true,
label: group.label, label: group.label,
position: this.group_index,
children: 0, children: 0,
disabled: group.disabled disabled: group.disabled
}); });
_ref = group.childNodes; _ref = group.childNodes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
option = _ref[_i]; option = _ref[_i];
this.add_option(option, group_id, group.disabled); _results.push(this.add_option(option, group_position, group.disabled));
} }
return this.group_index += 1; return _results;
}; };
OptionsParser.prototype.add_option = function(option, group_id, group_disabled) { SelectParser.prototype.add_option = function(option, group_position, group_disabled) {
var _ref; var _ref;
if (option.nodeName === "OPTION") { if (option.nodeName === "OPTION") {
if (option.text !== "") { if (option.text !== "") {
if (group_id || group_id === 0) { if (group_position != null) {
this.parsed[group_id].children += 1; this.parsed[group_position].children += 1;
} }
this.parsed.push({ this.parsed.push({
id: this.sel_index + this.group_index, array_index: this.parsed.length,
select_index: this.sel_index, options_index: this.options_index,
value: option.value, value: option.value,
text: option.text, text: option.text,
selected: option.selected, selected: option.selected,
disabled: (_ref = group_disabled === true) != null ? _ref : { disabled: (_ref = group_disabled === true) != null ? _ref : {
group_disabled: option.disabled group_disabled: option.disabled
}, },
group_id: group_id group_array_index: group_position
}); });
} else { } else {
this.parsed.push({ this.parsed.push({
array_index: this.parsed.length,
options_index: this.options_index,
empty: true empty: true
}); });
} }
return this.sel_index += 1; return this.options_index += 1;
} }
}; };
return OptionsParser; return SelectParser;
})(); })();
OptionsParser.select_to_array = function(select) { SelectParser.select_to_array = function(select) {
var child, parser, _i, _len, _ref; var child, parser, _i, _len, _ref;
parser = new OptionsParser(); parser = new SelectParser();
_ref = select.childNodes; _ref = select.childNodes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i]; child = _ref[_i];
@ -758,5 +759,5 @@
} }
return parser.parsed; return parser.parsed;
}; };
root.OptionsParser = OptionsParser; root.SelectParser = SelectParser;
}).call(this); }).call(this);

View file

@ -177,7 +177,7 @@ class Chosen
results_build: -> results_build: ->
startTime = new Date() startTime = new Date()
@parsing = true @parsing = true
@results_data = OptionsParser.select_to_array @form_field @results_data = SelectParser.select_to_array @form_field
if @is_multiple and @choices > 0 if @is_multiple and @choices > 0
@search_choices.find("li.search-choice").remove() @search_choices.find("li.search-choice").remove()
@ -203,18 +203,18 @@ class Chosen
result_add_group: (group) -> result_add_group: (group) ->
if not group.disabled if not group.disabled
group.dom_id = @form_field.id + "chzn_g_" + group.id group.dom_id = @form_field.id + "chzn_g_" + group.array_index
'<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>' '<li id="' + group.dom_id + '" class="group-result">' + $("<div />").text(group.label).html() + '</li>'
else else
"" ""
result_add_option: (option) -> result_add_option: (option) ->
if not option.disabled if not option.disabled
option.dom_id = @form_field.id + "chzn_o_" + option.id option.dom_id = @form_field.id + "chzn_o_" + option.array_index
classes = if option.selected and @is_multiple then [] else ["active-result"] classes = if option.selected and @is_multiple then [] else ["active-result"]
classes.push "result-selected" if option.selected classes.push "result-selected" if option.selected
classes.push "group-option" if option.group_id >= 0 classes.push "group-option" if option.group_array_index?
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + $("<div />").text(option.text).html() + '</li>' '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + $("<div />").text(option.text).html() + '</li>'
else else
@ -315,9 +315,9 @@ class Chosen
this.results_show() this.results_show()
choice_build: (item) -> choice_build: (item) ->
choice_id = @form_field.id + "_chzn_c_" + item.id choice_id = @form_field.id + "_chzn_c_" + item.array_index
@choices += 1 @choices += 1
@search_container.before '<li class="search-choice" id="' + choice_id + '"><span>' + item.text + '</span><a href="#" class="search-choice-close" rel="' + item.id + '"></a></li>' @search_container.before '<li class="search-choice" id="' + choice_id + '"><span>' + item.text + '</span><a href="#" class="search-choice-close" rel="' + item.array_index + '"></a></li>'
link = $('#' + choice_id).find("a").first() link = $('#' + choice_id).find("a").first()
link.click (evt) => this.choice_destroy_link_click(evt) link.click (evt) => this.choice_destroy_link_click(evt)
@ -353,7 +353,7 @@ class Chosen
item = @results_data[position] item = @results_data[position]
item.selected = true item.selected = true
@form_field.options[item.select_index].selected = true @form_field.options[item.options_index].selected = true
if @is_multiple if @is_multiple
this.choice_build item this.choice_build item
@ -376,7 +376,7 @@ class Chosen
result_data = @results_data[pos] result_data = @results_data[pos]
result_data.selected = false result_data.selected = false
@form_field.options[result_data.select_index].selected = false @form_field.options[result_data.options_index].selected = false
result = $(@form_field.id + "chzn_o_" + pos) result = $(@form_field.id + "chzn_o_" + pos)
result.removeClass("result-selected").addClass("active-result").show() result.removeClass("result-selected").addClass("active-result").show()
@ -408,7 +408,7 @@ class Chosen
$(option.dom_id).hide() $(option.dom_id).hide()
else if not (@is_multiple and option.selected) else if not (@is_multiple and option.selected)
found = false found = false
result_id = @form_field.id + "chzn_o_" + option.id result_id = option.dom_id
if regex.test option.text if regex.test option.text
found = true; found = true;
@ -434,7 +434,7 @@ class Chosen
this.result_activate $("#" + result_id) this.result_activate $("#" + result_id)
$("#" + @results_data[option.group_id].dom_id).show() if option.group_id? $("#" + @results_data[option.group_array_index].dom_id).show() if option.group_array_index?
else else
this.result_clear_highlight() if @result_highlight and result_id is @result_highlight.attr 'id' this.result_clear_highlight() if @result_highlight and result_id is @result_highlight.attr 'id'
this.result_deactivate $("#" + result_id) this.result_deactivate $("#" + result_id)
@ -577,11 +577,10 @@ get_side_border_padding = (elmt) ->
root.get_side_border_padding = get_side_border_padding root.get_side_border_padding = get_side_border_padding
class OptionsParser class SelectParser
constructor: -> constructor: ->
@group_index = 0 @options_index = 0
@sel_index = 0
@parsed = [] @parsed = []
add_node: (child) -> add_node: (child) ->
@ -591,38 +590,38 @@ class OptionsParser
this.add_option child this.add_option child
add_group: (group) -> add_group: (group) ->
group_id = @sel_index + @group_index group_position = @parsed.length
@parsed.push @parsed.push
id: group_id array_index: group_position
group: true group: true
label: group.label label: group.label
position: @group_index
children: 0 children: 0
disabled: group.disabled disabled: group.disabled
this.add_option( option, group_id, group.disabled ) for option in group.childNodes this.add_option( option, group_position, group.disabled ) for option in group.childNodes
@group_index += 1
add_option: (option, group_id, group_disabled) -> add_option: (option, group_position, group_disabled) ->
if option.nodeName is "OPTION" if option.nodeName is "OPTION"
if option.text != "" if option.text != ""
if group_id || group_id is 0 if group_position?
@parsed[group_id].children += 1 @parsed[group_position].children += 1
@parsed.push @parsed.push
id: @sel_index + @group_index array_index: @parsed.length
select_index: @sel_index options_index: @options_index
value: option.value value: option.value
text: option.text text: option.text
selected: option.selected selected: option.selected
disabled: ((group_disabled is true) ? group_disabled : option.disabled) disabled: ((group_disabled is true) ? group_disabled : option.disabled)
group_id: group_id group_array_index: group_position
else else
@parsed.push @parsed.push
array_index: @parsed.length
options_index: @options_index
empty: true empty: true
@sel_index += 1 @options_index += 1
OptionsParser.select_to_array = (select) -> SelectParser.select_to_array = (select) ->
parser = new OptionsParser() parser = new SelectParser()
parser.add_node( child ) for child in select.childNodes parser.add_node( child ) for child in select.childNodes
parser.parsed parser.parsed
root.OptionsParser = OptionsParser root.SelectParser = SelectParser

View file

@ -172,7 +172,7 @@ class Chosen
results_build: -> results_build: ->
startTime = new Date() startTime = new Date()
@parsing = true @parsing = true
@results_data = OptionsParser.select_to_array @form_field @results_data = SelectParser.select_to_array @form_field
if @is_multiple and @choices > 0 if @is_multiple and @choices > 0
@search_choices.select("li.search-choice").invoke("remove") @search_choices.select("li.search-choice").invoke("remove")
@ -198,18 +198,18 @@ class Chosen
result_add_group: (group) -> result_add_group: (group) ->
if not group.disabled if not group.disabled
group.dom_id = @form_field.id + "chzn_g_" + group.id group.dom_id = @form_field.id + "chzn_g_" + group.array_index
'<li id="' + group.dom_id + '" class="group-result">' + group.label.escapeHTML() + '</li>' '<li id="' + group.dom_id + '" class="group-result">' + group.label.escapeHTML() + '</li>'
else else
"" ""
result_add_option: (option) -> result_add_option: (option) ->
if not option.disabled if not option.disabled
option.dom_id = @form_field.id + "chzn_o_" + option.id option.dom_id = @form_field.id + "chzn_o_" + option.array_index
classes = if option.selected and @is_multiple then [] else ["active-result"] classes = if option.selected and @is_multiple then [] else ["active-result"]
classes.push "result-selected" if option.selected classes.push "result-selected" if option.selected
classes.push "group-option" if option.group_id >= 0 classes.push "group-option" if option.group_array_index?
'<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.text.escapeHTML() + '</li>' '<li id="' + option.dom_id + '" class="' + classes.join(' ') + '">' + option.text.escapeHTML() + '</li>'
else else
@ -309,9 +309,9 @@ class Chosen
this.results_show() this.results_show()
choice_build: (item) -> choice_build: (item) ->
choice_id = @form_field.id + "_chzn_c_" + item.id choice_id = @form_field.id + "_chzn_c_" + item.array_index
@choices += 1 @choices += 1
@search_container.insert { before: @choice_temp.evaluate({"id":choice_id, "choice":item.text, "position":item.id}) } @search_container.insert { before: @choice_temp.evaluate({"id":choice_id, "choice":item.text, "position":item.array_index}) }
link = $(choice_id).down('a') link = $(choice_id).down('a')
link.observe "click", (evt) => this.choice_destroy_link_click(evt) link.observe "click", (evt) => this.choice_destroy_link_click(evt)
@ -345,7 +345,7 @@ class Chosen
item = @results_data[position] item = @results_data[position]
item.selected = true item.selected = true
@form_field.options[item.select_index].selected = true @form_field.options[item.options_index].selected = true
if @is_multiple if @is_multiple
this.choice_build item this.choice_build item
@ -368,7 +368,7 @@ class Chosen
result_data = @results_data[pos] result_data = @results_data[pos]
result_data.selected = false result_data.selected = false
@form_field.options[result_data.select_index].selected = false @form_field.options[result_data.options_index].selected = false
result = $(@form_field.id + "chzn_o_" + pos) result = $(@form_field.id + "chzn_o_" + pos)
result.removeClassName("result-selected").addClassName("active-result").show() result.removeClassName("result-selected").addClassName("active-result").show()
@ -400,7 +400,7 @@ class Chosen
$(option.dom_id).hide() $(option.dom_id).hide()
else if not (@is_multiple and option.selected) else if not (@is_multiple and option.selected)
found = false found = false
result_id = @form_field.id + "chzn_o_" + option.id result_id = option.dom_id
if regex.test option.text if regex.test option.text
found = true; found = true;
@ -426,7 +426,7 @@ class Chosen
this.result_activate $(result_id) this.result_activate $(result_id)
$(@results_data[option.group_id].dom_id).show() if option.group_id? $(@results_data[option.group_array_index].dom_id).show() if option.group_array_index?
else else
this.result_clear_highlight() if $(result_id) is @result_highlight this.result_clear_highlight() if $(result_id) is @result_highlight
this.result_deactivate $(result_id) this.result_deactivate $(result_id)
@ -575,11 +575,10 @@ root.get_side_border_padding = get_side_border_padding
root = exports ? this root = exports ? this
class OptionsParser class SelectParser
constructor: -> constructor: ->
@group_index = 0 @options_index = 0
@sel_index = 0
@parsed = [] @parsed = []
add_node: (child) -> add_node: (child) ->
@ -589,38 +588,38 @@ class OptionsParser
this.add_option child this.add_option child
add_group: (group) -> add_group: (group) ->
group_id = @sel_index + @group_index group_position = @parsed.length
@parsed.push @parsed.push
id: group_id array_index: group_position
group: true group: true
label: group.label label: group.label
position: @group_index
children: 0 children: 0
disabled: group.disabled disabled: group.disabled
this.add_option( option, group_id, group.disabled ) for option in group.childNodes this.add_option( option, group_position, group.disabled ) for option in group.childNodes
@group_index += 1
add_option: (option, group_id, group_disabled) -> add_option: (option, group_position, group_disabled) ->
if option.nodeName is "OPTION" if option.nodeName is "OPTION"
if option.text != "" if option.text != ""
if group_id || group_id is 0 if group_position?
@parsed[group_id].children += 1 @parsed[group_position].children += 1
@parsed.push @parsed.push
id: @sel_index + @group_index array_index: @parsed.length
select_index: @sel_index options_index: @options_index
value: option.value value: option.value
text: option.text text: option.text
selected: option.selected selected: option.selected
disabled: ((group_disabled is true) ? group_disabled : option.disabled) disabled: ((group_disabled is true) ? group_disabled : option.disabled)
group_id: group_id group_array_index: group_position
else else
@parsed.push @parsed.push
array_index: @parsed.length
options_index: @options_index
empty: true empty: true
@sel_index += 1 @options_index += 1
OptionsParser.select_to_array = (select) -> SelectParser.select_to_array = (select) ->
parser = new OptionsParser() parser = new SelectParser()
parser.add_node( child ) for child in select.childNodes parser.add_node( child ) for child in select.childNodes
parser.parsed parser.parsed
root.OptionsParser = OptionsParser root.SelectParser = SelectParser