[SDC] rebase 1710 code
[sdc.git] / catalog-ui / src / app / services / components / utils / composition-left-palette-service.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 /**
21  * Created by obarda on 3/13/2016.
22  */
23 'use strict';
24 import {LeftPaletteComponent} from "../../../models/components/displayComponent";
25 import {Component} from "../../../models/components/component";
26 import {EventListenerService} from "../../event-listener-service";
27 import {ComponentFactory} from "../../../utils/component-factory";
28 import {IAppConfigurtaion} from "../../../models/app-config";
29 import {ResourceType, ComponentType, EVENTS} from "../../../utils/constants";
30 import {ComponentMetadata} from "../../../models/component-metadata";
31 import {Resource} from "app/models/components/resource";
32
33 export class LeftPaletteDataObject {
34     displayLeftPanelComponents:Array<LeftPaletteComponent>;
35     onFinishLoadingEvent:string;
36
37     constructor(onFinishEventListener:string) {
38
39         this.displayLeftPanelComponents = new Array<LeftPaletteComponent>();
40         this.onFinishLoadingEvent = onFinishEventListener;
41     }
42 }
43
44 export class LeftPaletteLoaderService {
45
46     static '$inject' = [
47         'Restangular',
48         'sdcConfig',
49         '$q',
50         'ComponentFactory',
51         'EventListenerService'
52
53     ];
54
55     constructor(protected restangular:restangular.IElement,
56                 protected sdcConfig:IAppConfigurtaion,
57                 protected $q:ng.IQService,
58                 protected ComponentFactory:ComponentFactory,
59                 protected EventListenerService:EventListenerService) {
60
61         this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
62
63     }
64
65     private serviceLeftPaletteData:LeftPaletteDataObject;
66     private resourceLeftPaletteData:LeftPaletteDataObject;
67     private resourcePNFLeftPaletteData:LeftPaletteDataObject;
68     private vlData:LeftPaletteDataObject;
69
70     public loadLeftPanel = (component:Component):void => {
71         this.serviceLeftPaletteData = new LeftPaletteDataObject(EVENTS.SERVICE_LEFT_PALETTE_UPDATE_EVENT);
72         this.resourceLeftPaletteData = new LeftPaletteDataObject(EVENTS.RESOURCE_LEFT_PALETTE_UPDATE_EVENT);
73         this.resourcePNFLeftPaletteData = new LeftPaletteDataObject(EVENTS.RESOURCE_PNF_LEFT_PALETTE_UPDATE_EVENT);
74         this.updateComponentLeftPalette(component);
75     }
76
77     private getResourceLeftPaletteDataByResourceType = (resourceType:string):LeftPaletteDataObject => {
78         if(resourceType == ResourceType.PNF) {
79             return this.resourcePNFLeftPaletteData;
80         }
81         return this.resourceLeftPaletteData;
82     }
83
84     private onFinishLoading = (componentType:string, leftPaletteData:LeftPaletteDataObject):void => {
85         this.EventListenerService.notifyObservers(leftPaletteData.onFinishLoadingEvent);
86     };
87
88     private updateLeftPalette = (componentType, componentInternalType:string, leftPaletteData:LeftPaletteDataObject):void => {
89
90         this.restangular.one("resources").one('/latestversion/notabstract/metadata').get({'internalComponentType': componentInternalType}).then((leftPaletteComponentMetadata:Array<ComponentMetadata>) => {
91             _.forEach(leftPaletteComponentMetadata, (componentMetadata:ComponentMetadata) => {
92                 leftPaletteData.displayLeftPanelComponents.push(new LeftPaletteComponent(componentMetadata));
93             });
94             this.onFinishLoading(componentType, leftPaletteData);
95         });
96     };
97
98     public getLeftPanelComponentsForDisplay = (component:Component):Array<LeftPaletteComponent> => {
99         switch (component.componentType) {
100             case ComponentType.SERVICE:
101                 return this.serviceLeftPaletteData.displayLeftPanelComponents;
102             default://resource
103                 return this.getResourceLeftPaletteDataByResourceType((<Resource>component).resourceType).displayLeftPanelComponents;
104         }
105     };
106
107     public updateComponentLeftPalette = (component:Component):void => {
108         switch (component.componentType) {
109             case ComponentType.SERVICE:
110                 this.updateLeftPalette(ComponentType.SERVICE, ComponentType.SERVICE, this.serviceLeftPaletteData);
111                 break;
112             case ComponentType.RESOURCE:
113                 this.updateLeftPalette(ComponentType.RESOURCE, (<Resource>component).resourceType, this.getResourceLeftPaletteDataByResourceType((<Resource>component).resourceType));
114                 break;
115             default:
116                 console.log('ERROR: Component type '+ component.componentType + ' is not exists');
117         }
118     };
119 }