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

abstract-chosen
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
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); }; };
root = typeof exports !== "undefined" && exports !== null ? exports : this;
$ = jQuery;
@ -201,7 +201,7 @@
var content, data, startTime, _i, _len, _ref;
startTime = new Date();
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) {
this.search_choices.find("li.search-choice").remove();
this.choices = 0;
@ -229,7 +229,7 @@
};
Chosen.prototype.result_add_group = function(group) {
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>';
} else {
return "";
@ -238,12 +238,12 @@
Chosen.prototype.result_add_option = function(option) {
var classes;
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"];
if (option.selected) {
classes.push("result-selected");
}
if (option.group_id >= 0) {
if (option.group_array_index != null) {
classes.push("group-option");
}
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) {
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.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();
return link.click(__bind(function(evt) {
return this.choice_destroy_link_click(evt);
@ -402,7 +402,7 @@
position = high_id.substr(high_id.lastIndexOf("_") + 1);
item = this.results_data[position];
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) {
this.choice_build(item);
} else {
@ -424,7 +424,7 @@
var result, result_data;
result_data = this.results_data[pos];
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.removeClass("result-selected").addClass("active-result").show();
this.result_clear_highlight();
@ -455,7 +455,7 @@
$(option.dom_id).hide();
} else if (!(this.is_multiple && option.selected)) {
found = false;
result_id = this.form_field.id + "chzn_o_" + option.id;
result_id = option.dom_id;
if (regex.test(option.text)) {
found = true;
results += 1;
@ -483,8 +483,8 @@
$("#" + result_id).html(text);
}
this.result_activate($("#" + result_id));
if (option.group_id != null) {
$("#" + this.results_data[option.group_id].dom_id).show();
if (option.group_array_index != null) {
$("#" + this.results_data[option.group_array_index].dom_id).show();
}
} else {
if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
@ -670,68 +670,69 @@
return side_border_padding = elmt.outerWidth() - elmt.width();
};
root.get_side_border_padding = get_side_border_padding;
OptionsParser = (function() {
function OptionsParser() {
this.group_index = 0;
this.sel_index = 0;
SelectParser = (function() {
function SelectParser() {
this.options_index = 0;
this.parsed = [];
}
OptionsParser.prototype.add_node = function(child) {
SelectParser.prototype.add_node = function(child) {
if (child.nodeName === "OPTGROUP") {
return this.add_group(child);
} else {
return this.add_option(child);
}
};
OptionsParser.prototype.add_group = function(group) {
var group_id, option, _i, _len, _ref;
group_id = this.sel_index + this.group_index;
SelectParser.prototype.add_group = function(group) {
var group_position, option, _i, _len, _ref, _results;
group_position = this.parsed.length;
this.parsed.push({
id: group_id,
array_index: group_position,
group: true,
label: group.label,
position: this.group_index,
children: 0,
disabled: group.disabled
});
_ref = group.childNodes;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _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;
if (option.nodeName === "OPTION") {
if (option.text !== "") {
if (group_id || group_id === 0) {
this.parsed[group_id].children += 1;
if (group_position != null) {
this.parsed[group_position].children += 1;
}
this.parsed.push({
id: this.sel_index + this.group_index,
select_index: this.sel_index,
array_index: this.parsed.length,
options_index: this.options_index,
value: option.value,
text: option.text,
selected: option.selected,
disabled: (_ref = group_disabled === true) != null ? _ref : {
group_disabled: option.disabled
},
group_id: group_id
group_array_index: group_position
});
} else {
this.parsed.push({
array_index: this.parsed.length,
options_index: this.options_index,
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;
parser = new OptionsParser();
parser = new SelectParser();
_ref = select.childNodes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
@ -739,5 +740,5 @@
}
return parser.parsed;
};
root.OptionsParser = OptionsParser;
root.SelectParser = SelectParser;
}).call(this);

View File

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

View File

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

View File

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