summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-ui/src/app/paletx/plx-datepicker/popover.ts
blob: 3d054120a5f17773ec5bbb99707498d6327cd7dc (plain)
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
import {
  Component,
  Directive,
  Input,
  Output,
  EventEmitter,
  ChangeDetectionStrategy,
  OnInit,
  OnDestroy,
  Injector,
  Renderer,
  ComponentRef,
  ElementRef,
  TemplateRef,
  ViewContainerRef,
  ComponentFactoryResolver,
  NgZone
} from '@angular/core';

import { listenToTriggers } from './util/triggers';
import { positionElements } from './util/positioning';
import { PopupService } from './util/popup';
import { OesDaterangePopoverConfig } from './popover-config';

let nextId = 0;

@Component({
  selector: 'ngb-popover-window',
  changeDetection: ChangeDetectionStrategy.OnPush,
  host: { '[class]': '"popover show popover-" + placement', 'role': 'tooltip', '[id]': 'id' },
  styles: [`

    .popover-title,.popover-content{
        background-color: #fff;
    }
     .popover-custom{
        padding:9px 5px !important;
     }


   `],
  template: `
    <h3 class="popover-title">{{title}}</h3><div class="popover-content popover-custom"><ng-content></ng-content></div>
    `
})
export class OesDaterangePopoverWindow {
  @Input() public placement: 'top' | 'bottom' | 'left' | 'right' = 'top';
  @Input() public title: string;
  @Input() public id: string;
}

/**
 * A lightweight, extensible directive for fancy oes-popover creation.
 */
@Directive({ selector: '[oesDaterangePopover]', exportAs: 'oesDaterangePopover' })
export class OesDaterangePopover implements OnInit, OnDestroy {
  /**
   * Content to be displayed as oes-popover.
   */
  @Input() public oesDaterangePopover: string | TemplateRef<any>;
  /**
   * Title of a oes-popover.
   */
  @Input() public popoverTitle: string;
  /**
   * Placement of a oes-popover. Accepts: "top", "bottom", "left", "right"
   */
  @Input() public placement: 'top' | 'bottom' | 'left' | 'right';
  /**
   * Specifies events that should trigger. Supports a space separated list of event names.
   */
  @Input() public triggers: string;
  /**
   * A selector specifying the element the oes-popover should be appended to.
   * Currently only supports "body".
   */
  @Input() public container: string;
  /**
   * Emits an event when the oes-popover is shown
   */
  @Output() public shown = new EventEmitter();
  /**
   * Emits an event when the oes-popover is hidden
   */
  @Output() public hidden = new EventEmitter();

  private _OesDaterangePopoverWindowId = `ngb-popover-${nextId++}`;
  private _popupService: PopupService<OesDaterangePopoverWindow>;
  private _windowRef: ComponentRef<OesDaterangePopoverWindow>;
  private _unregisterListenersFn;
  private _zoneSubscription: any;

  constructor(
    private _elementRef: ElementRef, private _renderer: Renderer, injector: Injector,
    componentFactoryResolver: ComponentFactoryResolver, viewContainerRef: ViewContainerRef, config: OesDaterangePopoverConfig,
    ngZone: NgZone) {
    this.placement = config.placement;
    this.triggers = config.triggers;
    this.container = config.container;
    this._popupService = new PopupService<OesDaterangePopoverWindow>(
      OesDaterangePopoverWindow, injector, viewContainerRef, _renderer, componentFactoryResolver);

    this._zoneSubscription = ngZone.onStable.subscribe(() => {
      if (this._windowRef) {
        positionElements(
          this._elementRef.nativeElement, this._windowRef.location.nativeElement, this.placement,
          this.container === 'body');
      }
    });
  }

  /**
   * Opens an element’s oes-popover. This is considered a “manual” triggering of the oes-popover.
   * The context is an optional value to be injected into the oes-popover template when it is created.
   */
  public open(context?: any) {
    if (!this._windowRef) {
      this._windowRef = this._popupService.open(this.oesDaterangePopover, context);
      this._windowRef.instance.placement = this.placement;
      this._windowRef.instance.title = this.popoverTitle;
      this._windowRef.instance.id = this._OesDaterangePopoverWindowId;

      this._renderer.setElementAttribute(this._elementRef.nativeElement, 'aria-describedby', this._OesDaterangePopoverWindowId);

      if (this.container === 'body') {
        window.document.querySelector(this.container).appendChild(this._windowRef.location.nativeElement);
      }

      // we need to manually invoke change detection since events registered via
      // Renderer::listen() are not picked up by change detection with the OnPush strategy
      this._windowRef.changeDetectorRef.markForCheck();
      this.shown.emit();
    }
  }

  /**
   * Closes an element’s oes-popover. This is considered a “manual” triggering of the oes-popover.
   */
  public close(): void {
    if (this._windowRef) {
      this._renderer.setElementAttribute(this._elementRef.nativeElement, 'aria-describedby', null);
      this._popupService.close();
      this._windowRef = null;
      this.hidden.emit();
    }
  }

  /**
   * Toggles an element’s oes-popover. This is considered a “manual” triggering of the oes-popover.
   */
  public toggle(): void {
    if (this._windowRef) {
      this.close();
    } else {
      this.open();
    }
  }

  /**
   * Returns whether or not the oes-popover is currently being shown
   */
  public isOpen(): boolean { return this._windowRef !== null; }

  public ngOnInit() {
    this._unregisterListenersFn = listenToTriggers(
      this._renderer, this._elementRef.nativeElement, this.triggers, this.open.bind(this), this.close.bind(this),
      this.toggle.bind(this));
  }

  public ngOnDestroy() {
    this.close();
    this._unregisterListenersFn();
    this._zoneSubscription.unsubscribe();
  }
}