<commit_before>/**
 * Created by <NAME> from Ubrainians for imCode
 * 24.07.17.
 */
Imcms.define("imcms-flags-builder", ["imcms-bem-builder", "imcms-languages-rest-api", "imcms", "jquery"], function (bemBuilder, languagesRestApi, imcms, $) {
    var FLAGS_CLASS = "imcms-flag",
        FLAG_ACTIVE_CLASS = FLAGS_CLASS + "--" + "active"
    ;

    var flagsBEM = new bemBuilder({
        block: "imcms-flags",
        elements: {
            "flag": FLAGS_CLASS
        }
    });

    function changeNeighborFlag($btn) {
        var $neighborFlag = ($btn.next("." + FLAGS_CLASS).length !== 0)
            ? $btn.next("." + FLAGS_CLASS)
            : $btn.prev("." + FLAGS_CLASS);

        $neighborFlag.toggleClass(FLAG_ACTIVE_CLASS);
    }

    function onFlagClick(event) {
        var $clickedFlag = $(this);

        if (!$clickedFlag.hasClass(FLAGS_CLASS)) {
            return;
        }

        if ($clickedFlag.hasClass(FLAG_ACTIVE_CLASS)) {
            event.preventDefault();

        } else {
            $clickedFlag.addClass(FLAG_ACTIVE_CLASS);
            changeNeighborFlag($clickedFlag);
        }
    }

    function buildFlag(tag, attributes, isActive, language) {
        var modifiers = [language.code];
        if (isActive) {
            modifiers.push("active");
        }

        attributes = attributes || {};
        attributes.title = language.name + "/" + language.nativeName;

        return flagsBEM.buildElement("flag", tag, attributes, modifiers).click(onFlagClick);
    }

    function mapLanguagesToFlags(languages, flagBuilderDataProducer) {
        return languages.map(function (language) {
            var flagBuilderData = flagBuilderDataProducer(language),
                isActive = language.code === imcms.language.code;

            return buildFlag.apply(null, flagBuilderData.concat([isActive, language]));
        });
    }

    return {
        flagsContainer: function (flagBuilderDataProducer) {
            var $result = flagsBEM.buildBlock("<div>", [], "flag");
            languagesRestApi.read().done(function (languages) {
                var flags = mapLanguagesToFlags(languages, flagBuilderDataProducer);
                $result.append(flags);
            });
            return $result;
        }
    }
});<commit_msg>IMCMS-273 - Implement languages switch: - Reload page when language is changed.<commit_after>/**
 * Created by <NAME> from Ubrainians for imCode
 * 24.07.17.
 */
Imcms.define("imcms-flags-builder", ["imcms-bem-builder", "imcms-languages-rest-api", "imcms", "jquery"], function (bemBuilder, languagesRestApi, imcms, $) {
    var FLAGS_CLASS = "imcms-flag",
        FLAG_ACTIVE_CLASS = FLAGS_CLASS + "--" + "active"
    ;

    var flagsBEM = new bemBuilder({
        block: "imcms-flags",
        elements: {
            "flag": FLAGS_CLASS
        }
    });

    function changeNeighborFlag($btn) {
        var $neighborFlag = ($btn.next("." + FLAGS_CLASS).length !== 0)
            ? $btn.next("." + FLAGS_CLASS)
            : $btn.prev("." + FLAGS_CLASS);

        $neighborFlag.toggleClass(FLAG_ACTIVE_CLASS);
    }

    function onFlagClick(event) {
        var $clickedFlag = $(this);

        if (!$clickedFlag.hasClass(FLAGS_CLASS)) {
            return;
        }

        if ($clickedFlag.hasClass(FLAG_ACTIVE_CLASS)) {
            event.preventDefault();

        } else {
            $clickedFlag.addClass(FLAG_ACTIVE_CLASS);
            changeNeighborFlag($clickedFlag);
        }
    }

    function buildFlag(tag, attributes, isActive, language) {
        var modifiers = [language.code];
        if (isActive) {
            modifiers.push("active");
        }

        attributes = attributes || {};
        attributes.title = language.name + "/" + language.nativeName;

        return flagsBEM.buildElement("flag", tag, attributes, modifiers).click(onFlagClick);
    }

    function mapLanguagesToFlags(languages, flagBuilderDataProducer) {
        return languages.map(function (language) {
            var flagBuilderData = flagBuilderDataProducer(language),
                isActive = language.code === imcms.language.code;

            return buildFlag.apply(null, flagBuilderData.concat([isActive, language]));
        });
    }

    function flagOnClick() {
        languagesRestApi.update({code: $(this).text()})
            .done(function () {
                location.reload(true);
            })
    }

    return {
        flagsContainer: function (flagBuilderDataProducer) {
            var $result = flagsBEM.buildBlock("<div>", [], "flag");
            languagesRestApi.read().done(function (languages) {
                var flags = mapLanguagesToFlags(languages, flagBuilderDataProducer);

                flags.forEach(function (flag) {
                    flag.click(flagOnClick);
                });

                $result.append(flags);
            });
            return $result;
        }
    }
});
{'fraction_non_alphanumeric': 0.13230205577040505, 'fraction_numerical': 0.0034602076124567475, 'mean_word_length': 2.2350230414746544, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, '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': '2498667', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1388, 'n_words': 244}