123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { Component, ComponentFactoryResolver, ElementRef, HostListener, Renderer, ViewChild, ViewContainerRef } from '@angular/core';
  2. import { Config } from '../../config/config';
  3. import { KEY_ESCAPE } from '../../platform/key';
  4. import { NavParams } from '../../navigation/nav-params';
  5. import { ViewController } from '../../navigation/view-controller';
  6. import { BLOCK_ALL, GestureController } from '../../gestures/gesture-controller';
  7. import { ModuleLoader } from '../../util/module-loader';
  8. /**
  9. * @hidden
  10. */
  11. export class PopoverCmp {
  12. constructor(_cfr, _elementRef, _renderer, _config, _navParams, _viewCtrl, gestureCtrl, moduleLoader) {
  13. this._cfr = _cfr;
  14. this._elementRef = _elementRef;
  15. this._renderer = _renderer;
  16. this._config = _config;
  17. this._navParams = _navParams;
  18. this._viewCtrl = _viewCtrl;
  19. this.moduleLoader = moduleLoader;
  20. this._gestureBlocker = gestureCtrl.createBlocker(BLOCK_ALL);
  21. this.d = _navParams.data.opts;
  22. _renderer.setElementClass(_elementRef.nativeElement, `popover-${_config.get('mode')}`, true);
  23. if (this.d.cssClass) {
  24. this.d.cssClass.split(' ').forEach(cssClass => {
  25. // Make sure the class isn't whitespace, otherwise it throws exceptions
  26. if (cssClass.trim() !== '')
  27. _renderer.setElementClass(_elementRef.nativeElement, cssClass, true);
  28. });
  29. }
  30. this.id = (++popoverIds);
  31. }
  32. ionViewPreLoad() {
  33. this._load(this._navParams.data.component);
  34. }
  35. _load(component) {
  36. if (component) {
  37. let cfr = this.moduleLoader.getComponentFactoryResolver(component);
  38. if (!cfr) {
  39. cfr = this._cfr;
  40. }
  41. const componentFactory = cfr.resolveComponentFactory(component);
  42. // ******** DOM WRITE ****************
  43. const componentRef = this._viewport.createComponent(componentFactory, this._viewport.length, this._viewport.parentInjector, []);
  44. this._viewCtrl._setInstance(componentRef.instance);
  45. this._enabled = true;
  46. // Subscribe to events in order to block gestures
  47. // TODO, should we unsubscribe? memory leak?
  48. this._viewCtrl.willEnter.subscribe(this._viewWillEnter.bind(this));
  49. this._viewCtrl.didLeave.subscribe(this._viewDidLeave.bind(this));
  50. }
  51. }
  52. _viewWillEnter() {
  53. this._gestureBlocker.block();
  54. }
  55. _viewDidLeave() {
  56. this._gestureBlocker.unblock();
  57. }
  58. _setCssClass(componentRef, className) {
  59. this._renderer.setElementClass(componentRef.location.nativeElement, className, true);
  60. }
  61. _bdClick() {
  62. if (this._enabled && this.d.enableBackdropDismiss) {
  63. return this._viewCtrl.dismiss(null, 'backdrop');
  64. }
  65. }
  66. _keyUp(ev) {
  67. if (this._enabled && ev.keyCode === KEY_ESCAPE && this._viewCtrl.isLast()) {
  68. this._bdClick();
  69. }
  70. }
  71. ngOnDestroy() {
  72. (void 0) /* assert */;
  73. this._gestureBlocker.destroy();
  74. }
  75. }
  76. PopoverCmp.decorators = [
  77. { type: Component, args: [{
  78. selector: 'ion-popover',
  79. template: '<ion-backdrop (click)="_bdClick()" [hidden]="!d.showBackdrop"></ion-backdrop>' +
  80. '<div class="popover-wrapper">' +
  81. '<div class="popover-arrow"></div>' +
  82. '<div class="popover-content">' +
  83. '<div class="popover-viewport">' +
  84. '<div #viewport nav-viewport></div>' +
  85. '</div>' +
  86. '</div>' +
  87. '</div>'
  88. },] },
  89. ];
  90. /** @nocollapse */
  91. PopoverCmp.ctorParameters = () => [
  92. { type: ComponentFactoryResolver, },
  93. { type: ElementRef, },
  94. { type: Renderer, },
  95. { type: Config, },
  96. { type: NavParams, },
  97. { type: ViewController, },
  98. { type: GestureController, },
  99. { type: ModuleLoader, },
  100. ];
  101. PopoverCmp.propDecorators = {
  102. '_viewport': [{ type: ViewChild, args: ['viewport', { read: ViewContainerRef },] },],
  103. '_keyUp': [{ type: HostListener, args: ['body:keyup', ['$event'],] },],
  104. };
  105. let popoverIds = -1;
  106. //# sourceMappingURL=popover-component.js.map