<commit_before>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
 * See README.md for more information
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import _ from "lodash";
import { dynamicSections, dynamicSectionsByKind } from "../dynamic-section-definitions";

const sectionManagerSvc = {
    serviceName: "DynamicSectionManager",
    service: ($location, $state, $stateParams, accessLogStore, localStorageService) => {
        let available = [];
        let active = [];
        let kind = null;

        function mkStorageKey() {
            return `waltz-user-section-ids-${kind}`;
        }


        /* Sort list of sections, ensuring 'change log' is the last one */
        function sortList(list) {
            return _.orderBy(list,
                    s => s === dynamicSections.changeLogSection
                        ? "zzz"
                        : _.toLower(s.name));
        }


        /* modifies a list (rather than replacing it) so watchers see changes */
        function blat(list, replacement) {
            list.splice(0, list.length, ...replacement);
        }


        function persistState() {
            const top3SectionIds = _
                .chain(active)
                .compact()
                .take(3)
                .map(s => s.id)
                .value();

            localStorageService.set(
                mkStorageKey(),
                top3SectionIds);

            $location.search("sections", _.join(top3SectionIds, ";"));
        }


        function initialise(newKind) {
            kind = newKind;
            blat(available, sortList(dynamicSectionsByKind[kind]));

            const previousViaParam = _.chain($location.search())
                .get("sections", "")
                .split(";")
                .map(s => Number(s))
                .value();

            const previousViaLocalHistory = localStorageService.get(mkStorageKey());

            const toActivate = _
                .chain(previousViaParam)
                .concat(previousViaLocalHistory)
                .map(sId => _.find(available, { id: sId }))
                .uniq()
                .compact()
                .take(3)
                .value();

            localStorageService.set(
                mkStorageKey(),
                _.map(toActivate, s => s.id));

            blat(active, toActivate);
        }


        function getAvailable() {
            return available;
        }


        function getActive() {
            return active;
        }


        function activate(d) {
            const state = `${$state.current.name}|${d.componentId}`;
            const infoPromise = accessLogStore.write(state, $stateParams);
            blat(
                active,<commit_msg>Log dynamic section clicks in access log<commit_after>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
 * See README.md for more information
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import _ from "lodash";
import { dynamicSections, dynamicSectionsByKind } from "../dynamic-section-definitions";

const sectionManagerSvc = {
    serviceName: "DynamicSectionManager",
    service: ($location, $state, $stateParams, accessLogStore, localStorageService) => {
        let available = [];
        let active = [];
        let kind = null;

        function mkStorageKey() {
            return `waltz-user-section-ids-${kind}`;
        }


        /* Sort list of sections, ensuring 'change log' is the last one */
        function sortList(list) {
            return _.orderBy(list,
                    s => s === dynamicSections.changeLogSection
                        ? "zzz"
                        : _.toLower(s.name));
        }


        /* modifies a list (rather than replacing it) so watchers see changes */
        function blat(list, replacement) {
            list.splice(0, list.length, ...replacement);
        }


        function persistState() {
            const top3SectionIds = _
                .chain(active)
                .compact()
                .take(3)
                .map(s => s.id)
                .value();

            localStorageService.set(
                mkStorageKey(),
                top3SectionIds);

            $location.search("sections", _.join(top3SectionIds, ";"));
        }


        function initialise(newKind) {
            kind = newKind;
            blat(available, sortList(dynamicSectionsByKind[kind]));

            const previousViaParam = _.chain($location.search())
                .get("sections", "")
                .split(";")
                .map(s => Number(s))
                .value();

            const previousViaLocalHistory = localStorageService.get(mkStorageKey());

            const toActivate = _
                .chain(previousViaParam)
                .concat(previousViaLocalHistory)
                .map(sId => _.find(available, { id: sId }))
                .uniq()
                .compact()
                .take(3)
                .value();

            localStorageService.set(
                mkStorageKey(),
                _.map(toActivate, s => s.id));

            // log component activations to access log
            _.chain(toActivate)
                .map(s => `${$state.current.name}|${s.componentId}`)
                .forEach(state => accessLogStore.write(state, $stateParams))
                .value();

            blat(active, toActivate);
        }


        function getAvailable() {
            return available;
        }


        function getActive() {
            return active;
        }


        function activate(d) {
            accessLogStore.write(`${$state.current.name}|${d.componentId}`, $stateParams);

            blat(
                active,
{'fraction_non_alphanumeric': 0.10329701429177869, 'fraction_numerical': 0.006509126927975096, 'mean_word_length': 1.7826771653543307, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3872973', 'n_tokens_mistral': 1877, 'n_tokens_neox': 1735, 'n_words': 527}