<commit_before>/* global $ */
/* global fabric */
/* global Office */
/* global sendHeadersRequest */
/* global Diagnostics */
/* global Errors */
/* global appInsights */
/* exported ParentFrame */

// Controller for Settings screen which controls what is being displayed
// and which UI to use.

var ParentFrame = (function () {
    var iFrame = null;
    var currentChoice = {};
    var deferredErrors = [];
    var deferredStatus = [];
    var headers = "";

    function choice(label, url, checked) {
        return { label: label, url: url, checked: checked };
    }

    var choices = [
        choice("classic", "classicDesktopFrame.html", false),
        choice("new", "newDesktopFrame.html", true),
        choice("new-mobile", "newMobilePaneIosFrame.html", false)
    ];

    function setDefault() {
        var uiDefault = getQueryVariable("default");
        if (uiDefault === null) {
            uiDefault = "new";
        }

        for (var iChoice = 0; iChoice < choices.length; iChoice++) {
            if (uiDefault === choices[iChoice].label) {
                choices[iChoice].checked = true;
            } else {
                choices[iChoice].checked = false;
            }
        }
    }

    function getQueryVariable(variable) {
        var vars = window.location.search.substring(1).split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] === variable) {
                return pair[1];
            }
        }
        return null;
    }

    function site() { return window.location.protocol + "//" + window.location.host; }

    function postMessageToFrame(eventName, data) {
        if (iFrame) {
            iFrame.postMessage({ eventName: eventName, data: data }, site());
        }
    }

    function eventListener(event) {
        if (!event || event.origin !== site()) return;

        if (event.data) {
            switch (event.data.eventName) {
                case "frameActive":
                    setFrame(event.source);
                    break;
                case "LogError":
                    Errors.log(JSON.parse(event.data.data.error), event.data.data.message);
                    break;
            }
        }
    }

    function initUI() {
        setDefault();
        addChoices();
        initFabric();

        try {
            var choice = Office.context.roamingSettings.get(getSettingsKey());
            var input = $("#uiToggle" + choice.label);
            input.prop("checked", true);
            go(choice);
        } catch (e) {
            goDefaultChoice();
        }

        registerItemChangedEvent();

        window.addEventListener("message", eventListener, false);
        loadNewItem();
    }

    function registerItemChangedEvent() {
        try {
            if (Office.context.mailbox.addHandlerAsync !== undefined) {
                Office.context.mailbox.addHandlerAsync(Office.EventType.ItemChanged,
                    function () {
                        Errors.clear();
                        Diagnostics.clear();
                        loadNewItem();
                    });
            }
        } catch (e) {
            Errors.log(e, "Could not register item changed event");
        }
    }

    function loadNewItem() {
        if (Office.context.mailbox.item) {
            sendHeadersRequest(function (_headers, apiUsed) {
                headers = _headers;
                Diagnostics.set("API used", apiUsed);
                renderItem(headers);
            });
        }
    }

    function setFrame(frame) {
        iFrame = frame;

        if (iFrame) {
            // If we have any deferred status, signal them
            for (var iStatus = 0; iStatus < deferredStatus.length; iStatus++) {
                postMessageToFrame("updateStatus", deferredStatus[iStatus]);
            }

            // Clear out the now displayed status
            deferredStatus = [];

            // If we have any deferred errors, signal them
            for (var iError = 0; iError < deferredErrors.length; iError++) {
                postMessageToFrame("showError",
                    {
                        error: JSON.stringify(deferredErrors[iError][0]),
                        message: deferredErrors[iError][1]
                    });
            }

            // Clear out the now displayed errors
            deferredErrors = [];

            renderItem(headers);
        }
    }

    function renderItem(headers) {
        if (appInsights && headers) appInsights.trackEvent("analyzeHeaders: ");
        postMessageToFrame("renderItem", headers);
    }

    // Tells the UI to show an error.
    function showError(error, message, suppressTracking) {
        Errors.log(error, message, suppressTracking);

        if (iFrame) {
            postMessageToFrame("showError", { error: JSON.stringify(error), message: message });
        } else {
            // We don't have an iFrame, so defer the message
            deferredErrors.push([error, message]);
        }
    }

    // Tells the UI to show an error.<commit_msg>fiurther cleanup<commit_after>/* global $ */
/* global fabric */
/* global Office */
/* global sendHeadersRequest */
/* global Diagnostics */
/* global Errors */
/* global appInsights */
/* exported ParentFrame */

// Controller for Settings screen which controls what is being displayed
// and which UI to use.

var ParentFrame = (function () {
    var iFrame = null;
    var currentChoice = {};
    var deferredErrors = [];
    var deferredStatus = [];
    var headers = "";

    function choice(label, url, checked) {
        return { label: label, url: url, checked: checked };
    }

    var choices = [
        choice("classic", "classicDesktopFrame.html", false),
        choice("new", "newDesktopFrame.html", true),
        choice("new-mobile", "newMobilePaneIosFrame.html", false)
    ];

    function setDefault() {
        var uiDefault = getQueryVariable("default");
        if (uiDefault === null) {
            uiDefault = "new";
        }

        for (var iChoice = 0; iChoice < choices.length; iChoice++) {
            if (uiDefault === choices[iChoice].label) {
                choices[iChoice].checked = true;
            } else {
                choices[iChoice].checked = false;
            }
        }
    }

    function getQueryVariable(variable) {
        var vars = window.location.search.substring(1).split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] === variable) {
                return pair[1];
            }
        }
        return null;
    }

    function site() { return window.location.protocol + "//" + window.location.host; }

    function postMessageToFrame(eventName, data) {
        if (iFrame) {
            iFrame.postMessage({ eventName: eventName, data: data }, site());
        }
    }

    function eventListener(event) {
        if (!event || event.origin !== site()) return;

        if (event.data) {
            switch (event.data.eventName) {
                case "frameActive":
                    setFrame(event.source);
                    break;
                case "LogError":
                    Errors.log(JSON.parse(event.data.data.error), event.data.data.message);
                    break;
            }
        }
    }

    function initUI() {
        setDefault();
        addChoices();
        initFabric();

        try {
            var choice = Office.context.roamingSettings.get(getSettingsKey());
            var input = $("#uiToggle" + choice.label);
            input.prop("checked", true);
            go(choice);
        } catch (e) {
            goDefaultChoice();
        }

        registerItemChangedEvent();

        window.addEventListener("message", eventListener, false);
        loadNewItem();
    }

    function registerItemChangedEvent() {
        try {
            if (Office.context.mailbox.addHandlerAsync !== undefined) {
                Office.context.mailbox.addHandlerAsync(Office.EventType.ItemChanged,
                    function () {
                        Errors.clear();
                        Diagnostics.clear();
                        loadNewItem();
                    });
            }
        } catch (e) {
            Errors.log(e, "Could not register item changed event");
        }
    }

    function loadNewItem() {
        if (Office.context.mailbox.item) {
            sendHeadersRequest(function (_headers, apiUsed) {
                headers = _headers;
                Diagnostics.set("API used", apiUsed);
                renderItem(headers);
            });
        }
    }

    function setFrame(frame) {
        iFrame = frame;

        if (iFrame) {
            // If we have any deferred status, signal them
            for (var iStatus = 0; iStatus < deferredStatus.length; iStatus++) {
                postMessageToFrame("updateStatus", deferredStatus[iStatus]);
            }

            // Clear out the now displayed status
            deferredStatus = [];

            // If we have any deferred errors, signal them
            for (var iError = 0; iError < deferredErrors.length; iError++) {
                postMessageToFrame("showError",
                    {
                        error: JSON.stringify(deferredErrors[iError][0]),
                        message: deferredErrors[iError][1]
                    });
            }

            // Clear out the now displayed errors
            deferredErrors = [];

            renderItem(headers);
        }
    }

    function renderItem(headers) {
        if (appInsights && headers) appInsights.trackEvent("analyzeHeaders");
        postMessageToFrame("renderItem", headers);
    }

    // Tells the UI to show an error.
    function showError(error, message, suppressTracking) {
        Errors.log(error, message, suppressTracking);

        if (iFrame) {
            postMessageToFrame("showError", { error: JSON.stringify(error), message: message });
        } else {
            // We don't have an iFrame, so defer the message
            deferredErrors.push([error, message]);
        }
    }

    // Tells the UI to show an error.
{'fraction_non_alphanumeric': 0.11864406779661017, 'fraction_numerical': 0.0017634956402468895, 'mean_word_length': 1.6743515850144093, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '613733', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2560, 'n_words': 626}