ngx-bootstrap
Version:
Native Angular Bootstrap Components
157 lines • 6.52 kB
JavaScript
import { Injectable, EventEmitter } from '@angular/core';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
import { ModalBackdropComponent } from './modal-backdrop.component';
import { ModalContainerComponent } from './modal-container.component';
import { BsModalRef, ClassName, modalConfigDefaults, ModalOptions, TransitionDurations } from './modal-options.class';
var BsModalService = (function () {
function BsModalService(clf) {
this.clf = clf;
// constructor props
this.config = modalConfigDefaults;
this.onShow = new EventEmitter();
this.onShown = new EventEmitter();
this.onHide = new EventEmitter();
this.onHidden = new EventEmitter();
this.isBodyOverflowing = false;
this.originalBodyPadding = 0;
this.scrollbarWidth = 0;
this.modalsCount = 0;
this.lastDismissReason = '';
this.loaders = [];
this._backdropLoader = this.clf.createLoader(null, null, null);
}
/** Shows a modal */
BsModalService.prototype.show = function (content, config) {
this.modalsCount++;
this._createLoaders();
this.config = Object.assign({}, modalConfigDefaults, config);
this._showBackdrop();
this.lastDismissReason = null;
return this._showModal(content);
};
BsModalService.prototype.hide = function (level) {
var _this = this;
if (this.modalsCount === 1) {
this._hideBackdrop();
this.resetScrollbar();
}
this.modalsCount = this.modalsCount >= 1 ? this.modalsCount - 1 : 0;
setTimeout(function () {
_this._hideModal(level);
_this.removeLoaders(level);
}, this.config.animated ? TransitionDurations.BACKDROP : 0);
};
BsModalService.prototype._showBackdrop = function () {
var isBackdropEnabled = this.config.backdrop || this.config.backdrop === 'static';
var isBackdropInDOM = !this.backdropRef || !this.backdropRef.instance.isShown;
if (this.modalsCount === 1) {
this.removeBackdrop();
if (isBackdropEnabled && isBackdropInDOM) {
this._backdropLoader
.attach(ModalBackdropComponent)
.to('body')
.show({ isAnimated: this.config.animated });
this.backdropRef = this._backdropLoader._componentRef;
}
}
};
BsModalService.prototype._hideBackdrop = function () {
var _this = this;
if (!this.backdropRef) {
return;
}
this.backdropRef.instance.isShown = false;
var duration = this.config.animated ? TransitionDurations.BACKDROP : 0;
setTimeout(function () { return _this.removeBackdrop(); }, duration);
};
BsModalService.prototype._showModal = function (content) {
var modalLoader = this.loaders[this.loaders.length - 1];
var bsModalRef = new BsModalRef();
var modalContainerRef = modalLoader
.provide({ provide: ModalOptions, useValue: this.config })
.provide({ provide: BsModalRef, useValue: bsModalRef })
.attach(ModalContainerComponent)
.to('body')
.show({ content: content, isAnimated: this.config.animated });
modalContainerRef.instance.level = this.getModalsCount();
bsModalRef.hide = function () {
modalContainerRef.instance.hide();
};
bsModalRef.content = modalLoader.getInnerComponent() || null;
return bsModalRef;
};
BsModalService.prototype._hideModal = function (level) {
var modalLoader = this.loaders[level - 1];
if (modalLoader) {
modalLoader.hide();
}
};
BsModalService.prototype.getModalsCount = function () {
return this.modalsCount;
};
BsModalService.prototype.setDismissReason = function (reason) {
this.lastDismissReason = reason;
};
BsModalService.prototype.removeBackdrop = function () {
this._backdropLoader.hide();
this.backdropRef = null;
};
/** AFTER PR MERGE MODAL.COMPONENT WILL BE USING THIS CODE*/
/** Scroll bar tricks */
/** @internal */
BsModalService.prototype.checkScrollbar = function () {
this.isBodyOverflowing = document.body.clientWidth < window.innerWidth;
this.scrollbarWidth = this.getScrollbarWidth();
};
BsModalService.prototype.setScrollbar = function () {
if (!document) {
return;
}
this.originalBodyPadding = parseInt(window.getComputedStyle(document.body).getPropertyValue('padding-right') || '0', 10);
if (this.isBodyOverflowing) {
document.body.style.paddingRight = this.originalBodyPadding + this.scrollbarWidth + "px";
}
};
BsModalService.prototype.resetScrollbar = function () {
document.body.style.paddingRight = this.originalBodyPadding + 'px';
};
// thx d.walsh
BsModalService.prototype.getScrollbarWidth = function () {
var scrollDiv = document.createElement('div');
scrollDiv.className = ClassName.SCROLLBAR_MEASURER;
document.body.appendChild(scrollDiv);
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
document.body.removeChild(scrollDiv);
return scrollbarWidth;
};
BsModalService.prototype._createLoaders = function () {
var loader = this.clf.createLoader(null, null, null);
this.copyEvent(loader.onBeforeShow, this.onShow);
this.copyEvent(loader.onShown, this.onShown);
this.copyEvent(loader.onBeforeHide, this.onHide);
this.copyEvent(loader.onHidden, this.onHidden);
this.loaders.push(loader);
};
BsModalService.prototype.removeLoaders = function (level) {
this.loaders.splice(level - 1, 1);
this.loaders.forEach(function (loader, i) {
loader.instance.level = i + 1;
});
};
BsModalService.prototype.copyEvent = function (from, to) {
var _this = this;
from.subscribe(function () {
to.emit(_this.lastDismissReason);
});
};
BsModalService.decorators = [
{ type: Injectable },
];
/** @nocollapse */
BsModalService.ctorParameters = function () { return [
{ type: ComponentLoaderFactory, },
]; };
return BsModalService;
}());
export { BsModalService };
//# sourceMappingURL=bs-modal.service.js.map