|
| 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 * as compiler from '@angular/compiler'; |
| 10 | +import {TemplateMigrator, Update} from '../../template-migrator'; |
| 11 | +import {replaceStartTag, replaceEndTag, visitElements} from '../../tree-traversal'; |
| 12 | + |
| 13 | +/** Stores a mat-chip-list with the mat-chip elements nested within it. */ |
| 14 | +interface ChipMap { |
| 15 | + chipList: compiler.TmplAstElement; |
| 16 | + chips: compiler.TmplAstElement[]; |
| 17 | +} |
| 18 | + |
| 19 | +export class ChipsTemplateMigrator extends TemplateMigrator { |
| 20 | + /** Stores the mat-chip-list elements with their nested mat-chip elements. */ |
| 21 | + chipMap?: ChipMap; |
| 22 | + |
| 23 | + /** All of the ChipMaps found while parsing a template AST. */ |
| 24 | + chipMaps: ChipMap[] = []; |
| 25 | + |
| 26 | + /** Chips that are not nested within mat-chip elements. */ |
| 27 | + standaloneChips: compiler.TmplAstElement[] = []; |
| 28 | + |
| 29 | + /** Input elements that have matChipInputFor attributes. */ |
| 30 | + chipInputs: compiler.TmplAstBoundAttribute[] = []; |
| 31 | + |
| 32 | + getUpdates(ast: compiler.ParsedTemplate): Update[] { |
| 33 | + this._gatherDomData(ast); |
| 34 | + const updates: Update[] = []; |
| 35 | + this.chipMaps.forEach(chipMap => { |
| 36 | + if (this._isChipGrid(chipMap.chipList)) { |
| 37 | + updates.push(...this._buildUpdatesForChipMap(chipMap, 'mat-chip-grid', 'mat-chip-row')); |
| 38 | + return; |
| 39 | + } |
| 40 | + updates.push(...this._buildUpdatesForChipMap(chipMap, 'mat-chip-listbox', 'mat-chip-option')); |
| 41 | + }); |
| 42 | + this.standaloneChips.forEach(chip => { |
| 43 | + updates.push(...this._buildTagUpdates(chip, 'mat-chip-option')); |
| 44 | + }); |
| 45 | + return updates; |
| 46 | + } |
| 47 | + |
| 48 | + /** Traverses the AST and stores all relevant DOM data needed for building updates. */ |
| 49 | + private _gatherDomData(ast: compiler.ParsedTemplate): void { |
| 50 | + this.chipMap = undefined; |
| 51 | + this.chipMaps = []; |
| 52 | + this.standaloneChips = []; |
| 53 | + this.chipInputs = []; |
| 54 | + |
| 55 | + visitElements( |
| 56 | + ast.nodes, |
| 57 | + (node: compiler.TmplAstElement) => { |
| 58 | + switch (node.name) { |
| 59 | + case 'input': |
| 60 | + this._handleInputNode(node); |
| 61 | + break; |
| 62 | + case 'mat-chip-list': |
| 63 | + this.chipMap = {chipList: node, chips: []}; |
| 64 | + break; |
| 65 | + case 'mat-chip': |
| 66 | + this.chipMap ? this.chipMap.chips.push(node) : this.standaloneChips.push(node); |
| 67 | + } |
| 68 | + }, |
| 69 | + (node: compiler.TmplAstElement) => { |
| 70 | + if (node.name === 'mat-chip-list') { |
| 71 | + this.chipMaps.push(this.chipMap!); |
| 72 | + this.chipMap = undefined; |
| 73 | + } |
| 74 | + }, |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + /** Returns the mat-chip-list and mat-chip updates for the given ChipMap. */ |
| 79 | + private _buildUpdatesForChipMap( |
| 80 | + chipMap: ChipMap, |
| 81 | + chipListTagName: string, |
| 82 | + chipTagName: string, |
| 83 | + ): Update[] { |
| 84 | + const updates: Update[] = []; |
| 85 | + updates.push(...this._buildTagUpdates(chipMap.chipList, chipListTagName)); |
| 86 | + chipMap.chips.forEach(chip => updates.push(...this._buildTagUpdates(chip, chipTagName))); |
| 87 | + return updates; |
| 88 | + } |
| 89 | + |
| 90 | + /** Creates and returns the start and end tag updates for the given node. */ |
| 91 | + private _buildTagUpdates(node: compiler.TmplAstElement, tagName: string): Update[] { |
| 92 | + return [ |
| 93 | + { |
| 94 | + location: node.startSourceSpan.start, |
| 95 | + updateFn: html => replaceStartTag(html, node, tagName), |
| 96 | + }, |
| 97 | + { |
| 98 | + location: node.endSourceSpan!.start, |
| 99 | + updateFn: html => replaceEndTag(html, node, tagName), |
| 100 | + }, |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + /** Stores the given input node if it has a matChipInputFor attribute. */ |
| 105 | + private _handleInputNode(node: compiler.TmplAstElement): void { |
| 106 | + node.inputs.forEach(attr => { |
| 107 | + if (attr.name === 'matChipInputFor') { |
| 108 | + this.chipInputs.push(attr); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + /** Returns true if the given mat-chip-list is referenced by any inputs. */ |
| 114 | + private _isChipGrid(node: compiler.TmplAstElement): boolean { |
| 115 | + return node.references.some(ref => { |
| 116 | + return this.chipInputs.some(attr => { |
| 117 | + return ref.name === (attr.value as compiler.ASTWithSource).source; |
| 118 | + }); |
| 119 | + }); |
| 120 | + } |
| 121 | +} |
0 commit comments