All files / Scripts/ui newDesktopFrame.ts

0% Statements 0/245
0% Branches 0/103
0% Functions 0/27
0% Lines 0/240

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 474 475 476                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import "../../Content/fluentCommon.css";
import "../../Content/newDesktopFrame.css";
 
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";
import { TabNavigation } from "../TabNavigation";
import { DomUtils } from "./domUtils";
 
// This is the "new" UI rendered in newDesktopFrame.html
 
// Overlay element for loading display
let overlayElement: HTMLElement | null = null;
 
function postError(error: unknown, message: string): void {
    Poster.postMessageToParent("LogError", { error: JSON.stringify(error), message: message });
}
 
function initializeFluentUI(): void {
    // Store references for overlay control
    overlayElement = DomUtils.getElement("#loading-overlay");
 
    // Override click so user can't dismiss overlay
    if (overlayElement) {
        overlayElement.addEventListener("click", function (e: Event): void {
            e.preventDefault();
            e.stopImmediatePropagation();
        });
    }
 
    // Fluent UI Web Components don't need JavaScript initialization for most components
    // Navigation and button behavior is handled with standard DOM events
 
    // Set up original headers toggle button
    const buttonElement = DomUtils.getElement("#orig-header-btn");
    if (buttonElement) {
        buttonElement.addEventListener("click", function (event: Event): void {
            if (event.currentTarget) {
                const currentTarget = event.currentTarget as HTMLElement;
                const btnIcon = currentTarget.querySelector(".fluent-icon--add, .fluent-icon--remove");
                const originalHeaders = DomUtils.getElement("#original-headers");
                const isExpanded = buttonElement.getAttribute("aria-expanded") === "true";
 
                if (!isExpanded) {
                    buttonElement.setAttribute("aria-expanded", "true");
                    if (originalHeaders) originalHeaders.style.display = "block";
                    if (btnIcon) {
                        btnIcon.classList.remove("fluent-icon--add");
                        btnIcon.classList.add("fluent-icon--remove");
                    }
                } else {
                    buttonElement.setAttribute("aria-expanded", "false");
                    if (originalHeaders) originalHeaders.style.display = "none";
                    if (btnIcon) {
                        btnIcon.classList.remove("fluent-icon--remove");
                        btnIcon.classList.add("fluent-icon--add");
                    }
                }
            }
        });
    }
 
    // Show summary by default
    DomUtils.showElement(".header-view[data-content='summary-view']");
    document.getElementById("summary-btn")!.focus();
 
    // Wire up click events for nav buttons
    DomUtils.getElements("#nav-bar .nav-button").forEach((button: Element) => {
        button.addEventListener("click", function (this: HTMLElement): void {
            // Fix for Bug 1691252 - To set aria-label dynamically on click based on button name
            const currentActive = DomUtils.getElement("#nav-bar .is-active");
            if (currentActive) {
                const activeButtonLabel = currentActive.querySelector(".button-label") as HTMLElement;
                if (activeButtonLabel) {
                    const activeButtonText = activeButtonLabel.textContent?.trim() || "";
                    currentActive.setAttribute("aria-label", activeButtonText);
                }
            }
 
            // Remove active from current active and hide its label
            if (currentActive) {
                currentActive.classList.remove("is-active");
            }
            DomUtils.hideAllElements("#nav-bar .button-label");
 
            // Add active class to clicked button and show its label
            this.classList.add("is-active");
            const thisLabel = this.querySelector(".button-label") as HTMLElement;
            if (thisLabel) thisLabel.style.display = "block";
 
            // Get content marker
            const content = this.getAttribute("data-content");
 
            // Fix for Bug 1691252 - To set aria-label as button after selection like "Summary Selected"
            const buttonText = thisLabel?.textContent?.trim() || "";
            const ariaLabel = buttonText + " Selected";
            this.setAttribute("aria-label", ariaLabel);
 
            // Hide all header views
            DomUtils.hideAllElements(".header-view");
 
            // Show the selected content view
            if (content) {
                DomUtils.showElement(`.header-view[data-content='${content}']`);
            }
        });
    });
 
    // Initialize label visibility - only show active button label
    DomUtils.hideAllElements("#nav-bar .button-label");
    const activeLabel = DomUtils.getElement("#nav-bar .is-active .button-label");
    if (activeLabel) activeLabel.style.display = "block";
 
    // Initialize iframe tab navigation handling
    TabNavigation.initializeIFrameTabHandling();
}
 
// Add document-level click handler to close callouts when clicking outside
document.addEventListener("click", function(event: Event) {
    const target = event.target as HTMLElement;
 
    // Don't close if clicking on a list item (that will handle its own toggle)
    if (target.closest(".hop-list-item")) {
        return;
    }
 
    // Close all open callouts
    document.querySelectorAll(".hop-details-overlay.is-shown").forEach(callout => {
        callout.classList.remove("is-shown");
        callout.classList.add("is-hidden");
    });
});
 
// Add escape key handler to close callouts
document.addEventListener("keydown", function(event: KeyboardEvent) {
    if (event.key === "Escape") {
        document.querySelectorAll(".hop-details-overlay.is-shown").forEach(callout => {
            callout.classList.remove("is-shown");
            callout.classList.add("is-hidden");
        });
    }
});
 
function updateStatus(message: string) {
    DomUtils.setText("#status-message", message);
    if (overlayElement) {
        overlayElement.style.display = "block";
    }
}
 
function addCalloutEntry(name: string, value: string | number | null, parent: HTMLElement) {
    if (value) {
        const clone = DomUtils.cloneTemplate("hop-entry-template");
        DomUtils.setTemplateText(clone, ".hop-label", name + ": ");
        DomUtils.setTemplateText(clone, ".hop-value", String(value));
        parent.appendChild(clone);
    }
}
 
function buildViews(headers: string) {
    const viewModel: HeaderModel = new HeaderModel(headers);
    // Build summary view
    const summaryList = document.querySelector(".summary-list") as HTMLElement;
    viewModel.summary.rows.forEach((row: SummaryRow) => {
        if (row.value) {
            const clone = DomUtils.cloneTemplate("summary-row-template");
            DomUtils.setTemplateText(clone, ".section-header", row.label);
            DomUtils.setTemplateText(clone, "code", row.value);
            summaryList.appendChild(clone);
        }
    });
 
    // Save original headers and show ui
    DomUtils.setText("#original-headers textarea", viewModel.originalHeaders);
    if (viewModel.originalHeaders) {
        DomUtils.showElement(".orig-header-ui");
    }
 
    // Build received view
    const receivedList = document.querySelector(".received-list") as HTMLElement;
 
    if (viewModel.receivedHeaders.rows.length > 0) {
        // Use HTML template for list creation
        const listClone = DomUtils.cloneTemplate("received-list-template");
        receivedList.appendChild(listClone);
        const list = receivedList.querySelector("ul") as HTMLElement;
 
        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
            // Use HTML template for list item creation
            const itemClone = DomUtils.cloneTemplate("list-item-template");
            const listItem = itemClone.querySelector("li") as HTMLElement;
            listItem.id = "received" + index;
            list.appendChild(itemClone);
 
            if (firstRow) {
                // Use HTML template for first row content
                const listItemElement = listItem;
                if (listItemElement) {
                    const clone = DomUtils.cloneTemplate("first-row-template");
                    DomUtils.setTemplateText(clone, ".from-value", String(row.from));
                    DomUtils.setTemplateText(clone, ".to-value", String(row.by));
                    listItemElement.appendChild(clone);
                }
                firstRow = false;
            } else {
                // Use HTML template for progress icon
                const progressClone = DomUtils.cloneTemplate("progress-icon-template");
 
                // Set the progress value for fluent-progress
                const percent = Number(row.percent.value ?? 0);
                const progressElement = progressClone.querySelector(".hop-progress") as HTMLElement;
                if (progressElement) {
                    progressElement.setAttribute("value", String(percent));
                    progressElement.setAttribute("max", "100");
                }
 
                // Set the description text
                const delayText = row.delay.value !== null ? String(row.delay.value) : "";
                DomUtils.setTemplateText(progressClone, ".progress-description", delayText);
 
                listItem.appendChild(progressClone);
 
                // Use HTML template for secondary text
                const listItemElement = listItem;
                if (listItemElement) {
                    const clone = DomUtils.cloneTemplate("secondary-text-template");
                    DomUtils.setTemplateText(clone, ".to-value", String(row.by));
                    listItemElement.appendChild(clone);
                }
            }
 
            // Add selection target using HTML template
            const selectionClone = DomUtils.cloneTemplate("selection-target-template");
            listItem.appendChild(selectionClone);
 
            // Callout - Use HTML template for callout structure
            const calloutClone = DomUtils.cloneTemplate("hop-template");
 
            // Add callout header to the tooltip content
            const calloutContent = calloutClone.querySelector(".hop-details-content") as HTMLElement;
            if (calloutContent) {
                const headerClone = DomUtils.cloneTemplate("hop-header-template");
                calloutContent.appendChild(headerClone);
            }
 
            listItem.appendChild(calloutClone);
 
            // Add callout entries
            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);
 
            function toggleCallout(listItem: HTMLElement) {
                const calloutElement = listItem.querySelector(".hop-details-overlay") as HTMLElement;
 
                // Check if this callout is currently shown BEFORE hiding others
                const isCurrentlyShown = calloutElement && calloutElement.classList.contains("is-shown");
 
                // Hide all callouts first
                document.querySelectorAll(".hop-details-overlay").forEach(callout => {
                    callout.classList.remove("is-shown");
                    callout.classList.add("is-hidden");
                });
 
                // If this callout was NOT currently shown, show it
                // If it WAS currently shown, leave it hidden (toggle behavior)
                if (calloutElement && !isCurrentlyShown) {
                    calloutElement.classList.remove("is-hidden");
                    calloutElement.classList.add("is-shown");
 
                    // Position the callout relative to the list item
                    const listItemRect = listItem.getBoundingClientRect();
                    const viewportWidth = window.innerWidth;
                    const viewportHeight = window.innerHeight;
 
                    // Center the callout horizontally relative to the viewport
                    const leftPosition = (viewportWidth - calloutElement.offsetWidth) / 2;
 
                    // Position below the list item so arrow points up to it
                    let topPosition = listItemRect.bottom + 15; // 15px gap for arrow
 
                    // Ensure callout stays within viewport
                    if (topPosition + calloutElement.offsetHeight > viewportHeight - 10) {
                        topPosition = viewportHeight - calloutElement.offsetHeight - 10;
                    }
                    if (topPosition < 10) {
                        topPosition = 10;
                    }
 
                    calloutElement.style.left = `${leftPosition}px`;
                    calloutElement.style.top = `${topPosition}px`;
                }
            }
 
            // Add click handler to show/hide callout
            listItem.addEventListener("click", function(event: Event) {
                const target = event.target as HTMLElement;
 
                // Don't handle the click if it was inside the callout content
                if (target.closest(".hop-details-overlay")) {
                    return;
                }
 
                event.preventDefault();
                toggleCallout(this);
            });
 
            // Add keyboard handler for accessibility (Enter/Space to show hop details)
            listItem.addEventListener("keydown", function(event: KeyboardEvent) {
                if (event.key === "Enter" || event.key === " ") {
                    event.preventDefault();
                    toggleCallout(this);
                }
            });
 
            // Make list item focusable for keyboard navigation
            listItem.setAttribute("tabindex", "0");
        });
 
        // Build antispam view
        const antispamList = document.querySelector(".antispam-list") as HTMLElement;
 
        // Forefront
        if (viewModel.forefrontAntiSpamReport.rows.length > 0) {
            // Use HTML template for section header
            DomUtils.appendTemplate("forefront-header-template", antispamList);
 
            // Create table using HTML template
            DomUtils.appendTemplate("antispam-table-template", antispamList);
 
            const tbodyElement = antispamList.querySelector("table:last-child tbody");
            if (tbodyElement) {
                viewModel.forefrontAntiSpamReport.rows.forEach((antispamrow: Row) => {
                    // Use HTML template for table rows
                    const rowClone = DomUtils.cloneTemplate("table-row-template");
 
                    // Set first cell content and id
                    const cells = rowClone.querySelectorAll("td");
                    if (cells.length >= 2) {
                        const cell0 = cells[0] as HTMLElement;
                        cell0.id = antispamrow.id;
                        cell0.textContent = antispamrow.label;
                    }
 
                    // Use helper for setting aria-labelledby attribute
                    DomUtils.setTemplateAttribute(rowClone, "td:nth-child(2)", "aria-labelledby", antispamrow.id);
                    DomUtils.setTemplateHTML(rowClone, "td:nth-child(2)", antispamrow.valueUrl); // Note: valueUrl may contain HTML
 
                    tbodyElement.appendChild(rowClone);
                });
            }
        }
 
        // Microsoft
        if (viewModel.antiSpamReport.rows.length > 0) {
            // Use HTML template for section header
            DomUtils.appendTemplate("microsoft-header-template", antispamList);
 
            // Create table using HTML template
            DomUtils.appendTemplate("antispam-table-template", antispamList);
 
            const tbodyElement2 = antispamList.querySelector("table:last-child tbody");
            if (tbodyElement2) {
                viewModel.antiSpamReport.rows.forEach((antispamrow: Row) => {
                    // Use HTML template for table rows
                    const rowClone = DomUtils.cloneTemplate("table-row-template");
 
                    // Set first cell content and id
                    const cells = rowClone.querySelectorAll("td");
                    if (cells.length >= 2) {
                        const cell0 = cells[0] as HTMLElement;
                        cell0.id = antispamrow.id;
                        cell0.textContent = antispamrow.label;
                    }
 
                    // Use helper for setting aria-labelledby attribute
                    DomUtils.setTemplateAttribute(rowClone, "td:nth-child(2)", "aria-labelledby", antispamrow.id);
                    DomUtils.setTemplateHTML(rowClone, "td:nth-child(2)", antispamrow.valueUrl); // Note: valueUrl may contain HTML
 
                    tbodyElement2.appendChild(rowClone);
                });
            }
        }
    }
 
    // Build other view
    const otherList = document.querySelector(".other-list") as HTMLElement;
 
    viewModel.otherHeaders.rows.forEach((otherRow: OtherRow) => {
        if (otherRow.value) {
            // Use HTML template for other headers
            const clone = DomUtils.cloneTemplate("other-row-template");
            const headerContent = otherRow.url ? otherRow.url : otherRow.header;
            DomUtils.setTemplateHTML(clone, ".section-header", headerContent); // May contain HTML (url)
            DomUtils.setTemplateText(clone, "code", otherRow.value);
            otherList.appendChild(clone);
        }
    });
 
    // Fluent UI Web Components handle their own initialization
    // Lists and callouts work with standard DOM interactions
}
 
function hideStatus(): void {
    if (overlayElement) {
        overlayElement.style.display = "none";
    }
}
 
function renderItem(headers: string): void {
    // Hide loading status as soon as we start rendering
    hideStatus();
 
    // Empty data
    DomUtils.clearElement(".summary-list");
    DomUtils.setText("#original-headers code", "");
    DomUtils.hideElement(".orig-header-ui");
    DomUtils.clearElement(".received-list");
    DomUtils.clearElement(".antispam-list");
    DomUtils.clearElement(".other-list");
    DomUtils.setText("#error-display .error-text", "");
    DomUtils.hideElement("#error-display");
 
    // Build views with the loaded data
    buildViews(headers);
}
 
// 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);
    DomUtils.setText("#error-display .error-text", message);
    DomUtils.showElement("#error-display");
}
 
function eventListener(event: MessageEvent): void {
    if (!event || event.origin !== Poster.site()) return;
 
    if (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;
        }
    }
}
 
// Initialize when DOM is ready
document.addEventListener("DOMContentLoaded", function() {
    try {
        initializeFluentUI();
        updateStatus(mhaStrings.mhaLoading);
        window.addEventListener("message", eventListener, false);
        Poster.postMessageToParent("frameActive");
    }
    catch (e) {
        postError(e, "Failed initializing frame");
        showError(e, "Failed initializing frame");
    }
});