re base code
[sdc.git] / catalog-ui / src / app / models / graph / nodes / composition-graph-nodes / composition-ci-node-base.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 import {ComponentInstance} from "../../../componentsInstances/componentInstance";
22 import {CommonCINodeBase} from "../common-ci-node-base";
23 import {ICanvasImage, ImageCreatorService} from "app/directives/graphs-v2/image-creator/image-creator.service";
24 import {ImagesUrl, GraphUIObjects} from "app/utils";
25 import {AngularJSBridge} from "app/services";
26
27 export interface ICompositionCiNodeBase {
28
29 }
30
31 export abstract class CompositionCiNodeBase extends CommonCINodeBase implements ICompositionCiNodeBase {
32
33     public textPosition:string; //need to move to cp UCPE
34     public isUcpe:boolean;
35     public isInsideGroup:boolean;
36     public isUcpePart:boolean;
37
38     constructor(instance:ComponentInstance,
39                 public imageCreator:ImageCreatorService) {
40         super(instance);
41         this.init();
42     }
43
44     private init() {
45         this.displayName = this.getDisplayName();
46         this.isUcpe = false;
47         this.isGroup = false;
48         this.isUcpePart = false;
49         this.isInsideGroup = false;
50     }
51     
52     
53     public setUncertifiedImageBgStyle(node:Cy.Collection, nodeMinSize:number):string {
54
55         let uncertifiedIconWidth:number = GraphUIObjects.HANDLE_SIZE;
56         let nodeWidth:number = node.data('imgWidth') || node.width();
57         let uncertifiedCanvasWidth: number = nodeWidth;
58
59         if (nodeWidth < nodeMinSize) { //uncertified icon will overlap too much of the node, need to expand canvas.
60             uncertifiedCanvasWidth = nodeWidth + uncertifiedIconWidth/2; //expand canvas so that only half of the icon overlaps with the node
61         }
62
63         const x = uncertifiedCanvasWidth - nodeWidth, y = x, width = nodeWidth, height = width;
64
65         const canvasImages:ICanvasImage[] = [
66             { src: this.imagesPath + this.componentInstance.icon + '.png', x, y, width, height},
67             { src: this.imagesPath + 'uncertified.png', x: 0, y: 0, width: uncertifiedIconWidth, height: uncertifiedIconWidth}
68         ];
69
70
71        //Create the image and update the node background styles
72         this.imageCreator.getMultiLayerBase64Image(canvasImages, uncertifiedCanvasWidth, uncertifiedCanvasWidth).then(img => this.updateNodeStyles(node,uncertifiedCanvasWidth,img));
73         return this.img; // Return the referance to the image (in Base64 format)
74     }
75
76     
77     public setArchivedImageBgStyle(node:Cy.Collection, nodeMinSize:number):string {        
78         let archivedIconWidth:number = GraphUIObjects.HANDLE_SIZE;
79         let nodeWidth:number = node.data('imgWidth') || node.width();
80         let archivedCanvasWidth: number = nodeWidth;
81
82         const x = archivedCanvasWidth - nodeWidth, y = x, width = nodeWidth, height = width;
83         const archiveImage = nodeWidth < 50? 'archive_small.png':'archive_big.png';
84
85         const canvasImages = [
86             { src: this.imagesPath + this.componentInstance.icon + '.png', x, y, width, height},
87             { src: AngularJSBridge.getAngularConfig().imagesPath + ImagesUrl.RESOURCE_ICONS + archiveImage, x, y, width, height}
88         ];
89
90         //Create the image and update the node background styles
91         this.imageCreator.getMultiLayerBase64Image(canvasImages, archivedCanvasWidth, archivedCanvasWidth).then(img => this.updateNodeStyles(node, archivedCanvasWidth, img));
92         return this.img; // Return the default img
93     }
94
95     protected getDisplayName():string {
96         let graphResourceName = AngularJSBridge.getFilter('graphResourceName');
97         let resourceName = AngularJSBridge.getFilter('resourceName');
98         return graphResourceName(resourceName(this.componentInstance.name));
99     }
100
101     //TODO:: move to Base class ???
102     private updateNodeStyles(node,canvasWidth,imageBase64){     
103         this.img = imageBase64;
104         node.style({
105             'background-image': this.img,
106             'background-width': canvasWidth,
107             'background-height': canvasWidth,
108             'background-position-x':0,
109             'background-position-y':0
110         });        
111     }
112
113 }