Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 0ba4bac

Browse files
CaerusKaruThomasBurleson
authored andcommitted
fix(docs): make splitter demo work in IE (#982)
* improve rxjs drag logic. add `throttleTime(50)` to reduce browser thrashing * fix conversion of pixel or percentage value to a raw pixel float value.
1 parent 7d2db14 commit 0ba4bac

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

src/apps/demo-app/src/app/github-issues/split/split-handle.directive.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Directive, ElementRef, Inject, Output} from '@angular/core';
22
import {DOCUMENT} from '@angular/common';
33
import {fromEvent, Observable} from 'rxjs';
4-
import {map, switchMap, takeUntil} from 'rxjs/operators';
4+
import {map, switchMap, takeUntil, throttleTime} from 'rxjs/operators';
55

66
@Directive({
77
selector: '[ngxSplitHandle]',
@@ -14,11 +14,20 @@ export class SplitHandleDirective {
1414
@Output() drag: Observable<{ x: number, y: number }>;
1515

1616
constructor(ref: ElementRef, @Inject(DOCUMENT) _document: any) {
17-
const getMouseEventPosition = (event: MouseEvent) => ({x: event.movementX, y: event.movementY});
17+
let last = {x: 0, y: 0};
18+
const scanPositionDelta = (event: MouseEvent) => {
19+
const current = {x: event.screenX, y: event.screenY};
20+
const delta = {x: current.x - last.x, y: current.y - last.y};
21+
last = current;
22+
return delta;
23+
};
1824

19-
const mousedown$ = fromEvent(ref.nativeElement, 'mousedown').pipe(map(getMouseEventPosition));
20-
const mousemove$ = fromEvent(_document, 'mousemove').pipe(map(getMouseEventPosition));
21-
const mouseup$ = fromEvent(_document, 'mouseup').pipe(map(getMouseEventPosition));
25+
const mousedown$ = fromEvent(ref.nativeElement, 'mousedown').pipe(map(scanPositionDelta));
26+
const mousemove$ = fromEvent(window, 'mousemove').pipe(
27+
throttleTime(50),
28+
map(scanPositionDelta)
29+
);
30+
const mouseup$ = fromEvent(window, 'mouseup');
2231

2332
this.drag = mousedown$.pipe(switchMap(() => mousemove$.pipe(takeUntil(mouseup$))));
2433
}

src/apps/demo-app/src/app/github-issues/split/split.directive.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ import {SplitAreaDirective} from './split-area.directive';
2222
}
2323
})
2424
export class SplitDirective implements AfterContentInit, OnDestroy {
25-
watcher: Subscription;
26-
27-
@Input('ngxSplit')
28-
direction = 'row';
29-
25+
@Input('ngxSplit') direction = 'row';
3026
@ContentChild(SplitHandleDirective) handle: SplitHandleDirective;
3127
@ContentChildren(SplitAreaDirective) areas: QueryList<SplitAreaDirective>;
3228

29+
private watcher: Subscription;
30+
3331
constructor(private elementRef: ElementRef) {}
3432

3533
ngAfterContentInit(): void {
36-
this.watcher = this.handle.drag.subscribe(pos => this.onDrag(pos));
34+
this.watcher = this.handle.drag.subscribe(this.onDrag.bind(this));
3735
}
3836

3937
ngOnDestroy() {
@@ -62,24 +60,17 @@ export class SplitDirective implements AfterContentInit, OnDestroy {
6260
* Use the pixel delta change to recalculate the area size (%)
6361
* Note: flex value may be '', %, px, or '<grow> <shrink> <basis>'
6462
*/
65-
calculateSize(value: string|number, delta: number) {
63+
calculateSize(value: string, delta: number): number {
6664
const containerSizePx = this.elementRef.nativeElement.clientWidth;
67-
const elementSizePx = Math.round(this.valueToPixel(value, containerSizePx));
65+
const elementSizePx = Math.round(valueToPixel(value, containerSizePx));
6866

6967
const elementSize = ((elementSizePx + delta) / containerSizePx) * 100;
7068
return Math.round(elementSize * 100) / 100;
7169
}
70+
}
7271

73-
/**
74-
* Convert the pixel or percentage value to a raw
75-
* pixel float value.
76-
*/
77-
valueToPixel(value: string | number, parentWidth: number): number {
78-
const isPercent = () => String(value).indexOf('px') < 0;
79-
let size = parseFloat(String(value));
80-
if (isPercent()) {
81-
size = parentWidth * (size / 100); // Convert percentage to actual pixel float value
82-
}
83-
return size;
84-
}
72+
/** Convert the pixel or percentage value to a raw pixel float value */
73+
function valueToPixel(value: string, parentWidth: number): number {
74+
const size = parseFloat(value);
75+
return !value.includes('px') ? parentWidth * (size / 100) : size;
8576
}

0 commit comments

Comments
 (0)