c6b6cc78b6eed7158a54c46fb01e072c73e01ee5
[ccsdk/cds.git] /
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import { Component, OnInit, EventEmitter, Output, ViewChild } from '@angular/core';
23 import { Store } from '@ngrx/store';
24 import * as JSZip from 'jszip';
25 import { Observable } from 'rxjs';
26
27 import { IBlueprint } from '../../../../common/core/store/models/blueprint.model';
28 import { IBlueprintState } from '../../../../common/core/store/models/blueprintState.model';
29 import { IAppState } from '../../../../common/core/store/state/app.state';
30 import { LoadBlueprintSuccess } from '../../../../common/core/store/actions/blueprint.action';
31
32 @Component({
33   selector: 'app-search-template',
34   templateUrl: './search-template.component.html',
35   styleUrls: ['./search-template.component.scss']
36 })
37 export class SearchTemplateComponent implements OnInit {
38   file: File;
39   localBluePrintData: IBlueprint;
40   fileText: object[];
41   blueprintState: IBlueprintState;
42   bpState: Observable<IBlueprintState>;
43
44   @ViewChild('fileInput') fileInput;
45   result: string = '';
46
47   public paths = [];
48   public tree;
49   private zipFile: JSZip = new JSZip();
50   private fileObject: any;
51
52   constructor(private store: Store<IAppState>) { }
53
54   ngOnInit() {
55   }
56
57   fileChanged(e: any) {
58     this.file = e.target.files[0];
59
60     // this.zipFile.loadAsync(this.file)
61     //   .then((zip) => {
62     //     if(zip) {            
63     //       this.buildFileViewData(zip);
64     //     }
65     //   });
66   }
67
68   updateBlueprintState() {
69     let fileReader = new FileReader();
70     fileReader.readAsText(this.file);
71     var me = this;
72     fileReader.onload = function () {
73       var data: IBlueprint = JSON.parse(fileReader.result.toString());
74       me.store.dispatch(new LoadBlueprintSuccess(data));
75     }
76   }
77
78   async buildFileViewData(zip) {
79     for (var file in zip.files) {
80       this.fileObject = {
81         name: zip.files[file].name,
82         data: ''
83       };
84       const value = <any>await  zip.files[file].async('string');
85       this.fileObject.data = value;
86       this.paths.push(this.fileObject); 
87     }
88     this.arrangeTreeData(this.paths);
89   }
90
91   arrangeTreeData(paths) {
92     const tree = [];
93
94     paths.forEach((path) => {
95
96       const pathParts = path.name.split('/');
97       pathParts.shift();
98       let currentLevel = tree;
99
100       pathParts.forEach((part) => {
101         const existingPath = currentLevel.filter(level => level.name === part);
102
103         if (existingPath.length > 0) {
104           currentLevel = existingPath[0].children;
105         } else {
106           const newPart = {
107             name: part,
108             children: [],
109             data: path.data
110           };
111
112           currentLevel.push(newPart);
113           currentLevel = newPart.children;
114         }
115       });
116     });
117     console.log('tree: ', tree);
118     return tree;
119   }
120 }