<commit_before>/**
 * @author <NAME> from Ubrainians for imCode
 * 20.08.18
 */
define(
    'imcms-html-filtering-policy-plugin',
    [
        'imcms-text-editor-toolbar-button-builder', 'imcms-html-filtering-policies', 'imcms-bem-builder', 'jquery',
        'imcms-text-editor-utils', 'imcms-i18n-texts'
    ],
    function (toolbarButtonBuilder, filteringPolicies, BEM, $, textUtils, texts) {

        texts = texts.toolTipText;

        const title = texts.htmlContent;

        const policyToName = {};
        policyToName[filteringPolicies.restricted] = texts.filterPolicy.restricted;
        policyToName[filteringPolicies.relaxed] = texts.filterPolicy.relaxed;
        policyToName[filteringPolicies.allowAll] = texts.filterPolicy.allowedAll;

        const policyToTitle = {};
        policyToTitle[filteringPolicies.restricted] = texts.filterPolicy.titleRestricted;
        policyToTitle[filteringPolicies.relaxed] = texts.filterPolicy.titleRelaxed;
        policyToTitle[filteringPolicies.allowAll] = texts.filterPolicy.titleAllowedAll;

        function getOnClick(editor, $btn) {
            const $textEditor = $(editor.$());

            return e => {
                const $target = $(e.target);

                if ($target.hasClass('settings-section__setting') && $target.parents('.filtering-policies').length) {
                    return;
                }

                const $policies = buildPoliciesSelect();

                $btn.append($policies);

                setTimeout(() => {
                    $(document).one('click', e => {
                        e.preventDefault();
                        e.stopPropagation();

                        const $target = $(e.target);

                        if ($target.hasClass('settings-section__setting') && $target.parents('.filtering-policies').length) {
                            const policy = $target.attr('data-policy');

                            $textEditor.attr('data-html-filtering-policy', policy)
                                .data('htmlFilteringPolicy', policy);


                            textUtils.saveContent(editor, textDTO => {
                                $textEditor.html(textDTO.text);
                            }, true);
                        }

                        $policies.remove();

                        return false;
                    });
                });
            };
        }

        function buildPoliciesSelect() {

            const elements = Object.keys(filteringPolicies).map(key => {
                const policyName = filteringPolicies[key];

                return {
                    'policy': $('<div>', {
                        'class': 'settings-section__setting',
                        text: policyToName[policyName],
                        title: policyToTitle[policyName],
                        'data-policy': policyName
                    })
                }
            });

            return new BEM({
                block: 'filtering-policies',
                elements: elements
            }).buildBlockStructure('<div>')
        }

        return {
            pluginName: '/html_filtering_policy',
            initHtmlFilteringPolicy: function (editor) {<commit_msg>Issue IMCMS-483: Improve the text-editor - Delete option "No filter"; - Remove saving from filter;<commit_after>/**
 * @author <NAME> from Ubrainians for imCode
 * 20.08.18
 */
define(
    'imcms-html-filtering-policy-plugin',
    [
        'imcms-text-editor-toolbar-button-builder', 'imcms-html-filtering-policies', 'imcms-bem-builder', 'jquery',
        'imcms-text-editor-utils', 'imcms-i18n-texts'
    ],
    function (toolbarButtonBuilder, filteringPolicies, BEM, $, textUtils, texts) {

        texts = texts.toolTipText;

        const title = texts.htmlContent;

        const policyToName = {};
        policyToName[filteringPolicies.restricted] = texts.filterPolicy.restricted;
        policyToName[filteringPolicies.relaxed] = texts.filterPolicy.relaxed;

        const policyToTitle = {};
        policyToTitle[filteringPolicies.restricted] = texts.filterPolicy.titleRestricted;
        policyToTitle[filteringPolicies.relaxed] = texts.filterPolicy.titleRelaxed;

        function getOnClick(editor, $btn) {
            const $textEditor = $(editor.$());

            return e => {
                const $target = $(e.target);

                if ($target.hasClass('settings-section__setting') && $target.parents('.filtering-policies').length) {
                    return;
                }

                const $policies = buildPoliciesSelect();

                $btn.append($policies);

                setTimeout(() => {
                    $(document).one('click', e => {
                        e.preventDefault();
                        e.stopPropagation();

                        const $target = $(e.target);

                        if ($target.hasClass('settings-section__setting') && $target.parents('.filtering-policies').length) {
                            const policy = $target.attr('data-policy');

                            $textEditor.attr('data-html-filtering-policy', policy)
                                .data('htmlFilteringPolicy', policy);


                            const filteringPolicy = $(editor.$()).data().htmlFilteringPolicy;
                            textUtils.filterContent(editor.getContent(), filteringPolicy, textDTO => {
                                $textEditor.html(textDTO.text);
                            });
                        }

                        $policies.remove();

                        return false;
                    });
                });
            };
        }

        function buildPoliciesSelect() {
            const displayedPolicies =[
                filteringPolicies.restricted,
                filteringPolicies.relaxed,
            ];

            const elements = displayedPolicies.map(policyName => ({
                    'policy': $('<div>', {
                        'class': 'settings-section__setting',
                        text: policyToName[policyName],
                        title: policyToTitle[policyName],
                        'data-policy': policyName
                    })
            }));

            return new BEM({
                block: 'filtering-policies',
                elements: elements
            }).buildBlockStructure('<div>')
        }

        return {
            pluginName: '/html_filtering_policy',
            initHtmlFilteringPolicy: function (editor) {
{'fraction_non_alphanumeric': 0.11594202898550725, 'fraction_numerical': 0.002898550724637681, 'mean_word_length': 1.5410852713178294, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '6276397', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1601, 'n_words': 251}