superdesk/superdesk-client-core

View on GitHub
scripts/apps/monitoring/controllers/MonitoringController.ts

Summary

Maintainability
C
1 day
Test Coverage
import _ from 'lodash';
import {appConfig} from 'appConfig';
import {getLabelForStage} from 'apps/workspace/content/constants';
import {IArticle} from 'superdesk-api';

/**
 * @ngdoc controller
 * @module superdesk.apps.monitoring
 */
MonitoringController.$inject = ['$rootScope', '$scope', '$location', 'desks', 'superdeskFlags',
    'search', '$filter', 'preferencesService', '$q'];
export function MonitoringController($rootScope, $scope, $location, desks, superdeskFlags,
    search, $filter, preferencesService, $q) {
    this.state = {};

    this.preview = preview;
    this.closePreview = closePreview;
    this.previewItem = null;

    this.selectedGroup = null;

    this.singleGroup = null;
    this.viewSingleGroup = viewSingleGroup;
    this.viewMonitoringHome = viewMonitoringHome;

    this.hasSwimlaneView = appConfig.features != null && appConfig.features.swimlane ? 1 : 0;
    this.columnsLimit = null;

    // init swimlane view using preferences - use session preferences if set, or fallback to user preferences
    $q.all({
        userPreference: preferencesService.get('monitoring:view'),
        sessionPreference: preferencesService.get('monitoring:view:session'),
    }).then(({userPreference, sessionPreference}) => {
        this.viewColumn = sessionPreference !== null ?
            sessionPreference === 'swimlane' : userPreference.view === 'swimlane';
        this.switchViewColumn(this.viewColumn);
    });

    this.selectGroup = selectGroup;
    this.switchViewColumn = switchViewColumn;

    this.queryParam = $location.search();

    this.edit = edit;
    this.editItem = null;

    this.totalItems = '';
    this.showRefresh = false;

    this.showHistoryTab = true;

    this.scrollTop = false;

    this.getGroupLabel = getGroupLabel;

    this.isDeskChanged = function() {
        return desks.changeDesk;
    };

    this.highlightsDeskChanged = function() {
        if (desks.changeDesk) {
            $location.url('/workspace/monitoring');
        }
    };

    var self = this;

    function preview(
        item: IArticle,
        calledFromOutsideAngular: boolean = false,
    ) {
        self.previewItem = item;
        self.state['with-preview'] = superdeskFlags.flags.previewing = !!item;
        const sendPreviewEvent =
            appConfig.list != null
            && appConfig.list.narrowView
            && search.singleLine
            && superdeskFlags.flags.authoring;

        const evnt = item ? 'rowview:narrow' : 'rowview:default';

        if (!_.isNil(self.previewItem)) {
            self.showHistoryTab = self.previewItem.state !== 'ingested' &&
            !_.includes(['archived', 'externalsource'], self.previewItem._type);
        }

        if (!item) {
            self.selectedGroup = null;
        }

        if (sendPreviewEvent) {
            $rootScope.$broadcast(evnt);
        }

        if (calledFromOutsideAngular) {
            $scope.$apply();
        }
    }

    function closePreview() {
        preview(null);
        if (self.viewColumn) {
            $rootScope.$broadcast('resize:header');
        }
    }

    function edit(item: IArticle) {
        self.editItem = item;
        self.state['with-authoring'] = !!item;
    }
    function viewSingleGroup(group, type) {
        group.singleViewType = type;
        self.singleGroup = group;
        $rootScope.$broadcast('stage:single');
    }

    function viewMonitoringHome() {
        self.singleGroup.singleViewType = null;
        self.singleGroup = null;
    }

    /**
     * @description Switches the view to swimlane or list in monitoring view and
     * returns a columnsLimit a number or null for swimlane or list respectively.
     * @param {Boolean} viewColumn if set to true then function returns columnsLimit
     * for swimlane as per configuration.
     * @param {Boolean} updateSession if set it will update user session setting.
     * @returns {Number|null} function returns columnsLimit null if viewColumn is false.
     */
    function switchViewColumn(viewColumn, updateSession, numberOfColumns) {
        self.viewColumn = viewColumn;

        if (self.viewColumn) {
            self.columnsLimit = numberOfColumns
                ? numberOfColumns
                : appConfig.features.swimlane.defaultNumberOfColumns;
        } else {
            self.columnsLimit = null;
        }

        $scope.$broadcast('view:column', {viewColumn: self.viewColumn});
        if (updateSession) {
            preferencesService.update({'monitoring:view:session': viewColumn}, 'monitoring:view:session');
        }
        return self.columnsLimit;
    }

    function selectGroup(group) {
        self.selectedGroup = self.selectedGroup && self.selectedGroup._id === group._id ? null : group;
        return self.selectedGroup;
    }

    function getGroupLabel(group, activeWorkspace) {
        let groupLabel;

        if (group.subheader) {
            groupLabel = activeWorkspace === 'workspace' ?
                group.header + ' ' + group.subheader : group.header + ' / ' + group.subheader;
        } else if (group.type === 'search') {
            groupLabel = group.header;
        } else {
            groupLabel = group.header + ' / ' + getLabelForStage(group);
        }

        return groupLabel;
    }
}