jQuery bug fixes

This commit is contained in:
Patrick Filler 2011-07-13 12:07:26 -04:00
parent 7f6874bfd0
commit b46227fc5f
2 changed files with 44 additions and 44 deletions

View file

@ -273,18 +273,20 @@
}; };
Chosen.prototype.result_do_highlight = function(el) { Chosen.prototype.result_do_highlight = function(el) {
var high_bottom, high_top, maxHeight, visible_bottom, visible_top; var high_bottom, high_top, maxHeight, visible_bottom, visible_top;
this.result_clear_highlight(); if (el) {
this.result_highlight = el; this.result_clear_highlight();
this.result_highlight.addClass("highlighted"); this.result_highlight = el;
maxHeight = parseInt(this.search_results.css("maxHeight"), 10); this.result_highlight.addClass("highlighted");
visible_top = this.search_results.scrollTop(); maxHeight = parseInt(this.search_results.css("maxHeight"), 10);
visible_bottom = maxHeight + visible_top; visible_top = this.search_results.scrollTop();
high_top = this.result_highlight.position().top; visible_bottom = maxHeight + visible_top;
high_bottom = high_top + this.result_highlight.outerHeight(); high_top = this.result_highlight.position().top;
if (high_bottom >= visible_bottom) { high_bottom = high_top + this.result_highlight.outerHeight();
return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0); if (high_bottom >= visible_bottom) {
} else if (high_top < visible_top) { return this.search_results.scrollTop((high_bottom - maxHeight) > 0 ? high_bottom - maxHeight : 0);
return this.search_results.scrollTop(high_top); } else if (high_top < visible_top) {
return this.search_results.scrollTop(high_top);
}
} }
}; };
Chosen.prototype.result_clear_highlight = function() { Chosen.prototype.result_clear_highlight = function() {
@ -451,7 +453,7 @@
startTime = new Date(); startTime = new Date();
this.no_results_clear(); this.no_results_clear();
results = 0; results = 0;
searchText = this.search_field.value === this.default_text ? "" : $.trim(this.search_field.value); searchText = this.search_field.val() === this.default_text ? "" : $.trim(this.search_field.val());
regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i'); zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i');
_ref = this.results_data; _ref = this.results_data;
@ -486,18 +488,18 @@
} else { } else {
text = option.text; text = option.text;
} }
if ($(result_id).innerHTML !== text) { if ($("#" + result_id).html !== text) {
$(result_id).text(text); $("#" + result_id).html(text);
} }
this.result_activate($(result_id)); this.result_activate($("#" + result_id));
if (option.group_id != null) { if (option.group_id != null) {
$(this.results_data[option.group_id].dom_id).show(); $("#" + this.results_data[option.group_id].dom_id).show();
} }
} else { } else {
if ($(result_id) === this.result_highlight) { if (this.result_highlight && result_id === this.result_highlight.attr('id')) {
this.result_clear_highlight(); this.result_clear_highlight();
} }
this.result_deactivate($(result_id)); this.result_deactivate($("#" + result_id));
} }
} }
} }
@ -530,9 +532,10 @@
} }
}; };
Chosen.prototype.no_results = function(terms) { Chosen.prototype.no_results = function(terms) {
return this.search_results.insert(this.no_results_temp.evaluate({ var no_results_html;
"terms": terms.escapeHTML() no_results_html = $('<li class="no-results">No results match "<span></span>"</li>');
})); no_results_html.find("span").first().text(terms);
return this.search_results.append(no_results_html);
}; };
Chosen.prototype.no_results_clear = function() { Chosen.prototype.no_results_clear = function() {
return this.search_results.find(".no-results").remove(); return this.search_results.find(".no-results").remove();
@ -675,11 +678,8 @@
root.get_side_border_padding = get_side_border_padding; root.get_side_border_padding = get_side_border_padding;
first_intersect = function(a1, a2) { first_intersect = function(a1, a2) {
var element, _i, _len; var element, _i, _len;
console.log(a2);
for (_i = 0, _len = a1.length; _i < _len; _i++) { for (_i = 0, _len = a1.length; _i < _len; _i++) {
element = a1[_i]; element = a1[_i];
console.log(element);
console.log($.inArray(element, a2));
if ($.inArray(element, a2)) { if ($.inArray(element, a2)) {
return element; return element;
} }

View file

@ -57,10 +57,6 @@ class Chosen
@result_single_selected = null @result_single_selected = null
@choices = 0 @choices = 0
# HTML Templates
#@no_results_temp = new Template('<li class="no-results">No results match "<span>#{terms}</span>"</li>')
set_up_html: -> set_up_html: ->
@container_id = @form_field.id + "_chzn" @container_id = @form_field.id + "_chzn"
@ -245,6 +241,7 @@ class Chosen
this.results_build() this.results_build()
result_do_highlight: (el) -> result_do_highlight: (el) ->
if el
this.result_clear_highlight(); this.result_clear_highlight();
@result_highlight = el; @result_highlight = el;
@ -253,10 +250,10 @@ class Chosen
maxHeight = parseInt @search_results.css("maxHeight"), 10 maxHeight = parseInt @search_results.css("maxHeight"), 10
visible_top = @search_results.scrollTop() visible_top = @search_results.scrollTop()
visible_bottom = maxHeight + visible_top visible_bottom = maxHeight + visible_top
high_top = @result_highlight.position().top high_top = @result_highlight.position().top
high_bottom = high_top + @result_highlight.outerHeight() high_bottom = high_top + @result_highlight.outerHeight()
#console.log visible_top, visible_bottom, high_top, high_bottom #console.log visible_top, visible_bottom, high_top, high_bottom
if high_bottom >= visible_bottom if high_bottom >= visible_bottom
@ -413,10 +410,10 @@ class Chosen
winnow_results: -> winnow_results: ->
startTime = new Date() startTime = new Date()
this.no_results_clear() this.no_results_clear()
results = 0 results = 0
searchText = if @search_field.value is @default_text then "" else $.trim @search_field.value searchText = if @search_field.val() is @default_text then "" else $.trim @search_field.val()
regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i') regex = new RegExp('^' + searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i') zregex = new RegExp(searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), 'i')
@ -448,17 +445,17 @@ class Chosen
else else
text = option.text text = option.text
$(result_id).text text if $(result_id).innerHTML != text $("#" + result_id).html text if $("#" + result_id).html != text
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_id].dom_id).show() if option.group_id?
else else
this.result_clear_highlight() if $(result_id) is @result_highlight 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)
if results < 1 and searchText.length if results < 1 and searchText.length
this.no_results(searchText) this.no_results searchText
else else
this.winnow_results_set_highlight() this.winnow_results_set_highlight()
@ -480,7 +477,10 @@ class Chosen
this.result_do_highlight do_high this.result_do_highlight do_high
no_results: (terms) -> no_results: (terms) ->
@search_results.insert @no_results_temp.evaluate({"terms":terms.escapeHTML()}) no_results_html = $('<li class="no-results">No results match "<span></span>"</li>')
no_results_html.find("span").first().text(terms)
@search_results.append no_results_html
no_results_clear: -> no_results_clear: ->
@search_results.find(".no-results").remove() @search_results.find(".no-results").remove()
@ -592,10 +592,10 @@ root.get_side_border_padding = get_side_border_padding
first_intersect = (a1, a2) -> first_intersect = (a1, a2) ->
# TODO for some reason, up arrow doesn't find the first # TODO for some reason, up arrow doesn't find the first
console.log a2 #console.log a2
for element in a1 for element in a1
console.log element #console.log element
console.log $.inArray element, a2 #console.log $.inArray element, a2
return element if $.inArray element, a2 return element if $.inArray element, a2
return null return null