2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 import * as _ from "lodash";
23 import {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
24 import {Activity} from "app/models";
25 import {ActivityLogService} from "app/services";
27 export interface IActivityLogViewModelScope extends IWorkspaceViewModelScope {
28 activityDateArray:Array<any>; //this is in order to sort the dates
29 activityLog:Array<Activity>;
32 tableHeadersList:Array<any>;
37 getActivityLog(uniqueId:string):void;
38 onVersionChanged(version:any):void;
39 parseAction(action:string):string;
40 sort(sortBy:string):void;
43 export class ActivityLogViewModel {
48 'Sdc.Services.ActivityLogService'
51 constructor(private $scope:IActivityLogViewModelScope,
52 private $state:ng.ui.IStateService,
53 private activityLogService:ActivityLogService) {
56 this.$scope.setValidState(true);
57 this.initSortedTableScope();
59 // Set default sorting
60 this.$scope.sortBy = 'logDate';
63 private initScope():void {
65 this.$scope.preVersion = this.$scope.component.version;
67 this.$scope.onVersionChanged = (version:any):void => {
68 if (version.versionNumber != this.$scope.component.version) {
69 this.$scope.isLoading = true;
70 this.$scope.getActivityLog(version.versionId);
74 this.$scope.getActivityLog = (uniqueId:any):void => {
76 let onError = (response) => {
77 this.$scope.isLoading = false;
78 console.info('onFaild', response);
82 let onSuccess = (response:Array<Activity>) => {
83 this.$scope.activityLog = _.sortBy(response, function (o) {
86 this.$scope.isLoading = false;
89 this.$scope.isLoading = true;
90 if (this.$scope.component.isResource()) {
91 this.activityLogService.getActivityLogService('resources', uniqueId).then(onSuccess, onError);
93 if (this.$scope.component.isService()) {
94 this.activityLogService.getActivityLogService('services', uniqueId).then(onSuccess, onError);
99 if (!this.$scope.activityLog || this.$scope.preVersion != this.$scope.component.version) {
100 this.$scope.getActivityLog(this.$scope.component.uniqueId);
103 this.$scope.parseAction = (action:string) => {
104 return action ? action.split(/(?=[A-Z])/).join(' ') : '';
109 private initSortedTableScope = ():void => {
110 this.$scope.tableHeadersList = [
111 {title: 'Date', property: 'dateFormat'},
112 {title: 'Action', property: 'ACTION'},
113 {title: 'Comment', property: 'COMMENT'},
114 {title: 'Username', property: 'MODIFIER'},
115 {title: 'Status', property: 'STATUS'}
118 this.$scope.sort = (sortBy:string):void => {
119 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
120 this.$scope.sortBy = sortBy;