Project

General

Profile

1
import {Component, ViewEncapsulation, ElementRef, EventEmitter, Output, Input, ViewChild} from '@angular/core';
2

    
3
declare var UIkit: any;
4

    
5
@Component({
6
  selector: 'modal-alert',
7
  template: `
8
    <div #element class="uk-modal" uk-modal>
9
      <div class="uk-modal-dialog uk-modal-body uk-animation-fast uk-margin-medium-bottom uk-text-left"
10
           [ngClass]="classBody">
11
        <div [ngClass]="classTitle" class="uk-modal-title" [hidden]=!alertHeader>
12
          <button class="uk-modal-close-default uk-float-right" (click)='cancel()' uk-close></button>
13
          <h4 class="modal-title">{{alertTitle}}</h4>
14
        </div>
15
        <div class="uk-margin">
16
          <div [hidden]=!alertMessage>
17
            {{message}}
18
          </div>
19
          <ng-content></ng-content>
20
        </div>
21
        <div class="uk-grid uk-flex uk-flex-middle">
22
          <label *ngIf="choice" class="uk-width-1-2 checkbox">
23
            <input type="checkbox" [(ngModel)]="select">
24
            <span class="uk-margin-small-left" style="font-weight: normal;">Don't show this message again</span>
25
          </label>
26
          <div [ngClass]="(choice)?'uk-width-1-2':'uk-width-1-1'">
27
            <div *ngIf="okButtonLeft" class="uk-text-right" [hidden]=!alertFooter>
28
              <span [hidden]=!okButton>
29
                <button *ngIf="okDisabled" class="uk-button md-btn disabled uk-button-default ignoreCommunityPanelBackground" disabled>{{okButtonText}}</button>
30
                <button *ngIf="!okDisabled"
31
                      class="uk-button md-btn md-btn-primary portal-button ignoreCommunityPanelBackground"
32
                      (click)="ok()">{{okButtonText}}</button>
33
              </span>
34
              <span [hidden]=!cancelButton>
35
                <button class="uk-button md-btn uk-button-default uk-margin-small-left"
36
                        (click)="cancel()">{{cancelButtonText}}</button>
37
              </span>
38
            </div>
39
            <div *ngIf="!okButtonLeft" class="uk-text-right" [hidden]=!alertFooter>
40
               <span [hidden]=!cancelButton>
41
                  <button class="uk-button md-btn uk-button-default uk-margin-small-right"
42
                          (click)="cancel()">{{cancelButtonText}}</button>
43
              </span>
44
              <span [hidden]=!okButton>
45
              <button *ngIf="okDisabled" class="uk-button md-btn disabled uk-button-default ignoreCommunityPanelBackground"
46
                      disabled>{{okButtonText}}</button>
47
              <button *ngIf="!okDisabled"
48
                      class="uk-button md-btn md-btn-primary portal-button ignoreCommunityPanelBackground"
49
                      (click)="ok()">{{okButtonText}}</button>
50
            </span>
51
            </div>
52
          </div>
53
        </div>
54
      </div>
55
    </div>
56
  `,
57
  encapsulation: ViewEncapsulation.None,
58
})
59
/**
60
 * API to an open alert window.
61
 */
62
export class AlertModal {
63
  @Input() classTitle: string = "";
64
  @Input() classBody: string = "";
65
  /**
66
   * Caption for the title.
67
   */
68
  public alertTitle: string;
69
  /**
70
   * Describes if the alert contains Ok Button.
71
   * The default Ok button will close the alert and emit the callback.
72
   * Defaults to true.
73
   */
74
  public okButton: boolean = true;
75
  /**
76
   * Caption for the OK button.
77
   * Default: Ok
78
   */
79
  public okButtonText: string = 'Ok';
80
  /**
81
   * Describes if the alert contains cancel Button.
82
   * The default Cancelbutton will close the alert.
83
   * Defaults to true.
84
   */
85
  public cancelButton: boolean = true;
86
  /**
87
   * Caption for the Cancel button.
88
   * Default: Cancel
89
   */
90
  public cancelButtonText: string = 'Cancel';
91
  /**
92
   * if the alertMessage is true it will show the contentString inside alert body.
93
   */
94
  public alertMessage: boolean = true;
95
  /**
96
   * Some message/content can be set in message which will be shown in alert body.
97
   */
98
  public message: string;
99
  /**
100
   * if the value is true alert footer will be visible or else it will be hidden.
101
   */
102
  public alertFooter: boolean = true;
103
  /**
104
   * shows alert header if the value is true.
105
   */
106
  public alertHeader: boolean = true;
107
  /**
108
   *  if the value is true ok button align on the left, else on the right
109
   */
110
  public okButtonLeft: boolean = true;
111

    
112
  /**
113
   *  if the value is true ok button is disabled
114
   */
115
  @Input()
116
  public okDisabled: boolean = false;
117

    
118
  /**
119
   *  If the value is true, a checkbox option will be appeared on the right side of footer
120
   */
121
  @Input()
122
  public choice: boolean = false;
123

    
124
  /**
125
   * if the value is true then on ok clicked, modal will stay open.
126
   */
127
  public stayOpen: boolean = false;
128

    
129
  /**
130
   *  Value will be emitted if @choice is true
131
   */
132
  public select: boolean = false;
133
  /**
134
   * Emitted when a ok button was clicked
135
   * or when Ok method is called.
136
   */
137
  @Output() public alertOutput: EventEmitter<any> = new EventEmitter();
138

    
139
  @ViewChild('element') element: ElementRef;
140

    
141
  constructor() {
142
  }
143

    
144
  /**
145
   * Opens a alert window creating backdrop.
146
   */
147
  open() {
148
    UIkit.modal(this.element.nativeElement).show();
149
  }
150

    
151
  /**
152
   *  ok method closes the modal and emits modalOutput.
153
   */
154
  ok() {
155
    if (!this.stayOpen) {
156
      this.cancel();
157
    }
158
    if (!this.choice) {
159
      this.alertOutput.emit(true);
160
    } else {
161
      this.alertOutput.emit({
162
        value: true,
163
        choice: this.select
164
      });
165
    }
166
  }
167

    
168
  /**
169
   *  cancel method closes the modal.
170
   */
171
  cancel() {
172
    UIkit.modal(this.element.nativeElement).hide();
173
  }
174
}
(1-1/8)