|
| 1 | +import { |
| 2 | + Component, |
| 3 | + Injector, |
| 4 | + ElementRef, |
| 5 | + Inject |
| 6 | +} from "@angular/core"; |
| 7 | +import { |
| 8 | + trigger, |
| 9 | + state, |
| 10 | + style, |
| 11 | + transition, |
| 12 | + animate |
| 13 | +} from "@angular/animations"; |
| 14 | +import Modal from "./modal.decorator"; |
| 15 | + |
| 16 | +/** |
| 17 | + * Component to create standard modals for presenting content or asking for user's input. |
| 18 | + * It can show as a passive modal showing only text or show as a transactional modal with |
| 19 | + * multiple buttons for different actions for the user to choose from. |
| 20 | + * |
| 21 | + * Using a modal in your application requires `ibm-modal-placeholder` which would generally be |
| 22 | + * placed near the end of your app component template (app.component.ts or app.component.html) as: |
| 23 | + * |
| 24 | + * ```html |
| 25 | + * <ibm-modal-placeholder></ibm-modal-placeholder> |
| 26 | + * ``` |
| 27 | + * |
| 28 | + * Example of opening the modal: |
| 29 | + * |
| 30 | + * ```typescript |
| 31 | + * \@Component({ |
| 32 | + * selector: "app-modal-demo", |
| 33 | + * template: ` |
| 34 | + * <button class="btn--primary" (click)="openModal()">Open modal</button> |
| 35 | + * <ibm-modal-placeholder></ibm-modal-placeholder>` |
| 36 | + * }) |
| 37 | + * export class ModalDemo { |
| 38 | + * openModal() { |
| 39 | + * this.modalService.show({ |
| 40 | + * modalType: "default", |
| 41 | + * modalLabel: "optional header text", |
| 42 | + * modalTitle: "Modal modalTitle", |
| 43 | + * text: "Modal text", |
| 44 | + * buttons: [{ |
| 45 | + * text: "Button text", |
| 46 | + * type: "primary", |
| 47 | + * click: clickFunction |
| 48 | + * }] |
| 49 | + * }); |
| 50 | + * } |
| 51 | + * } |
| 52 | + * ``` |
| 53 | + * |
| 54 | + * @export |
| 55 | + * @class AlertModalComponent |
| 56 | + */ |
| 57 | +@Modal() |
| 58 | +@Component({ |
| 59 | + selector: "ibm-alert-modal", |
| 60 | + template: ` |
| 61 | + <ibm-modal [modalType]="modalType"> |
| 62 | + <ibm-modal-header (closeSelect)="closeModal()"> |
| 63 | + <p class="bx--modal-header__label bx--type-delta">{{modalLabel}}</p> |
| 64 | + <p class="bx--modal-header__heading bx--type-beta">{{modalTitle}}</p> |
| 65 | + </ibm-modal-header> |
| 66 | + <div class="bx--modal-content"> |
| 67 | + <p>{{modalContent}}</p> |
| 68 | + </div> |
| 69 | + <ibm-modal-footer *ngIf="buttons.length > 0"> |
| 70 | + <ng-container *ngFor="let button of buttons; let i = index"> |
| 71 | + <button |
| 72 | + ibmButton="{{button.type}}" |
| 73 | + (click)="buttonClicked(i)" |
| 74 | + [id]="button.id" |
| 75 | + [attr.modal-primary-focus]="(button.type.indexOf('primary') !== -1 ? '' : null)"> |
| 76 | + {{button.text}} |
| 77 | + </button> |
| 78 | + </ng-container> |
| 79 | + </ibm-modal-footer> |
| 80 | + </ibm-modal> |
| 81 | + ` |
| 82 | +}) |
| 83 | +export class AlertModalComponent { |
| 84 | + /** |
| 85 | + * Creates an instance of `AlertModalComponent`. |
| 86 | + * @param {ModalService} modalService |
| 87 | + * @memberof AlertModalComponent |
| 88 | + */ |
| 89 | + constructor( |
| 90 | + @Inject("modalType") public modalType = "default", |
| 91 | + @Inject("modalLabel") public modalLabel: string, |
| 92 | + @Inject("modalTitle") public modalTitle: string, |
| 93 | + @Inject("modalContent") public modalContent: string, |
| 94 | + @Inject("buttons") public buttons = [] |
| 95 | + ) { |
| 96 | + for (let i = 0; i < this.buttons.length; i++) { |
| 97 | + const button = this.buttons[i]; |
| 98 | + if (!button.id) { |
| 99 | + button.id = `alert-modal-button-${i}`; |
| 100 | + } |
| 101 | + if (!button.type) { |
| 102 | + button.type = "secondary"; |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + buttonClicked(buttonIndex) { |
| 108 | + const button = this.buttons[buttonIndex]; |
| 109 | + if (button.click) { |
| 110 | + button.click(); |
| 111 | + } |
| 112 | + |
| 113 | + this.closeModal(); |
| 114 | + } |
| 115 | + |
| 116 | + closeModal() { |
| 117 | + // let call the @Modal.destroy() explicitly or otherwise the modal won't close |
| 118 | + this["destroy"](); |
| 119 | + } |
| 120 | +} |
0 commit comments