CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / plugins / plugins-context-view-model.ts
1 /*
2
3 * Copyright (c) 2018 AT&T Intellectual Property.
4
5 *
6
7 * Licensed under the Apache License, Version 2.0 (the "License");
8
9 * you may not use this file except in compliance with the License.
10
11 * You may obtain a copy of the License at
12
13 *
14
15 *     http://www.apache.org/licenses/LICENSE-2.0
16
17 *
18
19 * Unless required by applicable law or agreed to in writing, software
20
21 * distributed under the License is distributed on an "AS IS" BASIS,
22
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25 * See the License for the specific language governing permissions and
26
27 * limitations under the License.
28
29 */
30
31 import {IUserProperties, Plugin} from "app/models";
32 import {CacheService} from "app/services";
33 import {PluginsService} from "../../../../ng2/services/plugins.service";
34 import {IWorkspaceViewModelScope} from "../../workspace-view-model";
35
36
37 interface IPluginsContextViewModelScope extends IWorkspaceViewModelScope {
38     plugin: Plugin;
39     user: IUserProperties;
40     queryParams: Object;
41     isLoading: boolean;
42     show: boolean;
43
44     onLoadingDone(plugin: Plugin): void;
45 }
46
47 export class PluginsContextViewModel {
48     static '$inject' = [
49         '$scope',
50         '$stateParams',
51         'Sdc.Services.CacheService',
52         'PluginsService'
53     ];
54
55     constructor(private $scope: IPluginsContextViewModelScope,
56                 private $stateParams: any,
57                 private cacheService: CacheService,
58                 private pluginsService: PluginsService) {
59
60         this.initScope();
61     }
62
63     private initScope = (): void => {
64         this.$scope.show = false;
65         this.$scope.plugin = this.pluginsService.getPluginByStateUrl(this.$stateParams.path);
66         this.$scope.user = this.cacheService.get('user');
67
68         this.$scope.isLoading = true;
69
70         this.$scope.queryParams = {
71             userId: this.$scope.user.userId,
72             userRole: this.$scope.user.role,
73             displayType: "context",
74             contextType: this.$scope.component.getComponentSubType(),
75             uuid: this.$scope.component.uuid,
76             lifecycleState: this.$scope.component.lifecycleState,
77             isOwner: this.$scope.component.lastUpdaterUserId === this.$scope.user.userId,
78             version: this.$scope.component.version,
79             parentUrl: window.location.origin,
80             eventsClientId: this.$scope.plugin.pluginId
81         };
82
83         if (this.$stateParams.queryParams) {
84             _.assign(this.$scope.queryParams, this.$stateParams.queryParams);
85         }
86
87         this.$scope.onLoadingDone = (plugin: Plugin) => {
88             if (plugin.pluginId == this.$scope.plugin.pluginId) {
89                 this.$scope.isLoading = false;
90             }
91         };
92
93     }
94 }