All files / Scripts/ui Table.ts

92.56% Statements 224/242
81.63% Branches 40/49
91.3% Functions 42/46
92.27% Lines 215/233

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 4461x     1x       1x 1x             1x 11x 11x   11x 27x 27x 27x 27x 27x 27x         39x 39x 39x 39x         13x 13x         66x 66x 50x   16x   16x 16x 16x 16x 16x 16x       16x 16x 16x 16x   16x 16x 16x             16x 16x 16x 16x   16x 16x 16x     16x 16x 16x 16x       1181x 135x   1046x         33x 33x 33x   33x 319x 319x 319x 319x 319x 319x 319x 319x 88x   231x   319x 319x     319x 319x 319x 319x     319x             35x 35x       668x 668x 668x 1x     1x     1x   667x 667x           208x 208x 208x 208x         47x 47x 47x         26x 192x     26x       24x 1703x     1703x 1703x 209x 139x       1703x 1564x 1564x   139x 139x               23x     23x 184x       23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x       23x       23x 23x 23x 23x 23x         23x 23x 23x         23x 23x     23x 391x       23x 92x       23x 23x               23x     23x   23x       132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x 132x     132x 11x 11x 11x 11x 11x 11x   11x         132x 132x 132x     132x 132x       132x       22x 22x 22x   22x 22x 22x 22x   22x 132x             1x 1x 1x       1x 1x 1x       2x 1x   1x     2x       12x 12x         11x     27x 11x     11x 11x     11x   11x                       11x   11x 11x 11x 11x 11x 11x 11x   11x 11x 11x 11x   11x 11x     11x                 11x 11x     11x 11x     11x   11x           11x   11x 11x                      
import $ from "jquery";
 
import { HeaderModel } from "../HeaderModel";
import { mhaStrings } from "../mhaStrings";
import { OtherRow } from "../row/OtherRow";
import { ReceivedRow } from "../row/ReceivedRow";
import { Row } from "../row/Row";
import { Column } from "../table/Column";
import { ITable } from "../table/ITable";
 
type Binding = {
    name: string;
    visible: (table: Table) => boolean;
}
 
export class Table {
    private viewModel: HeaderModel = <HeaderModel>{};
    private showExtra = false;
 
    private visibilityBindings: Binding[] = [
        { name: "#lineBreak", visible: function (table: Table) { return table.viewModel.hasData; } },
        { name: "#response", visible: function (table: Table) { return table.viewModel.hasData; } },
        { name: "#status", visible: function (table: Table) { return !!table.viewModel.status; } },
        { name: ".extraCol", visible: function (table: Table) { return table.showExtra; } },
        { name: "#clearButton", visible: function (table: Table) { return table.viewModel.hasData; } },
        { name: "#copyButton", visible: function (table: Table) { return table.viewModel.hasData; } }
    ];
 
    // Adjusts response under our lineBreak
    private positionResponse(): void {
        const lineBreak = $("#lineBreak");
        if (lineBreak && lineBreak[0]) {
            const responseTop: number = lineBreak[0]?.offsetTop + (lineBreak.height() ?? 0);
            $("#response").css("top", responseTop + 15);
        }
    }
 
    private toggleCollapse(id: string): void {
        $("#"+ id).toggleClass("hiddenElement");
        this.positionResponse();
    }
 
    // Wraps an element into a collapsible pane with a title
    public makeResizablePane(id: string, paneClass: string, title: string, visibility: (table: Table) => boolean): void {
        const pane = $("#" + id);
        if (pane.hasClass("collapsibleElement")) {
            return;
        }
        const hidden = pane.hasClass("hiddenElement");
 
        pane.addClass("collapsibleElement");
        const wrap = $(document.createElement("div"));
        wrap.addClass("collapsibleWrapper");
        if (visibility) {
            wrap.attr("id", id + "Wrapper");
            this.visibilityBindings.push({ name: "#" + id + "Wrapper", visible: visibility });
        }
 
        // Fix for Bug 1691235 - Create a hidden h2 element for a11y
        const hiddenHeading = $(document.createElement("h2"));
        hiddenHeading.attr("id", "button-heading");
        hiddenHeading.addClass("header-hidden");
        hiddenHeading.text(title);
 
        const header = $(document.createElement("button"));
        header.addClass(paneClass);
        header.on("click", this, function (eventObject) {
            const table: Table = eventObject.data as Table;
            Iif (table) {
                table.toggleCollapse(id);
                header.attr("aria-expanded", header.attr("aria-expanded") === "true" ? "false" : "true");
            }
        });
        header.text(title);
        header.attr("tabindex", 0);
        header.attr("type", "button");
        header.attr("aria-expanded", !hidden ? "true" : "false");
 
        const switchSpan = $(document.createElement("span"));
        switchSpan.attr("aria-hidden", "true");
        switchSpan.addClass("collapsibleSwitch");
 
        // Now that everything is built, put it together
        pane.wrap(wrap);
        pane.before(hiddenHeading);
        pane.before(header);
        header.append(switchSpan);
    }
 
    private makeVisible(id: string, visible: boolean): void {
        if (visible) {
            $(id).removeClass("hiddenElement");
        } else {
            $(id).addClass("hiddenElement");
        }
    }
 
    private makeSummaryTable(summaryName: string, rows: Row[], tag: string): void {
        const summaryList = $(summaryName);
        if (summaryList) {
            summaryList.addClass("summaryList");
 
            rows.forEach((summaryRow: Row) => {
                const id = summaryRow.header + tag;
                const row = document.createElement("tr");
                if (row !== null) {
                    row.id = id;
                    summaryList.append(row); // Must happen before we append cells to appease IE7
                    const headerCell = $(row.insertCell(-1));
                    if (headerCell) {
                        if (summaryRow.url) {
                            headerCell.html(summaryRow.url);
                        } else {
                            headerCell.text(summaryRow.label);
                        }
                        headerCell.addClass("summaryHeader");
                        headerCell.attr("id", summaryRow.id);
                    }
 
                    const valCell = $(row.insertCell(-1));
                    if (valCell) {
                        valCell.attr("id", id + "Val");
                        valCell.attr("aria-labelledby", summaryRow.id);
                    }
 
                    this.makeVisible("#" + id, false);
                }
            });
        }
    }
 
    private setArrows(table: string, colName: string, sortOrder: number): void {
        $("#" + table + " th button").attr("aria-sort", "none");
        $("#" + table + " th #" + colName).attr("aria-sort", sortOrder === 1 ? "descending" : "ascending");
    }
 
    private setRowValue(row: Row, type: string): void {
        const headerVal = $("#" + row.header + type + "Val");
        if (headerVal) {
            if (row.value) {
                Iif (row.valueUrl) {
                    headerVal.html(row.valueUrl);
                } else {
                    headerVal.text(row.value);
                }
 
                this.makeVisible("#" + row.header + type, true);
            } else {
                headerVal.text("");
                this.makeVisible("#" + row.header + type, false);
            }
        }
    }
 
    private appendCell(row: HTMLTableRowElement, text: string | number | null, html: string, cellClass: string): void {
        const cell = $(row.insertCell(-1));
        if (text) { cell.text(text); }
        if (html) { cell.html(html); }
        if (cellClass) { cell.addClass(cellClass); }
    }
 
    // Restores table to empty state so we can repopulate it
    private emptyTableUI(id: string): void {
        $("#" + id + " tr:not(.tableHeader)").remove(); // Remove the rows
        $("#" + id + " th").removeClass("emptyColumn"); // Restore header visibility
        $("#" + id + " th").removeClass("hiddenElement"); // Restore header visibility
    }
 
    // Recompute visibility with the current viewModel. Does not repopulate.
    public recalculateVisibility(): void {
        this.visibilityBindings.forEach((binding: Binding) => {
            this.makeVisible(binding.name, binding.visible(this));
        });
 
        this.positionResponse();
    }
 
    private hideEmptyColumns(id: string): void {
        $("#" + id + " th").each(function (i) {
            let keep = 0;
 
            // Find a child cell which has data
            const children = $(this).parents("table").find("tr td:nth-child(" + (i + 1).toString() + ")");
            children.each(function () {
                if (this.innerHTML !== "") {
                    keep++;
                }
            });
 
            if (keep === 0) {
                $(this).addClass("emptyColumn");
                children.addClass("emptyColumn");
            } else {
                $(this).removeClass("emptyColumn");
                children.removeClass("emptyColumn");
            }
        });
    }
 
    // Rebuilds content and recalculates what sections should be displayed
    // Repopulate the UI with the current viewModel
    public rebuildSections(viewModel: HeaderModel): void {
        this.viewModel = viewModel;
 
        // Summary
        this.viewModel.summary.rows.forEach((row: Row) => {
            this.setRowValue(row, "SUM");
        });
 
        // Received
        this.emptyTableUI("receivedHeaders");
        this.viewModel.receivedHeaders.rows.forEach((receivedRow: ReceivedRow) => {
            const row: HTMLTableRowElement = document.createElement("tr");
            $("#receivedHeaders").append(row); // Must happen before we append cells to appease IE7
            this.appendCell(row, receivedRow.hop.value, "", "");
            this.appendCell(row, receivedRow.from.value, "", "");
            this.appendCell(row, receivedRow.by.value, "", "");
            this.appendCell(row, receivedRow.date.value, "", "");
            let labelClass = "hotBarLabel";
            if (receivedRow.delaySort.value !== null && typeof receivedRow.delaySort.value === "number" && receivedRow.delaySort.value < 0) {
                labelClass += " negativeCell";
            }
 
            const hotBar =
                "<div class='hotBarContainer'>" +
                "   <div class='" + labelClass + "'>" + receivedRow.delay + "</div>" +
                "   <div class='hotBarBar' style='width:" + receivedRow.percent + "%'></div>" +
                "</div>";
            this.appendCell(row, "", hotBar, "hotBarCell");
            this.appendCell(row, receivedRow.with.value, "", "");
            this.appendCell(row, receivedRow.id.value, "", "extraCol");
            this.appendCell(row, receivedRow.for.value, "", "extraCol");
            this.appendCell(row, receivedRow.via.value, "", "extraCol");
        });
 
        // Calculate heights for the hotbar cells (progress bars in Delay column)
        // Not clear why we need to do this
        $(".hotBarCell").each(function () {
            const height: number | undefined = $(this).height();
            Iif (height) {
                $(this).find(".hotBarContainer").height(height);
            }
        });
 
        $("#receivedHeaders tr:odd").addClass("oddRow");
        this.hideEmptyColumns("receivedHeaders");
 
        // Forefront AntiSpam Report
        this.viewModel.forefrontAntiSpamReport.rows.forEach((row: Row) => {
            this.setRowValue(row, "FFAS");
        });
 
        // AntiSpam Report
        this.viewModel.antiSpamReport.rows.forEach((row: Row) => {
            this.setRowValue(row, "AS");
        });
 
        // Other
        this.emptyTableUI("otherHeaders");
        this.viewModel.otherHeaders.rows.forEach((otherRow: OtherRow) => {
            const row: HTMLTableRowElement = document.createElement("tr");
            $("#otherHeaders").append(row); // Must happen before we append cells to appease IE7
            this.appendCell(row, otherRow.number.toString(), "", "");
            this.appendCell(row, otherRow.header, otherRow.url, "");
            this.appendCell(row, otherRow.value, "", "allowBreak");
        });
 
        $("#otherHeaders tr:odd").addClass("oddRow");
 
        // Original headers
        $("#originalHeaders").text(this.viewModel.originalHeaders);
 
        this.recalculateVisibility();
    }
 
    private makeSortableColumn(tableName: string, column: Column): JQuery<HTMLElement> {
        const header = $(document.createElement("th"));
        if (header !== null) {
            const headerButton = $(document.createElement("button"));
            if (headerButton !== null) {
                headerButton.addClass("tableHeaderButton");
                headerButton.attr("id", column.id);
                headerButton.attr("type", "button");
                headerButton.attr("role", "columnheader");
                headerButton.attr("aria-sort", "none");
                headerButton.text(column.label);
                if (column.class !== null) {
                    headerButton.addClass(column.class);
                }
 
                headerButton.on("click", this, function (eventObject) {
                    const table: Table = eventObject.data as Table;
                    if (table) {
                        if (table.viewModel[tableName] instanceof ITable) {
                            const itable = table.viewModel[tableName] as ITable;
                            itable.doSort(column.id);
                            table.setArrows(itable.tableName, itable.sortColumn,
                                itable.sortOrder);
                            table.rebuildSections(table.viewModel);
                        }
                    }
                });
 
                const arrowSpan = $(document.createElement("span"));
                arrowSpan.attr("aria-hidden", "true");
                arrowSpan.addClass("sortArrow");
 
                // Now that everything is built, put it together
                headerButton.append(arrowSpan);
                header.append(headerButton);
            }
        }
 
        return header;
    }
 
    private addColumns(tableName: string, columns: Column[]): void {
        const tableHeader = $(document.createElement("thead"));
        if (tableHeader !== null) {
            $("#" + tableName).append(tableHeader);
 
            const headerRow = $(document.createElement("tr"));
            if (headerRow !== null) {
                headerRow.addClass("tableHeader");
                tableHeader.append(headerRow); // Must happen before we append cells to appease IE7
 
                columns.forEach((column: Column) => {
                    headerRow.append(this.makeSortableColumn(tableName, column));
                });
            }
        }
    }
 
    private hideExtraColumns(): void {
        this.showExtra = false;
        $("#leftArrow").addClass("hiddenElement");
        $("#rightArrow").removeClass("hiddenElement");
    }
 
    private showExtraColumns(): void {
        this.showExtra = true;
        $("#rightArrow").addClass("hiddenElement");
        $("#leftArrow").removeClass("hiddenElement");
    }
 
    private toggleExtraColumns(): void {
        if (this.showExtra) {
            this.hideExtraColumns();
        } else {
            this.showExtraColumns();
        }
 
        this.recalculateVisibility();
    }
 
    public resetArrows(): void {
        this.setArrows("receivedHeaders", "hop", 1);
        this.setArrows("otherHeaders", "number", 1);
    }
 
    // Initialize UI with an empty viewModel
    public initializeTableUI(viewModel: HeaderModel): void {
        this.viewModel = viewModel;
 
        // Headers
        this.makeResizablePane("originalHeaders", "sectionHeader", mhaStrings.mhaOriginalHeaders, (table: Table) => { return table.viewModel.originalHeaders.length > 0; });
        this.toggleCollapse("originalHeaders"); // start this section hidden
 
        // Summary
        this.makeResizablePane("summary", "sectionHeader", mhaStrings.mhaSummary, function (table: Table) { return table.viewModel.summary.exists(); });
        this.makeSummaryTable("#summary", this.viewModel.summary.rows, "SUM");
 
        // Received
        this.makeResizablePane("receivedHeaders", "tableCaption", mhaStrings.mhaReceivedHeaders, function (table: Table) { return table.viewModel.receivedHeaders.exists(); });
 
        const receivedColumns = [
            new Column("hop", mhaStrings.mhaReceivedHop, ""),
            new Column("from", mhaStrings.mhaReceivedSubmittingHost, ""),
            new Column("by", mhaStrings.mhaReceivedReceivingHost, ""),
            new Column("date", mhaStrings.mhaReceivedTime, ""),
            new Column("delay", mhaStrings.mhaReceivedDelay, ""),
            new Column("with", mhaStrings.mhaReceivedType, ""),
            new Column("id", mhaStrings.mhaReceivedId, "extraCol"),
            new Column("for", mhaStrings.mhaReceivedFor, "extraCol"),
            new Column("via", mhaStrings.mhaReceivedVia, "extraCol")
        ];
 
        this.addColumns(this.viewModel.receivedHeaders.tableName, receivedColumns);
 
        const withColumn = $("#receivedHeaders #with");
        if (withColumn !== null) {
            const leftSpan = $(document.createElement("span"));
            leftSpan.attr("id", "leftArrow");
            leftSpan.addClass("collapsibleArrow");
            leftSpan.addClass("hiddenElement");
            leftSpan.html("&lArr;");
 
            const rightSpan = $(document.createElement("span"));
            rightSpan.attr("id", "rightArrow");
            rightSpan.addClass("collapsibleArrow");
            rightSpan.html("&rArr;");
 
            withColumn.append(leftSpan);
            withColumn.append(rightSpan);
        }
 
        $("#receivedHeaders .collapsibleArrow").on("click", this, function (eventObject) {
            const table: Table = eventObject.data as Table;
            Iif (table) {
                table.toggleExtraColumns();
                eventObject.stopPropagation();
            }
        });
 
        // FFAS
        this.makeResizablePane("forefrontAntiSpamReport", "sectionHeader", mhaStrings.mhaForefrontAntiSpamReport, function (table: Table) { return table.viewModel.forefrontAntiSpamReport.exists(); });
        this.makeSummaryTable("#forefrontAntiSpamReport", this.viewModel.forefrontAntiSpamReport.rows, "FFAS");
 
        // AntiSpam
        this.makeResizablePane("antiSpamReport", "sectionHeader", mhaStrings.mhaAntiSpamReport, (table: Table) => { return table.viewModel.antiSpamReport.exists(); });
        this.makeSummaryTable("#antiSpamReport", this.viewModel.antiSpamReport.rows, "AS");
 
        // Other
        this.makeResizablePane("otherHeaders", "tableCaption", mhaStrings.mhaOtherHeaders, function (table: Table) { return table.viewModel.otherHeaders.rows.length > 0; });
 
        const otherColumns = [
            new Column("number", mhaStrings.mhaNumber, ""),
            new Column("header", mhaStrings.mhaHeader, ""),
            new Column("value", mhaStrings.mhaValue, "")
        ];
 
        this.addColumns(this.viewModel.otherHeaders.tableName, otherColumns);
 
        this.resetArrows();
        this.rebuildSections(this.viewModel);
    }
 
    // Rebuilds the UI with a new viewModel
    // Used by mha.ts and Default.ts to rebuild with new viewModel
    public rebuildTables(viewModel: HeaderModel): void {
        this.viewModel = viewModel;
        this.rebuildSections(this.viewModel);
        this.hideExtraColumns();
    }
}