/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { Inject, Injectable, InjectionToken, Optional, SkipSelf } from '@angular/core'; import { Subject } from 'rxjs'; import { debounceTime, distinctUntilChanged, startWith } from 'rxjs/operators'; import * as i0 from "@angular/core"; /** Injection token used for an implementation of MenuStack. */ export const MENU_STACK = new InjectionToken('cdk-menu-stack'); /** Provider that provides the parent menu stack, or a new menu stack if there is no parent one. */ export const PARENT_OR_NEW_MENU_STACK_PROVIDER = { provide: MENU_STACK, deps: [[new Optional(), new SkipSelf(), new Inject(MENU_STACK)]], useFactory: (parentMenuStack) => parentMenuStack || new MenuStack(), }; /** Provider that provides the parent menu stack, or a new inline menu stack if there is no parent one. */ export const PARENT_OR_NEW_INLINE_MENU_STACK_PROVIDER = (orientation) => ({ provide: MENU_STACK, deps: [[new Optional(), new SkipSelf(), new Inject(MENU_STACK)]], useFactory: (parentMenuStack) => parentMenuStack || MenuStack.inline(orientation), }); /** The next available menu stack ID. */ let nextId = 0; /** * MenuStack allows subscribers to listen for close events (when a MenuStackItem is popped off * of the stack) in order to perform closing actions. Upon the MenuStack being empty it emits * from the `empty` observable specifying the next focus action which the listener should perform * as requested by the closer. */ export class MenuStack { constructor() { /** The ID of this menu stack. */ this.id = `${nextId++}`; /** All MenuStackItems tracked by this MenuStack. */ this._elements = []; /** Emits the element which was popped off of the stack when requested by a closer. */ this._close = new Subject(); /** Emits once the MenuStack has become empty after popping off elements. */ this._empty = new Subject(); /** Emits whether any menu in the menu stack has focus. */ this._hasFocus = new Subject(); /** Observable which emits the MenuStackItem which has been requested to close. */ this.closed = this._close; /** Observable which emits whether any menu in the menu stack has focus. */ this.hasFocus = this._hasFocus.pipe(startWith(false), debounceTime(0), distinctUntilChanged()); /** * Observable which emits when the MenuStack is empty after popping off the last element. It * emits a FocusNext event which specifies the action the closer has requested the listener * perform. */ this.emptied = this._empty; /** * Whether the inline menu associated with this menu stack is vertical or horizontal. * `null` indicates there is no inline menu associated with this menu stack. */ this._inlineMenuOrientation = null; } /** Creates a menu stack that originates from an inline menu. */ static inline(orientation) { const stack = new MenuStack(); stack._inlineMenuOrientation = orientation; return stack; } /** * Adds an item to the menu stack. * @param menu the MenuStackItem to put on the stack. */ push(menu) { this._elements.push(menu); } /** * Pop items off of the stack up to and including `lastItem` and emit each on the close * observable. If the stack is empty or `lastItem` is not on the stack it does nothing. * @param lastItem the last item to pop off the stack. * @param options Options that configure behavior on close. */ close(lastItem, options) { const { focusNextOnEmpty, focusParentTrigger } = { ...options }; if (this._elements.indexOf(lastItem) >= 0) { let poppedElement; do { poppedElement = this._elements.pop(); this._close.next({ item: poppedElement, focusParentTrigger }); } while (poppedElement !== lastItem); if (this.isEmpty()) { this._empty.next(focusNextOnEmpty); } } } /** * Pop items off of the stack up to but excluding `lastItem` and emit each on the close * observable. If the stack is empty or `lastItem` is not on the stack it does nothing. * @param lastItem the element which should be left on the stack * @return whether or not an item was removed from the stack */ closeSubMenuOf(lastItem) { let removed = false; if (this._elements.indexOf(lastItem) >= 0) { removed = this.peek() !== lastItem; while (this.peek() !== lastItem) { this._close.next({ item: this._elements.pop() }); } } return removed; } /** * Pop off all MenuStackItems and emit each one on the `close` observable one by one. * @param options Options that configure behavior on close. */ closeAll(options) { const { focusNextOnEmpty, focusParentTrigger } = { ...options }; if (!this.isEmpty()) { while (!this.isEmpty()) { const menuStackItem = this._elements.pop(); if (menuStackItem) { this._close.next({ item: menuStackItem, focusParentTrigger }); } } this._empty.next(focusNextOnEmpty); } } /** Return true if this stack is empty. */ isEmpty() { return !this._elements.length; } /** Return the length of the stack. */ length() { return this._elements.length; } /** Get the top most element on the stack. */ peek() { return this._elements[this._elements.length - 1]; } /** Whether the menu stack is associated with an inline menu. */ hasInlineMenu() { return this._inlineMenuOrientation != null; } /** The orientation of the associated inline menu. */ inlineMenuOrientation() { return this._inlineMenuOrientation; } /** Sets whether the menu stack contains the focused element. */ setHasFocus(hasFocus) { this._hasFocus.next(hasFocus); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.1", ngImport: i0, type: MenuStack, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "16.1.1", ngImport: i0, type: MenuStack }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.1", ngImport: i0, type: MenuStack, decorators: [{ type: Injectable }] }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVudS1zdGFjay5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uLy4uLy4uLy4uL3NyYy9jZGsvbWVudS9tZW51LXN0YWNrLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVILE9BQU8sRUFBQyxNQUFNLEVBQUUsVUFBVSxFQUFFLGNBQWMsRUFBRSxRQUFRLEVBQUUsUUFBUSxFQUFDLE1BQU0sZUFBZSxDQUFDO0FBQ3JGLE9BQU8sRUFBYSxPQUFPLEVBQUMsTUFBTSxNQUFNLENBQUM7QUFDekMsT0FBTyxFQUFDLFlBQVksRUFBRSxvQkFBb0IsRUFBRSxTQUFTLEVBQUMsTUFBTSxnQkFBZ0IsQ0FBQzs7QUFlN0UsK0RBQStEO0FBQy9ELE1BQU0sQ0FBQyxNQUFNLFVBQVUsR0FBRyxJQUFJLGNBQWMsQ0FBWSxnQkFBZ0IsQ0FBQyxDQUFDO0FBRTFFLG1HQUFtRztBQUNuRyxNQUFNLENBQUMsTUFBTSxpQ0FBaUMsR0FBRztJQUMvQyxPQUFPLEVBQUUsVUFBVTtJQUNuQixJQUFJLEVBQUUsQ0FBQyxDQUFDLElBQUksUUFBUSxFQUFFLEVBQUUsSUFBSSxRQUFRLEVBQUUsRUFBRSxJQUFJLE1BQU0sQ0FBQyxVQUFVLENBQU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