Catalog alignment
[sdc.git] / catalog-ui / src / app / directives / print-graph-screen / print-graph-screen.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 'use strict';
22 import {IAppMenu, Component, IAppConfigurtaion} from "app/models";
23 import {UrlToBase64Service} from "app/services";
24
25 export interface IPrintGraphScreenScope extends ng.IScope {
26     entity:Component;
27 }
28
29
30 export class PrintGraphScreenDirective implements ng.IDirective {
31
32     constructor(private $filter:ng.IFilterService,
33                 private  sdcMenu:IAppMenu,
34                 private sdcConfig:IAppConfigurtaion,
35                 private urlToBase64Service:UrlToBase64Service) {
36     }
37
38     scope = {
39         entity: '='
40     };
41     restrict = 'A';
42     link = (scope:IPrintGraphScreenScope, element:any) => {
43
44
45         element.bind('click', function () {
46             printScreen();
47         });
48
49
50         let printScreen = ():void => {
51             //
52             //     let pdf :any = new jsPDF('landscape', 'mm', 'a4');
53             //     pdf.setProperties({
54             //         title: scope.entity.name,
55             //         subject: 'Design Snapshot for ' + scope.entity.name,
56             //         author: scope.entity.creatorFullName,
57             //         keywords: scope.entity.tags.join(', '),
58             //         creator: scope.entity.creatorFullName
59             //     });
60             //
61             //     // A4 measures is 210 × 297 millimeters
62             //     let pdfWidth :number = 297,
63             //         pdfHeight :number = 210,
64             //         leftColumnWidth :number = 80;
65             //
66             //     //left bar background
67             //     pdf.setDrawColor(0);
68             //     pdf.setFillColor(248, 249, 251);
69             //     pdf.rect(0, 0, leftColumnWidth, pdfHeight, 'F');
70             //
71             //     //entity name
72             //     pdf.setFontSize(12);
73             //     pdf.setTextColor(38, 61, 77);
74             //     let splitTitle :any = pdf.splitTextToSize(scope.entity.name, 50);
75             //     pdf.text(22, 15 - (splitTitle.length - 1) * 2, splitTitle);
76             //
77             //     //line
78             //     pdf.setLineWidth(0.2);
79             //     pdf.setDrawColor(208, 209, 213);
80             //     pdf.line(0, 28, leftColumnWidth, 28);
81             //
82             //
83             //     pdf.setFontSize(10);
84             //     let properties :any = getPdfProperties();
85             //
86             //     let topOffset :number = 39, lines;
87             //     properties.forEach( (item:any) => {
88             //         if (!item.value) {
89             //             return;
90             //         }
91             //         if (item.title === 'Description:') {
92             //             topOffset += 5;
93             //         }
94             //
95             //         pdf.setTextColor(38, 61, 77);
96             //         pdf.text(5, topOffset, item.title);
97             //         pdf.setTextColor(102, 102, 102);
98             //         lines = pdf.splitTextToSize(item.value, 49);
99             //         pdf.text(5 + item.offset, topOffset, lines[0]);
100             //         if (lines.length > 1) {
101             //             lines = pdf.splitTextToSize(item.value.substring(lines[0].length + 1), 65);
102             //             if (lines.length > 8) {
103             //                 lines = lines.slice(0, 7);
104             //                 lines[lines.length - 1] += '...';
105             //             }
106             //             pdf.text(5, topOffset + 4, lines);
107             //             topOffset += 4 * (lines.length);
108             //         }
109             //
110             //         topOffset += 6;
111             //     });
112             //
113             //
114             //     //another background in case the text was too long
115             //     let declarationLineOffset :number = 176;
116             //     pdf.setDrawColor(0);
117             //     pdf.setFillColor(248, 249, 251);
118             //     pdf.rect(0, declarationLineOffset, leftColumnWidth, pdfHeight - declarationLineOffset, 'F');
119             //     //line
120             //     pdf.setLineWidth(0.2);
121             //     pdf.setDrawColor(208, 209, 213);
122             //     pdf.line(0, declarationLineOffset, leftColumnWidth, declarationLineOffset);
123             //
124             //     //declaration
125             //     pdf.setFontSize(10.5);
126             //     pdf.setTextColor(38, 61, 77);
127             //     pdf.text(5, 185, 'Declaration');
128             //     pdf.setFontSize(9);
129             //     pdf.setTextColor(102, 102, 102);
130             //     pdf.setFontType('bold');
131             //     pdf.text(5, 190, this.$filter('translate')('PDF_FILE_DECLARATION_BOLD'));
132             //     pdf.setFontType('normal');
133             //     pdf.text(5, 194, pdf.splitTextToSize(this.$filter('translate')('PDF_FILE_DECLARATION'), 65));
134             //
135             //     //entity icon
136             //     let self = this;
137             //     let addEntityIcon:Function = () => {
138             //         let iconPath:string = self.sdcConfig.imagesPath + '/styles/images/';
139             //         if (scope.entity.isService()) {
140             //             iconPath += 'service-icons/' + scope.entity.icon + '.png';
141             //         } else {
142             //             iconPath += 'resource-icons/' + scope.entity.icon + '.png';
143             //         }
144             //         self.urlToBase64Service.downloadUrl(iconPath, (base64string:string):void => {
145             //             if (base64string) {
146             //                 pdf.addImage(base64string, 'JPEG', 5, 7, 15, 15);
147             //             }
148             //             pdf.save(scope.entity.name + '.pdf');
149             //         });
150             //     };
151             //
152             //     //actual snapshop of canvas
153             //     let diagramDiv :any = document.getElementById('myDiagram');
154             //     let diagram :go.Diagram = go.Diagram.fromDiv(diagramDiv), canvasImg = new Image();
155             //     diagram.startTransaction('print screen');
156             //     let canvasImgBase64:any = diagram.makeImageData({
157             //         //scale: 1,
158             //         size: new go.Size(pdfHeight * 5, NaN),
159             //         background: 'white',
160             //         type: 'image/jpeg'
161             //     });
162             //     diagramDiv.firstElementChild.toDataURL();
163             //     diagram.commitTransaction('print screen');
164             //
165             //     canvasImg.onload = () => {
166             //         if (canvasImg.height > 0) {
167             //             let canvasImgRatio:number = Math.min((pdfWidth - leftColumnWidth - 15) / canvasImg.width, pdfHeight / canvasImg.height);
168             //             let canvasImgWidth:number = canvasImg.width * canvasImgRatio,
169             //                 canvasImgHeight:number = canvasImg.height * canvasImgRatio;
170             //             let canvasImgOffset:number = (pdfHeight - canvasImgHeight) / 2;
171             //             pdf.addImage(canvasImg, 'JPEG', leftColumnWidth, canvasImgOffset, canvasImgWidth, canvasImgHeight);
172             //
173             //             addEntityIcon();
174             //         }
175             //     };
176             //
177             //     if(canvasImg.src === 'data:,') { //empty canvas
178             //         addEntityIcon();
179             //     } else {
180             //         canvasImg.src = canvasImgBase64;
181             //     }
182         };
183
184
185         let getPdfProperties = ():Array<any> => {
186             // return [
187             //     {title: this.$filter('translate')('GENERAL_LABEL_TYPE'), value: scope.entity.getComponentSubType(), offset: 10},
188             //     {title: this.$filter('translate')('GENERAL_LABEL_VERSION'), value: scope.entity.version, offset: 15},
189             //     {title: this.$filter('translate')('GENERAL_LABEL_CATEGORY'), value: scope.entity.categories.length ? scope.entity.categories[0].name : '', offset: 16},
190             //     {title: this.$filter('translate')('GENERAL_LABEL_CREATION_DATE'), value: this.$filter('date')(scope.entity.creationDate, 'MM/dd/yyyy'), offset: 24},
191             //     {title: this.$filter('translate')('GENERAL_LABEL_AUTHOR'), value: scope.entity.creatorFullName, offset: 13},
192             //     {title: this.$filter('translate')('GENERAL_LABEL_CONTACT_ID'), value: scope.entity.contactId, offset: 41},
193             //     {title: this.$filter('translate')('GENERAL_LABEL_STATUS'), value: (<any>this.sdcMenu).LifeCycleStatuses[scope.entity.lifecycleState].text, offset: 13},
194             //     {title: this.$filter('translate')('GENERAL_LABEL_DESCRIPTION'), value: scope.entity.description, offset: 20},
195             //     {title: this.$filter('translate')('GENERAL_LABEL_TAGS'), value: scope.entity.tags.join(', '), offset: 10}
196             // ];
197             return null;
198         };
199
200
201     };
202
203     public static factory = ($filter:ng.IFilterService, sdcMenu:IAppMenu, sdcConfig:IAppConfigurtaion, urlToBase64Service:UrlToBase64Service)=> {
204         return new PrintGraphScreenDirective($filter, sdcMenu, sdcConfig, urlToBase64Service);
205     };
206
207 }
208
209 PrintGraphScreenDirective.factory.$inject = ['$filter', 'sdcMenu', 'sdcConfig', 'Sdc.Services.UrlToBase64Service'];