/
popover.ts
492 lines (413 loc) · 16.7 KB
/
popover.ts
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
import {Component, ComponentResolver, ElementRef, HostListener, Renderer, ViewChild, ViewContainerRef} from '@angular/core';
import {addSelector} from '../../config/bootstrap';
import {Animation} from '../../animations/animation';
import {Transition, TransitionOptions} from '../../transitions/transition';
import {Config} from '../../config/config';
import {NavParams} from '../nav/nav-params';
import {Platform} from '../../platform/platform';
import {Key} from '../../util/key';
import {isPresent, isUndefined, isDefined} from '../../util/util';
import {nativeRaf, CSS} from '../../util/dom';
import {ViewController} from '../nav/view-controller';
const POPOVER_IOS_BODY_PADDING = 2;
const POPOVER_MD_BODY_PADDING = 12;
/**
* @name Popover
* @description
* A Popover is a dialog that appears on top of the current page.
* It can be used for anything, but generally it is used for overflow
* actions that don't fit in the navigation bar.
*
* ### Creating
* A popover can be created by calling the `create` method. The view
* to display in the popover should be passed as the first argument.
* Any data to pass to the popover view can optionally be passed in
* the second argument. Options for the popover can optionally be
* passed in the third argument. See the [create](#create) method
* below for all available options.
*
* ### Presenting
* To present a popover, call the `present` method on the [NavController](../../nav/NavController).
* The first argument passed to the `present` should be the popover. In order
* to position the popover relative to the element clicked, the event needs to be
* passed as the second argument. If the event is not passed, the popover will be
* positioned in the center of the current view. See the [usage](#usage) section for
* an example of passing this event.
*
* ### Dismissing
* To dismiss the popover after creation, call the `dismiss()` method on the
* `Popover` instance. The popover can also be dismissed from within the popover's
* view by calling the `dismiss()` method on the [ViewController](../../nav/ViewController).
* The `onDismiss` function can be called to perform an action after the popover
* is dismissed. The popover will dismiss when the backdrop is clicked, but this
* can be disabled by setting `enableBackdropDismiss` to `false` in the popover
* options.
*
* > Note that after the component is dismissed, it will not be usable anymore and
* another one must be created. This can be avoided by wrapping the creation and
* presentation of the component in a reusable function as shown in the [usage](#usage)
* section below.
*
* @usage
*
* To open a popover on the click of a button, pass `$event` to the method
* which creates and presents the popover:
*
* ```html
* <button (click)="presentPopover($event)">
* <ion-icon name="more"></ion-icon>
* </button>
* ```
*
* ```ts
* @Component({})
* class MyPage {
* constructor(private nav: NavController) {}
*
* presentPopover(myEvent) {
* let popover = Popover.create(PopoverPage);
* this.nav.present(popover, {
* ev: myEvent
* });
* }
* }
* ```
*
* The `PopoverPage` will display inside of the popover, and
* can be anything. Below is an example of a page with items
* that close the popover on click.
*
* ```ts
* @Component({
* template: `
* <ion-list>
* <ion-list-header>Ionic</ion-list-header>
* <button ion-item (click)="close()">Learn Ionic</button>
* <button ion-item (click)="close()">Documentation</button>
* <button ion-item (click)="close()">Showcase</button>
* <button ion-item (click)="close()">GitHub Repo</button>
* </ion-list>
* `
* })
* class PopoverPage {
* constructor(private viewCtrl: ViewController) {}
*
* close() {
* this.viewCtrl.dismiss();
* }
* }
* ```
*
*
* @demo /docs/v2/demos/popover/
*/
export class Popover extends ViewController {
constructor(componentType: any, data: any = {}, opts: PopoverOptions = {}) {
opts.showBackdrop = isPresent(opts.showBackdrop) ? !!opts.showBackdrop : true;
opts.enableBackdropDismiss = isPresent(opts.enableBackdropDismiss) ? !!opts.enableBackdropDismiss : true;
data.componentType = componentType;
data.opts = opts;
super(PopoverCmp, data);
this.viewType = 'popover';
this.isOverlay = true;
// by default, popovers should not fire lifecycle events of other views
// for example, when a popover enters, the current active view should
// not fire its lifecycle events because it's not conceptually leaving
this.fireOtherLifecycles = false;
}
/**
* @private
*/
getTransitionName(direction: string) {
let key = (direction === 'back' ? 'popoverLeave' : 'popoverEnter');
return this._nav && this._nav.config.get(key);
}
/**
* Create a popover with the following options
*
* | Option | Type | Description |
* |-----------------------|------------|------------------------------------------------------------------------------------------------------------------|
* | cssClass |`string` | An additional class for custom styles. |
* | showBackdrop |`boolean` | Whether to show the backdrop. Default true. |
* | enableBackdropDismiss |`boolean` | Whether the popover should be dismissed by tapping the backdrop. Default true. |
*
*
* @param {object} componentType The Popover
* @param {object} data Any data to pass to the Popover view
* @param {object} opts Popover options
*/
static create(componentType: any, data = {}, opts: PopoverOptions = {}) {
return new Popover(componentType, data, opts);
}
}
/**
* @private
*/
@Component({
selector: 'ion-popover',
template:
'<ion-backdrop (click)="bdClick($event)" [class.hide-backdrop]="!d.showBackdrop"></ion-backdrop>' +
'<div class="popover-wrapper">' +
'<div class="popover-arrow"></div>' +
'<div class="popover-content">' +
'<div class="popover-viewport">' +
'<div #viewport></div>' +
'</div>' +
'</div>' +
'</div>'
})
class PopoverCmp {
@ViewChild('viewport', {read: ViewContainerRef}) viewport: ViewContainerRef;
private d: any;
private enabled: boolean;
private id: number;
private showSpinner: boolean;
constructor(
private _compiler: ComponentResolver,
private _elementRef: ElementRef,
private _renderer: Renderer,
private _config: Config,
private _navParams: NavParams,
private _viewCtrl: ViewController
) {
this.d = _navParams.data.opts;
if (this.d.cssClass) {
_renderer.setElementClass(_elementRef.nativeElement, this.d.cssClass, true);
}
this.id = (++popoverIds);
}
ionViewWillEnter() {
addSelector(this._navParams.data.componentType, 'ion-popover-inner');
this._compiler.resolveComponent(this._navParams.data.componentType).then((componentFactory) => {
let componentRef = this.viewport.createComponent(componentFactory, this.viewport.length, this.viewport.parentInjector);
this._viewCtrl.setInstance(componentRef.instance);
// manually fire ionViewWillEnter() since PopoverCmp's ionViewWillEnter already happened
this._viewCtrl.fireWillEnter();
});
}
ngAfterViewInit() {
let activeElement: any = document.activeElement;
if (document.activeElement) {
activeElement.blur();
}
this.enabled = true;
}
dismiss(role: any): Promise<any> {
return this._viewCtrl.dismiss(null, role);
}
bdTouch(ev: UIEvent) {
ev.preventDefault();
ev.stopPropagation();
}
bdClick() {
if (this.enabled && this.d.enableBackdropDismiss) {
this.dismiss('backdrop');
}
}
@HostListener('body:keyup', ['$event'])
private _keyUp(ev: KeyboardEvent) {
if (this.enabled && this._viewCtrl.isLast() && ev.keyCode === Key.ESCAPE ) {
this.bdClick();
}
}
}
export interface PopoverOptions {
cssClass?: string;
showBackdrop?: boolean;
enableBackdropDismiss?: boolean;
}
/**
* Animations for popover
*/
class PopoverTransition extends Transition {
constructor(opts: TransitionOptions) {
super(opts);
}
mdPositionView(nativeEle: HTMLElement, ev: any) {
let originY = 'top';
let originX = 'left';
let popoverWrapperEle = <HTMLElement>nativeEle.querySelector('.popover-wrapper');
// Popover content width and height
let popoverEle = <HTMLElement>nativeEle.querySelector('.popover-content');
let popoverDim = popoverEle.getBoundingClientRect();
let popoverWidth = popoverDim.width;
let popoverHeight = popoverDim.height;
// Window body width and height
let bodyWidth = window.innerWidth;
let bodyHeight = window.innerHeight;
// If ev was passed, use that for target element
let targetDim = ev && ev.target && ev.target.getBoundingClientRect();
let targetTop = (targetDim && 'top' in targetDim) ? targetDim.top : (bodyHeight / 2) - (popoverHeight / 2);
let targetLeft = (targetDim && 'left' in targetDim) ? targetDim.left : (bodyWidth / 2) - (popoverWidth / 2);
let targetWidth = targetDim && targetDim.width || 0;
let targetHeight = targetDim && targetDim.height || 0;
let popoverCSS = {
top: targetTop,
left: targetLeft
};
// If the popover left is less than the padding it is off screen
// to the left so adjust it, else if the width of the popover
// exceeds the body width it is off screen to the right so adjust
if (popoverCSS.left < POPOVER_MD_BODY_PADDING) {
popoverCSS.left = POPOVER_MD_BODY_PADDING;
} else if (popoverWidth + POPOVER_MD_BODY_PADDING + popoverCSS.left > bodyWidth) {
popoverCSS.left = bodyWidth - popoverWidth - POPOVER_MD_BODY_PADDING;
originX = 'right';
}
// If the popover when popped down stretches past bottom of screen,
// make it pop up if there's room above
if (targetTop + targetHeight + popoverHeight > bodyHeight && targetTop - popoverHeight > 0) {
popoverCSS.top = targetTop - popoverHeight;
nativeEle.className = nativeEle.className + ' popover-bottom';
originY = 'bottom';
// If there isn't room for it to pop up above the target cut it off
} else if (targetTop + targetHeight + popoverHeight > bodyHeight) {
popoverEle.style.bottom = POPOVER_MD_BODY_PADDING + 'px';
}
popoverEle.style.top = popoverCSS.top + 'px';
popoverEle.style.left = popoverCSS.left + 'px';
popoverEle.style[CSS.transformOrigin] = originY + ' ' + originX;
// Since the transition starts before styling is done we
// want to wait for the styles to apply before showing the wrapper
popoverWrapperEle.style.opacity = '1';
}
iosPositionView(nativeEle: HTMLElement, ev: any) {
let originY = 'top';
let originX = 'left';
let popoverWrapperEle = <HTMLElement>nativeEle.querySelector('.popover-wrapper');
// Popover content width and height
let popoverEle = <HTMLElement>nativeEle.querySelector('.popover-content');
let popoverDim = popoverEle.getBoundingClientRect();
let popoverWidth = popoverDim.width;
let popoverHeight = popoverDim.height;
// Window body width and height
let bodyWidth = window.innerWidth;
let bodyHeight = window.innerHeight;
// If ev was passed, use that for target element
let targetDim = ev && ev.target && ev.target.getBoundingClientRect();
let targetTop = (targetDim && 'top' in targetDim) ? targetDim.top : (bodyHeight / 2) - (popoverHeight / 2);
let targetLeft = (targetDim && 'left' in targetDim) ? targetDim.left : (bodyWidth / 2);
let targetWidth = targetDim && targetDim.width || 0;
let targetHeight = targetDim && targetDim.height || 0;
// The arrow that shows above the popover on iOS
var arrowEle = <HTMLElement>nativeEle.querySelector('.popover-arrow');
let arrowDim = arrowEle.getBoundingClientRect();
var arrowWidth = arrowDim.width;
var arrowHeight = arrowDim.height;
// If no ev was passed, hide the arrow
if (!targetDim) {
arrowEle.style.display = 'none';
}
let arrowCSS = {
top: targetTop + targetHeight,
left: targetLeft + (targetWidth / 2) - (arrowWidth / 2)
};
let popoverCSS = {
top: targetTop + targetHeight + (arrowHeight - 1),
left: targetLeft + (targetWidth / 2) - (popoverWidth / 2)
};
// If the popover left is less than the padding it is off screen
// to the left so adjust it, else if the width of the popover
// exceeds the body width it is off screen to the right so adjust
if (popoverCSS.left < POPOVER_IOS_BODY_PADDING) {
popoverCSS.left = POPOVER_IOS_BODY_PADDING;
} else if (popoverWidth + POPOVER_IOS_BODY_PADDING + popoverCSS.left > bodyWidth) {
popoverCSS.left = bodyWidth - popoverWidth - POPOVER_IOS_BODY_PADDING;
originX = 'right';
}
// If the popover when popped down stretches past bottom of screen,
// make it pop up if there's room above
if (targetTop + targetHeight + popoverHeight > bodyHeight && targetTop - popoverHeight > 0) {
arrowCSS.top = targetTop - (arrowHeight + 1);
popoverCSS.top = targetTop - popoverHeight - (arrowHeight - 1);
nativeEle.className = nativeEle.className + ' popover-bottom';
originY = 'bottom';
// If there isn't room for it to pop up above the target cut it off
} else if (targetTop + targetHeight + popoverHeight > bodyHeight) {
popoverEle.style.bottom = POPOVER_IOS_BODY_PADDING + '%';
}
arrowEle.style.top = arrowCSS.top + 'px';
arrowEle.style.left = arrowCSS.left + 'px';
popoverEle.style.top = popoverCSS.top + 'px';
popoverEle.style.left = popoverCSS.left + 'px';
popoverEle.style[CSS.transformOrigin] = originY + ' ' + originX;
// Since the transition starts before styling is done we
// want to wait for the styles to apply before showing the wrapper
popoverWrapperEle.style.opacity = '1';
}
}
class PopoverPopIn extends PopoverTransition {
constructor(private enteringView: ViewController, private leavingView: ViewController, private opts: TransitionOptions) {
super(opts);
let ele = enteringView.pageRef().nativeElement;
let backdrop = new Animation(ele.querySelector('ion-backdrop'));
let wrapper = new Animation(ele.querySelector('.popover-wrapper'));
wrapper.fromTo('opacity', '0.01', '1');
backdrop.fromTo('opacity', '0.01', '0.08');
this
.easing('ease')
.duration(100)
.add(backdrop)
.add(wrapper);
}
play() {
nativeRaf(() => {
this.iosPositionView(this.enteringView.pageRef().nativeElement, this.opts.ev);
super.play();
});
}
}
Transition.register('popover-pop-in', PopoverPopIn);
class PopoverPopOut extends PopoverTransition {
constructor(private enteringView: ViewController, private leavingView: ViewController, private opts: TransitionOptions) {
super(opts);
let ele = leavingView.pageRef().nativeElement;
let backdrop = new Animation(ele.querySelector('ion-backdrop'));
let wrapper = new Animation(ele.querySelector('.popover-wrapper'));
wrapper.fromTo('opacity', '1', '0');
backdrop.fromTo('opacity', '0.08', '0');
this
.easing('ease')
.duration(500)
.add(backdrop)
.add(wrapper);
}
}
Transition.register('popover-pop-out', PopoverPopOut);
class PopoverMdPopIn extends PopoverTransition {
constructor(private enteringView: ViewController, private leavingView: ViewController, private opts: TransitionOptions) {
super(opts);
let ele = enteringView.pageRef().nativeElement;
let content = new Animation(ele.querySelector('.popover-content'));
let viewport = new Animation(ele.querySelector('.popover-viewport'));
content.fromTo('scale', '0.001', '1');
viewport.fromTo('opacity', '0', '1');
this
.easing('cubic-bezier(0.36,0.66,0.04,1)')
.duration(300)
.add(content)
.add(viewport);
}
play() {
nativeRaf(() => {
this.mdPositionView(this.enteringView.pageRef().nativeElement, this.opts.ev);
super.play();
});
}
}
Transition.register('popover-md-pop-in', PopoverMdPopIn);
class PopoverMdPopOut extends PopoverTransition {
constructor(private enteringView: ViewController, private leavingView: ViewController, private opts: TransitionOptions) {
super(opts);
let ele = leavingView.pageRef().nativeElement;
let wrapper = new Animation(ele.querySelector('.popover-wrapper'));
wrapper.fromTo('opacity', '1', '0');
this
.easing('ease')
.duration(500)
.fadeIn()
.add(wrapper);
}
}
Transition.register('popover-md-pop-out', PopoverMdPopOut);
let popoverIds = -1;