{"size": 5213, "ext": "js", "max_stars_repo_path": "src/app/components/admin-panel/collectives/collectives.component.js", "max_stars_repo_name": "charliechauri/comunarr-file-manager-client", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/app/components/admin-panel/collectives/collectives.component.js", "max_issues_repo_name": "charliechauri/comunarr-file-manager-client", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/app/components/admin-panel/collectives/collectives.component.js", "max_forks_repo_name": "charliechauri/comunarr-file-manager-client", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 36.2013888889, "max_line_length": 143, "alphanum_fraction": 0.5311720698}
import angular from 'angular';
import Promise from 'ypromise';
import template from './collectives.html';
import dialogTemplate from './collectives.dialog.html';

export const CollectivesComponent = {
    bindings: {},
    controller: class CollectivesComponent {
        constructor($scope, $mdDialog, CollectivesService, ProjectsService, ResponseHandler, Validator) {
            'ngInject';
            this.$scope = $scope;
            this.$mdDialog = $mdDialog;
            this.CollectivesService = CollectivesService;
            this.ProjectsService = ProjectsService;
            this.ResponseHandler = ResponseHandler;
            this.Validator = Validator;
        }

        /**
         * Gets all collectives, projects and the relationship between them. Then build for each collective adds the related projects
         */
        $onInit() {
            this.collectives = [];
            this.projects = [];
            this.getCatalogs();
        }

        getCatalogs() {
            return Promise.all([this.getCollectives(), this.getProjects()]).then(() => {
                this.assignProjectIdIntoCollectives(false);
            });
        }

        /**
         * Get all collectives
         */
        getCollectives(forceRefresh) {
            return this.CollectivesService.get(forceRefresh).then(collectives => this.collectives = collectives);
        }

        /**
         * Get all projects
         */
        getProjects(forceRefresh) {
            return this.ProjectsService.get(forceRefresh).then(projects => this.projects = projects);
        }

        assignProjectIdIntoCollectives(forceRefresh) {
            this.CollectivesService.getRelatedProjects(forceRefresh).then(relations => {
                this.collectives.forEach(collective => {
                    collective.comunarrProjects = [];

                    const relationsToCollective = relations.filter(relation => relation.idCollective === collective.id);

                    relationsToCollective.forEach(relationToCollective => {
                        collective.comunarrProjects.push(this.projects.find(project => project.id === relationToCollective.idComunarrProject));
                    });
                });
            });
        }

        /**
         * Validate if a project is in a collective
         * @param {any} project
         * @param {any} collective
         */
        isProjectInCollective(project, collective) {
            return collective.comunarrProjects.find(pro => pro.id === project.id);
        }

        /**
         * Add/remove a project in a collective
         * @param {any} project
         * @param {any} collective
         */
        toggleProjectToCollective(project, collective) {
            const isAlready = collective.comunarrProjects.find(pro => pro.id === project.id);

            if (isAlready) {
                const index = collective.comunarrProjects.map(pro => pro.id).indexOf(project.id);
                collective.comunarrProjects.splice(index, 1);
            } else {
                collective.comunarrProjects.push(project);
            }
        }

        /**
         * Add or update a collective
         * @param {any} collective
         * @param {any} targetEvent
         * @param {string} method 
         */
        addOrEdit(collective = this.getNewCollective(), targetEvent, method) {
            this.isEditing = !!collective.id;
            this.selectedCollective = angular.copy(collective);

            this.$mdDialog
                .show({
                    preserveScope: true,
                    scope: this.$scope,
                    targetEvent,
                    template: dialogTemplate
                })
                .then(collective => {
                    collective.name === collective.name.trim();
                    if (!this.Validator.isUnique(this.collectives, 'name', this.selectedCollective, this.isEditing, true, 'colectivo')) {
                        this.selectedCollective = null;
                        return;
                    }

                    if (!this.isEditing) {
                        delete collective.id;
                        delete collective.status;
                    }
                    this.CollectivesService[method](collective).then(response => {
                        this.ResponseHandler.success(response);
                        this.selectedCollective = this.getNewCollective();
                        this.assignProjectIdIntoCollectives(true);
                        this.getCatalogs();
                    });
                })
                .catch(() => {
                    this.selectedCollective = this.getNewCollective();
                });
        }

        /**
         * Returns a new instance of a collective
         * @return {any}
         */
        getNewCollective() {
            return { id: null, name: '', status: true, comunarrProjects: [] };
        }

        /**
         * Receives list of objects and return only that ones that have status true
         * @param {any} list 
         */
        filterByActive(list) {
            return list.filter(item => item.status);
        }
    },
    template
};