|
| 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 {ApplicationRef, ComponentRef, createComponent, EnvironmentInjector} from '@angular/core'; |
| 10 | + |
| 11 | +import {bindAction, profile} from '../util'; |
| 12 | + |
| 13 | +import {TableComponent} from './table'; |
| 14 | +import {buildTable, emptyTable, initTableUtils, TableCell} from './util'; |
| 15 | + |
| 16 | +const DEFAULT_COLS_COUNT = '40'; |
| 17 | +const DEFAULT_ROWS_COUNT = '200'; |
| 18 | + |
| 19 | +function getUrlParamValue(name: string): string|null { |
| 20 | + const url = new URL(document.location.href); |
| 21 | + return url.searchParams.get(name); |
| 22 | +} |
| 23 | + |
| 24 | +export function syncUrlParamsToForm(): {cols: string, rows: string} { |
| 25 | + let cols = getUrlParamValue('cols') ?? DEFAULT_COLS_COUNT; |
| 26 | + let rows = getUrlParamValue('rows') ?? DEFAULT_ROWS_COUNT; |
| 27 | + (document.getElementById('cols') as HTMLInputElement).value = cols; |
| 28 | + (document.getElementById('rows') as HTMLInputElement).value = rows; |
| 29 | + return {cols, rows}; |
| 30 | +} |
| 31 | + |
| 32 | +export function init(appRef: ApplicationRef, insertSsrContent = true) { |
| 33 | + let tableComponentRef: ComponentRef<TableComponent>; |
| 34 | + const injector = appRef.injector; |
| 35 | + const environmentInjector = injector.get(EnvironmentInjector); |
| 36 | + |
| 37 | + let data: TableCell[][] = []; |
| 38 | + |
| 39 | + const setInput = (data: TableCell[][]) => { |
| 40 | + if (tableComponentRef) { |
| 41 | + tableComponentRef.setInput('data', data); |
| 42 | + tableComponentRef.changeDetectorRef.detectChanges(); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + function destroyDom() { |
| 47 | + setInput(emptyTable); |
| 48 | + } |
| 49 | + |
| 50 | + function updateDom() { |
| 51 | + data = buildTable(); |
| 52 | + setInput(data); |
| 53 | + } |
| 54 | + |
| 55 | + function createDom() { |
| 56 | + const hostElement = document.getElementById('table'); |
| 57 | + tableComponentRef = createComponent(TableComponent, {environmentInjector, hostElement}); |
| 58 | + setInput(data); |
| 59 | + } |
| 60 | + |
| 61 | + function prepare() { |
| 62 | + destroyDom(); |
| 63 | + data = buildTable(); |
| 64 | + |
| 65 | + if (insertSsrContent) { |
| 66 | + // Prepare DOM structure, similar to what SSR would produce. |
| 67 | + const hostElement = document.getElementById('table'); |
| 68 | + hostElement.setAttribute('ngh', '0'); |
| 69 | + hostElement.textContent = ''; // clear existing DOM contents |
| 70 | + hostElement.appendChild(createTableDom(data)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + function noop() {} |
| 75 | + |
| 76 | + initTableUtils(); |
| 77 | + |
| 78 | + bindAction('#prepare', prepare); |
| 79 | + bindAction('#createDom', createDom); |
| 80 | + bindAction('#updateDom', updateDom); |
| 81 | + bindAction('#createDomProfile', profile(createDom, prepare, 'create')); |
| 82 | + bindAction('#updateDomProfile', profile(updateDom, noop, 'update')); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Creates DOM to represent a table, similar to what'd be generated |
| 87 | + * during the SSR. |
| 88 | + */ |
| 89 | +function createTableDom(data: TableCell[][]) { |
| 90 | + const table = document.createElement('table'); |
| 91 | + const tbody = document.createElement('tbody'); |
| 92 | + table.appendChild(tbody); |
| 93 | + this._renderCells = []; |
| 94 | + for (let r = 0; r < data.length; r++) { |
| 95 | + const dataRow = data[r]; |
| 96 | + const tr = document.createElement('tr'); |
| 97 | + // Mark created DOM nodes, so that we can verify that |
| 98 | + // they were *not* re-created during hydration. |
| 99 | + (tr as any).__existing = true; |
| 100 | + tbody.appendChild(tr); |
| 101 | + const renderRow = []; |
| 102 | + for (let c = 0; c < dataRow.length; c++) { |
| 103 | + const dataCell = dataRow[c]; |
| 104 | + const renderCell = document.createElement('td'); |
| 105 | + // Mark created DOM nodes, so that we can verify that |
| 106 | + // they were *not* re-created during hydration. |
| 107 | + (renderCell as any).__existing = true; |
| 108 | + if (r % 2 === 0) { |
| 109 | + renderCell.style.backgroundColor = 'grey'; |
| 110 | + } |
| 111 | + tr.appendChild(renderCell); |
| 112 | + renderRow[c] = renderCell; |
| 113 | + renderCell.textContent = dataCell.value; |
| 114 | + } |
| 115 | + // View container anchor |
| 116 | + const comment = document.createComment(''); |
| 117 | + tr.appendChild(comment); |
| 118 | + } |
| 119 | + // View container anchor |
| 120 | + const comment = document.createComment(''); |
| 121 | + tbody.appendChild(comment); |
| 122 | + return table; |
| 123 | +} |
0 commit comments