|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {Injectable} from '@angular/core'; |
| 10 | +import {Observable, Subject, timer} from 'rxjs'; |
| 11 | +import {audit, distinctUntilChanged, filter, map, share} from 'rxjs/operators'; |
| 12 | + |
| 13 | +import {CELL_SELECTOR, ROW_SELECTOR} from './constants'; |
| 14 | +import {closest} from './polyfill'; |
| 15 | + |
| 16 | +/** The delay between mouse out events and hiding hover content. */ |
| 17 | +const DEFAULT_MOUSE_OUT_DELAY_MS = 30; |
| 18 | + |
| 19 | +/** |
| 20 | + * Service for sharing delegated events and state for triggering table edits. |
| 21 | + */ |
| 22 | +@Injectable() |
| 23 | +export class EditEventDispatcher { |
| 24 | + /** A subject that indicates which table cell is currently editing. */ |
| 25 | + readonly editing = new Subject<Element|null>(); |
| 26 | + |
| 27 | + /** A subject that indicates which table row is currently hovered. */ |
| 28 | + readonly hovering = new Subject<Element|null>(); |
| 29 | + |
| 30 | + /** A subject that emits mouse move events for table rows. */ |
| 31 | + readonly mouseMove = new Subject<Element|null>(); |
| 32 | + |
| 33 | + /** The table cell that has an active edit lens (or null). */ |
| 34 | + private _currentlyEditing: Element|null = null; |
| 35 | + |
| 36 | + private readonly _hoveringDistinct = this.hovering.pipe(distinctUntilChanged(), share()); |
| 37 | + private readonly _editingDistinct = this.editing.pipe(distinctUntilChanged(), share()); |
| 38 | + |
| 39 | + constructor() { |
| 40 | + this._editingDistinct.subscribe(cell => { |
| 41 | + this._currentlyEditing = cell; |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets an Observable that emits true when the specified element's cell |
| 47 | + * is editing and false when not. |
| 48 | + */ |
| 49 | + editingCell(element: Element|EventTarget): Observable<boolean> { |
| 50 | + let cell: Element|null = null; |
| 51 | + |
| 52 | + return this._editingDistinct.pipe( |
| 53 | + map(editCell => editCell === (cell || (cell = closest(element, CELL_SELECTOR)))), |
| 54 | + distinctUntilChanged(), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Stops editing for the specified cell. If the specified cell is not the current |
| 60 | + * edit cell, does nothing. |
| 61 | + */ |
| 62 | + doneEditingCell(element: Element|EventTarget): void { |
| 63 | + const cell = closest(element, CELL_SELECTOR); |
| 64 | + |
| 65 | + if (this._currentlyEditing === cell) { |
| 66 | + this.editing.next(null); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Gets an Observable that emits true when the specified element's row |
| 72 | + * is being hovered over and false when not. Hovering is defined as when |
| 73 | + * the mouse has momentarily stopped moving over the cell. |
| 74 | + */ |
| 75 | + hoveringOnRow(element: Element|EventTarget): Observable<boolean> { |
| 76 | + let row: Element|null = null; |
| 77 | + |
| 78 | + return this._hoveringDistinct.pipe( |
| 79 | + map(hoveredRow => hoveredRow === (row || (row = closest(element, ROW_SELECTOR)))), |
| 80 | + audit( |
| 81 | + (hovering) => hovering ? this.mouseMove.pipe(filter(hoveredRow => hoveredRow === row)) : |
| 82 | + timer(DEFAULT_MOUSE_OUT_DELAY_MS)), |
| 83 | + distinctUntilChanged(), |
| 84 | + ); |
| 85 | + } |
| 86 | +} |
0 commit comments