Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | import {ProgressWatcher} from '../dom/progress-watcher';
export interface InviewProgressItem {
range: number | number[];
element?: HTMLElement;
className?: string;
activeCallback?: Function;
inactiveCallback?: Function;
}
/**
* A class that adds or removes css classes to a given element based on
* progress values.
*
* const inviewProgress = new InviewProgress();
*
* // Adds the css class 'active' to the el element while progress is
* // from 0.2 to 0.4. If out of range, active gets removed.
* this.inviewProgress.add({
* range: [0.2, 0.4],
* element: el,
* className: "active"
* })
*
*
*
* // Add active callbacks. Called on every progress update when active.
* this.inviewProgress.add({
* range: [0.2, 0.4],
* activeCallback: (progress:number, direction:number)=> {
* }
* })
*
*
* // Add inactive callbacks. Called on every progress update when inactive.
* this.inviewProgress.add({
* range: [0.2, 0.4],
* inactiveCallback: (progress:number, direction:number)=> {
* }
* })
*
* inviewProgress.update(0.2)
*/
export class InviewProgress {
private progressWatcher: ProgressWatcher;
constructor() {
this.progressWatcher = new ProgressWatcher();
}
add(inviewProgressItem: InviewProgressItem) {
const setActive = (progress: number, direction: number) => {
if (inviewProgressItem.className && inviewProgressItem.element) {
inviewProgressItem.element.classList.add(inviewProgressItem.className);
}
if (inviewProgressItem.activeCallback) {
inviewProgressItem.activeCallback(progress, direction);
}
};
const setInactive = (progress: number, direction: number) => {
if (inviewProgressItem.className && inviewProgressItem.element) {
inviewProgressItem.element.classList.remove(
inviewProgressItem.className
);
}
if (inviewProgressItem.inactiveCallback) {
inviewProgressItem.inactiveCallback(progress, direction);
}
};
this.progressWatcher.add({
range: inviewProgressItem.range,
callback: (progress: number, direction: number) => {
setActive(progress, direction);
},
inactiveCallback: (progress: number, direction: number) => {
setInactive(progress, direction);
},
});
}
/**
* Clears out the css class inviews.
*/
public clear() {
this.progressWatcher.clear();
}
/**
* Sets the progress value.
* @param progress
*/
public setProgress(progress: number): void {
this.progressWatcher.setProgress(progress);
}
/**
* Gets the internal instance of progress Watcher.
*/
public getProgressWatcher() {
return this.progressWatcher;
}
public dispose() {
this.progressWatcher.dispose();
}
}
|