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 | 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x | import { Choice } from "./Choice";
import { DeferredError } from "./DeferredError";
import { diagnostics } from "./Diag";
import { Errors } from "./Errors";
import { Poster } from "./Poster";
import { Strings } from "./Strings";
import { TabNavigation } from "./TabNavigation";
import { GetHeaders } from "./ui/getHeaders/GetHeaders";
import { ParentFrameUtils } from "./utils/ParentFrameUtils";
// Fluent UI Web Components interfaces
interface FluentDialog extends HTMLElement {
hidden: boolean;
addEventListener(type: "dismiss", listener: (event: Event) => void): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
}
interface FluentRadioGroup extends HTMLElement {
value: string;
}
interface FluentRadio extends HTMLElement {
value: string;
checked: boolean;
}
interface FluentCheckbox extends HTMLElement {
checked: boolean;
}
export class ParentFrame {
private static iFrame: Window | null;
private static currentChoice = {} as Choice;
private static deferredErrors: DeferredError[] = [];
private static deferredStatus: string[] = [];
private static headers = "";
private static modelToString = "";
protected static telemetryCheckbox: FluentCheckbox | null = null;
private static choices: Array<Choice> = [
{ label: "classic", url: "classicDesktopFrame.html", checked: false },
{ label: "new", url: "newDesktopFrame.html", checked: true },
{ label: "new-mobile", url: "newMobilePaneIosFrame.html", checked: false }
];
private static setDefault(): void {
let uiDefault: string = ParentFrameUtils.getQueryVariable("default");
if (uiDefault === null) {
uiDefault = "new";
}
ParentFrameUtils.setDefaultChoice(ParentFrame.choices, uiDefault);
}
private static postMessageToFrame(eventName: string, data: string | { error: string, message: string }): void {
if (ParentFrame.iFrame) {
Poster.postMessageToFrame(ParentFrame.iFrame, eventName, data);
}
}
private static render(): void {
if (ParentFrame.headers) diagnostics.trackEvent({ name: "analyzeHeaders" });
ParentFrame.postMessageToFrame("renderItem", ParentFrame.headers);
}
private static setFrame(frame: Window): void {
ParentFrame.iFrame = frame;
TabNavigation.setIFrame(frame);
if (ParentFrame.iFrame) {
// If we have any deferred status, signal them
ParentFrame.deferredStatus.forEach((status: string) => {
ParentFrame.postMessageToFrame("updateStatus", status);
});
// Clear out the now displayed status
ParentFrame.deferredStatus = [];
// If we have any deferred errors, signal them
ParentFrame.deferredErrors.forEach((deferredError: DeferredError) => {
ParentFrame.postMessageToFrame("showError",
{
error: JSON.stringify(deferredError.error),
message: deferredError.message
});
});
// Clear out the now displayed errors
ParentFrame.deferredErrors = [];
ParentFrame.render();
}
}
private static eventListener(event: MessageEvent): void {
if (!event || event.origin !== Poster.site()) return;
if (event.data) {
switch (event.data.eventName) {
case "frameActive":
ParentFrame.setFrame(event.source as Window);
break;
case "LogError":
Errors.log(JSON.parse(event.data.data.error), event.data.data.message);
break;
case "modelToString":
ParentFrame.modelToString = event.data.data;
break;
}
}
}
private static async loadNewItem() {
if (Office.context.mailbox.item) {
await GetHeaders.send(function (headers: string, apiUsed: string): void {
ParentFrame.headers = headers;
diagnostics.set("API used", apiUsed);
ParentFrame.render();
});
}
}
private static registerItemChangedEvent(): void {
try {
if (Office.context.mailbox.addHandlerAsync !== undefined) {
Office.context.mailbox.addHandlerAsync(Office.EventType.ItemChanged,
function (): void {
Errors.clear();
diagnostics.clear();
ParentFrame.loadNewItem();
});
}
} catch (e) {
Errors.log(e, "Could not register item changed event");
}
}
// Tells the UI to show an error.
public static showError(error: unknown, message: string, suppressTracking?: boolean): void {
Errors.log(error, message, suppressTracking);
if (ParentFrame.iFrame) {
ParentFrame.postMessageToFrame("showError", { error: JSON.stringify(error), message: message });
} else {
// We don't have an iFrame, so defer the message
ParentFrame.deferredErrors.push(<DeferredError>{ error: error, message: message });
}
}
// Tells the UI to show an error.
public static updateStatus(statusText: string): void {
if (ParentFrame.iFrame) {
ParentFrame.postMessageToFrame("updateStatus", statusText);
} else {
// We don't have an iFrame, so defer the status
ParentFrame.deferredStatus.push(statusText);
}
}
// Display primary UI
private static go(choice: Choice): void {
ParentFrame.iFrame = null;
ParentFrame.currentChoice = choice;
(document.getElementById("uiFrame") as HTMLIFrameElement).src = choice.url;
if (Office.context) {
Office.context.roamingSettings.set(ParentFrameUtils.getSettingsKey(), choice);
Office.context.roamingSettings.saveAsync();
}
}
private static goDefaultChoice(): void {
const choice = ParentFrame.choices.find((c: Choice) => c.checked);
if (choice) {
ParentFrame.go(choice);
}
}
// Create list of choices to display for the UI types
private static addChoices(): void {
const radioGroup = document.getElementById("uiChoice") as FluentRadioGroup;
if (!radioGroup) return;
// Clear existing options
radioGroup.innerHTML = "";
ParentFrame.choices.forEach((choice: Choice, iChoice: number) => {
const radio = document.createElement("fluent-radio") as FluentRadio;
radio.value = iChoice.toString();
radio.textContent = choice.label;
if (choice.checked) {
radio.checked = true;
}
radioGroup.appendChild(radio);
});
}
// Hook the UI together for display
private static initFluent(): void {
const header: Element | null = document.querySelector(".header-row");
if (!header) return;
const dialogSettings = document.getElementById("dialog-Settings") as FluentDialog;
const dialogDiagnostics = document.getElementById("dialog-Diagnostics") as FluentDialog;
if (!dialogSettings || !dialogDiagnostics) return;
// Ensure dialogs are initially hidden
dialogSettings.hidden = true;
dialogDiagnostics.hidden = true;
// Add click-outside-to-dismiss functionality
dialogSettings.addEventListener("click", (e) => {
if (e.target === dialogSettings) {
dialogSettings.hidden = true;
}
});
dialogDiagnostics.addEventListener("click", (e) => {
if (e.target === dialogDiagnostics) {
dialogDiagnostics.hidden = true;
}
});
// Add escape key to dismiss dialogs and enter key to apply settings
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
if (!dialogSettings.hidden) {
dialogSettings.hidden = true;
}
if (!dialogDiagnostics.hidden) {
dialogDiagnostics.hidden = true;
}
}
if (e.key === "Enter" && !dialogSettings.hidden) {
// Only trigger OK action when focused on radio buttons or checkboxes
const activeElement = document.activeElement;
const isRadioOrCheckbox = activeElement &&
(activeElement.tagName.toLowerCase() === "fluent-radio" ||
activeElement.tagName.toLowerCase() === "fluent-checkbox");
if (isRadioOrCheckbox) {
// Trigger the same action as the OK button
const radioGroup = document.getElementById("uiChoice") as FluentRadioGroup;
if (radioGroup && radioGroup.value) {
const iChoice = parseInt(radioGroup.value);
const choice: Choice | undefined = ParentFrame.choices[iChoice];
if (choice && choice.label !== ParentFrame.currentChoice.label) {
ParentFrame.go(choice);
}
}
// Update telemetry setting
if (ParentFrame.telemetryCheckbox) {
diagnostics.setSendTelemetry(ParentFrame.telemetryCheckbox.checked);
}
dialogSettings.hidden = true;
e.preventDefault(); // Prevent default form submission behavior
}
}
if (e.key === "Enter" && !dialogDiagnostics.hidden) {
// Close diagnostics dialog when Enter is pressed on the code box
const activeElement = document.activeElement;
if (activeElement && activeElement.id === "diagpre") {
dialogDiagnostics.hidden = true;
e.preventDefault();
}
}
});
const telemetryCheckbox = document.getElementById("telemetryInput") as FluentCheckbox;
if (telemetryCheckbox) {
ParentFrame.telemetryCheckbox = telemetryCheckbox;
ParentFrame.setSendTelemetryUI(diagnostics.canSendTelemetry());
}
// Wire up action buttons
const okButton = document.getElementById("actionsSettings-OK");
okButton?.addEventListener("click", () => {
// Get selected choice from radio group
const radioGroup = document.getElementById("uiChoice") as FluentRadioGroup;
if (radioGroup && radioGroup.value) {
const iChoice = parseInt(radioGroup.value);
const choice: Choice | undefined = ParentFrame.choices[iChoice];
if (choice && choice.label !== ParentFrame.currentChoice.label) {
ParentFrame.go(choice);
}
}
// Update telemetry setting
if (ParentFrame.telemetryCheckbox) {
diagnostics.setSendTelemetry(ParentFrame.telemetryCheckbox.checked);
}
dialogSettings.hidden = true;
});
const diagButton = document.getElementById("actionsSettings-diag");
diagButton?.addEventListener("click", () => {
const diagnosticsText = ParentFrameUtils.getDiagnosticsString();
const diagnosticsElement = document.getElementById("diagnostics");
if (diagnosticsElement) {
diagnosticsElement.textContent = diagnosticsText;
}
// Hide settings dialog and show diagnostics dialog
dialogSettings.hidden = true;
dialogDiagnostics.hidden = false;
document.getElementById("diagpre")?.focus();
});
const diagOkButton = document.getElementById("actionsDiag-OK");
diagOkButton?.addEventListener("click", () => {
dialogDiagnostics.hidden = true;
});
const settingsButton = document.getElementById("settingsButton");
settingsButton?.addEventListener("click", () => {
// Set the current choice in the radio group
const radioGroup = document.getElementById("uiChoice") as FluentRadioGroup;
if (radioGroup) {
const currentIndex = ParentFrame.choices.findIndex(c => c.label === ParentFrame.currentChoice.label);
if (currentIndex >= 0) {
radioGroup.value = currentIndex.toString();
}
}
dialogSettings.hidden = false;
});
const copyButton = document.getElementById("copyButton");
copyButton?.addEventListener("click", () => {
Strings.copyToClipboard(ParentFrame.modelToString);
// Show status message for accessibility
const statusMessage = document.getElementById("statusMessage");
if (statusMessage) {
statusMessage.classList.add("show");
// Hide after 2 seconds
setTimeout(() => {
statusMessage.classList.remove("show");
}, 2000);
}
// Maintain focus on copy button for keyboard users
copyButton.focus();
});
// Initialize tab navigation handling
TabNavigation.initialize();
}
public static setSendTelemetryUI(sendTelemetry: boolean) {
if (ParentFrame.telemetryCheckbox) {
ParentFrame.telemetryCheckbox.checked = sendTelemetry;
}
}
public static async initUI() {
ParentFrame.setDefault();
ParentFrame.addChoices();
ParentFrame.initFluent();
try {
const choice: Choice = Office.context.roamingSettings.get(ParentFrameUtils.getSettingsKey());
const sendTelemetry: boolean = Office.context.roamingSettings.get("sendTelemetry");
diagnostics.initSendTelemetry(sendTelemetry);
ParentFrame.go(choice);
} catch {
ParentFrame.goDefaultChoice();
}
ParentFrame.registerItemChangedEvent();
window.addEventListener("message", ParentFrame.eventListener, false);
await ParentFrame.loadNewItem();
}
public static get choice(): Choice { return ParentFrame.currentChoice; }
}
|