All files / Scripts/ui newDesktopFrame.ts

0% Statements 0/202
0% Branches 0/47
0% Functions 0/24
0% Lines 0/197

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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
import "office-ui-fabric-js/dist/css/fabric.min.css";
import "office-ui-fabric-js/dist/css/fabric.components.min.css";
import "../../Content/fabric.css";
import "../../Content/newDesktopFrame.css";
import $ from "jquery";
import { fabric } from "office-ui-fabric-js/dist/js/fabric";
 
import { findTabStops } from "../findTabStops";
import { HeaderModel } from "../HeaderModel";
import { mhaStrings } from "../mhaStrings";
import { Poster } from "../Poster";
import { OtherRow } from "../row/OtherRow";
import { ReceivedRow } from "../row/ReceivedRow";
import { Row } from "../row/Row";
import { SummaryRow } from "../row/SummaryRow";
 
// This is the "new" UI rendered in newDesktopFrame.html
 
let overlay: fabric.Overlay;
let spinner: fabric.Spinner;
 
function postError(error: unknown, message: string): void {
    Poster.postMessageToParent("LogError", { error: JSON.stringify(error), message: message });
}
 
function initializeFabric(): void {
    const overlayComponent: HTMLElement | null = document.querySelector(".ms-Overlay");
    Iif (!overlayComponent) return;
 
    // Override click so user can't dismiss overlay
    overlayComponent.addEventListener("click", function (e: Event): void {
        e.preventDefault();
        e.stopImmediatePropagation();
    });
    overlay = new fabric["Overlay"](overlayComponent);
 
    const spinnerElement: HTMLElement | null = document.querySelector(".ms-Spinner");
    Iif (!spinnerElement) return;
 
    spinner = new fabric["Spinner"](spinnerElement);
    spinner.stop();
 
    const commandBarElements: NodeListOf<Element> = document.querySelectorAll(".ms-CommandBar");
    Array.prototype.forEach.call(commandBarElements, (commandBarElement: Element) => {
        new fabric["CommandBar"](commandBarElement);
    });
 
    const commandButtonElements: NodeListOf<HTMLElement> = document.querySelectorAll(".ms-CommandButton");
    Array.prototype.forEach.call(commandButtonElements, (commandButtonElement: HTMLElement) => {
        new fabric["CommandButton"](commandButtonElement);
    });
 
    const buttonElement: HTMLElement | null = document.querySelector("#orig-header-btn");
    Iif (!buttonElement) return;
    new fabric["Button"](buttonElement, function (event: Event): void {
        Iif (event.currentTarget) {
            const btnIcon: JQuery<HTMLElement> = $(event.currentTarget).find(".ms-Icon");
            if (btnIcon.hasClass("ms-Icon--Add")) {
                buttonElement.setAttribute("aria-expanded", "true");
                $("#original-headers").show();
                btnIcon.removeClass("ms-Icon--Add").addClass("ms-Icon--Remove");
            } else {
                buttonElement.setAttribute("aria-expanded", "false");
                $("#original-headers").hide();
                btnIcon.removeClass("ms-Icon--Remove").addClass("ms-Icon--Add");
            }
        }
    });
 
    // Show summary by default
    $(".header-view[data-content='summary-view']").show();
    document.getElementById("summary-btn")!.focus();
 
    // Wire up click events for nav buttons
    $("#nav-bar .ms-CommandButton").click(function (): void {
        // Fix for Bug 1691252 - To set aria-label dynamically on click based on button name
        Iif ($("#nav-bar .is-active .ms-CommandButton-button .ms-CommandButton-label")!.length !== 0) {
            $("#nav-bar .is-active .ms-CommandButton-button").attr("aria-label", $("#nav-bar .is-active .ms-CommandButton-button .ms-CommandButton-label").text());
        }
 
        // Remove active from current active
        $("#nav-bar .is-active").removeClass("is-active");
        // Add active class to clicked button
        $(this).addClass("is-active");
 
        // Get content marker
        const content: string | undefined = $(this).attr("data-content");
        // Hide sub-views
 
        // Fix for Bug 1691252 - To set aria-label as button after selection like "Summary Selected"
        const ariaLabel = $(this).find(".ms-CommandButton-label")!.text() + " Selected";
        $(this).find(".ms-CommandButton-label")!.attr("aria-label",ariaLabel);
        $(this).find("button.ms-CommandButton-button").attr("aria-label",ariaLabel);
        $(".header-view").hide();
        $(".header-view[data-content='" + content + "']").show();
    });
 
    // Insert the settings and copy buttons into the tab order for the ribbon
    // This handles tabbing into from these buttons.
    // Tabbing out from these buttons is over in parentframe.ts
    document.addEventListener("keydown", function (e) {
        Iif (e.key === "Tab") {
            const shiftPressed = e.shiftKey;
            const focused: HTMLElement = document.activeElement as HTMLElement;
            // console.log("Shift pressed = " + shiftPressed);
            // console.log("Focused element:" + focused + " class:" + focused.className + " id:" + focused.id + " title:" + focused.title);
 
            // Tab from Other goes to copy button
            if (!shiftPressed && focused.id === "other-btn") {
                window.parent.document.getElementById("copyButton")!.focus();
                e.preventDefault();
            }
            // Tab back from Summary goes to end of view
            else if (shiftPressed && focused.id === "summary-btn") {
                const view = document.querySelector(".header-view[style*=\"display: block\"]") as HTMLElement;
                const tabStops = findTabStops(view);
                // Set focus on last element in the list if we can
                Iif (tabStops.length > 0){
                    tabStops[tabStops.length - 1]?.focus();
                    e.preventDefault();
                }
            }
            // If we're tabbing off of the view, we want to tab to the appropriate ribbon button
            else
            {
                const view = document.querySelector(".header-view[style*=\"display: block\"]") as HTMLElement;
                const tabStops = findTabStops(view);
 
                if (shiftPressed){
                    // If our current focus is the first element in the list, we want to move focus to the copy button
                    Iif (tabStops.length > 0 && focused === tabStops[0]){
                        window.parent.document.getElementById("settingsButton")!.focus();
                        e.preventDefault();
                    }
                }
                else{
                    // If our current focus is the last element in the list, we want to move focus to the summary-btn
                    Iif (tabStops.length > 0 && focused === tabStops[tabStops.length - 1]){
                        document.getElementById("summary-btn")!.focus();
                        e.preventDefault();
                    }
                }
            }
        }
    });
}
 
function updateStatus(message: string) {
    $(".status-message").text(message);
    overlay.show();
    spinner.start();
}
 
function makeBold(text: string) {
    return "<span class=\"ms-fontWeight-semibold\">" + text + "</span>";
}
 
function addCalloutEntry(name: string, value: string | number | null, parent: JQuery<HTMLElement>) {
    Iif (value) {
        $("<p/>")
            .addClass("ms-Callout-subText")
            .html(makeBold(name + ": ") + value)
            .appendTo(parent);
    }
}
 
function buildViews(headers: string) {
    const viewModel: HeaderModel = new HeaderModel(headers);
    // Build summary view
    const summaryList = $(".summary-list");
    viewModel.summary.rows.forEach((row: SummaryRow) => {
        Iif (row.value) {
            $("<div/>")
                .addClass("ms-font-s")
                .addClass("ms-fontWeight-semibold")
                .text(row.label)
                .appendTo(summaryList);
            const headerVal = $("<div/>")
                .addClass("code-box")
                .appendTo(summaryList);
            const pre = $("<pre/>").appendTo(headerVal);
            $("<code/>")
                .text(row.value)
                .appendTo(pre);
        }
    });
 
    // Save original headers and show ui
    $("#original-headers code").text(viewModel.originalHeaders);
    Iif (viewModel.originalHeaders) {
        $(".orig-header-ui").show();
    }
 
    // Build received view
    const receivedList = $(".received-list");
 
    Iif (viewModel.receivedHeaders.rows.length > 0) {
        const list = $("<ul/>")
            .addClass("ms-List")
            .appendTo(receivedList);
 
        let firstRow = true;
        viewModel.receivedHeaders.rows.forEach((row: ReceivedRow, index) => {
            // Fix for Bug 1846002 - Added attr ID to set focus for the first element in the list
            const listItem = $("<li/>")
                .addClass("ms-ListItem")
                .attr("tabindex", 0)
                .attr("id", "received" + index)
                .addClass("ms-ListItem--document")
                .appendTo(list);
 
            if (firstRow) {
                $("<span/>")
                    .addClass("ms-ListItem-primaryText")
                    .html(makeBold("From: ") + row.from)
                    .appendTo(listItem);
 
                $("<span/>")
                    .addClass("ms-ListItem-secondaryText")
                    .html(makeBold("To: ") + row.by)
                    .appendTo(listItem);
                firstRow = false;
            } else {
                const wrap = $("<div/>")
                    .addClass("progress-icon")
                    .appendTo(listItem);
 
                const iconbox = $("<div/>")
                    .addClass("ms-font-xxl")
                    .addClass("down-icon")
                    .appendTo(wrap);
 
                $("<i/>")
                    .addClass("ms-Icon")
                    .addClass("ms-Icon--Down")
                    .appendTo(iconbox);
 
                const delay = $("<div/>")
                    .addClass("ms-ProgressIndicator")
                    .appendTo(wrap);
 
                const bar = $("<div/>")
                    .addClass("ms-ProgressIndicator-itemProgress")
                    .appendTo(delay);
 
                $("<div/>")
                    .addClass("ms-ProgressIndicator-progressTrack")
                    .appendTo(bar);
 
                const width: number = 1.8 * (Number(row.percent.value ?? 0));
 
                $("<div/>")
                    .addClass("ms-ProgressIndicator-progressBar")
                    .css("width", width)
                    .appendTo(bar);
 
                $("<div/>")
                    .addClass("ms-ProgressIndicator-itemDescription")
                    .text(row.delay.value !== null ? row.delay.value : "")
                    .appendTo(delay);
 
                $("<span/>")
                    .addClass("ms-ListItem-secondaryText")
                    .html(makeBold("To: ") + row.by)
                    .appendTo(listItem);
            }
 
            index=index+1;
            $("<div/>")
                .addClass("ms-ListItem-selectionTarget")
                .appendTo(listItem);
 
            // Callout
            const callout = $("<div/>")
                .addClass("ms-Callout is-hidden")
                .appendTo(listItem);
 
            const calloutMain = $("<div/>")
                .addClass("ms-Callout-main")
                .appendTo(callout);
 
            $("<div/>")
                .addClass("ms-Callout-close")
                .attr("style","display:none")
                .appendTo(calloutMain);
 
            const calloutHeader = $("<div/>")
                .addClass("ms-Callout-header")
                .appendTo(calloutMain);
 
            $("<p/>")
                .addClass("ms-Callout-title")
                .text("Hop Details")
                .appendTo(calloutHeader);
 
            const calloutInner = $("<div/>")
                .addClass("ms-Callout-inner")
                .appendTo(calloutMain);
 
            const calloutContent: JQuery<HTMLElement> = $("<div/>")
                .addClass("ms-Callout-content")
                .appendTo(calloutInner);
 
            addCalloutEntry("From", row.from.value, calloutContent);
            addCalloutEntry("To", row.by.value, calloutContent);
            addCalloutEntry("Time", row.date.value, calloutContent);
            addCalloutEntry("Type", row.with.value, calloutContent);
            addCalloutEntry("ID", row.id.value, calloutContent);
            addCalloutEntry("For", row.for.value, calloutContent);
            addCalloutEntry("Via", row.via.value, calloutContent);
        });
 
        // Build antispam view
        const antispamList = $(".antispam-list");
 
        // Forefront
        Iif (viewModel.forefrontAntiSpamReport.rows.length > 0) {
            $("<div/>")
                .addClass("ms-font-m")
                .text("Forefront Antispam Report")
                .appendTo(antispamList);
 
            $("<hr/>").appendTo(antispamList);
            const table = $("<table/>")
                .addClass("ms-Table")
                .addClass("ms-Table--fixed")
                .addClass("spam-report")
                .appendTo(antispamList);
            const tbody = $("<tbody/>")
                .appendTo(table);
            viewModel.forefrontAntiSpamReport.rows.forEach((antispamrow: Row) => {
                const row = $("<tr/>").appendTo(tbody);
                $("<td/>")
                    .text(antispamrow.label)
                    .attr("id", antispamrow.id)
                    .appendTo(row);
                $("<td/>")
                    .html(antispamrow.valueUrl)
                    .attr("aria-labelledby", antispamrow.id)
                    .appendTo(row);
            });
        }
 
        // Microsoft
        Iif (viewModel.antiSpamReport.rows.length > 0) {
            $("<div/>")
                .addClass("ms-font-m")
                .text("Microsoft Antispam Report")
                .appendTo(antispamList);
 
            $("<hr/>").appendTo(antispamList);
            const table = $("<table/>")
                .addClass("ms-Table")
                .addClass("ms-Table--fixed")
                .addClass("spam-report")
                .appendTo(antispamList);
            const tbody = $("<tbody/>")
                .appendTo(table);
            viewModel.antiSpamReport.rows.forEach((antispamrow: Row) => {
                const row = $("<tr/>").appendTo(tbody);
                $("<td/>")
                    .text(antispamrow.label)
                    .attr("id", antispamrow.id)
                    .appendTo(row);
                $("<td/>")
                    .html(antispamrow.valueUrl)
                    .attr("aria-labelledby", antispamrow.id)
                    .appendTo(row);
            });
        }
    }
 
    // Build other view
    const otherList = $(".other-list");
 
    viewModel.otherHeaders.rows.forEach((otherRow: OtherRow) => {
        Iif (otherRow.value) {
            const headerName = $("<div/>")
                .addClass("ms-font-s")
                .addClass("ms-fontWeight-semibold")
                .text(otherRow.header)
                .appendTo(otherList);
            Iif (otherRow.url) {
                headerName.html(otherRow.url);
            }
            const headerVal = $("<div/>")
                .addClass("code-box")
                .appendTo(otherList);
            const pre = $("<pre/>").appendTo(headerVal);
            $("<code/>")
                .text(otherRow.value)
                .appendTo(pre);
        }
    });
 
    // Initialize any fabric lists added
    const listElements: NodeListOf<HTMLElement> = document.querySelectorAll(".ms-List");
    Array.prototype.forEach.call(listElements, (listElement: HTMLElement) => {
        new fabric["List"](listElement);
    });
 
    const listItemElements: NodeListOf<HTMLElement> = document.querySelectorAll(".ms-ListItem");
    Array.prototype.forEach.call(listItemElements, (listItem: HTMLElement) => {
        new fabric["ListItem"](listItem);
 
        // Init corresponding callout
        const calloutElement: HTMLElement | null = listItem.querySelector(".ms-Callout");
        Iif (!calloutElement) return;
        new fabric["Callout"](calloutElement, listItem, "right");
    });
}
 
function hideStatus(): void {
    spinner.stop();
    overlay.hide();
}
 
function renderItem(headers: string): void {
    // Empty data
    $(".summary-list").empty();
    $("#original-headers code").empty();
    $(".orig-header-ui").hide();
    $(".received-list").empty();
    $(".antispam-list").empty();
    $(".other-list").empty();
    $("#error-display .ms-MessageBar-text").empty();
    $("#error-display").hide();
 
    // Load new itemDescription
    updateStatus(mhaStrings.mhaLoading);
    buildViews(headers);
    hideStatus();
}
 
// Handles rendering of an error.
// Does not log the error - caller is responsible for calling PostError
function showError(error: unknown, message: string): void {
    console.error("Error:", error);
    $("#error-display .ms-MessageBar-text").text(message);
    $("#error-display").show();
}
 
function eventListener(event: MessageEvent): void {
    Iif (!event || event.origin !== Poster.site()) return;
 
    Iif (event.data) {
        switch (event.data.eventName) {
            case "showError":
                showError(JSON.parse(event.data.data.error), event.data.data.message);
                break;
            case "updateStatus":
                updateStatus(event.data.data);
                break;
            case "renderItem":
                renderItem(event.data.data);
                break;
        }
    }
}
 
$(function() {
    try {
        initializeFabric();
        updateStatus(mhaStrings.mhaLoading);
        window.addEventListener("message", eventListener, false);
        Poster.postMessageToParent("frameActive");
    }
    catch (e) {
        postError(e, "Failed initializing frame");
        showError(e, "Failed initializing frame");
    }
});