Skip to content

Commit

Permalink
trackElement method added
Browse files Browse the repository at this point in the history
  • Loading branch information
alisahinozcelik committed Jun 28, 2018
1 parent 0195f1b commit a59615e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@
"typedoc": "^0.11.1",
"typescript": "^2.9.2",
"update-json": "^1.0.0"
},
"dependencies": {
"@gen-tech/js-utils": "^0.16.1",
"@gen-tech/resize-manager": "^0.1.4",
"@gen-tech/scroll-manager": "^0.2.1"
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { PositionTracker } from "./position-tracker";
import { PositionTracker } from './position-tracker';

export { PositionTracker } from "./position-tracker";

export default new PositionTracker();
46 changes: 45 additions & 1 deletion src/position-tracker.ts
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
export class PositionTracker {}
import scrollManager from "@gen-tech/scroll-manager";
import resizeManager from "@gen-tech/resize-manager";
import { Observable } from 'rxjs';
import { merge, map, throttleTime } from "rxjs/operators";
import { noop } from "@gen-tech/js-utils";

export interface IElementPosition {
left: number;
top: number;
}

export class PositionTracker {

constructor(
private defaultThrottleTime = 90
) {
}

public trackElement(element: HTMLElement): Observable<IElementPosition> {
const trigger = resizeManager.root.throttleBy(0)
.pipe(
merge(scrollManager.root.throttleBy(0)),
merge(resizeManager.observe(element.parentElement).throttleBy(0)),
merge(scrollManager.observe(element.parentElement).throttleBy(0))
);

return this.trackElementByOwnTrigger(element, trigger);
}

public trackElementByOwnTrigger(
element: HTMLElement,
trigger: Observable<any>,
_throttleTime = this.defaultThrottleTime
): Observable<IElementPosition> {
return trigger.pipe(
throttleTime(_throttleTime),
map(() => this.getElementPosition(element))
);
}

public getElementPosition(element: HTMLElement): IElementPosition {
const { top, left } = element.getBoundingClientRect();
return {top, left};
}
}

0 comments on commit a59615e

Please sign in to comment.