// tslint:disable-next-line: no-namespace import { LogicGraph, LogicGraphNode } from "../logics/logic_graph" import { LogicTable } from "../logics/logic_table" import * as AttributeNames from "../common/attribute_names" import * as DefaultClassNames from "../common/default_class_names" import { LogicText } from "../logics/logic_text"; export class TableDictionary { public static IndexName = "___GraphTableSVG_Console_Index"; public static ValueName = "___GraphTableSVG_Console_Value"; private columnMapper: Map = new Map(); private rows: Map[] = new Array(); private objects: any[] = new Array(); // columnValues: Map= new Map(); // itemCount : number = 0; constructor() { this.columnMapper.set(TableDictionary.IndexName, 0); // this.columnValues.set("index", []); } public construct(item: any) { if (item instanceof Array) { item.forEach((v) => { this.add(v); }) } else { this.add(item); } } public addValue(i: number, key: string, value: any) { const column = this.columnMapper.get(key); if (column === undefined) { this.columnMapper.set(key, this.columnMapper.size); } this.rows[i].set(key, value); } public add(item: any) { this.rows.push(new Map()); this.objects.push(item); const x = this.rows.length - 1; this.addValue(x, TableDictionary.IndexName, x.toString()); if (item instanceof Array) { for (let i = 0; i < item.length; i++) { const cell = item[i]; if (cell != undefined) { this.addValue(x, i.toString(), cell); } } } else { if (typeof item === "string" || typeof item === "number" || typeof item === "boolean") { this.addValue(x, TableDictionary.ValueName, item.toString()); } else if (typeof item === "object") { Object.keys(item).forEach((key) => { const value = item[key]; this.addValue(x, key.toString(), value); }) } } } public toLogicTable(): LogicTable { const table = new LogicTable({ columnCount: this.columnMapper.size, rowCount: this.rows.length + 1 }); this.columnMapper.forEach((value, key) => { const logicText : LogicText = new LogicText(); logicText.class = DefaultClassNames.defaultConsoleColumnTitleCellTextClass; //table.cells[0][value].textClass = DefaultClassNames.defaultConsoleColumnTitleCellTextClass; table.cells[0][value].backgroundOption.class = DefaultClassNames.defaultConsoleColumnTitleCellBackgroundClass; if (key == TableDictionary.IndexName) { logicText.textContent = "(index)"; } else if (key == TableDictionary.ValueName) { logicText.textContent = "(value)"; } else { logicText.textContent = key; } table.cells[0][value].text = logicText; }) this.rows.forEach((map, index) => { const tableIndex = index + 1; for (let i = 0; i < this.columnMapper.size; i++) { table.cells[tableIndex][i].text.textContent = "undefined"; table.cells[tableIndex][i].text.textContent = DefaultClassNames.defaultConsoleColumnTitleCellUndefinedTextClass; } map.forEach((value, key) => { const columnIndex = this.columnMapper.get(key); if (columnIndex != undefined) { const cell = this.rows[index].get(key); if (cell == null) { table.cells[tableIndex][columnIndex].text.textContent = "null"; } else if (cell != undefined) { table.cells[tableIndex][columnIndex].text.textContent = cell.toString(); table.cells[tableIndex][columnIndex].text.class = DefaultClassNames.defaultTextClass; } } }) }) return table; } createNode(item: any, graph: LogicGraph, dic: Map): LogicGraphNode { if (typeof item === "object") { let node = dic.get(item); if (node !== undefined) { return node; } else { node = graph.addNode(); if (item !== undefined && item != null) { dic.set(item, node); Object.keys(item).forEach((key) => { const value = (item as any)[key]; const child = this.createNode(value, graph, dic); const edge = graph.createEdge(); edge.endNodeIndex = graph.getIndex(child); edge.text = key.toString(); node!.addEdge(edge); }) } else { node.text = "null"; } return node; } } else { const node = graph.addNode(); if (typeof item === "undefined") { node.text = "undefined"; } else { node.text = item.toString(); } return node; } } public toLogicGraph(): LogicGraph { const dic: Map = new Map(); const graph = new LogicGraph(); this.rows.forEach((v, i) => { const obj = this.objects[i]; this.createNode(obj, graph, dic); }) return graph; } }