UNPKG

modalicious

Version:
381 lines (362 loc) 18.6 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common'), require('@angular/animations')) : typeof define === 'function' && define.amd ? define('modalicious', ['exports', '@angular/core', '@angular/common', '@angular/animations'], factory) : (global = global || self, factory(global.modalicious = {}, global.ng.core, global.ng.common, global.ng.animations)); }(this, function (exports, core, common, animations) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ function __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __metadata(metadataKey, metadataValue) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue); } function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } var ModalHostDirective = /** @class */ (function () { function ModalHostDirective(viewContainerRef) { this.viewContainerRef = viewContainerRef; } ModalHostDirective = __decorate([ core.Directive({ selector: '[mod-modal-host]' }), __metadata("design:paramtypes", [core.ViewContainerRef]) ], ModalHostDirective); return ModalHostDirective; }()); var ModalInstanceService = /** @class */ (function () { function ModalInstanceService(cancel, resolve) { this.cancel = cancel; this.resolve = resolve; } return ModalInstanceService; }()); var ModalContainerComponent = /** @class */ (function () { function ModalContainerComponent(modalInstanceService) { this.modalInstanceService = modalInstanceService; this.useFixedPosition = false; this.showContent = false; this.showBackdrop = true; } Object.defineProperty(ModalContainerComponent.prototype, "backdrop", { get: function () { return this.showBackdrop ? 'active' : 'inactive'; }, enumerable: true, configurable: true }); Object.defineProperty(ModalContainerComponent.prototype, "content", { get: function () { return this.showContent ? 'visible' : 'hidden'; }, enumerable: true, configurable: true }); ModalContainerComponent.prototype.onKeyDown = function (event) { if (event.key === 'Escape') { this.modalInstanceService.cancel('Escape key hit'); event.preventDefault(); } }; Object.defineProperty(ModalContainerComponent.prototype, "positionStyle", { get: function () { return this.useFixedPosition ? 'fixed' : 'absolute'; }, enumerable: true, configurable: true }); ModalContainerComponent.prototype.ngAfterViewInit = function () { var _this = this; setTimeout(function () { _this.showContent = true; }, 50); }; ModalContainerComponent.prototype.hide = function () { var _this = this; return new Promise(function (resolve) { _this.showBackdrop = false; setTimeout(function () { resolve(); }, 200); }); }; __decorate([ core.Input(), __metadata("design:type", Boolean) ], ModalContainerComponent.prototype, "useFixedPosition", void 0); __decorate([ core.ViewChild(ModalHostDirective, { static: true }), __metadata("design:type", ModalHostDirective) ], ModalContainerComponent.prototype, "modalHost", void 0); __decorate([ core.HostListener('keydown', ['$event']), __metadata("design:type", Function), __metadata("design:paramtypes", [KeyboardEvent]), __metadata("design:returntype", void 0) ], ModalContainerComponent.prototype, "onKeyDown", null); __decorate([ core.HostBinding('style.position'), __metadata("design:type", Object), __metadata("design:paramtypes", []) ], ModalContainerComponent.prototype, "positionStyle", null); ModalContainerComponent = __decorate([ core.Component({ selector: 'mod-modal-container', template: "<div class=\"mod-modal-table\" [@backdrop]=\"backdrop\">\r\n <div class=\"mod-modal-table-cell\">\r\n <div class=\"mod-modal-content\" [@content]=\"content\">\r\n <ng-template mod-modal-host></ng-template>\r\n </div>\r\n </div>\r\n</div>\r\n", animations: [ animations.trigger('backdrop', [ animations.state('inactive', animations.style({ opacity: 0 })), animations.state('active', animations.style({ backgroundColor: 'rgba(0, 0, 0, .85)', opacity: 1 })), animations.transition('void => active', [ animations.style({ backgroundColor: 'rgba(0,0,0,0)' }), animations.animate('50ms ease-in') ]), animations.transition('active => inactive', [ animations.animate('150ms ease-out') ]) ]), animations.trigger('content', [ animations.state('hidden', animations.style({ opacity: 0, transform: 'translateY(50px)' })), animations.state('visible', animations.style({ opacity: 1, transform: 'translateY(0)' })), animations.transition('hidden => visible', [ animations.animate('150ms ease-in') ]) ]) ], styles: [":host{overflow-x:hidden;overflow-y:auto;left:0;top:0;right:0;bottom:0;display:block;background-color:rgba(0,0,0,0)}:host .mod-modal-table-cell{display:table-cell;vertical-align:middle;text-align:center;padding:0}:host .mod-modal-table{width:100%;height:100%;display:table}:host .mod-modal-content{display:inline-block;width:100%}"] }), __metadata("design:paramtypes", [ModalInstanceService]) ], ModalContainerComponent); return ModalContainerComponent; }()); var noViewContainerRefMessage = "Modalicious: You must call setRootViewContainerRef on ModalService from your root component before calling ModalService.show()"; var ModalService = /** @class */ (function () { function ModalService(factoryResolver, appRef) { this.factoryResolver = factoryResolver; this.appRef = appRef; } ModalService.prototype.setModalPositioning = function (useFixed) { this.defaultToFixedPositioning = useFixed; }; ModalService.prototype.setRootViewContainerRef = function (viewContainerRef) { this.rootViewContainer = viewContainerRef; }; ModalService.prototype.show = function (componentClass, providers) { var _this = this; if (providers === void 0) { providers = []; } if (!this.rootViewContainer) throw new Error(noViewContainerRefMessage); return new Promise(function (resolve, reject) { var containerComponent; var containerFactory = _this.factoryResolver .resolveComponentFactory(ModalContainerComponent); var close = function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, containerComponent.instance.hide()]; case 1: _a.sent(); this.appRef.detachView(containerComponent.hostView); containerComponent.destroy(); return [2 /*return*/]; } }); }); }; var modalInstance = new ModalInstanceService(function (reason) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, close()]; case 1: _a.sent(); reject(reason); return [2 /*return*/]; } }); }); }, function (result) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, close()]; case 1: _a.sent(); resolve(result); return [2 /*return*/]; } }); }); }); function modalInstanceFactory() { return modalInstance; } var containerInjector = core.Injector.create({ providers: [ { provide: ModalInstanceService, useFactory: modalInstanceFactory, deps: [] }, ], parent: _this.rootViewContainer.parentInjector }); containerComponent = containerFactory .create(containerInjector); if (_this.defaultToFixedPositioning) { containerComponent.instance.useFixedPosition = true; } var modalHostView = containerComponent.instance.modalHost.viewContainerRef; var modalFactory = _this.factoryResolver .resolveComponentFactory(componentClass); var injector = core.Injector.create({ providers: __spread([ { provide: ModalInstanceService, useFactory: modalInstanceFactory, deps: [] } ], providers), parent: _this.rootViewContainer.parentInjector }); var modalComponent = modalFactory .create(injector); modalHostView.insert(modalComponent.hostView); _this.rootViewContainer.insert(containerComponent.hostView); }); }; ModalService = __decorate([ core.Injectable(), __metadata("design:paramtypes", [core.ComponentFactoryResolver, core.ApplicationRef]) ], ModalService); return ModalService; }()); var ModalCloseDirective = /** @class */ (function () { function ModalCloseDirective(modalInstance) { this.modalInstance = modalInstance; this.value = null; } ModalCloseDirective.prototype.close = function () { this.modalInstance.resolve(this.value); }; __decorate([ core.Input('mod-modal-close'), __metadata("design:type", Object) ], ModalCloseDirective.prototype, "value", void 0); __decorate([ core.HostListener('click'), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", void 0) ], ModalCloseDirective.prototype, "close", null); ModalCloseDirective = __decorate([ core.Directive({ selector: '[mod-modal-close]' }), __metadata("design:paramtypes", [ModalInstanceService]) ], ModalCloseDirective); return ModalCloseDirective; }()); var ModaliciousModule = /** @class */ (function () { function ModaliciousModule() { } ModaliciousModule_1 = ModaliciousModule; ModaliciousModule.forRoot = function () { return { ngModule: ModaliciousModule_1, providers: [ModalService] }; }; var ModaliciousModule_1; ModaliciousModule = ModaliciousModule_1 = __decorate([ core.NgModule({ imports: [ common.CommonModule ], entryComponents: [ ModalContainerComponent ], declarations: [ModalContainerComponent, ModalHostDirective, ModalCloseDirective], exports: [ ModalCloseDirective ] }) ], ModaliciousModule); return ModaliciousModule; }()); exports.ModalCloseDirective = ModalCloseDirective; exports.ModalInstanceService = ModalInstanceService; exports.ModalService = ModalService; exports.ModaliciousModule = ModaliciousModule; exports.ɵa = ModalContainerComponent; exports.ɵb = ModalHostDirective; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=modalicious.umd.js.map