• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd.
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14*/
15
16const { AttributeArea } = require("./attr/AttributeArea");
17const { ModifyNode } = require("./hcs/ModifyNode");
18const { DataType, NodeType } = require("./hcs/NodeTools");
19const { NodeTools } = require("./hcs/NodeTools");
20
21class AttrEditor {
22    constructor() {
23        AttributeArea.gi().registRecvCallback(this.onChange);
24        this.files_ = []
25        this.callback_ = null;
26    }
27    setFiles(files) {
28        this.files_ = files;
29    }
30    freshEditor(fn, node) {
31        this.filePoint_ = fn;
32        this.node_ = node;
33        if (fn in this.files_) {
34            this.root_ = this.files_[fn];
35        }
36
37        AttributeArea.gi().clear();
38        if (this.node_ != null) {
39            AttributeArea.gi().addTitle(this.node_.name_)
40            switch (this.node_.type_) {
41                case 6://Node
42                    switch (this.node_.nodeType_) {
43                        case 0://Data class nodes, not inherit
44                            this.freshDataNodeNotInheritEditor(this.node_);
45                            break;
46                        case 1://Copy class nodes
47                        case 2://Reference modification class nodes
48                        case 5://Data class nodes, inherit
49                            this.freshcopyNodeEditor(this.node_);
50                            break;
51                        case 3://Deletion class nodes
52                            this.freshdeleteNodeEditor(this.node_);
53                            break;
54                        case 4://Templete Class nodes
55                            this.freshTempletNodeEditor(this.node_);
56                            break;
57                        default:
58                            break;
59                    }
60                    break;
61                case 7://Attribute
62                    this.freshDefineAttributeEditor(this.node_);
63                    break;
64            }
65        }
66        else AttributeArea.gi().addTitle("属性编辑区")
67
68        AttributeArea.gi().flush();
69    }
70    // Node0 -- Data class nodes, not inherit
71    freshDataNodeNotInheritEditor(node) {// Edit data class node
72        AttributeArea.gi().addInput("name", "节点名称", node.name_, this.root_ == this.node_);
73        AttributeArea.gi().addGap(0);
74        AttributeArea.gi().addSelect("node_type", "节点类型", AttrEditor.NODE_TYPE_STR,
75        AttrEditor.NODE_TYPE_STR[node.nodeType_], this.root_ == this.node_);
76        AttributeArea.gi().addGap(0);
77        AttributeArea.gi().addDotLine();
78        AttributeArea.gi().addButton("add_child_node", "添加子节点");
79        AttributeArea.gi().addButton("add_child_attr", "添加子属性");
80        AttributeArea.gi().addButtonDelete("delete", "删除");
81    }
82    changeDataNodeNotInherit(searchId, type, value) {
83        if (searchId == "node_type") {
84            ModifyNode.modifyNodeType(this.files_, this.root_, this.node_, AttrEditor.NODE_TYPE_STR.indexOf(value));
85            this.freshEditor(this.filePoint_, this.node_);
86        }
87        if (searchId == "name" && this.node_.name_ != value) {
88          ModifyNode.modifyName(this.files_, this.root_, this.node_, value);
89        }
90        if (searchId == "add_child_node") ModifyNode.addChildNode(this.root_, this.node_);
91        if (searchId == "add_child_attr") ModifyNode.addChildAttr(this.root_, this.node_);
92        if (searchId == "delete") {
93            ModifyNode.deleteNode(this.node_);
94            this.freshEditor();
95        }
96    }
97    //Node1 -- Copy class nodes
98    //Node2 -- Reference modification class nodes
99    //Node5 -- Data class nodes, inherit
100    freshcopyNodeEditor(node) {
101        AttributeArea.gi().addInput("name", "节点名称", node.name_);
102        AttributeArea.gi().addGap(0);
103        AttributeArea.gi().addInput("node_type", "节点类型", AttrEditor.NODE_TYPE_STR[node.nodeType_], true);
104        AttributeArea.gi().addGap(0);
105        AttributeArea.gi().addLabelButton("change_target", node.ref_, "源节点");
106        AttributeArea.gi().addGap(0);
107        AttributeArea.gi().addDotLine();
108        AttributeArea.gi().addButton("add_child_node", "添加子节点");
109        AttributeArea.gi().addButton("add_child_attr", "添加子属性");
110        AttributeArea.gi().addButtonDelete("delete", "删除");
111    }
112    changecopyNode(searchId, type, value) {
113        if (searchId == "name" && this.node_.name_ != value) {
114          ModifyNode.modifyName(this.files_, this.root_, this.node_, value);
115        }
116        if (searchId == "change_target") {
117            this.callCallback("change_target", this.node_);
118        }
119        if (searchId == "add_child_node") ModifyNode.addChildNode(this.root_, this.node_);
120        if (searchId == "add_child_attr") ModifyNode.addChildAttr(this.root_, this.node_);
121        if (searchId == "delete") {
122            ModifyNode.deleteNode(this.node_);
123            this.freshEditor();
124        }
125    }
126    //Node3 -- Deletion class nodes
127    freshdeleteNodeEditor(node) {
128        AttributeArea.gi().addInput("name", "节点名称", node.name_);
129        AttributeArea.gi().addGap(0);
130        AttributeArea.gi().addInput("node_type", "节点类型", AttrEditor.NODE_TYPE_STR[node.nodeType_], true);
131        AttributeArea.gi().addGap(0);
132        AttributeArea.gi().addButtonDelete("delete", "删除");
133    }
134    changedeleteNode(searchId, type, value) {
135        if (searchId == "name" && this.node_.name_ != value) {
136          ModifyNode.modifyName(this.files_, this.root_, this.node_, value);
137        }
138        if (searchId == "delete") {
139            ModifyNode.deleteNode(this.node_);
140            this.freshEditor();
141        }
142    }
143    //Node4 -- Templete Class nodes
144    freshTempletNodeEditor(node) {
145        AttributeArea.gi().addInput("name", "节点名称", node.name_);
146        AttributeArea.gi().addGap(0);
147        AttributeArea.gi().addInput("node_type", "节点类型", AttrEditor.NODE_TYPE_STR[node.nodeType_], true);
148        AttributeArea.gi().addGap(0);
149        AttributeArea.gi().addDotLine();
150        AttributeArea.gi().addButton("add_child_node", "添加子节点");
151        AttributeArea.gi().addButton("add_child_attr", "添加子属性");
152        AttributeArea.gi().addButtonDelete("delete", "删除");
153    }
154    changeTempletNode(searchId, type, value) {
155        if (searchId == "name") {
156          ModifyNode.modifyName(this.files_, this.root_, this.node_, value);
157        }
158        if (searchId == "add_child_node") ModifyNode.addChildNode(this.root_, this.node_);
159        if (searchId == "add_child_attr") ModifyNode.addChildAttr(this.root_, this.node_);
160        if (searchId == "delete") {
161            ModifyNode.deleteNode(this.node_);
162            this.freshEditor();
163        }
164    }
165
166
167    //Properties0 Define class
168    freshDefineAttributeEditor(node) {//Define class Attribute
169        let v = node.value_;
170        AttributeArea.gi().addInput("name", "名称", node.name_);
171        AttributeArea.gi().addGap(0);
172
173        if (v.type_ == DataType.INT8 ||
174            v.type_ == DataType.INT16 ||
175            v.type_ == DataType.INT32 ||
176            v.type_ == DataType.INT64) {
177            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[0]);
178            AttributeArea.gi().addGap(0);
179            AttributeArea.gi().addInput("value", "属性值", NodeTools.jinZhi10ToX(v.value_, v.jinzhi_));
180            AttributeArea.gi().addGap(0);
181        }
182        else if (v.type_ == DataType.STRING) {
183            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[1]);
184            AttributeArea.gi().addGap(0);
185            AttributeArea.gi().addInput("value", "属性值", v.value_);
186            AttributeArea.gi().addGap(0);
187        }
188        else if (v.type_ == DataType.ARRAY) {
189            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[2]);
190            AttributeArea.gi().addGap(0);
191            AttributeArea.gi().addTextArea("value", "属性值", NodeTools.arrayToString(v, 20));
192            AttributeArea.gi().addGap(0);
193        }
194        else if (v.type_ == DataType.BOOL) {
195            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[3]);
196            AttributeArea.gi().addGap(0);
197            AttributeArea.gi().addSelect("value", "属性值", [true, false], v.value_ == 1);
198            AttributeArea.gi().addGap(0);
199        }
200        else if (v.type_ == DataType.REFERENCE) {
201            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[4]);
202            AttributeArea.gi().addGap(0);
203            AttributeArea.gi().addLabelButton("change_target", v.value_, "源节点");
204            AttributeArea.gi().addGap(0);
205        }
206        else if (v.type_ == DataType.DELETE) {
207            AttributeArea.gi().addSelect("value_type", "类型", AttrEditor.ATTR_TYPE_STR, AttrEditor.ATTR_TYPE_STR[5]);
208            AttributeArea.gi().addGap(0);
209        }
210
211        AttributeArea.gi().addButtonDelete("delete", "删除");
212    }
213
214    getNodeValue(v, value) {
215        switch (AttrEditor.ATTR_TYPE_STR.indexOf(value)) {
216            case 0://int
217                v.type_ = DataType.INT8;
218                v.value_ = 0;
219                v.jinzhi_ = 10;
220                break;
221            case 1://str
222                v.type_ = DataType.STRING;
223                v.value_ = "";
224                break;
225            case 2://array
226                v.type_ = DataType.ARRAY;
227                v.value_ = [];
228                break;
229            case 3://bool
230                v.type_ = DataType.BOOL;
231                v.value_ = 1;
232                break;
233            case 4://ref
234                v.type_ = DataType.REFERENCE;
235                v.value_ = "unknow";
236                break;
237            case 5://delete
238                v.type_ = DataType.DELETE;
239                break;
240        }
241        this.freshEditor(this.filePoint_, this.node_);
242    }
243
244    changeDefineAttribute(searchId, type, value) {
245        let v = this.node_.value_;
246        if (searchId == "name" && value != this.node_.name_) {
247          ModifyNode.modifyName(this.files_, this.root_, this.node_, value);
248        }
249        if (searchId == "value_type") {
250          this.getNodeValue(v, value);
251        }
252        if (searchId == "change_target") {
253            this.callCallback("change_target", v);
254        }
255        if (searchId == "value") {
256            if (v.type_ == DataType.INT8 ||
257                v.type_ == DataType.INT16 ||
258                v.type_ == DataType.INT32 ||
259                v.type_ == DataType.INT64) {
260                let ret = NodeTools.jinZhiXTo10(value)
261                if (!isNaN(ret[0])) {
262                    v.value_ = ret[0]
263                    v.jinzhi_ = ret[1]
264                }
265            }
266            else if (v.type_ == DataType.STRING) {
267                v.value_ = value;
268            }
269            else if (v.type_ == DataType.ARRAY) {
270                v.value_ = NodeTools.stringToArray(value);
271            }
272            else if (v.type_ == DataType.BOOL) {
273                v.value_ = value == "true" ? 1 : 0;
274            }
275        }
276        if (searchId == "delete") {
277            ModifyNode.deleteNode(this.node_);
278            this.freshEditor();
279        }
280    }
281    //propertie1 Deletion class
282    //propertie2 Reference modification class
283    onChange(searchId, type, value) {
284        let pattr = AttrEditor.gi();
285        if (pattr.node_ != null) {
286            AttributeArea.gi().addTitle(pattr.node_.name_)
287            switch (pattr.node_.type_) {
288                case 6://Node
289                    switch (pattr.node_.nodeType_) {
290                        case 0://Data class nodes, not inherit
291                            pattr.changeDataNodeNotInherit(searchId, type, value);
292                            break;
293                        case 1://Copy class nodes
294                        case 2://Reference modification class nodes
295                        case 5://Data class nodes, inherit
296                            pattr.changecopyNode(searchId, type, value);
297                            break;
298                        case 3://Deletion class nodes
299                            pattr.changedeleteNode(searchId, type, value);
300                            break;
301                        case 4://Templete Class nodes
302                            pattr.changeTempletNode(searchId, type, value);
303                            break;
304                        default:
305                            break;
306                    }
307                    break;
308                case 7://Attribute
309                    pattr.changeDefineAttribute(searchId, type, value);
310                    break;
311            }
312            pattr.callCallback("writefile")
313        }
314    }
315    callCallback(type, value) {
316        if (this.callback_ != null) {
317            this.callback_(type, value);
318        }
319    }
320    registCallback(func) {
321        this.callback_ = func;
322    }
323}
324
325AttrEditor.NODE_TYPE_STR = ["数据类不继承", "复制类", "引用修改类", "删除类", "模板类", "数据类继承"];
326AttrEditor.ATTR_CLASS = ["定义类属性", "删除类属性", "引用类属性"];
327
328AttrEditor.ATTR_TYPE_STR = ["整数", "字符串", "数组", "布尔", "引用", "删除"]
329
330AttrEditor.pInstance_ = null;
331AttrEditor.gi = function () {
332    if (AttrEditor.pInstance_ == null) AttrEditor.pInstance_ = new AttrEditor();
333    return AttrEditor.pInstance_;
334}
335
336module.exports = {
337    AttrEditor
338}
339