Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored scrolling functionality to fail gracefully #1186

Merged
merged 1 commit into from
May 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions chosen/chosen.jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,9 @@ Copyright (c) 2011 by Harvest
this.search_results.mouseout(function(evt) {
_this.search_results_mouseout(evt);
});
this.search_results.bind('mousewheel', function(evt) {
this.search_results.bind('mousewheel DOMMouseScroll', function(evt) {
_this.search_results_mousewheel(evt);
});
this.search_results.bind('DOMMouseScroll', function(evt) {
_this.search_results_mousewheel_ff(evt);
});
this.form_field_jq.bind("liszt:updated", function(evt) {
_this.results_update_field(evt);
});
Expand Down Expand Up @@ -533,19 +530,15 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.search_results_mousewheel = function(evt) {
this.search_results[0].scrollTop -= evt.wheelDelta;
return evt.preventDefault();
};
var delta, _ref1, _ref2;

Chosen.prototype.search_results_mousewheel_ff = function(evt) {
var bottom_overflow, delta, target, top_overflow, _ref1;

target = evt.currentTarget;
delta = ((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || -evt.detail;
bottom_overflow = target.scrollTop + $(target).outerHeight() - target.scrollHeight >= 0;
top_overflow = target.scrollTop <= 0;
if (target.scrollHeight > $(target).outerHeight() && ((delta < 0 && bottom_overflow) || (delta > 0 && top_overflow))) {
return evt.preventDefault();
delta = -((_ref1 = evt.originalEvent) != null ? _ref1.wheelDelta : void 0) || ((_ref2 = evt.originialEvent) != null ? _ref2.detail : void 0);
if (delta != null) {
evt.preventDefault();
if (evt.type === 'DOMMouseScroll') {
delta = delta * 40;
}
return this.search_results.scrollTop(delta + this.search_results.scrollTop());
}
};

Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.jquery.min.js

Large diffs are not rendered by default.

22 changes: 9 additions & 13 deletions chosen/chosen.proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ Copyright (c) 2011 by Harvest
return _this.search_results_mousewheel(evt);
});
this.search_results.observe("DOMMouseScroll", function(evt) {
return _this.search_results_mousewheel_ff(evt);
return _this.search_results_mousewheel(evt);
});
this.form_field.observe("liszt:updated", function(evt) {
return _this.results_update_field(evt);
Expand Down Expand Up @@ -522,19 +522,15 @@ Copyright (c) 2011 by Harvest
};

Chosen.prototype.search_results_mousewheel = function(evt) {
this.search_results.scrollTop -= evt.wheelDelta;
return evt.preventDefault();
};
var delta;

Chosen.prototype.search_results_mousewheel_ff = function(evt) {
var bottom_overflow, delta, target, top_overflow;

target = evt.currentTarget;
delta = evt.wheelDelta || (evt.originalEvent && evt.originalEvent.wheelDelta) || -evt.detail;
bottom_overflow = target.scrollTop + target.getHeight() - target.scrollHeight >= 0;
top_overflow = target.scrollTop <= 0;
if (target.scrollHeight > target.getHeight() && ((delta < 0 && bottom_overflow) || (delta > 0 && top_overflow))) {
return evt.preventDefault();
delta = -evt.wheelDelta || evt.detail;
if (delta != null) {
evt.preventDefault();
if (evt.type === 'DOMMouseScroll') {
delta = delta * 40;
}
return this.search_results.scrollTop = delta + this.search_results.scrollTop;
}
};

Expand Down
2 changes: 1 addition & 1 deletion chosen/chosen.proto.min.js

Large diffs are not rendered by default.

21 changes: 6 additions & 15 deletions coffee/chosen.jquery.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ class Chosen extends AbstractChosen
@search_results.mouseup (evt) => this.search_results_mouseup(evt); return
@search_results.mouseover (evt) => this.search_results_mouseover(evt); return
@search_results.mouseout (evt) => this.search_results_mouseout(evt); return
@search_results.bind 'mousewheel', (evt) => this.search_results_mousewheel(evt); return
@search_results.bind 'DOMMouseScroll', (evt) => this.search_results_mousewheel_ff(evt); return # for Firefox
@search_results.bind 'mousewheel DOMMouseScroll', (evt) => this.search_results_mousewheel(evt); return

@form_field_jq.bind "liszt:updated", (evt) => this.results_update_field(evt); return
@form_field_jq.bind "liszt:activate", (evt) => this.activate_field(evt); return
Expand Down Expand Up @@ -126,20 +125,12 @@ class Chosen extends AbstractChosen
container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled

# scrolling event handler for all but Firefox
search_results_mousewheel: (evt) ->
@search_results[0].scrollTop -= evt.wheelDelta
evt.preventDefault()

# scrolling event handler for Firefox
search_results_mousewheel_ff: (evt) ->
target = evt.currentTarget
delta = evt.originalEvent?.wheelDelta or -evt.detail
bottom_overflow = target.scrollTop + $(target).outerHeight() - target.scrollHeight >= 0
top_overflow = target.scrollTop <= 0

if target.scrollHeight > $(target).outerHeight() and ((delta < 0 and bottom_overflow) or (delta > 0 and top_overflow))
evt.preventDefault()
delta = -evt.originalEvent?.wheelDelta or evt.originialEvent?.detail
if delta?
evt.preventDefault()
delta = delta * 40 if evt.type is 'DOMMouseScroll'
@search_results.scrollTop(delta + @search_results.scrollTop())

blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClass "chzn-container-active"
Expand Down
21 changes: 6 additions & 15 deletions coffee/chosen.proto.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Chosen extends AbstractChosen
@search_results.observe "mouseover", (evt) => this.search_results_mouseover(evt)
@search_results.observe "mouseout", (evt) => this.search_results_mouseout(evt)
@search_results.observe "mousewheel", (evt) => this.search_results_mousewheel(evt)
@search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel_ff(evt) # for Firefox
@search_results.observe "DOMMouseScroll", (evt) => this.search_results_mousewheel(evt)

@form_field.observe "liszt:updated", (evt) => this.results_update_field(evt)
@form_field.observe "liszt:activate", (evt) => this.activate_field(evt)
Expand Down Expand Up @@ -116,21 +116,12 @@ class Chosen extends AbstractChosen
container_mouseup: (evt) ->
this.results_reset(evt) if evt.target.nodeName is "ABBR" and not @is_disabled

# scrolling event handler for all but Firefox
search_results_mousewheel: (evt) ->
@search_results.scrollTop -= evt.wheelDelta
evt.preventDefault()

# scrolling event handler for Firefox
search_results_mousewheel_ff: (evt) ->
target = evt.currentTarget
delta = evt.wheelDelta or (evt.originalEvent and evt.originalEvent.wheelDelta) or -evt.detail
bottom_overflow = target.scrollTop + target.getHeight() - target.scrollHeight >= 0
top_overflow = target.scrollTop <= 0

if target.scrollHeight > target.getHeight() and ((delta < 0 and bottom_overflow) or (delta > 0 and top_overflow))
evt.preventDefault()

delta = -evt.wheelDelta or evt.detail
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this work in the Prototype version, but we have to use originalEvent in the jQuery version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newer versions of jQuery do not support evt.wheelData or evt.detail. Apparently jQuery overrides the event but shoves all the original event properties in originalEvent. There's not a lot of info I can find.

http://stackoverflow.com/questions/8886281/event-wheeldelta-returns-undefined

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jQuery listeners receive a jQuery.Event object normalizing differences between browsers. evt.originalEvent is the native event object being wrapped by jQuery. See http://api.jquery.com/category/events/event-object/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. What @stof said 😃

if delta?
evt.preventDefault()
delta = delta * 40 if evt.type is 'DOMMouseScroll'
@search_results.scrollTop = delta + @search_results.scrollTop

blur_test: (evt) ->
this.close_field() if not @active_field and @container.hasClassName("chzn-container-active")
Expand Down