UNPKG

ngx-bootstrap

Version:
303 lines 11.1 kB
import { Directive, ElementRef, EventEmitter, Input, Output, Renderer2, ViewContainerRef } from '@angular/core'; import { filter } from 'rxjs/operators'; import { ComponentLoaderFactory } from 'ngx-bootstrap/component-loader'; import { BsDropdownConfig } from './bs-dropdown.config'; import { BsDropdownContainerComponent } from './bs-dropdown-container.component'; import { BsDropdownState } from './bs-dropdown.state'; import { isBs3 } from 'ngx-bootstrap/utils'; import { AnimationBuilder } from '@angular/animations'; import { dropdownAnimation } from './dropdown-animations'; export class BsDropdownDirective { constructor(_elementRef, _renderer, _viewContainerRef, _cis, _state, _config, _builder) { this._elementRef = _elementRef; this._renderer = _renderer; this._viewContainerRef = _viewContainerRef; this._cis = _cis; this._state = _state; this._config = _config; /** * This attribute indicates that the dropdown should be opened upwards */ this.dropup = false; // todo: move to component loader this._isInlineOpen = false; this._isDisabled = false; this._subscriptions = []; this._isInited = false; // set initial dropdown state from config this._state.autoClose = this._config.autoClose; this._state.insideClick = this._config.insideClick; this._state.isAnimated = this._config.isAnimated; this._factoryDropDownAnimation = _builder.build(dropdownAnimation); // create dropdown component loader this._dropdown = this._cis .createLoader(this._elementRef, this._viewContainerRef, this._renderer) .provide({ provide: BsDropdownState, useValue: this._state }); this.onShown = this._dropdown.onShown; this.onHidden = this._dropdown.onHidden; this.isOpenChange = this._state.isOpenChange; } /** * Indicates that dropdown will be closed on item or document click, * and after pressing ESC */ set autoClose(value) { this._state.autoClose = value; } get autoClose() { return this._state.autoClose; } /** * Indicates that dropdown will be animated */ set isAnimated(value) { this._state.isAnimated = value; } get isAnimated() { return this._state.isAnimated; } /** * This attribute indicates that the dropdown shouldn't close on inside click when autoClose is set to true */ set insideClick(value) { this._state.insideClick = value; } get insideClick() { return this._state.insideClick; } /** * Disables dropdown toggle and hides dropdown menu if opened */ set isDisabled(value) { this._isDisabled = value; this._state.isDisabledChange.emit(value); if (value) { this.hide(); } } get isDisabled() { return this._isDisabled; } /** * Returns whether or not the popover is currently being shown */ get isOpen() { if (this._showInline) { return this._isInlineOpen; } return this._dropdown.isShown; } set isOpen(value) { if (value) { this.show(); } else { this.hide(); } } get isBs4() { return !isBs3(); } get _showInline() { return !this.container; } ngOnInit() { // fix: seems there are an issue with `routerLinkActive` // which result in duplicated call ngOnInit without call to ngOnDestroy // read more: https://github.com/valor-software/ngx-bootstrap/issues/1885 if (this._isInited) { return; } this._isInited = true; // attach DOM listeners this._dropdown.listen({ // because of dropdown inline mode outsideClick: false, triggers: this.triggers, show: () => this.show() }); // toggle visibility on toggle element click this._subscriptions.push(this._state.toggleClick.subscribe((value) => this.toggle(value))); // hide dropdown if set disabled while opened this._subscriptions.push(this._state.isDisabledChange .pipe(filter((value) => value)) .subscribe(( /*value: boolean*/) => this.hide())); } /** * Opens an element’s popover. This is considered a “manual” triggering of * the popover. */ show() { if (this.isOpen || this.isDisabled) { return; } if (this._showInline) { if (!this._inlinedMenu) { this._state.dropdownMenu.then((dropdownMenu) => { this._dropdown.attachInline(dropdownMenu.viewContainer, dropdownMenu.templateRef); this._inlinedMenu = this._dropdown._inlineViewRef; this.addBs4Polyfills(); if (this._inlinedMenu) { this._renderer.addClass(this._inlinedMenu.rootNodes[0].parentNode, 'open'); } this.playAnimation(); }) // swallow errors .catch(); } this.addBs4Polyfills(); this._isInlineOpen = true; this.onShown.emit(true); this._state.isOpenChange.emit(true); this.playAnimation(); return; } this._state.dropdownMenu.then(dropdownMenu => { // check direction in which dropdown should be opened const _dropup = this.dropup || (typeof this.dropup !== 'undefined' && this.dropup); this._state.direction = _dropup ? 'up' : 'down'; const _placement = this.placement || (_dropup ? 'top start' : 'bottom start'); // show dropdown this._dropdown .attach(BsDropdownContainerComponent) .to(this.container) .position({ attachment: _placement }) .show({ content: dropdownMenu.templateRef, placement: _placement }); this._state.isOpenChange.emit(true); }) // swallow error .catch(); } /** * Closes an element’s popover. This is considered a “manual” triggering of * the popover. */ hide() { if (!this.isOpen) { return; } if (this._showInline) { this.removeShowClass(); this.removeDropupStyles(); this._isInlineOpen = false; this.onHidden.emit(true); } else { this._dropdown.hide(); } this._state.isOpenChange.emit(false); } /** * Toggles an element’s popover. This is considered a “manual” triggering of * the popover. With parameter <code>true</code> allows toggling, with parameter <code>false</code> * only hides opened dropdown. Parameter usage will be removed in ngx-bootstrap v3 */ toggle(value) { if (this.isOpen || !value) { return this.hide(); } return this.show(); } /** @internal */ _contains(event) { // todo: valorkin fix typings return this._elementRef.nativeElement.contains(event.target) || (this._dropdown.instance && this._dropdown.instance._contains(event.target)); } ngOnDestroy() { // clean up subscriptions and destroy dropdown for (const sub of this._subscriptions) { sub.unsubscribe(); } this._dropdown.dispose(); } addBs4Polyfills() { if (!isBs3()) { this.addShowClass(); this.checkRightAlignment(); this.addDropupStyles(); } } playAnimation() { if (this._state.isAnimated && this._inlinedMenu) { setTimeout(() => { if (this._inlinedMenu) { this._factoryDropDownAnimation.create(this._inlinedMenu.rootNodes[0]).play(); } }); } } addShowClass() { if (this._inlinedMenu && this._inlinedMenu.rootNodes[0]) { this._renderer.addClass(this._inlinedMenu.rootNodes[0], 'show'); } } removeShowClass() { if (this._inlinedMenu && this._inlinedMenu.rootNodes[0]) { this._renderer.removeClass(this._inlinedMenu.rootNodes[0], 'show'); } } checkRightAlignment() { if (this._inlinedMenu && this._inlinedMenu.rootNodes[0]) { const isRightAligned = this._inlinedMenu.rootNodes[0].classList.contains('dropdown-menu-right'); this._renderer.setStyle(this._inlinedMenu.rootNodes[0], 'left', isRightAligned ? 'auto' : '0'); this._renderer.setStyle(this._inlinedMenu.rootNodes[0], 'right', isRightAligned ? '0' : 'auto'); } } addDropupStyles() { if (this._inlinedMenu && this._inlinedMenu.rootNodes[0]) { // a little hack to not break support of bootstrap 4 beta this._renderer.setStyle(this._inlinedMenu.rootNodes[0], 'top', this.dropup ? 'auto' : '100%'); this._renderer.setStyle(this._inlinedMenu.rootNodes[0], 'transform', this.dropup ? 'translateY(-101%)' : 'translateY(0)'); this._renderer.setStyle(this._inlinedMenu.rootNodes[0], 'bottom', 'auto'); } } removeDropupStyles() { if (this._inlinedMenu && this._inlinedMenu.rootNodes[0]) { this._renderer.removeStyle(this._inlinedMenu.rootNodes[0], 'top'); this._renderer.removeStyle(this._inlinedMenu.rootNodes[0], 'transform'); this._renderer.removeStyle(this._inlinedMenu.rootNodes[0], 'bottom'); } } } BsDropdownDirective.decorators = [ { type: Directive, args: [{ selector: '[bsDropdown], [dropdown]', exportAs: 'bs-dropdown', providers: [BsDropdownState], // eslint-disable-next-line @angular-eslint/no-host-metadata-property host: { '[class.dropup]': 'dropup', '[class.open]': 'isOpen', '[class.show]': 'isOpen && isBs4' } },] } ]; BsDropdownDirective.ctorParameters = () => [ { type: ElementRef }, { type: Renderer2 }, { type: ViewContainerRef }, { type: ComponentLoaderFactory }, { type: BsDropdownState }, { type: BsDropdownConfig }, { type: AnimationBuilder } ]; BsDropdownDirective.propDecorators = { placement: [{ type: Input }], triggers: [{ type: Input }], container: [{ type: Input }], dropup: [{ type: Input }], autoClose: [{ type: Input }], isAnimated: [{ type: Input }], insideClick: [{ type: Input }], isDisabled: [{ type: Input }], isOpen: [{ type: Input }], isOpenChange: [{ type: Output }], onShown: [{ type: Output }], onHidden: [{ type: Output }] }; //# sourceMappingURL=bs-dropdown.directive.js.map