refactor(action-sheet): adds proper TS types

This commit is contained in:
Manuel Mtz-Almeida
2017-05-05 00:57:16 +02:00
parent 7f7c3cd514
commit 3b4b29e832
13 changed files with 48 additions and 47 deletions

View File

@ -1,5 +1,6 @@
import { Component, ElementRef, HostListener, Renderer, ViewEncapsulation } from '@angular/core'; import { Component, ElementRef, HostListener, Renderer, ViewEncapsulation } from '@angular/core';
import { ActionSheetOptions, ActionSheetButton } from './action-sheet-options';
import { assert } from '../../util/util'; import { assert } from '../../util/util';
import { BlockerDelegate, GestureController, BLOCK_ALL } from '../../gestures/gesture-controller'; import { BlockerDelegate, GestureController, BLOCK_ALL } from '../../gestures/gesture-controller';
import { Config } from '../../config/config'; import { Config } from '../../config/config';
@ -26,10 +27,10 @@ import { ViewController } from '../../navigation/view-controller';
'{{b.text}}' + '{{b.text}}' +
'</button>' + '</button>' +
'</div>' + '</div>' +
'<div class="action-sheet-group" *ngIf="d.cancelButton">' + '<div class="action-sheet-group" *ngIf="cancelButton">' +
'<button ion-button="action-sheet-button" (click)="click(d.cancelButton)" class="action-sheet-cancel disable-hover" [attr.icon-left]="d.cancelButton.icon ? \'\' : null" [ngClass]="d.cancelButton.cssClass">' + '<button ion-button="action-sheet-button" (click)="click(cancelButton)" class="action-sheet-cancel disable-hover" [attr.icon-left]="cancelButton.icon ? \'\' : null" [ngClass]="cancelButton.cssClass">' +
'<ion-icon [name]="d.cancelButton.icon" *ngIf="d.cancelButton.icon" class="action-sheet-icon"></ion-icon>' + '<ion-icon [name]="cancelButton.icon" *ngIf="cancelButton.icon" class="action-sheet-icon"></ion-icon>' +
'{{d.cancelButton.text}}' + '{{cancelButton.text}}' +
'</button>' + '</button>' +
'</div>' + '</div>' +
'</div>' + '</div>' +
@ -41,15 +42,11 @@ import { ViewController } from '../../navigation/view-controller';
}, },
encapsulation: ViewEncapsulation.None, encapsulation: ViewEncapsulation.None,
}) })
export class ActionSheetCmp { export class ActionSheetCmp {
d: {
title?: string; d: ActionSheetOptions;
subTitle?: string; cancelButton: ActionSheetButton;
cssClass?: string;
buttons?: Array<any>;
enableBackdropDismiss?: boolean;
cancelButton: any;
};
descId: string; descId: string;
enabled: boolean; enabled: boolean;
hdrId: string; hdrId: string;
@ -89,30 +86,26 @@ export class ActionSheetCmp {
ionViewDidLoad() { ionViewDidLoad() {
// normalize the data // normalize the data
let buttons: any[] = []; this.d.buttons = this.d.buttons.map(button => {
this.d.buttons.forEach((button: any) => {
if (typeof button === 'string') { if (typeof button === 'string') {
button = { text: button }; button = { text: button };
} }
if (!button.cssClass) { if (!button.cssClass) {
button.cssClass = ''; button.cssClass = '';
} }
switch (button.role) {
if (button.role === 'cancel') { case 'cancel':
this.d.cancelButton = button; this.cancelButton = button;
return null;
} else { case 'destructive':
if (button.role === 'destructive') {
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive'; button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-destructive';
} else if (button.role === 'selected') { break;
case 'selected':
button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected'; button.cssClass = (button.cssClass + ' ' || '') + 'action-sheet-selected';
break;
} }
buttons.push(button); return button;
} }).filter(button => button !== null);
});
this.d.buttons = buttons;
} }
ionViewWillEnter() { ionViewWillEnter() {
@ -141,7 +134,7 @@ export class ActionSheetCmp {
} }
} }
click(button: any) { click(button: ActionSheetButton) {
if (!this.enabled) { if (!this.enabled) {
return; return;
} }
@ -163,8 +156,8 @@ export class ActionSheetCmp {
bdClick() { bdClick() {
if (this.enabled && this.d.enableBackdropDismiss) { if (this.enabled && this.d.enableBackdropDismiss) {
if (this.d.cancelButton) { if (this.cancelButton) {
this.click(this.d.cancelButton); this.click(this.cancelButton);
} else { } else {
this.dismiss('backdrop'); this.dismiss('backdrop');
@ -172,7 +165,7 @@ export class ActionSheetCmp {
} }
} }
dismiss(role: any): Promise<any> { dismiss(role: string): Promise<any> {
const opts: NavOptions = { const opts: NavOptions = {
minClickBlockDuration: 400 minClickBlockDuration: 400
}; };
@ -181,7 +174,7 @@ export class ActionSheetCmp {
ngOnDestroy() { ngOnDestroy() {
assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked'); assert(this.gestureBlocker.blocked === false, 'gesture blocker must be already unblocked');
this.d = null; this.d = this.cancelButton = null;
this.gestureBlocker.destroy(); this.gestureBlocker.destroy();
} }
} }

View File

@ -3,6 +3,14 @@ export interface ActionSheetOptions {
title?: string; title?: string;
subTitle?: string; subTitle?: string;
cssClass?: string; cssClass?: string;
buttons?: Array<any>; buttons?: (ActionSheetButton|string)[];
enableBackdropDismiss?: boolean; enableBackdropDismiss?: boolean;
} }
export interface ActionSheetButton {
text?: string;
role?: string;
icon?: string;
cssClass?: string;
handler?: () => boolean|void;
};

View File

@ -1,5 +1,5 @@
import { ActionSheetCmp } from './action-sheet-component'; import { ActionSheetCmp } from './action-sheet-component';
import { ActionSheetOptions } from './action-sheet-options'; import { ActionSheetOptions, ActionSheetButton } from './action-sheet-options';
import { ActionSheetSlideIn, ActionSheetMdSlideIn, ActionSheetSlideOut, ActionSheetMdSlideOut, ActionSheetWpSlideIn, ActionSheetWpSlideOut } from './action-sheet-transitions'; import { ActionSheetSlideIn, ActionSheetMdSlideIn, ActionSheetSlideOut, ActionSheetMdSlideOut, ActionSheetWpSlideIn, ActionSheetWpSlideOut } from './action-sheet-transitions';
import { App } from '../app/app'; import { App } from '../app/app';
import { Config } from '../../config/config'; import { Config } from '../../config/config';
@ -34,7 +34,7 @@ export class ActionSheet extends ViewController {
* @hidden * @hidden
*/ */
getTransitionName(direction: string): string { getTransitionName(direction: string): string {
let key = 'actionSheet' + (direction === 'back' ? 'Leave' : 'Enter'); const key = 'actionSheet' + (direction === 'back' ? 'Leave' : 'Enter');
return this._nav && this._nav.config.get(key); return this._nav && this._nav.config.get(key);
} }
@ -57,7 +57,7 @@ export class ActionSheet extends ViewController {
/** /**
* @param {object} button Action sheet button * @param {object} button Action sheet button
*/ */
addButton(button: any): ActionSheet { addButton(button: ActionSheetButton|string): ActionSheet {
this.data.buttons.push(button); this.data.buttons.push(button);
return this; return this;
} }

View File

@ -295,7 +295,7 @@ export class AlertCmp {
} }
} }
dismiss(role: any): Promise<any> { dismiss(role: string): Promise<any> {
const opts: NavOptions = { const opts: NavOptions = {
minClickBlockDuration: 400 minClickBlockDuration: 400
}; };

View File

@ -158,7 +158,7 @@ export class PageOne {
this.testPromptOpen = true; this.testPromptOpen = true;
}); });
alert.onDidDismiss((data: any, role: any) => { alert.onDidDismiss((data, role) => {
console.log('onDidDismiss, data:', data, 'role:', role); console.log('onDidDismiss, data:', data, 'role:', role);
}); });
} }

View File

@ -87,7 +87,7 @@ export class LoadingCmp {
} }
dismiss(role: any): Promise<any> { dismiss(role: string): Promise<any> {
if (this.durationTimeout) { if (this.durationTimeout) {
clearTimeout(this.durationTimeout); clearTimeout(this.durationTimeout);
} }

View File

@ -205,7 +205,7 @@ export class PickerCmp {
} }
} }
dismiss(role: any): Promise<any> { dismiss(role: string): Promise<any> {
return this._viewCtrl.dismiss(this.getSelected(), role); return this._viewCtrl.dismiss(this.getSelected(), role);
} }

View File

@ -370,7 +370,7 @@ export class Select extends BaseInput<any> implements OnDestroy {
this._fireFocus(); this._fireFocus();
overlay.onDidDismiss((value: any, role: string) => { overlay.onDidDismiss(() => {
this._fireBlur(); this._fireBlur();
this._overlay = undefined; this._overlay = undefined;
}); });
@ -381,7 +381,7 @@ export class Select extends BaseInput<any> implements OnDestroy {
/** /**
* Close the select interface. * Close the select interface.
*/ */
close() { close(): Promise<any> {
if (!this._overlay || !this.isFocus()) { if (!this._overlay || !this.isFocus()) {
return; return;
} }

View File

@ -96,7 +96,7 @@ export class ToastCmp implements AfterViewInit {
} }
} }
dismiss(role: any): Promise<any> { dismiss(role: string): Promise<any> {
clearTimeout(this.dismissTimeout); clearTimeout(this.dismissTimeout);
this.dismissTimeout = undefined; this.dismissTimeout = undefined;
return this._viewCtrl.dismiss(null, role, {disableApp: false}); return this._viewCtrl.dismiss(null, role, {disableApp: false});

View File

@ -42,7 +42,7 @@ export class OverlayProxy {
} }
} }
dismiss(data?: any, role?: any, navOptions?: NavOptions): Promise<any> { dismiss(data?: any, role?: string, navOptions?: NavOptions): Promise<any> {
if (this.overlay) { if (this.overlay) {
return this.overlay.dismiss(); return this.overlay.dismiss();
} }

View File

@ -2,7 +2,7 @@ import { NavOptions } from './nav-util';
export interface Overlay { export interface Overlay {
present(opts?: NavOptions): Promise<any>; present(opts?: NavOptions): Promise<any>;
dismiss(data?: any, role?: any, navOptions?: NavOptions): Promise<any>; dismiss(data?: any, role?: string, navOptions?: NavOptions): Promise<any>;
onDidDismiss(callback: Function): void; onDidDismiss(callback: Function): void;
onWillDismiss(callback: Function): void; onWillDismiss(callback: Function): void;
} }

View File

@ -167,7 +167,7 @@ export class ViewController {
* @param {NavOptions} NavOptions Options for the dismiss navigation. * @param {NavOptions} NavOptions Options for the dismiss navigation.
* @returns {any} data Returns the data passed in, if any. * @returns {any} data Returns the data passed in, if any.
*/ */
dismiss(data?: any, role?: any, navOptions: NavOptions = {}): Promise<any> { dismiss(data?: any, role?: string, navOptions: NavOptions = {}): Promise<any> {
if (!this._nav) { if (!this._nav) {
assert(this._state === STATE_DESTROYED, 'ViewController does not have a valid _nav'); assert(this._state === STATE_DESTROYED, 'ViewController does not have a valid _nav');
return Promise.resolve(false); return Promise.resolve(false);

View File

@ -591,7 +591,7 @@ export function mockNgModuleLoader(): NgModuleLoader {
export function mockOverlay() { export function mockOverlay() {
return { return {
present: (opts?: NavOptions) => { return Promise.resolve(); }, present: (opts?: NavOptions) => { return Promise.resolve(); },
dismiss: (data?: any, role?: any, navOptions?: NavOptions) => { return Promise.resolve(); }, dismiss: (data?: any, role?: string, navOptions?: NavOptions) => { return Promise.resolve(); },
onDidDismiss: (callback: Function) => { }, onDidDismiss: (callback: Function) => { },
onWillDismiss: (callback: Function) => { } onWillDismiss: (callback: Function) => { }
}; };