forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.ts
More file actions
615 lines (539 loc) · 16.3 KB
/
content.ts
File metadata and controls
615 lines (539 loc) · 16.3 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
import { ChangeDetectionStrategy, Component, ElementRef, Input, NgZone, Optional, ViewEncapsulation } from '@angular/core';
import { App } from '../app/app';
import { Ion } from '../ion';
import { Config } from '../../config/config';
import { Keyboard } from '../../util/keyboard';
import { nativeRaf, nativeTimeout, transitionEnd} from '../../util/dom';
import { ScrollView } from '../../util/scroll-view';
import { Tabs } from '../tabs/tabs';
import { ViewController } from '../nav/view-controller';
import { isTrueProperty } from '../../util/util';
/**
* @name Content
* @description
* The Content component provides an easy to use content area with
* some useful methods to control the scrollable area.
*
* The content area can also implement pull-to-refresh with the
* [Refresher](../../refresher/Refresher) component.
*
* @usage
* ```html
* <ion-content>
* Add your content here!
* </ion-content>
* ```
*
* To get a reference to the content component from a Page's logic,
* you can use Angular's `@ViewChild` annotation:
*
* ```ts
* import { Component, ViewChild } from '@angular/core';
* import { Content } from 'ionic-angular';
*
* @Component({...})
* export class MyPage{
* @ViewChild(Content) content: Content;
*
* scrollToTop() {
* this.content.scrollToTop();
* }
* }
* ```
*
* @advanced
*
* Resizing the content
*
*
* ```ts
* @Component({
* template: `
* <ion-header>
* <ion-navbar>
* <ion-title>Main Navbar</ion-title>
* </ion-navbar>
* <ion-toolbar *ngIf="showToolbar">
* <ion-title>Dynamic Toolbar</ion-title>
* </ion-toolbar>
* </ion-header>
* <ion-content>
* <button (click)="toggleToolbar()">Toggle Toolbar</button>
* </ion-content>
* `})
*
* class E2EPage {
* @ViewChild(Content) content: Content;
* showToolbar: boolean = false;
*
* toggleToolbar() {
* this.showToolbar = !this.showToolbar;
* this.content.resize();
* }
* }
* ```
*
*
* Scroll to a specific position
*
* ```ts
* import { Component, ViewChild } from '@angular/core';
* import { Content } from 'ionic-angular';
*
* @Component({
* template: `<ion-content>
* <button (click)="scrollTo()">Down 500px</button>
* </ion-content>`
* )}
* export class MyPage{
* @ViewChild(Content) content: Content;
*
* scrollTo() {
* // set the scrollLeft to 0px, and scrollTop to 500px
* // the scroll duration should take 200ms
* this.content.scrollTo(0, 500, 200);
* }
* }
* ```
*
*/
@Component({
selector: 'ion-content',
template:
'<scroll-content>' +
'<ng-content></ng-content>' +
'</scroll-content>' +
'<ng-content select="ion-fixed"></ng-content>' +
'<ng-content select="ion-refresher"></ng-content>',
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: {
'[class.statusbar-padding]': '_sbPadding'
}
})
export class Content extends Ion {
private _paddingTop: number;
private _paddingRight: number;
private _paddingBottom: number;
private _paddingLeft: number;
private _scrollPadding: number;
private _headerHeight: number;
private _footerHeight: number;
private _tabbarHeight: number;
private _tabsPlacement: string;
private _inputPolling: boolean = false;
private _scroll: ScrollView;
private _scLsn: Function;
private _sbPadding: boolean;
private _fullscreen: boolean;
private _scrollEle: HTMLElement;
private _footerEle: HTMLElement;
/**
* A number representing how many pixels the top of the content has been
* adjusted, which could be by either padding or margin.
*/
contentTop: number;
/**
* A number representing how many pixels the bottom of the content has been
* adjusted, which could be by either padding or margin.
*/
contentBottom: number;
constructor(
private _elementRef: ElementRef,
config: Config,
private _app: App,
private _keyboard: Keyboard,
private _zone: NgZone,
@Optional() viewCtrl: ViewController,
@Optional() private _tabs: Tabs
) {
super(_elementRef);
this._sbPadding = config.getBoolean('statusbarPadding', false);
if (viewCtrl) {
viewCtrl.setContent(this);
viewCtrl.setContentRef(_elementRef);
}
}
/**
* @private
*/
ngOnInit() {
this._scrollEle = this._elementRef.nativeElement.children[0];
this._zone.runOutsideAngular(() => {
this._scroll = new ScrollView(this._scrollEle);
this._scLsn = this.addScrollListener(this._app.setScrolling);
});
}
/**
* @private
*/
ngOnDestroy() {
this._scLsn && this._scLsn();
this._scroll && this._scroll.destroy();
this._scrollEle = this._footerEle = this._scLsn = this._scroll = null;
}
/**
* @private
*/
addScrollListener(handler: any) {
return this._addListener('scroll', handler);
}
/**
* @private
*/
addTouchStartListener(handler: any) {
return this._addListener('touchstart', handler);
}
/**
* @private
*/
addTouchMoveListener(handler: any) {
return this._addListener('touchmove', handler);
}
/**
* @private
*/
addTouchEndListener(handler: any) {
return this._addListener('touchend', handler);
}
/**
* @private
*/
addMouseDownListener(handler: any) {
return this._addListener('mousedown', handler);
}
/**
* @private
*/
addMouseUpListener(handler: any) {
return this._addListener('mouseup', handler);
}
/**
* @private
*/
addMouseMoveListener(handler: any) {
return this._addListener('mousemove', handler);
}
private _addListener(type: string, handler: any): Function {
if (!this._scrollEle) { return; }
// ensure we're not creating duplicates
this._scrollEle.removeEventListener(type, handler);
this._scrollEle.addEventListener(type, handler);
return () => {
if (this._scrollEle) {
this._scrollEle.removeEventListener(type, handler);
}
};
}
/**
* @private
*/
getScrollElement(): HTMLElement {
return this._scrollEle;
}
/**
* @private
* Call a method when scrolling has stopped
* @param {Function} callback The method you want perform when scrolling has ended
*/
onScrollEnd(callback: Function) {
let lastScrollTop: number = null;
let framesUnchanged: number = 0;
let _scrollEle = this._scrollEle;
function next() {
let currentScrollTop = _scrollEle.scrollTop;
if (lastScrollTop !== null) {
if (Math.round(lastScrollTop) === Math.round(currentScrollTop)) {
framesUnchanged++;
} else {
framesUnchanged = 0;
}
if (framesUnchanged > 9) {
return callback();
}
}
lastScrollTop = currentScrollTop;
nativeRaf(() => {
nativeRaf(next);
});
}
nativeTimeout(next, 100);
}
/**
* @private
*/
onScrollElementTransitionEnd(callback: Function) {
transitionEnd(this._scrollEle, callback);
}
/**
* Scroll to the specified position.
*
* @param {number} x The x-value to scroll to.
* @param {number} y The y-value to scroll to.
* @param {number} [duration] Duration of the scroll animation in milliseconds. Defaults to `300`.
* @returns {Promise} Returns a promise which is resolved when the scroll has completed.
*/
scrollTo(x: number, y: number, duration: number = 300): Promise<any> {
return this._scroll.scrollTo(x, y, duration);
}
/**
* Scroll to the top of the content component.
*
* @param {number} [duration] Duration of the scroll animation in milliseconds. Defaults to `300`.
* @returns {Promise} Returns a promise which is resolved when the scroll has completed.
*/
scrollToTop(duration: number = 300) {
return this._scroll.scrollToTop(duration);
}
/**
* Get the `scrollTop` property of the content's scrollable element.
* @returns {number}
*/
getScrollTop(): number {
return this._scroll.getTop();
}
/**
* Set the `scrollTop` property of the content's scrollable element.
* @param {number} top
*/
setScrollTop(top: number) {
this._scroll.setTop(top);
}
/**
* Scroll to the bottom of the content component.
* @param {number} [duration] Duration of the scroll animation in milliseconds. Defaults to `300`.
* @returns {Promise} Returns a promise which is resolved when the scroll has completed.
*/
scrollToBottom(duration: number = 300) {
return this._scroll.scrollToBottom(duration);
}
/**
* @private
*/
jsScroll(onScrollCallback: Function): Function {
return this._scroll.jsScroll(onScrollCallback);
}
/**
* @private
* DOM WRITE
*/
addCssClass(className: string) {
this.getNativeElement().classList.add(className);
}
/**
* @input {boolean} By default, content is positioned between the headers
* and footers. However, using `fullscreen="true"`, the content will be
* able to scroll "under" the headers and footers. At first glance the
* fullscreen option may not look any different than the default, however,
* by adding a transparency effect to a header then the content can be
* seen under the header as the user scrolls.
*/
@Input()
get fullscreen(): boolean {
return !!this._fullscreen;
}
set fullscreen(val: boolean) {
this._fullscreen = isTrueProperty(val);
}
/**
* @private
* DOM WRITE
*/
removeCssClass(className: string) {
this.getNativeElement().classList.remove(className);
}
/**
* @private
* DOM WRITE
*/
setScrollElementStyle(prop: string, val: any) {
this._scrollEle.style[prop] = val;
}
/**
* Returns the content and scroll elements' dimensions.
* @returns {object} dimensions The content and scroll elements' dimensions
* {number} dimensions.contentHeight content offsetHeight
* {number} dimensions.contentTop content offsetTop
* {number} dimensions.contentBottom content offsetTop+offsetHeight
* {number} dimensions.contentWidth content offsetWidth
* {number} dimensions.contentLeft content offsetLeft
* {number} dimensions.contentRight content offsetLeft + offsetWidth
* {number} dimensions.scrollHeight scroll scrollHeight
* {number} dimensions.scrollTop scroll scrollTop
* {number} dimensions.scrollBottom scroll scrollTop + scrollHeight
* {number} dimensions.scrollWidth scroll scrollWidth
* {number} dimensions.scrollLeft scroll scrollLeft
* {number} dimensions.scrollRight scroll scrollLeft + scrollWidth
*/
getContentDimensions() {
let _scrollEle = this._scrollEle;
let parentElement = _scrollEle.parentElement;
return {
contentHeight: parentElement.offsetHeight,
contentTop: parentElement.offsetTop,
contentBottom: parentElement.offsetTop + parentElement.offsetHeight,
contentWidth: parentElement.offsetWidth,
contentLeft: parentElement.offsetLeft,
contentRight: parentElement.offsetLeft + parentElement.offsetWidth,
scrollHeight: _scrollEle.scrollHeight,
scrollTop: _scrollEle.scrollTop,
scrollBottom: _scrollEle.scrollTop + _scrollEle.scrollHeight,
scrollWidth: _scrollEle.scrollWidth,
scrollLeft: _scrollEle.scrollLeft,
scrollRight: _scrollEle.scrollLeft + _scrollEle.scrollWidth,
};
}
/**
* @private
* DOM WRITE
* Adds padding to the bottom of the scroll element when the keyboard is open
* so content below the keyboard can be scrolled into view.
*/
addScrollPadding(newPadding: number) {
if (newPadding > this._scrollPadding) {
console.debug('content addScrollPadding', newPadding);
this._scrollPadding = newPadding;
this._scrollEle.style.paddingBottom = newPadding + 'px';
}
}
/**
* @private
* DOM WRITE
*/
clearScrollPaddingFocusOut() {
if (!this._inputPolling) {
this._inputPolling = true;
this._keyboard.onClose(() => {
this._scrollPadding = 0;
this._scrollEle.style.paddingBottom = (this._paddingBottom > 0 ? this._paddingBottom + 'px' : '');
this._inputPolling = false;
this.addScrollPadding(0);
}, 200, Infinity);
}
}
/**
* Tell the content to recalculate its dimensions. This should be called
* after dynamically adding headers, footers, or tabs.
*
*/
resize() {
nativeRaf(() => {
this.readDimensions();
this.writeDimensions();
});
}
/**
* @private
* DOM READ
*/
readDimensions() {
this._paddingTop = 0;
this._paddingRight = 0;
this._paddingBottom = 0;
this._paddingLeft = 0;
this._headerHeight = 0;
this._footerHeight = 0;
this._tabsPlacement = null;
let ele: HTMLElement = this._elementRef.nativeElement;
if (!ele) return;
let parentEle: HTMLElement = ele.parentElement;
let computedStyle: any;
for (var i = 0; i < parentEle.children.length; i++) {
ele = <HTMLElement>parentEle.children[i];
if (ele.tagName === 'ION-CONTENT') {
if (this._fullscreen) {
computedStyle = getComputedStyle(ele);
this._paddingTop = parsePxUnit(computedStyle.paddingTop);
this._paddingBottom = parsePxUnit(computedStyle.paddingBottom);
this._paddingRight = parsePxUnit(computedStyle.paddingRight);
this._paddingLeft = parsePxUnit(computedStyle.paddingLeft);
}
} else if (ele.tagName === 'ION-HEADER') {
this._headerHeight = ele.clientHeight;
} else if (ele.tagName === 'ION-FOOTER') {
this._footerHeight = ele.clientHeight;
this._footerEle = ele;
}
}
ele = parentEle;
let tabbarEle: HTMLElement;
while (ele && ele.tagName !== 'ION-MODAL' && !ele.classList.contains('tab-subpage')) {
if (ele.tagName === 'ION-TABS') {
tabbarEle = <HTMLElement>ele.firstElementChild;
this._tabbarHeight = tabbarEle.clientHeight;
if (this._tabsPlacement === null) {
// this is the first tabbar found, remember it's position
this._tabsPlacement = ele.getAttribute('tabsplacement');
}
}
ele = ele.parentElement;
}
}
/**
* @private
* DOM WRITE
*/
writeDimensions() {
let newVal: number;
let scrollEle = this._scrollEle;
if (!scrollEle) return;
// only write when it has changed
if (this._fullscreen) {
// adjust the content with padding, allowing content to scroll under headers/footers
// however, on iOS you cannot control the margins of the scrollbar (last tested iOS9.2)
// only add inline padding styles if the computed padding value, which would
// have come from the app's css, is different than the new padding value
newVal = this._headerHeight + this._paddingTop;
if (this._tabsPlacement === 'top') {
newVal += this._tabbarHeight;
}
if (newVal !== this.contentTop) {
scrollEle.style.paddingTop = (newVal > 0 ? newVal + 'px' : '');
this.contentTop = newVal;
}
newVal = this._footerHeight + this._paddingBottom;
if (this._tabsPlacement === 'bottom') {
newVal += this._tabbarHeight;
if (newVal > 0 && this._footerEle) {
this._footerEle.style.bottom = (newVal - this._footerHeight - this._paddingBottom) + 'px';
}
}
if (newVal !== this.contentBottom) {
scrollEle.style.paddingBottom = (newVal > 0 ? newVal + 'px' : '');
this.contentBottom = newVal;
}
} else {
// adjust the content with margins
newVal = this._headerHeight;
if (this._tabsPlacement === 'top') {
newVal += this._tabbarHeight;
}
if (newVal !== this.contentTop) {
scrollEle.style.marginTop = (newVal > 0 ? newVal + 'px' : '');
this.contentTop = newVal;
}
newVal = this._footerHeight;
if (this._tabsPlacement === 'bottom') {
newVal += this._tabbarHeight;
}
if (newVal !== this.contentBottom) {
scrollEle.style.marginBottom = (newVal > 0 ? newVal + 'px' : '');
this.contentBottom = newVal;
if (newVal > 0 && this._footerEle) {
this._footerEle.style.bottom = (newVal - this._footerHeight) + 'px';
}
}
}
if (this._tabsPlacement !== null && this._tabs) {
// set the position of the tabbar
if (this._tabsPlacement === 'top') {
this._tabs.setTabbarPosition(this._headerHeight, -1);
} else {
this._tabs.setTabbarPosition(-1, 0);
}
}
}
}
function parsePxUnit(val: string): number {
return (val.indexOf('px') > 0) ? parseInt(val, 10) : 0;
}