<commit_before>import React, { Component, PropTypes } from "react";
import { connect } from "react-redux";
import "./style.less";
import resx from "../../resources";
import { Scrollbars } from "react-custom-scrollbars";
import Tooltip from "dnn-tooltip";
import InputGroup from "dnn-input-group";
import Switch from "dnn-switch";
import Label from "dnn-label";
import SocialPanelBody from "dnn-social-panel-body";
import TranslationProgressBars from "../languageSettings/TranslationProgressBars";
import Button from "dnn-button";
import {
    languages as LanguagesActions,
    siteInfo as SiteInfoActions
} from "actions";
import utils from "utils";


class TranslatePageContent extends Component {
    constructor() {
        super();
        this.state = {
            pageList: [],
            basicSettings: null,
            languageBeingEdited: null
        };
        this.getProgressData = this.getProgressData.bind(this);
    }

    componentWillMount() {
        const {props, state} = this;
        this.setState({ languageBeingEdited: Object.assign({}, props.languageBeingEdited) });
        this.getPageList();
        this.getBasicSettings();
        this.getProgressData();
    }

    getPageList() {
        const {props} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.getPageList({cultureCode, portalId}, (data) => {
            this.setState({ pageList: data });
        }));
    }

    getBasicSettings() {
        const {props, state} = this;
        props.dispatch(SiteInfoActions.getPortalSettings(props.portalId, props.cultureCode, (data) => {
            this.setState({
                basicSettings: Object.assign(data.Settings)
            });
        }));
    }

    getProgressData() {
        const {props, state} = this;
        props.dispatch(LanguagesActions.getLocalizationProgress((data) => {
            this.setState(data);
            this.getPageList();
            if (data.InProgress && !data.Error) {
                return setTimeout(this.getProgressData, 1000);
            }
            if (data.Error) {
                return;
            }
            this.doneProgress();
        }));
    }

    doneProgress() {
        this.getPageList();
    }

    renderPageList() {
        const {pageList} = this.state;
        if (!pageList) {
            return;
        }
        return pageList.map((page) => {
            return <div className="page-list-item">
                <span>{page.PageName}</span>
                <a className="float-right" onClick={this.goToPageSettings.bind(this, page.PageId) }>{resx.get("EditPageSettings") }</a>
                <a className="float-right" target="_blank" href={page.ViewUrl}>{resx.get("ViewPage") }</a>
            </div>;
        });
    }

    onMarkAllPagesAsTranslated(cultureCode) {
        const portalId = this.props.portalId;
        this.props.dispatch(LanguagesActions.markAllPagesAsTranslated({cultureCode, portalId}, () => {
            utils.utilities.notify(resx.get("PagesSuccessfullyTranslated"));
            this.getPageList();
        }));
    }

    onEraseAllLocalizedPages() {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = this.props.portalId;
        utils.utilities.confirm(resx.get("EraseTranslatedPagesWarning").replace("{0}", cultureCode), resx.get("Yes"), resx.get("No"), () => {
            props.dispatch(LanguagesActions.deleteLanguagePages({portalId, cultureCode}, (data) => {
                utils.utilities.notify(resx.get("DeletedAllLocalizedPages"));
                this.getPageList();
            }));
        });
    }

    onPublishTranslatedPages(enable = true) {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.publishAllPages({ portalId, cultureCode, enable }, (data) => {
            utils.utilities.notify(resx.get("PublishedAllTranslatedPages"));
        }));
    }

    addPages() {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.localizeContent({ cultureCode, portalId }, (data) => {
            this.getProgressData();
        }));
    }

    onCancel() {
        this.props.closePersonaBarPage();
    }

    goToPageSettings(pageId) {
        let personaBar = window.parent.dnn ? window.parent.dnn.PersonaBar : null;
        if (personaBar) {
            personaBar.openPanel('Pages',
                {
                    viewParams:
                    {
                        pageId,
                        viewTab: "localization",
                        referral: utils.moduleName,
                        referralText: resx.get("BackToLanguages").substring(1)
                    }
                }
            );

            let event = document.createEvent("Event");
            event.initEvent("viewPageSettings", true, true);

            event.pageId = pageId;
            event.viewTab = "localization";
            event.referral = utils.moduleName;
            event.referralText = resx.get("BackToLanguages").substring(1);

            document.dispatchEvent(event);
        }
    }
    toggleActivateLanguage(languageBeingEdited) {
        this.props.dispatch(LanguagesActions.activateLanguage({
            portalId: this.props.portalId,
            cultureCode: languageBeingEdited.Code,
            enable: languageBeingEdited.Active
        }));
    }
    onToggleActive(active) {
        if (!active) {
            let {languageBeingEdited} = this.state;
            const languageName = this.props.languageDisplayMode.toLowerCase() === "native" ? languageBeingEdited.NativeName : languageBeingEdited.EnglishName;
            utils.utilities.confirm(resx.get("DeactivateLanguageWarning").replace("{0}", languageName), resx.get("Yes"), resx.get("No"), () => {
                languageBeingEdited.Active = !languageBeingEdited.Active;
                this.toggleActivateLanguage(languageBeingEdited);
                this.setState({ languageBeingEdited });
            });
        } else {
            let {languageBeingEdited} = this.state;
            languageBeingEdited.Active = !languageBeingEdited.Active;
            this.toggleActivateLanguage(languageBeingEdited);
            this.setState({ languageBeingEdited });
        }
    }
    render() {
        const {props, state} = this;
        const language = state.languageBeingEdited;
        const isEnabled = !!this.state.pageList.length && language.Enabled;
        return <SocialPanelBody
            className="translate-page-content"
            workSpaceTrayOutside={true}
            workSpaceTray={<div className="siteSettings-back dnn-grid-cell" onClick={props.closePersonaBarPage}>
                {resx.get("BackToLanguages") }
            </div>}
            workSpaceTrayVisible={true}>

            <div className="language-settings-page-list">
                {language && <div className="top-block">
                    <div className="language-block">
                        <img className="float-left" src={language.Icon} />
                        <span >{language.NativeName}</span>
                        {state.basicSettings && <span className="float-right">{state.basicSettings.PortalName}</span>}
                    </div>
                    <InputGroup>
                        <div className="activate-pages-switch"><commit_msg>PAE-24 - small fix<commit_after>import React, { Component, PropTypes } from "react";
import { connect } from "react-redux";
import "./style.less";
import resx from "../../resources";
import { Scrollbars } from "react-custom-scrollbars";
import Tooltip from "dnn-tooltip";
import InputGroup from "dnn-input-group";
import Switch from "dnn-switch";
import Label from "dnn-label";
import SocialPanelBody from "dnn-social-panel-body";
import TranslationProgressBars from "../languageSettings/TranslationProgressBars";
import Button from "dnn-button";
import {
    languages as LanguagesActions,
    siteInfo as SiteInfoActions
} from "actions";
import utils from "utils";


class TranslatePageContent extends Component {
    constructor() {
        super();
        this.state = {
            pageList: [],
            basicSettings: null,
            languageBeingEdited: null
        };
        this.getProgressData = this.getProgressData.bind(this);
    }

    componentWillMount() {
        const {props, state} = this;
        this.setState({ languageBeingEdited: Object.assign({}, props.languageBeingEdited) });
        this.getPageList();
        this.getBasicSettings();
        this.getProgressData();
    }

    getPageList() {
        const {props} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.getPageList({ cultureCode, portalId }, (data) => {
            this.setState({ pageList: data });
        }));
    }

    getBasicSettings() {
        const {props, state} = this;
        props.dispatch(SiteInfoActions.getPortalSettings(props.portalId, props.cultureCode, (data) => {
            this.setState({
                basicSettings: Object.assign(data.Settings)
            });
        }));
    }

    getProgressData() {
        const {props, state} = this;
        props.dispatch(LanguagesActions.getLocalizationProgress((data) => {
            this.setState(data);
            this.getPageList();
            if (data.InProgress && !data.Error) {
                return setTimeout(this.getProgressData, 1000);
            }
            if (data.Error) {
                return;
            }
            this.doneProgress();
        }));
    }

    doneProgress() {
        this.getPageList();
    }

    renderPageList() {
        const {pageList} = this.state;
        if (!pageList) {
            return;
        }
        return pageList.map((page) => {
            return <div className="page-list-item">
                <span>{page.PageName}</span>
                <a className="float-right" onClick={this.goToPageSettings.bind(this, page.PageId) }>{resx.get("EditPageSettings") }</a>
                <a className="float-right" target="_blank" href={page.ViewUrl}>{resx.get("ViewPage") }</a>
            </div>;
        });
    }

    onMarkAllPagesAsTranslated(cultureCode) {
        const portalId = this.props.portalId;
        this.props.dispatch(LanguagesActions.markAllPagesAsTranslated({ cultureCode, portalId }, () => {
            utils.utilities.notify(resx.get("PagesSuccessfullyTranslated"));
            this.getPageList();
        }));
    }

    onEraseAllLocalizedPages() {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = this.props.portalId;
        utils.utilities.confirm(resx.get("EraseTranslatedPagesWarning").replace("{0}", cultureCode), resx.get("Yes"), resx.get("No"), () => {
            props.dispatch(LanguagesActions.deleteLanguagePages({ portalId, cultureCode }, (data) => {
                utils.utilities.notify(resx.get("DeletedAllLocalizedPages"));
                this.getPageList();
            }));
        });
    }

    onPublishTranslatedPages(enable = true) {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.publishAllPages({ portalId, cultureCode, enable }, (data) => {
            utils.utilities.notify(resx.get("PublishedAllTranslatedPages"));
        }));
    }

    addPages() {
        const {props, state} = this;
        const cultureCode = props.languageBeingEdited.Code;
        const portalId = props.portalId;
        props.dispatch(LanguagesActions.localizeContent({ cultureCode, portalId }, (data) => {
            this.getProgressData();
        }));
    }

    onCancel() {
        this.props.closePersonaBarPage();
    }

    goToPageSettings(pageId) {
        let personaBar = window.parent.dnn ? window.parent.dnn.PersonaBar : null;
        if (personaBar) {
            personaBar.openPanel('Pages',
                {
                    viewParams:
                    {
                        pageId,
                        viewTab: "localization",
                        referral: utils.moduleName,
                        referralText: resx.get("BackToLanguages").substring(1)
                    }
                }
            );

            let event = document.createEvent("Event");
            event.initEvent("viewPageSettings", true, true);

            event.pageId = pageId;
            event.viewTab = "localization";
            event.referral = utils.moduleName;
            event.referralText = resx.get("BackToLanguages").substring(1);

            document.dispatchEvent(event);
        }
    }

    toggleActivateLanguage(languageBeingEdited) {
        this.props.dispatch(LanguagesActions.activateLanguage({
            portalId: this.props.portalId,
            cultureCode: languageBeingEdited.Code,
            enable: languageBeingEdited.Active
        }));
    }

    onToggleActive(active) {
        if (!active) {
            let {languageBeingEdited} = this.state;
            const languageName = this.props.languageDisplayMode.toLowerCase() === "native" ? languageBeingEdited.NativeName : languageBeingEdited.EnglishName;
            utils.utilities.confirm(resx.get("DeactivateLanguageWarning").replace("{0}", languageName), resx.get("Yes"), resx.get("No"), () => {
                languageBeingEdited.Active = !languageBeingEdited.Active;
                this.toggleActivateLanguage(languageBeingEdited);
                this.setState({ languageBeingEdited });
            });
        } else {
            let {languageBeingEdited} = this.state;
            languageBeingEdited.Active = !languageBeingEdited.Active;
            this.toggleActivateLanguage(languageBeingEdited);
            this.setState({ languageBeingEdited });
        }
    }

    render() {
        const {props, state} = this;
        const language = state.languageBeingEdited;
        const isEnabled = language.Enabled;
        return <SocialPanelBody
            className="translate-page-content"
            workSpaceTrayOutside={true}
            workSpaceTray={<div className="siteSettings-back dnn-grid-cell" onClick={props.closePersonaBarPage}>
                {resx.get("BackToLanguages") }
            </div>}
            workSpaceTrayVisible={true}>

            <div className="language-settings-page-list">
                {language && <div className="top-block">
                    <div className="language-block">
                        <img className="float-left" src={language.Icon} />
                        <span >{language.NativeName}</span>
                        {state.basicSettings && <span className="float-right">{state.basicSettings.PortalName}</span>}
                    </div>
                    <InputGroup>
                        <div className="activate-pages-switch">
{'fraction_non_alphanumeric': 0.12243960084033613, 'fraction_numerical': 0.0011817226890756302, 'mean_word_length': 2.1564442602569414, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 67, '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': '6547375', 'n_tokens_mistral': 4148, 'n_tokens_neox': 4036, 'n_words': 673}