|
| 1 | +import {QueryList} from '@angular/core'; |
| 2 | +import {ListKeyManager} from '../core/a11y/list-key-manager'; |
| 3 | +import {MdBasicChip} from './chip'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Manages keyboard events for the chip list and its chips. When instantiated |
| 7 | + * with a QueryList of MdBasicChip (i.e. any chip), it will ensure focus and |
| 8 | + * keyboard navigation are properly handled. |
| 9 | + */ |
| 10 | +export class ChipListKeyManager extends ListKeyManager { |
| 11 | + private _subscribed: MdBasicChip[] = []; |
| 12 | + |
| 13 | + constructor(private _chips: QueryList<MdBasicChip>) { |
| 14 | + super(_chips); |
| 15 | + |
| 16 | + // Go ahead and subscribe all of the initial chips |
| 17 | + this.subscribeChips(this._chips); |
| 18 | + |
| 19 | + // When the list changes, re-subscribe |
| 20 | + this._chips.changes.subscribe((chips: QueryList<MdBasicChip>) => { |
| 21 | + this.subscribeChips(chips); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Iterate through the list of chips and add them to our list of |
| 27 | + * subscribed chips. |
| 28 | + * |
| 29 | + * @param chips The list of chips to be subscribed. |
| 30 | + */ |
| 31 | + protected subscribeChips(chips: QueryList<MdBasicChip>): void { |
| 32 | + chips.forEach((chip: MdBasicChip) => { |
| 33 | + this.addChip(chip); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Add a specific chip to our subscribed list. If the chip has |
| 39 | + * already been subscribed, this ensures it is only subscribed |
| 40 | + * once. |
| 41 | + * |
| 42 | + * @param chip The chip to be subscribed (or checked for existing |
| 43 | + * subscription). |
| 44 | + */ |
| 45 | + protected addChip(chip: MdBasicChip) { |
| 46 | + // If we've already been subscribed to a parent, do nothing |
| 47 | + if (this._subscribed.indexOf(chip) > -1) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Watch for focus events outside of the keyboard navigation |
| 52 | + chip.didfocus.subscribe(() => { |
| 53 | + let chipIndex: number = this._chips.toArray().indexOf(chip); |
| 54 | + |
| 55 | + if (this.isValidIndex(chipIndex)) { |
| 56 | + this.setFocus(chipIndex, false); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + // On destroy, remove the item from our list, and check focus |
| 61 | + chip.destroy.subscribe(() => { |
| 62 | + let chipIndex: number = this._chips.toArray().indexOf(chip); |
| 63 | + |
| 64 | + if (this.isValidIndex(chipIndex)) { |
| 65 | + // Check whether the chip is the last item |
| 66 | + if (chipIndex < this._chips.length - 1) { |
| 67 | + this.setFocus(chipIndex); |
| 68 | + } else if (chipIndex - 1 >= 0) { |
| 69 | + this.setFocus(chipIndex - 1); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + this._subscribed.splice(this._subscribed.indexOf(chip), 1); |
| 74 | + chip.destroy.unsubscribe(); |
| 75 | + }); |
| 76 | + |
| 77 | + this._subscribed.push(chip); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Utility to ensure all indexes are valid. |
| 82 | + * |
| 83 | + * @param index The index to be checked. |
| 84 | + * @returns {boolean} True if the index is valid for our list of chips. |
| 85 | + */ |
| 86 | + private isValidIndex(index: number): boolean { |
| 87 | + return index >= 0 && index < this._chips.length; |
| 88 | + } |
| 89 | +} |
0 commit comments