<commit_before>/*
Copyright 2013 OCAD University

Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.

You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/

// Declare dependencies
/*global demo:true, fluid, gpii, jQuery, window*/

// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */

(function ($, fluid) {

    fluid.registerNamespace("gpii.discoveryTool");

    /**
     * These paths will need to be customized for the integration
     */
    fluid.defaults("gpii.discoveryTool.templateLoader", {
        gradeNames: ["fluid.uiOptions.resourceLoader", "autoInit"],
        templates: {
            highContrast: "%prefix/HighContrastPanelTemplate.html",
            lowContrast: "%prefix/LowContrastPanelTemplate.html",
            increaseSize: "%prefix/IncreaseSizePanelTemplate.html",
            simplify: "%prefix/SimplifyPanelTemplate.html",
            spoken: "%prefix/SpokenPanelTemplate.html",
            uiOptions: "%prefix/DiscoveryTool.html"
        }
    });

    fluid.defaults("gpii.discoveryTool.messageLoader", {
        gradeNames: ["fluid.uiOptions.resourceLoader", "autoInit"],
        templates: {
            uiOptions: "%prefix/DiscoveryTool.json"
        }
    });

    fluid.defaults("gpii.discoveryTool.rootModel", {
        gradeNames: ["fluid.uiOptions.rootModel", "autoInit"],
        members: {
            rootModel: {
                highContrast: false,
                lowContrast: false,
                increaseSize: false,
                simplify: false,
                spoken: false,

                textFont: "default",
                theme: "default",
                textSize: 1,
                lineSpace: 1,
                toc: false,
                links: false,
                inputsLarger: false,
                simplifyContent: false,
                selfVoicing: false
            }
        }
    });

    /*****************
     * The Discovery Tool
     * An instance of a Fat Panel UIO
     ****************/
    fluid.defaults("gpii.discoveryTool", {
        gradeNames: ["fluid.uiOptions.fatPanel", "autoInit"],
        selectors: {
            discoverIcon: ".flc-icon-discover"
        },
        keyBinding: {
            hideTool: $.ui.keyCode.ESCAPE
        },
        slidingPanel: {
            strings: {
                showLabel: {
                    expander: {
                        func: "gpii.discoveryTool.lookupMsg",
                        args: ["{slidingPanel}.msgBundle", "slidingPanelShowLabel"]
                    }
                },
                hideLabel: {
                    expander: {
                        func: "gpii.discoveryTool.lookupMsg",
                        args: ["{slidingPanel}.msgBundle", "slidingPanelHideLabel"]
                    }
                }                    
            },
            invokers: {
                showDiscoveryIcon: {
                    "this": "{discoveryTool}.dom.discoverIcon",
                    method: "show"
                },
                hideDiscoveryIcon: {
                    "this": "{discoveryTool}.dom.discoverIcon",
                    method: "hide"
                },
                setLabel: {
                    "this": "{that}.dom.toggleButton",
                    method: "attr",
                    args: ["aria-label", "{arguments}.0"]
                }
            },
            listeners: {
                "onCreate.showIcon": "{that}.showDiscoveryIcon",
                "onCreate.showLabel": {
                    listener: "{that}.setLabel",                    
                    args: "{that}.options.strings.showLabel"
                },
                "onPanelHide.showIcon": "{that}.showDiscoveryIcon",
                "onPanelHide.showLabel": {
                    listener: "{that}.setLabel",                    
                    args: "{that}.options.strings.showLabel"
                },
                "onPanelShow.showIcon": "{that}.hideDiscoveryIcon",
                "onPanelShow.showLabel": {
                    listener: "{that}.setLabel",                    
                    args: "{that}.options.strings.hideLabel"
                },                    
            }
        },
        invokers: {
            hideToolPanel: "{slidingPanel}.hidePanel",
            hideToolPanelInIframe: {
                funcName: "gpii.discoveryTool.hideToolPanelInIframe",
                args: ["{that}.hideToolPanel", "{slidingPanel}.dom.toggleButton"]
            }
        },
        listeners: {
            afterRender: {
                listener: "gpii.discoveryTool.initHideFuncs",
                args: "{that}",
                priority: "last"
            }
        }
    });

    // Pressing escape inside the discovery tool does:
    // 1. Hide the tool panel;
    // 2. Move focus to "show/hide" button
    gpii.discoveryTool.hideToolPanelInIframe = function (genericHideFunc, elementToFocus) {
        genericHideFunc();
        elementToFocus.focus();
    };

    gpii.discoveryTool.bindHideKey = function (key, element, hideFunc) {
        if (!element) {
            return;
        }

        var keybindingOpts = {
            additionalBindings: [{
                key: key,
                activateHandler: hideFunc
            }]
        };

        element.fluid("tabbable");
        fluid.activatable(element, null, keybindingOpts);
    };

    // Hide the discovery tool when clicking outside of the discovery tool panel or pressing escape
    gpii.discoveryTool.initHideFuncs = function (that) {
        var html = $("html");

        html.click(function () {
            that.hideToolPanel();
        });

        that.container.click(function (event) {
            event.stopPropagation();
        });

        var iframeHtml = that.iframeRenderer.iframe.contents().find("html");

        // Bind hide function onto the main page and the iframe for discovery tool
        gpii.discoveryTool.bindHideKey(that.options.keyBinding.hideTool, html, that.hideToolPanel);
        gpii.discoveryTool.bindHideKey(that.options.keyBinding.hideTool, iframeHtml, that.hideToolPanelInIframe);
    };
    
    // Currently this code is duplicated from SlidingPanel.js
    // FLUID-5119 filed to move it to the framework, after which this should be removed in favour of the
    // generalized code.
    gpii.discoveryTool.lookupMsg = function (messageResolver, value) {
        var looked = messageResolver.lookup([value]);
        return looked ? looked.template : looked;
    };    

    /*************
     * The base grade component for each individual panel
     *************/
    fluid.defaults("gpii.discoveryTool.defaultPanel", {
        gradeNames: ["fluid.uiOptions.defaultPanel", "autoInit"],
        sourceApplier: "{modelTransformer}.applier"
    });

    /*************
     * A component that transforms the panels' boolean models into UIO model values
     * and relays the changes
     *************/
    fluid.defaults("gpii.discoveryTool.modelTransformer", {
        gradeNames: ["fluid.modelComponent", "fluid.uiOptions.modelRelay", "autoInit"],
        model: {
            panelSelections: {
                highContrast: false,
                lowContrast: false,
                increaseSize: false,
                simplify: false,
                spoken: false
            },
            convertedModel: {
                textFont: "default",
                theme: "default",<commit_msg>GPII-175: Fixed the safari problem that pressing escape to hide discovery tool breaks the keyboard functionality of opening/closing the tool panel.<commit_after>/*
Copyright 2013 OCAD University

Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.

You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/

// Declare dependencies
/*global demo:true, fluid, gpii, jQuery, window*/

// JSLint options
/*jslint white: true, funcinvoke: true, undef: true, newcap: true, nomen: true, regexp: true, bitwise: true, browser: true, forin: true, maxerr: 100, indent: 4 */

(function ($, fluid) {

    fluid.registerNamespace("gpii.discoveryTool");

    /**
     * These paths will need to be customized for the integration
     */
    fluid.defaults("gpii.discoveryTool.templateLoader", {
        gradeNames: ["fluid.uiOptions.resourceLoader", "autoInit"],
        templates: {
            highContrast: "%prefix/HighContrastPanelTemplate.html",
            lowContrast: "%prefix/LowContrastPanelTemplate.html",
            increaseSize: "%prefix/IncreaseSizePanelTemplate.html",
            simplify: "%prefix/SimplifyPanelTemplate.html",
            spoken: "%prefix/SpokenPanelTemplate.html",
            uiOptions: "%prefix/DiscoveryTool.html"
        }
    });

    fluid.defaults("gpii.discoveryTool.messageLoader", {
        gradeNames: ["fluid.uiOptions.resourceLoader", "autoInit"],
        templates: {
            uiOptions: "%prefix/DiscoveryTool.json"
        }
    });

    fluid.defaults("gpii.discoveryTool.rootModel", {
        gradeNames: ["fluid.uiOptions.rootModel", "autoInit"],
        members: {
            rootModel: {
                highContrast: false,
                lowContrast: false,
                increaseSize: false,
                simplify: false,
                spoken: false,

                textFont: "default",
                theme: "default",
                textSize: 1,
                lineSpace: 1,
                toc: false,
                links: false,
                inputsLarger: false,
                simplifyContent: false,
                selfVoicing: false
            }
        }
    });

    /*****************
     * The Discovery Tool
     * An instance of a Fat Panel UIO
     ****************/
    fluid.defaults("gpii.discoveryTool", {
        gradeNames: ["fluid.uiOptions.fatPanel", "autoInit"],
        selectors: {
            discoverIcon: ".flc-icon-discover"
        },
        keyBinding: {
            hideTool: $.ui.keyCode.ESCAPE
        },
        slidingPanel: {
            strings: {
                showLabel: {
                    expander: {
                        func: "gpii.discoveryTool.lookupMsg",
                        args: ["{slidingPanel}.msgBundle", "slidingPanelShowLabel"]
                    }
                },
                hideLabel: {
                    expander: {
                        func: "gpii.discoveryTool.lookupMsg",
                        args: ["{slidingPanel}.msgBundle", "slidingPanelHideLabel"]
                    }
                }
            },
            invokers: {
                showDiscoveryIcon: {
                    "this": "{discoveryTool}.dom.discoverIcon",
                    method: "show"
                },
                hideDiscoveryIcon: {
                    "this": "{discoveryTool}.dom.discoverIcon",
                    method: "hide"
                },
                setLabel: {
                    "this": "{that}.dom.toggleButton",
                    method: "attr",
                    args: ["aria-label", "{arguments}.0"]
                }
            },
            listeners: {
                "onCreate.showIcon": "{that}.showDiscoveryIcon",
                "onCreate.showLabel": {
                    listener: "{that}.setLabel",
                    args: "{that}.options.strings.showLabel"
                },
                "onPanelHide.showIcon": "{that}.showDiscoveryIcon",
                "onPanelHide.showLabel": {
                    listener: "{that}.setLabel",
                    args: "{that}.options.strings.showLabel"
                },
                "onPanelShow.showIcon": "{that}.hideDiscoveryIcon",
                "onPanelShow.showLabel": {
                    listener: "{that}.setLabel",
                    args: "{that}.options.strings.hideLabel"
                },
            }
        },
        invokers: {
            hideToolPanel: "{slidingPanel}.hidePanel",
            hideToolPanelInIframe: {
                funcName: "gpii.discoveryTool.hideToolPanelInIframe",
                args: ["{that}.hideToolPanel", "{slidingPanel}.dom.toggleButton"]
            }
        },
        listeners: {
            afterRender: {
                listener: "gpii.discoveryTool.initHideFuncs",
                args: "{that}",
                priority: "last"
            }
        }
    });

    // Pressing escape inside the discovery tool does:
    // 1. Hide the tool panel;
    // 2. Move focus to "show/hide" button
    gpii.discoveryTool.hideToolPanelInIframe = function (genericHideFunc, elementToFocus) {
        genericHideFunc();
        elementToFocus.focus();
    };

    gpii.discoveryTool.bindHideKey = function (key, element, hideFunc) {
        if (!element) {
            return;
        }

        var keybindingOpts = {
            additionalBindings: [{
                key: key,
                activateHandler: function () {
                    hideFunc();
                    return false;
                }
            }]
        };

        element.fluid("tabbable");
        fluid.activatable(element, null, keybindingOpts);
    };

    // Hide the discovery tool when clicking outside of the discovery tool panel or pressing escape
    gpii.discoveryTool.initHideFuncs = function (that) {
        var html = $("html");

        html.click(function () {
            that.hideToolPanel();
        });

        that.container.click(function (event) {
            event.stopPropagation();
        });

        var iframeHtml = that.iframeRenderer.iframe.contents().find("html");

        // Bind hide function onto the main page and the iframe for discovery tool
        gpii.discoveryTool.bindHideKey(that.options.keyBinding.hideTool, html, that.hideToolPanel);
        gpii.discoveryTool.bindHideKey(that.options.keyBinding.hideTool, iframeHtml, that.hideToolPanelInIframe);
    };

    // Currently this code is duplicated from SlidingPanel.js
    // FLUID-5119 filed to move it to the framework, after which this should be removed in favour of the
    // generalized code.
    gpii.discoveryTool.lookupMsg = function (messageResolver, value) {
        var looked = messageResolver.lookup([value]);
        return looked ? looked.template : looked;
    };

    /*************
     * The base grade component for each individual panel
     *************/
    fluid.defaults("gpii.discoveryTool.defaultPanel", {
        gradeNames: ["fluid.uiOptions.defaultPanel", "autoInit"],
        sourceApplier: "{modelTransformer}.applier"
    });

    /*************
     * A component that transforms the panels' boolean models into UIO model values
     * and relays the changes
     *************/
    fluid.defaults("gpii.discoveryTool.modelTransformer", {
        gradeNames: ["fluid.modelComponent", "fluid.uiOptions.modelRelay", "autoInit"],
        model: {
            panelSelections: {
                highContrast: false,
                lowContrast: false,
                increaseSize: false,
                simplify: false,
                spoken: false
            },
            convertedModel: {
                textFont: "default",
                theme: "default",
{'fraction_non_alphanumeric': 0.11661658461735183, 'fraction_numerical': 0.002877053896809667, 'mean_word_length': 1.6820987654320987, 'pattern_counts': {'":': 18, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1351321', 'n_tokens_mistral': 4229, 'n_tokens_neox': 3915, 'n_words': 874}