ngx-bootstrap
Version:
Native Angular Bootstrap Components
174 lines • 7.21 kB
JavaScript
import { Injectable, EventEmitter, RendererFactory2 } from '@angular/core';
import { ComponentLoaderFactory } from '../component-loader/component-loader.factory';
import { ModalBackdropComponent } from './modal-backdrop.component';
import { ModalContainerComponent } from './modal-container.component';
import { CLASS_NAME, modalConfigDefaults, ModalOptions, TRANSITION_DURATIONS } from './modal-options.class';
import { BsModalRef } from './bs-modal-ref.service';
var BsModalService = /** @class */ (function () {
function BsModalService(rendererFactory, 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);
this._renderer = rendererFactory.createRenderer(null, null);
}
/** Shows a modal */
/** Shows a modal */
BsModalService.prototype.show = /** Shows a modal */
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 ? TRANSITION_DURATIONS.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 ? TRANSITION_DURATIONS.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, initialState: this.config.initialState, bsModalService: this });
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 */
/** AFTER PR MERGE MODAL.COMPONENT WILL BE USING THIS CODE */
/** Scroll bar tricks */
/** @internal */
BsModalService.prototype.checkScrollbar = /** AFTER PR MERGE MODAL.COMPONENT WILL BE USING THIS CODE */
/** Scroll bar tricks */
/** @internal */
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
// thx d.walsh
BsModalService.prototype.getScrollbarWidth =
// thx d.walsh
function () {
var scrollDiv = this._renderer.createElement('div');
this._renderer.addClass(scrollDiv, CLASS_NAME.SCROLLBAR_MEASURER);
this._renderer.appendChild(document.body, scrollDiv);
var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
this._renderer.removeChild(document.body, 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: RendererFactory2, },
{ type: ComponentLoaderFactory, },
]; };
return BsModalService;
}());
export { BsModalService };
//# sourceMappingURL=bs-modal.service.js.map