<commit_before>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013-2018 School of Business and Engineering Vaud, Comem, MEI
 * Licensed under the MIT License
 */


/* global I18n, tinyMCE, Promise */

/**
 * @fileOverview GameModel langueages management widgets
 * @author Maxence
 */
YUI.add('wegas-gamemodel-i18n', function(Y) {
    "use strict";
    var LanguagesManager,
        TranslationEditor,
        GameModelScriptUpgrader;


    function _getCfgFromNode(node) {
        var code = node.getAttribute("lang");
        if (node.hasClass("wegas-translation-inscript")) {
            var cfg = {
                type: "inscript",
                node: node,
                code: code,
                parentClass: node.getData('parentClass'),
                parentId: node.getData('parentId'),
                index: node.getData('index'),
                fieldName: node.getData('fieldName')
            };
            cfg.key = cfg.parentClass + "-" + cfg.parentId + "-" + cfg.fieldName + "#" + cfg.index + ":" + code;
            return cfg;
        } else {
            var trId = node.getData("trid");
            return {
                type: 'std',
                node: node,
                trId: trId,
                code: code,
                key: code + "-" + trId
            };
        }
    }

    LanguagesManager = Y.Base.create("wegas-i18n-manager", Y.Widget,
        [Y.WidgetParent, Y.WidgetChild, Y.Wegas.Editable, Y.Wegas.Parent], {
        initializer: function() {
            this.handlers = {};
            this.plug(Y.Plugin.TranslationEditor);
            this.plug(Y.Plugin.Injector);
            this.showTable = {};
            this.ghostLanguages = {};
            var langs = Y.Wegas.Facade.GameModel.cache.getCurrentGameModel().get("languages"),
                i, count = 0;
            // show first to languages by default
            for (i in langs) {
                if (langs[i] && langs[i].get("id") && count < 2) {
                    this.showTable[langs[i].get("id")] = true;
                    count++;
                }
            }
            this.isCurrentUserAdmin = !!Y.Wegas.Facade.User.cache.get("currentUser").get("roles").find(function(role) {
                return role.get("name") === "Administrator";
            });
        },
        destructor: function() {
            if (this.i18nMenu) {
                this.i18nMenu.destroy();
            }
            var k;
            for (k in this.handlers) {
                if (this.handlers.hasOwnProperty(k)) {
                    this.handlers[k].detach();
                }
            }
        },
        /**
         * Detect languages supported by translation service
         */
        initTranslationService: function() {
            Y.Wegas.Facade.GameModel.sendRequest({
                request: "/I18n/AvailableLanguages",
                cfg: {
                    method: "GET"
                },
                on: {
                    success: Y.bind(this.initSupportedLanguages, this),
                    failure: Y.bind(function() {
                        Y.Wegas.Alerts.showNotification("Error while fetching supported languages", {
                            iconCss: 'fa fa-warning'
                        });
                    }, this)
                }
            });
        },
        getUsage: function() {
            Y.Wegas.Facade.GameModel.sendRequest({
                request: "/I18n/Usage",
                cfg: {
                    method: "GET"
                },
                on: {
                    success: Y.bind(this.initUsage, this),
                    failure: Y.bind(function() {
                        Y.Wegas.Alerts.showNotification("Error while fetching usage", {
                            iconCss: 'fa fa-warning'
                        });
                    }, this)
                }
            });
        },
        getUsageRatio() {
            return this.usage.character_count / this.usage.character_limit;
        },
        initUsage: function(e) {
            this.usage = e.response.entity.get("val");
            this.isLimitReached = this.getUsageRatio() >= 1;
            this.syncUI();
        },
        initSupportedLanguages: function(e) {
            this.supportedLanguages = e.response.entities;
            this.syncUI();
        },
        renderUI: function() {
            this.header = new Y.Wegas.FlexList({
                cssClass: "wegas-i18n-manager--header",
                direction: 'horizontal'
            });

            this.title = new Y.Wegas.Text({
                cssClass: "wegas-i18n-manager--title",
                content: I18n.t("i18n.manager.title")
            });
            this.refreshButton = new Y.Button({
                label: "<i class=\"fa fa-refresh\"></i>"
            });
            this.addBtn = new Y.Wegas.Text({
                cssClass: "create-button",<commit_msg>fix ci<commit_after>/*
 * Wegas
 * http://wegas.albasim.ch
 *
 * Copyright (c) 2013-2018 School of Business and Engineering Vaud, Comem, MEI
 * Licensed under the MIT License
 */


/* global I18n, tinyMCE, Promise */

/**
 * @fileOverview GameModel langueages management widgets
 * @author Maxence
 */
YUI.add('wegas-gamemodel-i18n', function(Y) {
    "use strict";
    var LanguagesManager,
        TranslationEditor,
        GameModelScriptUpgrader;


    function _getCfgFromNode(node) {
        var code = node.getAttribute("lang");
        if (node.hasClass("wegas-translation-inscript")) {
            var cfg = {
                type: "inscript",
                node: node,
                code: code,
                parentClass: node.getData('parentClass'),
                parentId: node.getData('parentId'),
                index: node.getData('index'),
                fieldName: node.getData('fieldName')
            };
            cfg.key = cfg.parentClass + "-" + cfg.parentId + "-" + cfg.fieldName + "#" + cfg.index + ":" + code;
            return cfg;
        } else {
            var trId = node.getData("trid");
            return {
                type: 'std',
                node: node,
                trId: trId,
                code: code,
                key: code + "-" + trId
            };
        }
    }

    LanguagesManager = Y.Base.create("wegas-i18n-manager", Y.Widget,
        [Y.WidgetParent, Y.WidgetChild, Y.Wegas.Editable, Y.Wegas.Parent], {
        initializer: function() {
            this.handlers = {};
            this.plug(Y.Plugin.TranslationEditor);
            this.plug(Y.Plugin.Injector);
            this.showTable = {};
            this.ghostLanguages = {};
            var langs = Y.Wegas.Facade.GameModel.cache.getCurrentGameModel().get("languages"),
                i, count = 0;
            // show first to languages by default
            for (i in langs) {
                if (langs[i] && langs[i].get("id") && count < 2) {
                    this.showTable[langs[i].get("id")] = true;
                    count++;
                }
            }
            this.isCurrentUserAdmin = !!Y.Wegas.Facade.User.cache.get("currentUser").get("roles").find(function(role) {
                return role.get("name") === "Administrator";
            });
        },
        destructor: function() {
            if (this.i18nMenu) {
                this.i18nMenu.destroy();
            }
            var k;
            for (k in this.handlers) {
                if (this.handlers.hasOwnProperty(k)) {
                    this.handlers[k].detach();
                }
            }
        },
        /**
         * Detect languages supported by translation service
         */
        initTranslationService: function() {
            Y.Wegas.Facade.GameModel.sendRequest({
                request: "/I18n/AvailableLanguages",
                cfg: {
                    method: "GET"
                },
                on: {
                    success: Y.bind(this.initSupportedLanguages, this),
                    failure: Y.bind(function() {
                        Y.Wegas.Alerts.showNotification("Error while fetching supported languages", {
                            iconCss: 'fa fa-warning'
                        });
                    }, this)
                }
            });
        },
        getUsage: function() {
            Y.Wegas.Facade.GameModel.sendRequest({
                request: "/I18n/Usage",
                cfg: {
                    method: "GET"
                },
                on: {
                    success: Y.bind(this.initUsage, this),
                    failure: Y.bind(function() {
                        Y.Wegas.Alerts.showNotification("Error while fetching usage", {
                            iconCss: 'fa fa-warning'
                        });
                    }, this)
                }
            });
        },
        getUsageRatio: function() {
            return this.usage.character_count / this.usage.character_limit;
        },
        initUsage: function(e) {
            this.usage = e.response.entity.get("val");
            this.isLimitReached = this.getUsageRatio() >= 1;
            this.syncUI();
        },
        initSupportedLanguages: function(e) {
            this.supportedLanguages = e.response.entities;
            this.syncUI();
        },
        renderUI: function() {
            this.header = new Y.Wegas.FlexList({
                cssClass: "wegas-i18n-manager--header",
                direction: 'horizontal'
            });

            this.title = new Y.Wegas.Text({
                cssClass: "wegas-i18n-manager--title",
                content: I18n.t("i18n.manager.title")
            });
            this.refreshButton = new Y.Button({
                label: "<i class=\"fa fa-refresh\"></i>"
            });
            this.addBtn = new Y.Wegas.Text({
                cssClass: "create-button",
{'fraction_non_alphanumeric': 0.12607420887675666, 'fraction_numerical': 0.006672732787382469, 'mean_word_length': 1.2924681344148319, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, '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': '2498990', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2653, 'n_words': 477}