feb945798887b3a8f5816cd5cf385215ceeca877
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12 import { Component, OnInit } from "@angular/core";
13 import { TranslateService } from "@ngx-translate/core";
14 import { NodeTypeService } from "../../../services/node-type.service";
15 import { DisplayInfoService } from "../../../services/display-info.service";
16 import { NodeDataType } from "../../../model/node-data-type/node-data-type";
17 import { NodeType } from "../../../model/workflow/node-type.enum";
18 import { JsPlumbService } from "../../../services/jsplumb.service";
19 import { SettingService } from "../../../services/setting.service";
20 import { WorkflowUtil } from "../../../util/workflow-util";
21
22 @Component({
23     selector: 'wfm-toolbar-node',
24     templateUrl: 'toolbar-node.component.html',
25     styleUrls: ['../toolbar.component.css']
26 })
27 export class ToolbarNodeComponent implements OnInit {
28     public nodeCategories: any[] = [];
29     public nodeTypeEnum = NodeType;
30     public supportRest: boolean;
31
32     private needInitButton = false;
33
34     constructor(private nodeTypeService: NodeTypeService,
35         private displayInfoService: DisplayInfoService,
36         private jsPlumbService: JsPlumbService,
37         private settingService: SettingService,
38         public translate: TranslateService) {
39
40     }
41
42     public ngOnInit(): void {
43         this.settingService.getSetting().subscribe(setting => {
44             this.initSetting(setting);
45             this.displayInfoService.getDisplayInfo().subscribe(resp => {
46                 this.initNodeCategories(resp);
47                 this.needInitButton = true;
48             });
49         });
50     }
51
52     public ngAfterViewChecked(): void {
53         if (this.needInitButton) {
54             console.log('initJsPlumb');
55             this.initJsPlumb();
56             this.needInitButton = false;
57         }
58     }
59
60     private initSetting(setting: any): void {
61         this.supportRest = setting.supportRestNode;
62     }
63
64     private initJsPlumb(): void {
65         this.jsPlumbService.buttonDraggable();
66         this.jsPlumbService.buttonDroppable();
67     }
68
69     private initNodeCategories(displayInfo: any): void {
70         const defaultCategory = this.insertDefaultCategory();
71
72         const categoryData = displayInfo['categoryData'] || {};
73         for (let key in categoryData) {
74             const group = {
75                 id: key,
76                 displayName: categoryData[key].displayName,
77                 collapse: categoryData[key].collapse || false,
78                 nodes: []
79             };
80             this.nodeCategories.push(group);
81         }
82
83         const defaultNodes = displayInfo['nodes'] || {};
84         for (let nodeId in defaultNodes) {
85             const nodeType = this.nodeTypeService.getNodeDataTypeById(nodeId);
86             const node = defaultNodes[nodeId];
87             if (node && node.category) {
88                 const nodeCategory = this.nodeCategories.find(category => category.id === node.category);
89                 if (nodeCategory) {
90                     nodeCategory.nodes.push(nodeType);
91                 } else {
92                     defaultCategory.nodes.push(nodeType);
93                 }
94             } else {
95                 defaultCategory.nodes.push(nodeType);
96             }
97         }
98     }
99
100     private insertDefaultCategory(): any {
101         this.nodeCategories = [];
102         const defaultCategory = {
103             id: 'default',
104             displayName: {
105                 zh_CN: '任务',
106                 en_US: 'Task'
107             },
108             collapse: true,
109             nodes: []
110         };
111         this.nodeCategories.push(defaultCategory);
112
113         return defaultCategory;
114     }
115
116     public getDisplayName(data: any): string {
117         let language = 'zh_CN';
118         if (this.translate.currentLang.indexOf('en') > -1) {
119             language = 'en_US';
120         }
121         return data.displayName ? data.displayName[language] : data.id;
122     }
123
124     public getImageUrl(nodeType: NodeDataType): string {
125         const name = nodeType && nodeType.icon ? nodeType.icon.name : '';
126         return WorkflowUtil.GetIconFullPath(name);
127     }
128 }