File

src/modal/alert-modal.component.ts

Description

Component to create standard modals for presenting content or asking for user's input. It can show as a passive modal showing only text or show as a transactional modal with multiple buttons for different actions for the user to choose from.

Using a modal in your application requires cds-placeholder which would generally be placed near the end of your app component template (app.component.ts or app.component.html) as:

Example :
<cds-placeholder></cds-placeholder>

Example of opening the modal:

Example :
\@Component({
 selector: "app-modal-demo",
 template: `
  <button class="btn--primary" (click)="openModal()">Open modal</button>
  <cds-placeholder></cds-placeholder>`
})
export class ModalDemo {
    openModal() {
        this.modalService.show({
            modalType: "default",
            label: "optional header text",
            title: "Modal title",
            text: "Modal text",
            buttons: [{
                text: "Button text",
                type: "primary",
                click: clickFunction
            }]
        });
    }
}

Extends

BaseModal

Implements

AfterViewInit

Metadata

Index

Properties
Methods
Inputs
Outputs

Constructor

constructor(type: string, label: string, title: string, content: string, size: string, hasScrollingContent: boolean, buttons: [], onClose: Function, showCloseButton)

Creates an instance of AlertModal.

Parameters :
Name Type Optional
type string No
label string No
title string No
content string No
size string No
hasScrollingContent boolean No
buttons [] No
onClose Function No
showCloseButton No

Inputs

open
Type : boolean
Default value : false
Inherited from BaseModal
Defined in BaseModal:25

Controls the open state of the modal

Outputs

close
Type : EventEmitter
Inherited from BaseModal
Defined in BaseModal:20

Base event emitter to propagate close events

Methods

buttonClicked
buttonClicked(buttonIndex: string | number)
Parameters :
Name Type Optional
buttonIndex string | number No
Returns : void
dismissModal
dismissModal(trigger: any)
Parameters :
Name Type Optional
trigger any No
Returns : void
ngAfterViewInit
ngAfterViewInit()
Returns : boolean
closeModal
closeModal()
Inherited from BaseModal
Defined in BaseModal:30

Default method to handle closing the modal

Returns : void

Properties

Public buttons
Type : []
Default value : []
Decorators :
@Optional()
@Inject('buttons')
Public content
Type : string
Decorators :
@Optional()
@Inject('content')
Public hasScrollingContent
Type : boolean
Default value : null
Decorators :
@Optional()
@Inject('hasScrollingContent')
Public label
Type : string
Decorators :
@Optional()
@Inject('label')
modalContent
Type : literal type
Decorators :
@ViewChild('modalContent', {static: true})
Public onClose
Type : Function
Decorators :
@Optional()
@Inject('close')
Public showCloseButton
Default value : true
Decorators :
@Optional()
@Inject('showCloseButton')
Public size
Type : string
Decorators :
@Optional()
@Inject('size')
Public title
Type : string
Decorators :
@Optional()
@Inject('title')
Public type
Type : string
Default value : "default"
Decorators :
@Optional()
@Inject('type')
import {
	Component,
	Inject,
	ViewChild,
	AfterViewInit,
	Optional
} from "@angular/core";
import { BaseModal } from "./base-modal.class";

/**
 * Component to create standard modals for presenting content or asking for user's input.
 * It can show as a passive modal showing only text or show as a transactional modal with
 * multiple buttons for different actions for the user to choose from.
 *
 * Using a modal in your application requires `cds-placeholder` which would generally be
 * placed near the end of your app component template (app.component.ts or app.component.html) as:
 *
 * ```html
 * <cds-placeholder></cds-placeholder>
 * ```
 *
 * Example of opening the modal:
 *
 * ```typescript
 * \@Component({
 *  selector: "app-modal-demo",
 *  template: `
 *   <button class="btn--primary" (click)="openModal()">Open modal</button>
 *   <cds-placeholder></cds-placeholder>`
 * })
 * export class ModalDemo {
 * 	openModal() {
 * 		this.modalService.show({
 *			modalType: "default",
 *			label: "optional header text",
 *			title: "Modal title",
 *			text: "Modal text",
 *			buttons: [{
 *				text: "Button text",
 *				type: "primary",
 *				click: clickFunction
 *			}]
 *		});
 * 	}
 * }
 * ```
 */
@Component({
	selector: "cds-alert-modal, ibm-alert-modal",
	template: `
		<cds-modal
			[size]="size"
			[theme]="type"
			[ariaLabel]="title"
			[hasScrollingContent]="hasScrollingContent"
			[open]="open"
			(overlaySelected)="dismissModal('overlay')">
			<cds-modal-header (closeSelect)="dismissModal('close')" [showCloseButton]="showCloseButton">
				<p cdsModalHeaderLabel class="cds--type-delta">{{label}}</p>
				<p cdsModalHeaderHeading class="cds--type-beta">{{title}}</p>
			</cds-modal-header>
			<div cdsModalContent #modalContent>
				<p [innerHTML]="content"></p>
			</div>
			<cds-modal-footer *ngIf="buttons.length > 0">
				<ng-container *ngFor="let button of buttons; let i = index">
					<button
						[cdsButton]="button.type"
						(click)="buttonClicked(i)"
						[id]="button.id"
						[attr.modal-primary-focus]="(button.type.indexOf('primary') !== -1 ? '' : null)">
						{{button.text}}
					</button>
				</ng-container>
			</cds-modal-footer>
		</cds-modal>
	`
})
export class AlertModal extends BaseModal implements AfterViewInit {
	@ViewChild("modalContent", { static: true }) modalContent: { nativeElement: any; };
	/**
	 * Creates an instance of `AlertModal`.
	 */
	constructor(
		@Optional() @Inject("type") public type = "default",
		@Optional() @Inject("label") public label: string,
		@Optional() @Inject("title") public title: string,
		@Optional() @Inject("content") public content: string,
		@Optional() @Inject("size") public size: string,
		@Optional() @Inject("hasScrollingContent") public hasScrollingContent: boolean = null,
		@Optional() @Inject("buttons") public buttons = [],
		@Optional() @Inject("close") public onClose: Function,
		@Optional() @Inject("showCloseButton") public showCloseButton = true
	) {
		super();
		for (let i = 0; i < this.buttons.length; i++) {
			const button = this.buttons[i];
			if (!button.id) {
				button.id = `alert-modal-button-${i}`;
			}
			if (!button.type) {
				button.type = "secondary";
			}
		}
	}

	ngAfterViewInit() {
		if (!this.modalContent) { return false; }
		const element = this.modalContent.nativeElement;
		if (element.scrollHeight > element.clientHeight) {
			element.tabIndex = 0;
		} else {
			element.tabIndex = -1;
		}
	}

	buttonClicked(buttonIndex: string | number) {
		const button = this.buttons[buttonIndex];
		if (button.click) {
			button.click();
		}

		this.closeModal();
	}

	dismissModal(trigger: any) {
		if (this.onClose && this.onClose(trigger) === false) {
			return;
		}
		this.closeModal();
	}
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""