Skip to content

Commit bfd3314

Browse files
committed
feat(scroll): add domWrite and content elements to scroll events
1 parent 22d6bc5 commit bfd3314

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed

src/components/content/content.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { Img } from '../img/img';
77
import { Ion } from '../ion';
88
import { isTrueProperty, assert, removeArrayItem } from '../../util/util';
99
import { Keyboard } from '../../util/keyboard';
10-
import { ScrollView, ScrollDirection, ScrollEvent } from '../../util/scroll-view';
10+
import { ScrollView, ScrollEvent } from '../../util/scroll-view';
1111
import { Tabs } from '../tabs/tabs';
1212
import { transitionEnd } from '../../util/dom';
1313
import { ViewController } from '../../navigation/view-controller';
1414

15-
export { ScrollEvent, ScrollDirection } from '../../util/scroll-view';
15+
export { ScrollEvent } from '../../util/scroll-view';
1616

1717

1818
/**
@@ -242,7 +242,7 @@ export class Content extends Ion implements OnDestroy, OnInit {
242242
* @return {number}
243243
*/
244244
get velocityY(): number {
245-
return this._scroll.ev.velocityY || 0;
245+
return this._scroll.ev.velocityY;
246246
}
247247

248248
/**
@@ -251,24 +251,24 @@ export class Content extends Ion implements OnDestroy, OnInit {
251251
* @return {number}
252252
*/
253253
get velocityX(): number {
254-
return this._scroll.ev.velocityX || 0;
254+
return this._scroll.ev.velocityX;
255255
}
256256

257257
/**
258258
* The current, or last known, vertical scroll direction.
259259
*
260-
* @return {ScrollDirection}
260+
* @return {string}
261261
*/
262-
get directionY(): ScrollDirection {
262+
get directionY(): string {
263263
return this._scroll.ev.directionY;
264264
}
265265

266266
/**
267267
* The current, or last known, horizontal scroll direction.
268268
*
269-
* @return {ScrollDirection}
269+
* @return {string}
270270
*/
271-
get directionX(): ScrollDirection {
271+
get directionX(): string {
272272
return this._scroll.ev.directionX;
273273
}
274274

@@ -330,16 +330,18 @@ export class Content extends Ion implements OnDestroy, OnInit {
330330
const children = this._elementRef.nativeElement.children;
331331
assert(children && children.length >= 2, 'content needs at least two children');
332332

333-
this._fixedEle = children[0];
334-
this._scrollEle = children[1];
333+
const scroll = this._scroll;
334+
335+
scroll.ev.fixedElement = this._fixedEle = children[0];
336+
scroll.ev.scrollElement = this._scrollEle = children[1];
335337

336338
// subscribe to the scroll start
337-
this._scroll.scrollStart.subscribe(ev => {
339+
scroll.scrollStart.subscribe(ev => {
338340
this.ionScrollStart.emit(ev);
339341
});
340342

341343
// subscribe to every scroll move
342-
this._scroll.scroll.subscribe(ev => {
344+
scroll.scroll.subscribe(ev => {
343345
// remind the app that it's currently scrolling
344346
this._app.setScrolling();
345347

@@ -350,7 +352,7 @@ export class Content extends Ion implements OnDestroy, OnInit {
350352
});
351353

352354
// subscribe to the scroll end
353-
this._scroll.scrollEnd.subscribe(ev => {
355+
scroll.scrollEnd.subscribe(ev => {
354356
this.ionScrollEnd.emit(ev);
355357

356358
this.imgsUpdate();
@@ -576,6 +578,8 @@ export class Content extends Ion implements OnDestroy, OnInit {
576578
this._fTop = 0;
577579
this._fBottom = 0;
578580

581+
const scrollEvent = this._scroll.ev;
582+
579583
let ele: HTMLElement = this._elementRef.nativeElement;
580584
if (!ele) {
581585
assert(false, 'ele should be valid');
@@ -590,6 +594,8 @@ export class Content extends Ion implements OnDestroy, OnInit {
590594
ele = <HTMLElement>children[i];
591595
tagName = ele.tagName;
592596
if (tagName === 'ION-CONTENT') {
597+
scrollEvent.contentElement = ele;
598+
593599
// ******** DOM READ ****************
594600
this.scrollWidth = ele.scrollWidth;
595601
// ******** DOM READ ****************
@@ -605,10 +611,14 @@ export class Content extends Ion implements OnDestroy, OnInit {
605611
}
606612

607613
} else if (tagName === 'ION-HEADER') {
614+
scrollEvent.headerElement = ele;
615+
608616
// ******** DOM READ ****************
609617
this._hdrHeight = ele.clientHeight;
610618

611619
} else if (tagName === 'ION-FOOTER') {
620+
scrollEvent.footerElement = ele;
621+
612622
// ******** DOM READ ****************
613623
this._ftrHeight = ele.clientHeight;
614624
this._footerEle = ele;
@@ -793,7 +803,7 @@ export class Content extends Ion implements OnDestroy, OnInit {
793803

794804
}
795805

796-
export function updateImgs(imgs: Img[], scrollTop: number, scrollHeight: number, scrollDirectionY: ScrollDirection, requestableBuffer: number, renderableBuffer: number) {
806+
export function updateImgs(imgs: Img[], scrollTop: number, scrollHeight: number, scrollDirectionY: string, requestableBuffer: number, renderableBuffer: number) {
797807
// ok, so it's time to see which images, if any, should be requested and rendered
798808
// ultimately, if we're scrolling fast then don't bother requesting or rendering
799809
// when scrolling is done, then it needs to do a check to see which images are
@@ -809,7 +819,7 @@ export function updateImgs(imgs: Img[], scrollTop: number, scrollHeight: number,
809819
for (var i = 0, ilen = imgs.length; i < ilen; i++) {
810820
img = imgs[i];
811821

812-
if (scrollDirectionY === ScrollDirection.Up) {
822+
if (scrollDirectionY === 'up') {
813823
// scrolling up
814824
if (img.top < scrollBottom && img.bottom > scrollTop - renderableBuffer) {
815825
// scrolling up, img is within viewable area
@@ -870,7 +880,7 @@ export function updateImgs(imgs: Img[], scrollTop: number, scrollHeight: number,
870880
// update all imgs which are viewable
871881
priority1.sort(sortTopToBottom).forEach(i => i.update());
872882

873-
if (scrollDirectionY === ScrollDirection.Up) {
883+
if (scrollDirectionY === 'up') {
874884
// scrolling up
875885
priority2.sort(sortTopToBottom).reverse().forEach(i => i.update());
876886

src/components/infinite-scroll/test/infinite-scroll.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('Infinite Scroll', () => {
9696
let content: Content;
9797
let contentElementRef;
9898
let infiniteElementRef;
99-
let ev: ScrollEvent = {};
99+
let ev: ScrollEvent = (<any>{});
100100
let dom: MockDomController;
101101

102102
beforeEach(() => {

src/directives.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export { Card, CardContent, CardHeader, CardTitle } from './components/card/card
8484
export { Checkbox } from './components/checkbox/checkbox';
8585
export { Chip } from './components/chip/chip';
8686
export { ClickBlock } from './util/click-block';
87-
export { Content, ScrollEvent, ScrollDirection } from './components/content/content';
87+
export { Content, ScrollEvent } from './components/content/content';
8888
export { DateTime } from './components/datetime/datetime';
8989
export { FabContainer, FabButton, FabList } from './components/fab/fab';
9090
export { Grid, Row, Col } from './components/grid/grid';

src/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { ToastCmp } from './components/toast/toast-component';
5353
* Export Providers
5454
*/
5555
export { Config, setupConfig, ConfigToken } from './config/config';
56-
export { DomController } from './util/dom-controller';
56+
export { DomController, DomCallback } from './util/dom-controller';
5757
export { Platform, setupPlatform, UserAgentToken, DocumentDirToken, DocLangToken, NavigatorPlatformToken } from './platform/platform';
5858
export { Haptic } from './util/haptic';
5959
export { ImgLoader } from './components/img/img-loader';

src/util/dom-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { nativeRaf } from './dom';
77
import { removeArrayItem } from './util';
88

99

10-
export type DomCallback = { (timeStamp: number): void };
10+
export type DomCallback = { (timeStamp?: number): void };
1111

1212
export class DomDebouncer {
1313

src/util/scroll-view.ts

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { Subject } from 'rxjs/Subject';
22

33
import { assert } from './util';
44
import { CSS, nativeRaf, pointerCoord, rafFrames } from './dom';
5-
import { DomController } from './dom-controller';
5+
import { DomController, DomCallback } from './dom-controller';
66
import { eventOptions, listenEvent } from './ui-event-manager';
77

88

99
export class ScrollView {
10+
ev: ScrollEvent;
1011
isScrolling = false;
1112
scrollStart = new Subject<ScrollEvent>();
1213
scroll = new Subject<ScrollEvent>();
@@ -20,13 +21,30 @@ export class ScrollView {
2021
private _lsn: Function;
2122
private _endTmr: Function;
2223

23-
ev: ScrollEvent = {
24-
directionY: ScrollDirection.Down,
25-
directionX: null
26-
};
2724

28-
29-
constructor(private _dom: DomController) {}
25+
constructor(private _dom: DomController) {
26+
this.ev = {
27+
timeStamp: 0,
28+
scrollTop: 0,
29+
scrollLeft: 0,
30+
startY: 0,
31+
startX: 0,
32+
deltaY: 0,
33+
deltaX: 0,
34+
velocityY: 0,
35+
velocityX: 0,
36+
directionY: 'down',
37+
directionX: null,
38+
domWrite: function(fn: DomCallback, ctx?: any) {
39+
_dom.write(fn, ctx);
40+
},
41+
contentElement: null,
42+
fixedElement: null,
43+
scrollElement: null,
44+
headerElement: null,
45+
footerElement: null
46+
};
47+
}
3048

3149
init(ele: HTMLElement, contentTop: number, contentBottom: number) {
3250
if (!this.initialized) {
@@ -111,8 +129,8 @@ export class ScrollView {
111129
ev.velocityX = ((movedLeft / timeOffset) * FRAME_MS);
112130

113131
// figure out which direction we're scrolling
114-
ev.directionY = (movedTop > 0 ? ScrollDirection.Up : ScrollDirection.Down);
115-
ev.directionX = (movedLeft > 0 ? ScrollDirection.Left : ScrollDirection.Right);
132+
ev.directionY = (movedTop > 0 ? 'up' : 'down');
133+
ev.directionX = (movedLeft > 0 ? 'left' : 'right');
116134
}
117135
}
118136

@@ -121,7 +139,7 @@ export class ScrollView {
121139

122140
// debounce for a moment after the last scroll event
123141
self._endTmr && self._endTmr();
124-
self._endTmr = rafFrames(5, function scrollEnd() {
142+
self._endTmr = rafFrames(6, function scrollEnd() {
125143
// haven't scrolled in a while, so it's a scrollend
126144
self.isScrolling = false;
127145

@@ -345,7 +363,7 @@ export class ScrollView {
345363
if (this._js) {
346364
return this._t;
347365
}
348-
return this._t = this._el.scrollTop || 0;
366+
return this._t = this._el.scrollTop;
349367
}
350368

351369
/**
@@ -355,7 +373,7 @@ export class ScrollView {
355373
if (this._js) {
356374
return 0;
357375
}
358-
return this._l = this._el.scrollLeft || 0;
376+
return this._l = this._el.scrollLeft;
359377
}
360378

361379
/**
@@ -492,34 +510,32 @@ export class ScrollView {
492510
this.stop();
493511
this._endTmr && this._endTmr();
494512
this._lsn && this._lsn();
495-
this._el = this._dom = null;
513+
let ev = this.ev;
514+
ev.domWrite = ev.contentElement = ev.fixedElement = ev.scrollElement = ev.headerElement = null;
515+
this._lsn = this._el = this._dom = this.ev = ev = null;
496516
}
497517

498518
}
499519

500520

501521
export interface ScrollEvent {
502-
scrollTop?: number;
503-
scrollLeft?: number;
504-
startY?: number;
505-
startX?: number;
506-
deltaY?: number;
507-
deltaX?: number;
508-
timeStamp?: number;
509-
velocityY?: number;
510-
velocityX?: number;
511-
directionY?: ScrollDirection;
512-
directionX?: ScrollDirection;
513-
}
514-
515-
516-
export enum ScrollDirection {
517-
Up, Down, Left, Right
518-
}
519-
520-
521-
export interface DomFn {
522-
(callback: Function): void;
522+
timeStamp: number;
523+
scrollTop: number;
524+
scrollLeft: number;
525+
startY: number;
526+
startX: number;
527+
deltaY: number;
528+
deltaX: number;
529+
velocityY: number;
530+
velocityX: number;
531+
directionY: string;
532+
directionX: string;
533+
domWrite: {(fn: DomCallback, ctx?: any)};
534+
contentElement: HTMLElement;
535+
fixedElement: HTMLElement;
536+
scrollElement: HTMLElement;
537+
headerElement: HTMLElement;
538+
footerElement: HTMLElement;
523539
}
524540

525541

0 commit comments

Comments
 (0)