Fix arrow up/down behavior without relying on buggy intersect method.

This commit is contained in:
Patrick Filler 2011-07-13 12:37:37 -04:00
parent d16cf5f3da
commit 465eb61eb6
2 changed files with 29 additions and 59 deletions

View file

@ -22,7 +22,7 @@
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/ var $, Chosen, OptionsParser, first_intersect, get_side_border_padding, root;
*/ var $, Chosen, OptionsParser, 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;
@ -541,33 +541,30 @@
return this.search_results.find(".no-results").remove();
};
Chosen.prototype.keydown_arrow = function() {
var actives, next, sibs;
actives = this.search_results.find("li.active-result");
if (actives.length) {
if (!this.result_highlight) {
this.result_do_highlight($(actives[0]));
} else if (this.results_showing) {
sibs = this.result_highlight.nextAll();
next = first_intersect(sibs, actives);
if (next) {
this.result_do_highlight($(next));
}
var first_active, next_sib;
if (!this.result_highlight) {
first_active = this.search_results.find("li.active-result").first();
if (first_active) {
this.result_do_highlight($(first_active));
}
if (!this.results_showing) {
return this.results_show();
} else if (this.results_showing) {
next_sib = this.result_highlight.nextAll("li.active-result").first();
if (next_sib) {
this.result_do_highlight(next_sib);
}
}
if (!this.results_showing) {
return this.results_show();
}
};
Chosen.prototype.keyup_arrow = function() {
var actives, prev, sibs;
var prev_sibs;
if (!this.results_showing && !this.is_multiple) {
return this.results_show();
} else if (this.result_highlight) {
sibs = this.result_highlight.prevAll();
actives = this.search_results.find("li.active-result");
prev = first_intersect(sibs, actives);
if (prev) {
return this.result_do_highlight($(prev));
prev_sibs = this.result_highlight.prevAll("li.active-result");
if (prev_sibs.length) {
return this.result_do_highlight(prev_sibs.first());
} else {
if (this.choices > 0) {
this.results_hide();
@ -677,17 +674,6 @@
return side_border_padding = elmt.outerWidth() - elmt.width();
};
root.get_side_border_padding = get_side_border_padding;
first_intersect = function(a1, a2) {
var element, _i, _len;
for (_i = 0, _len = a1.length; _i < _len; _i++) {
element = a1[_i];
if ($.inArray(element, a2)) {
return element;
}
}
return null;
};
root.first_intersect = first_intersect;
OptionsParser = (function() {
function OptionsParser() {
this.group_index = 0;

View file

@ -486,26 +486,22 @@ class Chosen
@search_results.find(".no-results").remove()
keydown_arrow: ->
actives = @search_results.find "li.active-result"
if actives.length
if not @result_highlight
this.result_do_highlight $(actives[0])
else if @results_showing
sibs = @result_highlight.nextAll()
next = first_intersect sibs, actives
this.result_do_highlight $(next) if next
this.results_show() if not @results_showing
if not @result_highlight
first_active = @search_results.find("li.active-result").first()
this.result_do_highlight $(first_active) if first_active
else if @results_showing
next_sib = @result_highlight.nextAll("li.active-result").first()
this.result_do_highlight next_sib if next_sib
this.results_show() if not @results_showing
keyup_arrow: ->
if not @results_showing and not @is_multiple
this.results_show()
this.results_show()
else if @result_highlight
sibs = @result_highlight.prevAll()
actives = @search_results.find "li.active-result"
prev = first_intersect sibs, actives
if prev
this.result_do_highlight ($ prev)
prev_sibs = @result_highlight.prevAll("li.active-result")
if prev_sibs.length
this.result_do_highlight prev_sibs.first()
else
this.results_hide() if @choices > 0
this.result_clear_highlight()
@ -591,18 +587,6 @@ get_side_border_padding = (elmt) ->
root.get_side_border_padding = get_side_border_padding
first_intersect = (a1, a2) ->
# TODO for some reason, up arrow doesn't find the first
#console.log a2
for element in a1
#console.log element
#console.log $.inArray element, a2
return element if $.inArray element, a2
return null
root.first_intersect = first_intersect
class OptionsParser
constructor: ->