| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  | import BaseUIElement from "../BaseUIElement"; | 
					
						
							|  |  |  | import {Utils} from "../../Utils"; | 
					
						
							|  |  |  | import Translations from "../i18n/Translations"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class Table extends BaseUIElement { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private readonly _header: BaseUIElement[]; | 
					
						
							|  |  |  |     private readonly _contents: BaseUIElement[][]; | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |     private readonly _contentStyle: string[][]; | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |     constructor(header: (BaseUIElement | string)[],  | 
					
						
							|  |  |  |                 contents: (BaseUIElement | string)[][], | 
					
						
							|  |  |  |                 contentStyle?: string[][]) { | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |         super(); | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |         this._contentStyle = contentStyle ?? []; | 
					
						
							|  |  |  |         this._header = header?.map(Translations.W); | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |         this._contents = contents.map(row => row.map(Translations.W)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected InnerConstructElement(): HTMLElement { | 
					
						
							|  |  |  |         const table = document.createElement("table") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const headerElems = Utils.NoNull((this._header ?? []).map(elems => elems.ConstructElement())) | 
					
						
							|  |  |  |         if (headerElems.length > 0) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const tr = document.createElement("tr"); | 
					
						
							|  |  |  |             headerElems.forEach(headerElem => { | 
					
						
							|  |  |  |                 const td = document.createElement("th") | 
					
						
							|  |  |  |                 td.appendChild(headerElem) | 
					
						
							|  |  |  |                 tr.appendChild(td) | 
					
						
							|  |  |  |             }) | 
					
						
							|  |  |  |             table.appendChild(tr) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |         for (let i = 0; i < this._contents.length; i++){ | 
					
						
							|  |  |  |             let row = this._contents[i]; | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |             const tr = document.createElement("tr") | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |             for (let j = 0; j < row.length; j++){ | 
					
						
							|  |  |  |                 let elem = row[j]; | 
					
						
							|  |  |  |                 const htmlElem = elem?.ConstructElement() | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |                 if (htmlElem === undefined) { | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |                 let style = undefined; | 
					
						
							|  |  |  |                 if(this._contentStyle !== undefined && this._contentStyle[i] !== undefined && this._contentStyle[j]!== undefined){ | 
					
						
							|  |  |  |                     style = this._contentStyle[i][j] | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |                 const td = document.createElement("td") | 
					
						
							| 
									
										
										
										
											2021-06-16 14:23:53 +02:00
										 |  |  |                 td.style.cssText = style; | 
					
						
							| 
									
										
										
										
											2021-06-15 00:55:12 +02:00
										 |  |  |                 td.appendChild(htmlElem) | 
					
						
							|  |  |  |                 tr.appendChild(td) | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             table.appendChild(tr) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return table; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     AsMarkdown(): string { | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         const headerMarkdownParts =  this._header.map(hel => hel?.AsMarkdown() ?? " ") | 
					
						
							|  |  |  |         const header =headerMarkdownParts.join(" | "); | 
					
						
							|  |  |  |         const headerSep = headerMarkdownParts.map(part => '-'.repeat(part.length + 2)).join("|") | 
					
						
							|  |  |  |         const table = this._contents.map(row => row.map(el => el.AsMarkdown()?? " ").join("|")).join("\n") | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         return [header, headerSep, table, ""].join("\n") | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |