[sdc] update to the current code base
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / activity-log / activity-log.ts
1 'use strict';
2 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
3 import {Activity} from "app/models";
4 import {ActivityLogService} from "app/services";
5
6 export interface IActivityLogViewModelScope extends IWorkspaceViewModelScope {
7     activityDateArray:Array<any>; //this is in order to sort the dates
8     activityLog:Array<Activity>;
9     preVersion:string;
10
11     tableHeadersList:Array<any>;
12     reverse:boolean;
13     sortBy:string;
14     searchBind:string;
15
16     getActivityLog(uniqueId:string):void;
17     onVersionChanged(version:any):void;
18     parseAction(action:string):string;
19     sort(sortBy:string):void;
20 }
21
22 export class ActivityLogViewModel {
23
24     static '$inject' = [
25         '$scope',
26         '$state',
27         'Sdc.Services.ActivityLogService'
28     ];
29
30     constructor(private $scope:IActivityLogViewModelScope,
31                 private $state:ng.ui.IStateService,
32                 private activityLogService:ActivityLogService) {
33
34         this.initScope();
35         this.$scope.setValidState(true);
36         this.initSortedTableScope();
37         this.$scope.updateSelectedMenuItem();
38
39         // Set default sorting
40         this.$scope.sortBy = 'logDate';
41     }
42
43     private initScope():void {
44
45         this.$scope.preVersion = this.$scope.component.version;
46
47         this.$scope.onVersionChanged = (version:any):void => {
48             if (version.versionNumber != this.$scope.component.version) {
49                 this.$scope.isLoading = true;
50                 this.$scope.getActivityLog(version.versionId);
51             }
52         };
53
54         this.$scope.getActivityLog = (uniqueId:any):void => {
55
56             let onError = (response) => {
57                 this.$scope.isLoading = false;
58                 console.info('onFaild', response);
59
60             };
61
62             let onSuccess = (response:Array<Activity>) => {
63                 this.$scope.activityLog = _.sortBy(response, function (o) {
64                     return o.TIMESTAMP;
65                 }); //response; //
66                 this.$scope.isLoading = false;
67             };
68
69             this.$scope.isLoading = true;
70             if (this.$scope.component.isResource()) {
71                 this.activityLogService.getActivityLogService('resources', uniqueId).then(onSuccess, onError);
72             }
73             if (this.$scope.component.isService()) {
74                 this.activityLogService.getActivityLogService('services', uniqueId).then(onSuccess, onError);
75             }
76
77         };
78
79         if (!this.$scope.activityLog || this.$scope.preVersion != this.$scope.component.version) {
80             this.$scope.getActivityLog(this.$scope.component.uniqueId);
81         }
82
83         this.$scope.parseAction = (action:string) => {
84             return action ? action.split(/(?=[A-Z])/).join(' ') : '';
85         };
86
87     }
88
89     private initSortedTableScope = ():void => {
90         this.$scope.tableHeadersList = [
91             {title: 'Date', property: 'dateFormat'},
92             {title: 'Action', property: 'ACTION'},
93             {title: 'Comment', property: 'COMMENT'},
94             {title: 'Username', property: 'MODIFIER'},
95             {title: 'Status', property: 'STATUS'}
96         ];
97
98         this.$scope.sort = (sortBy:string):void => {
99             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
100             this.$scope.sortBy = sortBy;
101         };
102     };
103 }