import { Injectable } from '@angular/core'; import { App,ViewController, NavOptions } from 'ionic-angular' import { ModalComponent, ModalTipComponent, ModalAlertComponent,ModalAlertWithCloseComponent,ModalConfirmComponent,ModalPromptComponent,ModalMaskComponent,ModalProgressComponent, ModalOption,PromptOption } from './index'; // == 模态框 @Injectable() export class Modal { modal timer closeMonitor // 路由监控关闭 constructor(public _app: App) {} /** * 激活模态框 * @param component * @param option * @param autoClose * @returns {ModalController} */ action = (component, option, autoClose?:any) => { const defaultModalOptions = { enterAnimation: 'modal-pop-in', leaveAnimation: 'modal-pop-out' }; option = Object.assign({}, defaultModalOptions, option); this.closeMonitor = option.closeMonitor || false; this.modal = this.create(component, option); // 创建模态组件 if(!option.autoOpen) return this.modal;// 手动打开直接返回弹窗 this.modal.present().then(() => { if(this.modal.instance.onPresent) this.modal.instance.onPresent(); }); // 自动关闭开始 if(autoClose){ // 默认3秒后自动关闭 const delay = typeof autoClose === 'number' ? autoClose : 3000; this.timer = setTimeout(()=>{ this.modal.dismiss().catch(() => {}); if(option.success) option.success(true, this.modal, 'autoClose'); },delay); } return this.modal; } /** * 构建模态框 * @param component 模态框组件 * @param option 模态框配置 */ create = (component: any, option: any = {}) => { return new ModalController(this._app, component, option); } // 无按钮弹窗 tip = (option: any, callback?: Function, autoClose?:any) => { if(!option) return; option = this._mergeOption(option, callback); //option.showBackdrop = false; option.enableBackdropDismiss = true; // 点击背景可关闭 return this.action(ModalTipComponent, option, autoClose); } // promise类型,下同 tip2 = (option) => { return new Promise((resolve)=>{ this.tip(option,function (isOk) { resolve(isOk); }); }); } // 带1个按钮弹窗 alert = (option: string | ModalOption, callback?: Function) => { if(!option) return; option = this._mergeOption(option, callback); return this.action(ModalAlertComponent, option); } alert2 = (option) => { return new Promise((resolve)=>{ this.alert(option,function (isOk) { resolve(isOk); }); }); } alertWithClose = (option: string | ModalOption, callback?: Function) => { if(!option) return; option = this._mergeOption(option, callback); return this.action(ModalAlertWithCloseComponent, option); } // 带2个按钮弹窗 confirm = (option: string | ModalOption, callback?: Function) => { if(!option) return; option = this._mergeOption(option, callback); return this.action(ModalConfirmComponent, option); } confirm2 = (option) => { return new Promise((resolve)=>{ this.confirm(option,function (isOk) { resolve(isOk); }); }); } // 带表单弹窗 prompt = (option: string | PromptOption, callback?: Function) => { if(!option) return; option = this._mergeOption(option, callback); return this.action(ModalPromptComponent, option); } prompt2 = (option) => { return new Promise((resolve)=>{ this.prompt(option,function (isOk) { resolve(isOk); }); }); } // 全屏浮层 mask = (option: any, callback?: Function) => { option = this._mergeOption(option, callback); return this.action(ModalMaskComponent, option); } /** * 进度条弹窗 * @param option 弹窗 * @param callback 回调 * @returns {any|ModalController} */ progress = (option: string | ModalOption, callback?: Function) => { option = this._mergeOption(option, callback); return this.action(ModalProgressComponent, option); } /** * 创建判断方法 * @param type 提示方法 */ createJudgeTip = (type?) => { /** * * @param condition 判断条件 * @param message 提示信息 * @parem type 提示类型 */ return (condition, message, callback?) => { if(!condition){ if(typeof type == 'function'){ type(message, callback); }else if(type == 'alert') { this.alert(message, callback); }else if(type == 'confirm'){ this.confirm(message, callback) }else{ this.tip(message, callback); } } return condition; } } // 合并配置 _mergeOption = (option: string | ModalOption, callback?:Function):any => { let mergeOption:any = {}; if(typeof option == 'string'){ mergeOption.content = option || ''; }else{ mergeOption = option; } // 默认自动打开,不自动关闭,自动触发dismiss事件 mergeOption.autoOpen = typeof mergeOption.autoOpen !== 'boolean' ? true : mergeOption.autoOpen; mergeOption.autoClose = typeof mergeOption.autoClose !== 'boolean' ? false : mergeOption.autoClose; mergeOption.autoDismiss = typeof mergeOption.autoDismiss !== 'boolean' ? true : mergeOption.autoDismiss; if(callback) mergeOption.success = mergeOption.success || callback; return mergeOption; } } /** * 模态框控制层,往app内自动添加modal组件 */ export class ModalController extends ViewController { public _app: App; public _enterAnimation: string; public _leaveAnimation: string; constructor(app: App, component: any, option: any) { option = option || {}; option.component = component; // modal内的子组件 const opts:any = {}; // 默认Modal模块配置 opts.showBackdrop = typeof option.showBackdrop == 'boolean' ? !!option.showBackdrop : true; opts.enableBackdropDismiss = option.enableBackdropDismiss ? !!option.enableBackdropDismiss : false; opts.enterAnimation = option.enterAnimation; opts.leaveAnimation = option.leaveAnimation; option.opts = opts; // console.log('option',option); // 将Modal组件传入app控件,data可在NavParams访问 super(ModalComponent, option, null); this._app = app; this._enterAnimation = option.enterAnimation; this._leaveAnimation = option.leaveAnimation; this.isOverlay = true; } // 过渡动画名称 getTransitionName(direction: string): string { let key: string; if (direction === 'back') { if (this._leaveAnimation) { return this._leaveAnimation; } key = 'modalLeave'; } else { if (this._enterAnimation) { return this._enterAnimation; } key = 'modalEnter'; } return this._nav && this._nav.config.get(key); } // 往app添加modal组件 present(navOptions: NavOptions = {}) { navOptions.minClickBlockDuration = navOptions.minClickBlockDuration || 400; return this._app.present(this, navOptions); } }