-
Notifications
You must be signed in to change notification settings - Fork 337
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
Support hover events #44
Merged
compwright
merged 1 commit into
chartjs:master
from
compwright:feature-annotation-events
Dec 2, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
var helpers = require('./helpers.js'); | ||
|
||
module.exports = function(Chart) { | ||
var chartHelpers = Chart.helpers; | ||
|
||
var dblClickSpeed = Chart.Annotation.dblClickSpeed || 350; // ms | ||
|
||
function getEventHandlerName(eventName) { | ||
return 'on' + eventName[0].toUpperCase() + eventName.substring(1); | ||
} | ||
|
||
function createMouseEvent(type, previousEvent) { | ||
try { | ||
return new MouseEvent(type, previousEvent); | ||
} catch (exception) { | ||
try { | ||
var m = document.createEvent('MouseEvent'); | ||
m.initMouseEvent( | ||
type, | ||
previousEvent.canBubble, | ||
previousEvent.cancelable, | ||
previousEvent.view, | ||
previousEvent.detail, | ||
previousEvent.screenX, | ||
previousEvent.screenY, | ||
previousEvent.clientX, | ||
previousEvent.clientY, | ||
previousEvent.ctrlKey, | ||
previousEvent.altKey, | ||
previousEvent.shiftKey, | ||
previousEvent.metaKey, | ||
previousEvent.button, | ||
previousEvent.relatedTarget | ||
); | ||
return m; | ||
} catch (exception2) { | ||
var e = document.createEvent('Event'); | ||
e.initEvent( | ||
type, | ||
previousEvent.canBubble, | ||
previousEvent.cancelable | ||
); | ||
return e; | ||
} | ||
} | ||
} | ||
|
||
function collapseHoverEvents(events) { | ||
var hover = false; | ||
var filteredEvents = events.filter(function(eventName) { | ||
switch (eventName) { | ||
case 'mouseenter': | ||
case 'mouseover': | ||
case 'mouseout': | ||
case 'mouseleave': | ||
hover = true; | ||
return false; | ||
|
||
default: | ||
return true; | ||
} | ||
}); | ||
if (hover && filteredEvents.indexOf('mousemove') === -1) { | ||
filteredEvents.push('mousemove'); | ||
} | ||
return filteredEvents; | ||
} | ||
|
||
function dispatcher(e) { | ||
var position = chartHelpers.getRelativePosition(e, this.chart); | ||
var element = helpers.getNearestItems(this.annotations, position); | ||
var events = collapseHoverEvents(this.options.annotation.events); | ||
var eventHandlers = []; | ||
var eventHandlerName = getEventHandlerName(e.type); | ||
var options = (element || {}).options; | ||
|
||
// Detect hover events | ||
if (e.type === 'mousemove') { | ||
if (element && !element.hovering) { | ||
// hover started | ||
['mouseenter', 'mouseover'].forEach(function(eventName) { | ||
var eventHandlerName = getEventHandlerName(eventName); | ||
var hoverEvent = createMouseEvent(eventName, e); // recreate the event to match the handler | ||
element.hovering = true; | ||
if (typeof options[eventHandlerName] === 'function') { | ||
eventHandlers.push([ options[eventHandlerName], hoverEvent, element ]); | ||
} | ||
}); | ||
} else if (!element) { | ||
// hover ended | ||
this.annotations.forEach(function(element) { | ||
if (element.hovering) { | ||
element.hovering = false; | ||
var options = element.options; | ||
['mouseout', 'mouseleave'].forEach(function(eventName) { | ||
var eventHandlerName = getEventHandlerName(eventName); | ||
var hoverEvent = createMouseEvent(eventName, e); // recreate the event to match the handler | ||
if (typeof options[eventHandlerName] === 'function') { | ||
eventHandlers.push([ options[eventHandlerName], hoverEvent, element ]); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
// Suppress duplicate click events during a double click | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this comment. Very detailed :) |
||
// 1. click -> 2. click -> 3. dblclick | ||
// | ||
// 1: wait dblClickSpeed ms, then fire click | ||
// 2: cancel (1) if it is waiting then wait dblClickSpeed ms then fire click, else fire click immediately | ||
// 3: cancel (1) or (2) if waiting, then fire dblclick | ||
if (element && events.indexOf('dblclick') > -1 && typeof options.onDblclick === 'function') { | ||
if (e.type === 'click' && typeof options.onClick === 'function') { | ||
clearTimeout(element.clickTimeout); | ||
element.clickTimeout = setTimeout(function() { | ||
delete element.clickTimeout; | ||
options.onClick.call(element, e); | ||
}, dblClickSpeed); | ||
e.stopImmediatePropagation(); | ||
e.preventDefault(); | ||
return; | ||
} else if (e.type === 'dblclick' && element.clickTimeout) { | ||
clearTimeout(element.clickTimeout); | ||
delete element.clickTimeout; | ||
} | ||
} | ||
|
||
// Dispatch the event to the usual handler, but only if we haven't substituted it | ||
if (element && typeof options[eventHandlerName] === 'function' && eventHandlers.length === 0) { | ||
eventHandlers.push([ options[eventHandlerName], e, element ]); | ||
} | ||
|
||
if (eventHandlers.length > 0) { | ||
e.stopImmediatePropagation(); | ||
e.preventDefault(); | ||
eventHandlers.forEach(function(eventHandler) { | ||
// [handler, event, element] | ||
eventHandler[0].call(eventHandler[2], eventHandler[1]); | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
dispatcher: dispatcher, | ||
collapseHoverEvents: collapseHoverEvents | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be a good idea to support touch events as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eventually, yes, but it will need some care to do it right in a way that works cross-browser and also works for the user. Probably need some hit radius logic added, and integration with Hammer.js. That's more than I can get into right now.