2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5 ===================================================================
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
12 http://www.apache.org/licenses/LICENSE-2.0
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============================================
22 import { Component, OnInit, ViewChild } from '@angular/core';
23 import { FlatTreeControl } from '@angular/cdk/tree';
24 import { MatTreeFlatDataSource, MatTreeFlattener } from '@angular/material/tree';
25 import { IBlueprint } from 'src/app/common/core/store/models/blueprint.model';
26 import "ace-builds/webpack-resolver";
28 import 'brace/ext/language_tools';
29 import 'ace-builds/src-min-noconflict/snippets/html';
30 import * as JSZip from 'jszip';
31 import { saveAs } from 'file-saver';
33 import { IAppState } from '../../../../common/core/store/state/app.state';
34 import { Store } from '@ngrx/store';
35 import { Observable } from 'rxjs';
36 import { IBlueprintState } from 'src/app/common/core/store/models/blueprintState.model';
37 import { LoadBlueprintSuccess, SetBlueprintState } from '../../../../common/core/store/actions/blueprint.action'
46 const TREE_DATA: Node[] = [
50 { name: 'activation-blueprint.json' },
51 { name: 'artifacts_types.json' },
52 { name: 'data_types.json' },
57 /** Flat node with expandable and level information */
58 interface ExampleFlatNode {
66 selector: 'app-editor',
67 templateUrl: './editor.component.html',
68 styleUrls: ['./editor.component.scss']
70 export class EditorComponent implements OnInit {
72 @ViewChild('editor') editor;
73 blueprintdata: IBlueprint;
74 blueprint: IBlueprint;
75 bpState: Observable<IBlueprintState>;
81 blueprintName: string;
82 fileExtension: string;
84 private zipFile: JSZip = new JSZip();
86 selectedFolder: string;
87 activationBlueprint: string;
88 isNameTextboxEnablled : boolean = false;
90 filetoDelete : string;
92 private transformer = (node: Node, level: number) => {
94 expandable: !!node.children && node.children.length > 0,
100 treeControl = new FlatTreeControl<ExampleFlatNode>(
101 node => node.level, node => node.expandable);
103 treeFlattener = new MatTreeFlattener(
104 this.transformer, node => node.level, node => node.expandable, node => node.children);
106 dataSource = new MatTreeFlatDataSource(this.treeControl, this.treeFlattener);
108 constructor(private store: Store<IAppState>) {
109 this.dataSource.data = TREE_DATA;
110 this.bpState = this.store.select('blueprint');
111 // this.dataSource.data = TREE_DATA;
114 hasChild = (_: number, node: ExampleFlatNode) => node.expandable;
117 this.editorContent();
118 this.dataSource.data = this.filesTree;
122 console.log('selected file:' + file);
125 this.editor.setTheme("eclipse");
126 this.editor.getEditor().setOptions({
127 // enableBasicAutocompletion: true,
131 this.editor.getEditor().commands.addCommand({
132 name: "showOtherCompletions",
134 exec: function (editor) {
138 this.bpState.subscribe(
140 var blueprintState: IBlueprintState = { blueprint: blueprintdata.blueprint, isLoadSuccess: blueprintdata.isLoadSuccess, isSaveSuccess: blueprintdata.isSaveSuccess, isUpdateSuccess: blueprintdata.isUpdateSuccess };
141 this.blueprintdata = blueprintState.blueprint;
142 this.filesTree = blueprintdata.files;
143 this.filesData = blueprintdata.filesData;
144 this.dataSource.data = this.filesTree;
145 this.blueprintName = blueprintdata.name;
147 for (let key in this.blueprintdata) {
148 if (this.blueprintdata.hasOwnProperty(key)) {
149 blueprint.push(this.blueprintdata[key]);
152 // this.text = JSON.stringify(this.blueprintdata, null, '\t');
153 // this.editor.getEditor().getSession().setMode("ace/mode/json");
154 this.editor.getEditor().getSession().setTabSize(2);
155 this.editor.getEditor().getSession().setUseWrapMode(true);
156 this.setEditorMode();
161 console.log(this.blueprint);
162 this.filesData.forEach(fileNode => {
163 if (this.selectedFile && fileNode.name.includes(this.blueprintName.trim()) && fileNode.name.includes(this.selectedFile.trim())) {
164 fileNode.data = this.text;
165 } else if (this.selectedFile && fileNode.name.includes(this.selectedFile.trim())) {
166 fileNode.data = this.text;
170 if (this.selectedFile && this.selectedFile == this.blueprintName.trim()) {
171 this.blueprint = JSON.parse(this.text);
173 this.blueprint = this.blueprintdata;
176 let blueprintState = {
177 blueprint: this.blueprint,
178 name: this.blueprintName,
179 files: this.filesTree,
180 filesData: this.filesData
182 this.store.dispatch(new SetBlueprintState(blueprintState));
183 // console.log(this.text);
186 selectFileToView(file) {
187 this.selectedFile = file.name;
188 this.filetoDelete = file.name;
189 this.filesData.forEach((fileNode) => {
190 if (fileNode.name.includes(file.name)) {
191 this.text = fileNode.data;
194 this.fileExtension = this.selectedFile.substr(this.selectedFile.lastIndexOf('.') + 1);
195 // console.log(this.fileExtension);
196 this.setEditorMode();
200 this.zipFile.generateAsync({ type: "blob" })
211 this.filesData.forEach((path) => {
212 this.zipFile.file(path.name, path.data);
218 var zipFilename = "baseconfiguration.zip";
219 this.zipFile.generateAsync({ type: "blob" })
221 saveAs(blob, zipFilename);
225 switch (this.fileExtension) {
230 this.mode = 'python';
233 this.mode = 'kotlin';
242 this.mode = 'velocity';
250 this.selectedFolder = node.name;
251 this.filetoDelete = node.name;
253 // this.createFolderOrFile(node.name, 'folder');
256 createFolderOrFile(name) {
257 let newFilesData: [any] = this.filesData;
266 for(let i=0; i< this.filesData.length; i++) {
267 if (this.filesData[i].name.includes(this.selectedFolder)) {
268 if(this.fileAction == 'createFolder') {
269 newFileNode.name = this.filesData[i].name + name.srcElement.value + '/';
270 newFileNode.data = '';
272 newFileNode1.name = this.filesData[i].name + name.srcElement.value + '/README.md'
273 newFileNode1.data = name.srcElement.value + ' Folder';
275 newFileNode.name = this.filesData[i].name + name.srcElement.value;
276 newFileNode.data = '';
282 this.filesData.splice(this.findIndexForNewNode()+1, 0, newFileNode);
283 this.filesData.splice(this.findIndexForNewNode()+1, 0, newFileNode1);
284 this.arrangeTreeData(this.filesData);
287 findIndexForNewNode() {
289 for(let i=0; i< this.filesData.length; i++) {
290 if (this.filesData[i].name.includes(this.selectedFolder)) {
294 return indexForNewNode;
297 arrangeTreeData(paths) {
300 paths.forEach((path) => {
302 const pathParts = path.name.split('/');
303 // pathParts.shift();
304 let currentLevel = tree;
306 pathParts.forEach((part) => {
307 const existingPath = currentLevel.filter(level => level.name === part);
309 if (existingPath.length > 0) {
310 currentLevel = existingPath[0].children;
318 if(part.trim() == this.blueprintName.trim()) {
319 this.activationBlueprint = path.data;
320 newPart.data = JSON.parse(this.activationBlueprint.toString());
321 console.log('newpart', newPart);
323 if(newPart.name !== '') {
324 currentLevel.push(newPart);
325 currentLevel = newPart.children;
330 this.dataSource.data = tree;
331 this.filesTree = tree;
332 this.isNameTextboxEnablled = false;
333 this.updateBlueprint();
336 enableNameInputEl(action) {
337 this.fileAction = action;
338 if (action == 'createFolder' || action == 'createFile') {
339 this.isNameTextboxEnablled = true;
343 deleteFolderOrFile(action) {
344 for(let i=0;i< this.filesData.length ; i++) {
345 if(this.filesData[i].name.includes(this.filetoDelete.trim())) {
346 this.filesData.splice(i, 1);
350 this.arrangeTreeData(this.filesData);