UNPKG

@ciri/ngx-popup

Version:

An angular popup component that can customize animation.

904 lines (895 loc) 27.5 kB
import { Injectable, ɵɵdefineInjectable, ɵɵinject, EventEmitter, Component, ViewEncapsulation, ChangeDetectionStrategy, ChangeDetectorRef, Input, Output, ViewChild, ApplicationRef, ComponentFactoryResolver, Injector, Inject, INJECTOR, forwardRef, ElementRef, Renderer2, NgModule } from '@angular/core'; import { NG_VALUE_ACCESSOR, FormsModule } from '@angular/forms'; import { AnimationBuilder, style, animate } from '@angular/animations'; import { Observable, Subject, Subscription } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; import { DOCUMENT, CommonModule } from '@angular/common'; /** * @fileoverview added by tsickle * Generated from: lib/animation.service.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var AnimationService = /** @class */ (function () { function AnimationService(builder) { this.builder = builder; } /** * @param {?} element * @param {?} animation * @return {?} */ AnimationService.prototype.makeAnimation = /** * @param {?} element * @param {?} animation * @return {?} */ function (element, animation) { var _this = this; return new Observable((/** * @param {?} observer * @return {?} */ function (observer) { // first define a reusable animation /** @type {?} */ var myAnimation = _this.builder.build(animation) // use the returned factory object to create a player ; // use the returned factory object to create a player /** @type {?} */ var player = myAnimation.create(element); player.play(); player.onDone((/** * @return {?} */ function () { observer.next(); observer.complete(); })); return (/** * @return {?} */ function () { player.destroy(); }); })); }; AnimationService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ AnimationService.ctorParameters = function () { return [ { type: AnimationBuilder } ]; }; /** @nocollapse */ AnimationService.ngInjectableDef = ɵɵdefineInjectable({ factory: function AnimationService_Factory() { return new AnimationService(ɵɵinject(AnimationBuilder)); }, token: AnimationService, providedIn: "root" }); return AnimationService; }()); if (false) { /** * @type {?} * @private */ AnimationService.prototype.builder; } /** * @fileoverview added by tsickle * Generated from: lib/overlay/overlay.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var OverlayComponent = /** @class */ (function () { function OverlayComponent(animation, cdr) { this.animation = animation; this.cdr = cdr; this.opacity = 0.5; this.clickOverlay = new EventEmitter(); this.afterClose = new EventEmitter(); this._visible = false; this.destroy$ = new Subject(); } Object.defineProperty(OverlayComponent.prototype, "visible", { get: /** * @return {?} */ function () { return this._visible; }, set: /** * @param {?} value * @return {?} */ function (value) { var _this = this; this.animationSub && this.animationSub.unsubscribe(); if (value) { this._visible = true; this.cdr.detectChanges(); this.animationSub = this.makeAnimation('enter') .pipe(takeUntil(this.destroy$)) .subscribe((/** * @return {?} */ function () { })); } else { this.animationSub = this.makeAnimation('leave') .pipe(takeUntil(this.destroy$)) .subscribe((/** * @return {?} */ function () { _this._visible = false; _this.cdr.detectChanges(); _this.afterClose.emit(); })); } }, enumerable: true, configurable: true }); Object.defineProperty(OverlayComponent.prototype, "styles", { get: /** * @return {?} */ function () { return { 'z-index': this.zIndex, 'background': "rgba(0, 0, 0, " + this.opacity + ")", }; }, enumerable: true, configurable: true }); /** * @return {?} */ OverlayComponent.prototype.ngOnInit = /** * @return {?} */ function () { }; /** * @return {?} */ OverlayComponent.prototype.ngOnDestroy = /** * @return {?} */ function () { this.destroy$.next(); this.destroy$.complete(); }; /** * @return {?} */ OverlayComponent.prototype.onClick = /** * @return {?} */ function () { this.clickOverlay.emit(); }; /** * @param {?} state * @return {?} */ OverlayComponent.prototype.makeAnimation = /** * @param {?} state * @return {?} */ function (state) { /** @type {?} */ var isEnter = state === 'enter'; /** @type {?} */ var el = this.container.nativeElement; /** @type {?} */ var animation = isEnter ? [style({ opacity: 0 }), animate('.3s ease', style({ opacity: 1 }))] : [style({ opacity: 1 }), animate('.3s ease', style({ opacity: 0 }))]; return this.animation.makeAnimation(el, animation); }; OverlayComponent.decorators = [ { type: Component, args: [{ selector: 'popup-overlay', template: "<div\n class=\"overlay\"\n #container\n [hidden]=\"!visible\"\n [ngStyle]=\"styles\"\n (click)=\"onClick()\"\n></div>\n", encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, styles: [".overlay{position:fixed;top:0;right:0;bottom:0;left:0}"] }] } ]; /** @nocollapse */ OverlayComponent.ctorParameters = function () { return [ { type: AnimationService }, { type: ChangeDetectorRef } ]; }; OverlayComponent.propDecorators = { zIndex: [{ type: Input }], opacity: [{ type: Input }], visible: [{ type: Input }], clickOverlay: [{ type: Output }], afterClose: [{ type: Output }], container: [{ type: ViewChild, args: ['container', { static: false },] }] }; return OverlayComponent; }()); if (false) { /** @type {?} */ OverlayComponent.prototype.zIndex; /** @type {?} */ OverlayComponent.prototype.opacity; /** @type {?} */ OverlayComponent.prototype.clickOverlay; /** @type {?} */ OverlayComponent.prototype.afterClose; /** * @type {?} * @private */ OverlayComponent.prototype.container; /** * @type {?} * @private */ OverlayComponent.prototype._visible; /** * @type {?} * @private */ OverlayComponent.prototype.destroy$; /** * @type {?} * @private */ OverlayComponent.prototype.animationSub; /** * @type {?} * @private */ OverlayComponent.prototype.animation; /** * @type {?} * @private */ OverlayComponent.prototype.cdr; } /** * @fileoverview added by tsickle * Generated from: lib/overlay/overlay-helper.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @record */ function OverlayOptions() { } if (false) { /** @type {?} */ OverlayOptions.prototype.opacity; /** @type {?|undefined} */ OverlayOptions.prototype.zIndex; /** @type {?|undefined} */ OverlayOptions.prototype.onClick; /** @type {?|undefined} */ OverlayOptions.prototype.getContainer; } /** * \@dynamic */ var OverlayHelper = /** @class */ (function () { function OverlayHelper(appRef, cfr, injector, document) { this.appRef = appRef; this.cfr = cfr; this.injector = injector; this.document = document; } /** * @param {?} options * @return {?} */ OverlayHelper.prototype.open = /** * @param {?} options * @return {?} */ function (options) { /** @type {?} */ var opts = Object.assign(this.getDefaultOptions(), options); var opacity = opts.opacity, zIndex = opts.zIndex, getContainer = opts.getContainer; /** @type {?} */ var factory = this.cfr.resolveComponentFactory(OverlayComponent); /** @type {?} */ var componentRef = factory.create(this.injector); this.appRef.attachView(componentRef.hostView); var nativeElement = componentRef.location.nativeElement; /** @type {?} */ var container = getContainer(); container.insertBefore(nativeElement, container.firstChild); /** @type {?} */ var inst = componentRef.instance; inst.opacity = opacity; inst.zIndex = zIndex; inst.visible = true; this.setListener(componentRef, opts); return (/** * @return {?} */ function () { inst.visible = false; }); }; /** * @private * @return {?} */ OverlayHelper.prototype.getDefaultOptions = /** * @private * @return {?} */ function () { return { opacity: 0.5, getContainer: (/** * @return {?} */ function () { return document.body; }) }; }; /** * @private * @param {?} componentRef * @param {?} opts * @return {?} */ OverlayHelper.prototype.setListener = /** * @private * @param {?} componentRef * @param {?} opts * @return {?} */ function (componentRef, opts) { /** @type {?} */ var inst = componentRef.instance; /** @type {?} */ var subs = new Subscription(); var onClick = opts.onClick; subs.add(inst.afterClose.subscribe((/** * @return {?} */ function () { componentRef.destroy(); }))); if (onClick) { subs.add(inst.clickOverlay.subscribe((/** * @return {?} */ function () { onClick(); }))); } componentRef.onDestroy((/** * @return {?} */ function () { subs.unsubscribe(); })); }; OverlayHelper.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ OverlayHelper.ctorParameters = function () { return [ { type: ApplicationRef }, { type: ComponentFactoryResolver }, { type: Injector }, { type: Document, decorators: [{ type: Inject, args: [DOCUMENT,] }] } ]; }; /** @nocollapse */ OverlayHelper.ngInjectableDef = ɵɵdefineInjectable({ factory: function OverlayHelper_Factory() { return new OverlayHelper(ɵɵinject(ApplicationRef), ɵɵinject(ComponentFactoryResolver), ɵɵinject(INJECTOR), ɵɵinject(DOCUMENT)); }, token: OverlayHelper, providedIn: "root" }); return OverlayHelper; }()); if (false) { /** * @type {?} * @private */ OverlayHelper.prototype.appRef; /** * @type {?} * @private */ OverlayHelper.prototype.cfr; /** * @type {?} * @private */ OverlayHelper.prototype.injector; /** * @type {?} * @private */ OverlayHelper.prototype.document; } /** * @fileoverview added by tsickle * Generated from: lib/popup/popup.component.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var zIndex = 9999; /** * @record */ function PopupAnimations() { } if (false) { /** @type {?|undefined} */ PopupAnimations.prototype.enter; /** @type {?|undefined} */ PopupAnimations.prototype.leave; } /** @enum {string} */ var Position = { center: "center", top: "top", right: "right", bottom: "bottom", left: "left", }; var PopupComponent = /** @class */ (function () { function PopupComponent(hostElRef, cdr, renderer, overlayHelper, animation) { this.hostElRef = hostElRef; this.cdr = cdr; this.renderer = renderer; this.overlayHelper = overlayHelper; this.animation = animation; /* inputs -------------------------- */ this.position = Position.center; /** 弹窗位置 */ /** * 弹窗位置 */ this.animations = {}; /** 自定义动画 */ /** * 自定义动画 */ this.overlay = true; /** 是否显示蒙版 */ /** * 是否显示蒙版 */ this.overlayOpacity = 0.5; /** 蒙版透明度 0~1 */ /** * 蒙版透明度 0~1 */ this.closeOnClickOverlay = true; /** 是否允许点击蒙版时自动关闭弹框 */ /** * 是否允许点击蒙版时自动关闭弹框 */ this.zIndex = zIndex++; /** 同 css z-index */ /** * 同 css z-index */ this.externalClass = {}; /** 自定义类名 */ /** * 自定义类名 */ /* outputs -------------------------- */ this.clickOverlay = new EventEmitter(); /** 点击蒙版时触发 */ /** * 点击蒙版时触发 */ this.beforeOpen = new EventEmitter(); /** 打开之前触发(还未执行进场动画) */ /** * 打开之前触发(还未执行进场动画) */ this.afterOpen = new EventEmitter(); /** 打开之后触发(进场动画执行完毕) */ /** * 打开之后触发(进场动画执行完毕) */ this.beforeClose = new EventEmitter(); /** 关闭之前触发(还未执行离场动画) */ /** * 关闭之前触发(还未执行离场动画) */ this.afterClose = new EventEmitter(); /** 关闭之后触发(离场动画执行完毕) */ this.destroy$ = new Subject(); this.dirty = false; this.leaving = false; } Object.defineProperty(PopupComponent.prototype, "el", { get: /** * @return {?} */ function () { return this.container.nativeElement; }, enumerable: true, configurable: true }); /** * @return {?} */ PopupComponent.prototype.ngOnDestroy = /** * @return {?} */ function () { this.destroy$.next(); this.destroy$.complete(); }; /** * @param {?} fn * @return {?} */ PopupComponent.prototype.registerOnChange = /** * @param {?} fn * @return {?} */ function (fn) { this.onChange = fn; }; /** * @param {?} fn * @return {?} */ PopupComponent.prototype.registerOnTouched = /** * @param {?} fn * @return {?} */ function (fn) { }; /** * @param {?} value * @return {?} */ PopupComponent.prototype.writeValue = /** * @param {?} value * @return {?} */ function (value) { if (value === null) { return; } if (!this.dirty && !value) { return; } if (this.animationSub) { this.animationSub.unsubscribe(); } if (value) { this.open(); } else { this.close(); } this.onChange(value); this.dirty = true; }; /** * @private * @return {?} */ PopupComponent.prototype.open = /** * @private * @return {?} */ function () { var _this = this; // 避免闪烁 this.renderer.setStyle(this.el, 'opacity', 0); this.visible = true; this.cdr.markForCheck(); this.beforeOpen.emit(); this.leaving = false; // 优化性能 setTimeout((/** * @return {?} */ function () { _this.renderer.removeStyle(_this.el, 'opacity'); _this.overlay && _this.openOverlay(); _this.animationSub = _this.makeAnimation('enter') .pipe(takeUntil(_this.destroy$)) .subscribe((/** * @return {?} */ function () { _this.afterOpen.emit(); })); }), 0); }; /** * @private * @return {?} */ PopupComponent.prototype.close = /** * @private * @return {?} */ function () { var _this = this; this.beforeClose.emit(); this.leaving = true; this.overlay && this.closeOverlay(); this.animationSub = this.makeAnimation('leave') .pipe(takeUntil(this.destroy$)) .subscribe((/** * @return {?} */ function () { _this.visible = false; _this.cdr.detectChanges(); _this.afterClose.emit(); _this.leaving = false; })); }; /** * @private * @return {?} */ PopupComponent.prototype.openOverlay = /** * @private * @return {?} */ function () { var _this = this; this.closeOverlay = this.overlayHelper.open({ opacity: this.overlayOpacity, zIndex: this.zIndex, getContainer: (/** * @return {?} */ function () { return _this.hostElRef.nativeElement; }), onClick: (/** * @return {?} */ function () { _this.clickOverlay.emit(); if (_this.closeOnClickOverlay && _this.visible && !_this.leaving) { _this.writeValue(false); } }) }); }; /** * @private * @param {?} state * @return {?} */ PopupComponent.prototype.makeAnimation = /** * @private * @param {?} state * @return {?} */ function (state) { /** @type {?} */ var el = this.container.nativeElement; /** @type {?} */ var animation = state === 'leave' ? this.getLeaveAnimation() : this.getEnterAnimation(); return this.animation.makeAnimation(el, animation); }; /** * @private * @return {?} */ PopupComponent.prototype.getEnterAnimation = /** * @private * @return {?} */ function () { var _a; return this.animations.enter || (_a = {}, _a[Position.center] = [style({ opacity: 0 }), animate('.3s ease', style({ opacity: 1 }))], _a[Position.top] = [style({ transform: 'translate3d(0, -100%, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, 0, 0)' }))], _a[Position.right] = [style({ transform: 'translate3d(100%, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, 0, 0)' }))], _a[Position.bottom] = [style({ transform: 'translate3d(0, 100%, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, 0, 0)' }))], _a[Position.left] = [style({ transform: 'translate3d(-100%, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, 0, 0)' }))], _a)[this.position]; }; /** * @private * @return {?} */ PopupComponent.prototype.getLeaveAnimation = /** * @private * @return {?} */ function () { var _a; return this.animations.leave || (_a = {}, _a[Position.center] = [style({ opacity: 1 }), animate('.3s ease', style({ opacity: 0 }))], _a[Position.top] = [style({ transform: 'translate3d(0, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, -100%, 0)' }))], _a[Position.right] = [style({ transform: 'translate3d(0, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(100%, 0, 0)' }))], _a[Position.bottom] = [style({ transform: 'translate3d(0, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(0, 100%, 0)' }))], _a[Position.left] = [style({ transform: 'translate3d(0, 0, 0)' }), animate('.3s ease', style({ transform: 'translate3d(-100%, 0, 0)' }))], _a)[this.position]; }; PopupComponent.decorators = [ { type: Component, args: [{ selector: 'ngx-popup', template: "<div class=\"ngx-popup-wrapper is-{{ position }}\" [hidden]=\"!visible\" [style.zIndex]=\"zIndex\">\n <div class=\"ngx-popup\" #container [ngClass]=\"externalClass\">\n <ng-content></ng-content>\n </div>\n</div>\n", encapsulation: ViewEncapsulation.None, changeDetection: ChangeDetectionStrategy.OnPush, providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef((/** * @return {?} */ function () { return PopupComponent; })), multi: true } ], styles: [".ngx-popup-wrapper{position:fixed;top:0;right:0;bottom:0;left:0;display:flex;pointer-events:none}.ngx-popup-wrapper[hidden]{display:none}.ngx-popup-wrapper .ngx-popup{pointer-events:auto}.ngx-popup-wrapper.is-center{align-items:center;justify-content:center}.ngx-popup-wrapper.is-top{align-items:flex-start;justify-content:center}.ngx-popup-wrapper.is-top .ngx-popup{width:100%}.ngx-popup-wrapper.is-right{align-items:center;justify-content:flex-end}.ngx-popup-wrapper.is-right .ngx-popup{height:100%}.ngx-popup-wrapper.is-bottom{align-items:flex-end;justify-content:center}.ngx-popup-wrapper.is-bottom .ngx-popup{width:100%}.ngx-popup-wrapper.is-left{align-items:center;justify-content:flex-start}.ngx-popup-wrapper.is-left .ngx-popup{height:100%}"] }] } ]; /** @nocollapse */ PopupComponent.ctorParameters = function () { return [ { type: ElementRef }, { type: ChangeDetectorRef }, { type: Renderer2 }, { type: OverlayHelper }, { type: AnimationService } ]; }; PopupComponent.propDecorators = { position: [{ type: Input }], animations: [{ type: Input }], overlay: [{ type: Input }], overlayOpacity: [{ type: Input }], closeOnClickOverlay: [{ type: Input }], zIndex: [{ type: Input }], externalClass: [{ type: Input }], clickOverlay: [{ type: Output }], beforeOpen: [{ type: Output }], afterOpen: [{ type: Output }], beforeClose: [{ type: Output }], afterClose: [{ type: Output }], container: [{ type: ViewChild, args: ['container', { static: false },] }] }; return PopupComponent; }()); if (false) { /** @type {?} */ PopupComponent.prototype.position; /** * 弹窗位置 * @type {?} */ PopupComponent.prototype.animations; /** * 自定义动画 * @type {?} */ PopupComponent.prototype.overlay; /** * 是否显示蒙版 * @type {?} */ PopupComponent.prototype.overlayOpacity; /** * 蒙版透明度 0~1 * @type {?} */ PopupComponent.prototype.closeOnClickOverlay; /** * 是否允许点击蒙版时自动关闭弹框 * @type {?} */ PopupComponent.prototype.zIndex; /** * 同 css z-index * @type {?} */ PopupComponent.prototype.externalClass; /** * 自定义类名 * @type {?} */ PopupComponent.prototype.clickOverlay; /** * 点击蒙版时触发 * @type {?} */ PopupComponent.prototype.beforeOpen; /** * 打开之前触发(还未执行进场动画) * @type {?} */ PopupComponent.prototype.afterOpen; /** * 打开之后触发(进场动画执行完毕) * @type {?} */ PopupComponent.prototype.beforeClose; /** * 关闭之前触发(还未执行离场动画) * @type {?} */ PopupComponent.prototype.afterClose; /** * 关闭之后触发(离场动画执行完毕) * @type {?} * @private */ PopupComponent.prototype.container; /** @type {?} */ PopupComponent.prototype.visible; /** * @type {?} * @private */ PopupComponent.prototype.destroy$; /** * @type {?} * @private */ PopupComponent.prototype.animationSub; /** * @type {?} * @private */ PopupComponent.prototype.dirty; /** * @type {?} * @private */ PopupComponent.prototype.leaving; /** * @type {?} * @private */ PopupComponent.prototype.onChange; /** * @type {?} * @private */ PopupComponent.prototype.closeOverlay; /** * @type {?} * @private */ PopupComponent.prototype.hostElRef; /** * @type {?} * @private */ PopupComponent.prototype.cdr; /** * @type {?} * @private */ PopupComponent.prototype.renderer; /** * @type {?} * @private */ PopupComponent.prototype.overlayHelper; /** * @type {?} * @private */ PopupComponent.prototype.animation; } /** * @fileoverview added by tsickle * Generated from: lib/popup.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var PopupModule = /** @class */ (function () { function PopupModule() { } PopupModule.decorators = [ { type: NgModule, args: [{ declarations: [OverlayComponent, PopupComponent], imports: [CommonModule, FormsModule], exports: [CommonModule, FormsModule, PopupComponent], entryComponents: [OverlayComponent] },] } ]; return PopupModule; }()); /** * @fileoverview added by tsickle * Generated from: public-api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: ciri-ngx-popup.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { PopupComponent, PopupModule, Position, OverlayHelper as ɵa, AnimationService as ɵb, OverlayComponent as ɵc }; //# sourceMappingURL=ciri-ngx-popup.js.map