-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#196 add basic time-slice functionality
- Loading branch information
Michael Mrowetz
committed
Jun 24, 2017
1 parent
a07766f
commit 4092002
Showing
7 changed files
with
115 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** feature detection if browser supports `passive` event-listener */ | ||
let passiveSupported = false; | ||
|
||
// feature detection if passive event listener are supported | ||
try { | ||
const options = Object.defineProperty({}, "passive", { | ||
get() { passiveSupported = true; }, | ||
}); | ||
window.addEventListener("test", null, options); // test add enpyt evt listener | ||
} catch (err) { /** ignore */} | ||
|
||
/** | ||
* If true browser supports `passive` event listerners | ||
*/ | ||
export const supportsPassiveEventListener: boolean = passiveSupported; |
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,74 @@ | ||
import { supportsPassiveEventListener } from "../../helpers/feature-detection"; | ||
import { Context } from "../../typing/context"; | ||
|
||
/** | ||
* Calculates current position in Milliseconds | ||
* @param offsetX offset on the X-axis relative to `fullWidth` in px | ||
* @param leftColumnWidthPerc width of the the left (description) column | ||
* @param fullWidth Width of the full chart (including the left column) | ||
* @param fullDuration Duration in milliseconds | ||
*/ | ||
const toPosInMs = (offsetX: number, leftColumnWidthPerc: number, fullWidth: number, fullDuration: number) => { | ||
const leftWidthPx = fullWidth / 100 * leftColumnWidthPerc; | ||
if (offsetX <= leftWidthPx) { | ||
return null; // not over waterfall content | ||
} | ||
const rigthWidthPx = fullWidth - leftWidthPx; | ||
const posRightPx = offsetX - leftWidthPx; | ||
const posRightPerc = posRightPx / rigthWidthPx; | ||
/** Curent position in Milliseconds */ | ||
return fullDuration * posRightPerc; | ||
}; | ||
|
||
/** | ||
* Generates a slice-change check `mousemove` callback | ||
* @param context context object | ||
*/ | ||
const sliceChangeCheck = (context: Context) => { | ||
const fullDuration = context.unit * 100; | ||
const options = context.options; | ||
const leftColumnWidthPerc = context.options.leftColumnWith; | ||
const slices = options.timeSlices; | ||
const lastSlideIndex = options.timeSlices.length - 1; | ||
|
||
const onMouseMoveSliceCheck = (evt: MouseEvent) => { | ||
const tar = evt.currentTarget as SVGSVGElement; | ||
const currMs = toPosInMs(evt.offsetX, leftColumnWidthPerc, tar.clientWidth, fullDuration); | ||
if (currMs === null) { | ||
if (context.activeTimeslice !== null) { | ||
options.timeSliceOnLeave(context.activeTimeslice, evt); | ||
context.activeTimeslice = null; | ||
} | ||
return; | ||
} | ||
for (let i = 0; i <= lastSlideIndex; i++) { | ||
if ( currMs >= slices[i] && (i === lastSlideIndex || currMs < slices[i + 1])) { | ||
if (context.activeTimeslice !== slices[i] ) { | ||
if (context.activeTimeslice !== null) { | ||
options.timeSliceOnLeave(context.activeTimeslice, evt); | ||
} | ||
context.activeTimeslice = slices[i]; | ||
options.timeSliceOnEnter(slices[i], evt); | ||
} | ||
} | ||
} | ||
}; | ||
return onMouseMoveSliceCheck; | ||
}; | ||
|
||
/** | ||
* Sets up the event listener and detection for the time-slice hit-detection | ||
* @param holder | ||
* @param context | ||
*/ | ||
export const setupTimeSlices = (holder: SVGElement, context: Context) => { | ||
const pass: any = supportsPassiveEventListener ? { | ||
passive: true, | ||
} : false; // ts does not have typing for passive yet - use any | ||
|
||
holder.addEventListener("mousemove", sliceChangeCheck(context), pass); | ||
holder.addEventListener("mouseleave", (evt: MouseEvent) => { | ||
context.options.timeSliceOnLeave(context.activeTimeslice, evt); | ||
context.activeTimeslice = null; | ||
}, pass); | ||
}; |
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