zhuguifei
2026-03-10 2c1fd10c6fbabb8e9f0e9f07fe66fb36c008e883
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<jsp:include page="../../../initlib/initAll.jsp"></jsp:include>
<script src="go.release.js"></script> 
<script type="text/javascript">
$(function(){
    collapsePanel();
     $("#myOverview").offset(function (n, c) {
         newPos = new Object();
         newPos.left = $("#myDiagram").offset().left;
         newPos.top = $("#myDiagram").offset().top;
         return newPos;
     });
});
function expandPanel(){
    $('#mainLayout').layout('expand','east');
}
 
function collapsePanel(){
    $('#mainLayout').layout('collapse','east');
}
</script>
<script id="code">
    function init() {
        var $ = go.GraphObject.make; // for conciseness in defining templates
        // 必须是ID,或者依赖div
        myDiagram = $(go.Diagram, "myDiagram", {
            //整个拓扑图的位置
            initialContentAlignment : go.Spot.Center,
            // 确保所有用户只创建树
            validCycle : go.Diagram.CycleDestinationTree,
            "draggingTool.dragsTree": true,
            "commandHandler.deletesTree": true,
            //用户一次只能选择一个
            maxSelectionCount : 1,
            layout : $(go.TreeLayout, {
                treeStyle : go.TreeLayout.StyleLastParents,
                arrangement : go.TreeLayout.ArrangementHorizontal,
                // 对于大多数的树的性质
                angle : 90,
                layerSpacing : 35,
                //为“最后的父母属性”:
                alternateAngle : 90,
                alternateLayerSpacing : 35,
                alternateAlignment : go.TreeLayout.AlignmentBus,
                alternateNodeSpacing : 20
            }),
            //支持编辑HTML中的属性选择的人
            "ChangedSelection" : onSelectionChanged,
            "TextEdited" : onTextEdited,
            // 是否能撤销恢复
            "undoManager.isEnabled" : true
        }); 
         /* myDiagram = $(go.Diagram, "myDiagram", {
                allowCopy: true,
                allowDelete: true,
                allowMove: true,
                initialContentAlignment: go.Spot.Center,
                initialAutoScale: go.Diagram.Uniform,
                //用户一次只能选择一个
                maxSelectionCount : 1,
                // 确保所有用户只创建树
                validCycle : go.Diagram.CycleDestinationTree,
                layout: $(FlatTreeLayout,{ 
                      angle: 90, 
                      compaction: go.TreeLayout.CompactionNone 
                  }),
                "undoManager.isEnabled": true
          }); */
        //添加修改监听事件
        // 当文件被修改,添加一个“*”的称号,使“保存”按钮
        myDiagram.addDiagramListener("Modified", function(e) {
            var button = document.getElementById("SaveButton");
            if (button)
                button.disabled = !myDiagram.isModified;
            var idx = document.title.indexOf("*");
            if (myDiagram.isModified) {
                if (idx < 0)
                    document.title += "*";
            } else {
                if (idx >= 0)
                    document.title = document.title.substr(0, idx);
            }
        });
          
          
        //级别颜色 
        var levelColors = [ "#30B340/#30AA40", "#2672EC/#2E8DEF",
                "#8C0095/#A700AE", "#5133AB/#643EBF", "#008299/#00A0B1",
                "#D24726/#DC572E", "#008A00/#00A600", "#094AB2/#0A5BC4" ];
 
        // 重写treelayout.commitnodes也修改基于树的深度平面背景刷
        myDiagram.layout.commitNodes = function() {
            //行为的标准
            go.TreeLayout.prototype.commitNodes.call(myDiagram.layout); 
            // 然后通过所有的顶点和设置相应的节点的shape.fill
            // 刷新一个依赖于treevertex.level
            myDiagram.layout.network.vertexes.each(function(v) {
                if (v.node) {
                    var level = v.level % (levelColors.length);
                    var colors = levelColors[level].split("/");
                    var shape = v.node.findObject("SHAPE");
                    if (shape){
                        shape.fill = $(go.Brush, go.Brush.Linear, {
                            0 : colors[0],
                            1 : colors[1],
                            start : go.Spot.Left,
                            end : go.Spot.Right
                        });
                    }
                }
            });
        }
 
        // 绑定节点双击事件,添加一个新节点,在该节点的下一个级别
        function nodeDoubleClick(e, obj) {
            var clicked = obj.part;
            if (clicked !== null) {
                var thisemp = clicked.data;
                myDiagram.startTransaction("添加员工");
                var nextkey = (myDiagram.model.nodeDataArray.length + 1).toString();
                var newemp = {
                    key : nextkey,
                    name : "(new person)",
                    title : "",
                    parent : thisemp.key
                };
                myDiagram.model.addNodeData(newemp);
                myDiagram.commitTransaction("添加员工");
            }
        }
 
        // 判断节点是否可以拖动
        function mayWorkFor(node1, node2) {
            //必须是一个节点
            if (!(node1 instanceof go.Node))
                return false; 
            //不能节点本身
            if (node1 === node2)
                return false; 
            //不能是你的下级
            if (node2.isInTreeOf(node1))
                return false; 
            var root = node2.findTreeRoot();
            //不能是根节点
            if(root && node1 === root)
                return false;
            return true;
        }
 
        // 这个方法提供了一个共同的风格大部分textblocks。
        // 这些值可以在一个特定的TextBlock重写。
        function textStyle() {
            return {
                font : "bold 14pt 微软雅黑",
                stroke : "white"
            };
        }
 
        //获取头像图片
        function findHeadShot(key) {
            return "plugins/gojs/assets/img/HS" + key + ".png";
        }
        myDiagram.toolManager.hoverDelay = 200;
        //定义节点模版
        myDiagram.nodeTemplate = $(go.Node, "Vertical", {
                doubleClick : nodeDoubleClick,
                selectionObjectName: "BODY"
            },{
                toolTip:
                    $(go.Adornment, "Auto", $(go.Shape, {
                            fill: "lightyellow" 
                        }),
                        $(go.Panel, "Horizontal", $(go.Picture, {
                            maxSize : new go.Size(39, 50),
                            margin : new go.Margin(6, 8, 6, 10),
                        }, 
                        //绑定图片资源
                        new go.Binding("source", "key", findHeadShot)),
                        $(go.TextBlock, { margin: 3 },new go.Binding("text", "key")))
                    )
            },{ 
                //鼠标拖动节点事件
                mouseDragEnter : function(e, node, prev) {
                    var diagram = node.diagram;
                    var selnode = diagram.selection.first();
                    var shape = null;
                    if (!mayWorkFor(selnode, node)){
                        alert();
                        myDiagram.UndoManager().undo();
                    }else{
                        shape =  node.findObject("SHAPE");
                    }
                    if (shape) {
                        shape._prevFill = shape.fill; //记住原来的
                        shape.fill = "darkred";
                    }
                },
                mouseOver: function(e,node,text){
                    //alert();
                },
                //拖动离开
                mouseDragLeave : function(e, node, next) {
                    var shape = node.findObject("SHAPE");
                    if (shape && shape._prevFill) {
                        shape.fill = shape._prevFill; //保存原来的
                    }
                },
                //鼠标按下事件
                mouseDrop : function(e, node) {
                    var diagram = node.diagram;
                    //假设在选择一个节点
                    var selnode = diagram.selection.first(); 
                    if (mayWorkFor(selnode, node)) {
                        // 找到任何现有的连接到所选节点
                        var link = selnode.findTreeParentLink();
                        if (link !== null) { 
                            //重置连接
                            link.fromNode = node;
                        } else { 
                            //否则创建一个新连接
                            diagram.toolManager.linkingTool.insertLink(node,node.port, selnode, selnode.port);
                        }
                    }
                }
            },
            //排序,有node.text是data.name
            new go.Binding("text", "name"),
            // 结合part.layername控制节点层取决于是否选择
            new go.Binding("layerName", "isSelected", function(sel) {
                return sel ? "Foreground" : "";
            }).ofObject(),
            $(go.Panel, "Auto",{
                    name : "BODY"
                },
                //定义节点的外形 圆角矩形
                $(go.Shape, "RoundedRectangle", {
                    name : "SHAPE",
                    fill : "white",
                    stroke : null,
                    //设置节点属性
                    portId : "",
                    fromLinkable : true,
                    toLinkable : true,
                    cursor : "pointer"
                }),
                //定义水平面板
                $(go.Panel, "Horizontal", 
                    /* $(go.Picture, {
                            maxSize : new go.Size(39, 50),
                            margin : new go.Margin(6, 8, 6, 10),
                        }, 
                        //绑定图片资源
                        new go.Binding("source", "key", findHeadShot)
                    ), */
                    //定义面板上的文字显示布局
                    $(go.Panel, "Table", {
                            maxSize : new go.Size(300, 999),
                            margin : new go.Margin(15, 20, 15, 20),
                            defaultAlignment : go.Spot.Left
                        }, 
                        $(go.RowColumnDefinition, { //两列
                            column : 2,
                            width : 4
                        }), 
                        $(go.TextBlock, textStyle(), {//名称
                                row : 0,
                                column : 0,
                                columnSpan : 5,
                                font : "14pt 微软雅黑",
                                editable : true,
                                isMultiline : false,
                                minSize : new go.Size(10, 16)
                            }, 
                            new go.Binding("text", "name").makeTwoWay()
                        )
                        //标题显示
                        /* $(go.TextBlock, "部门名称: ", textStyle(), {
                            row : 1,
                            column : 0
                        }),
                        $(go.TextBlock, textStyle(), {
                                row : 1,
                                column : 1,
                                columnSpan : 4,
                                editable : true,
                                isMultiline : false,
                                minSize : new go.Size(10, 14),
                                margin : new go.Margin(0, 0, 0,0)
                            }, 
                            new go.Binding("text", "title").makeTwoWay()
                        ), 
                        $(go.TextBlock, "节点ID: ", textStyle(), {
                            row : 2,
                            column : 0
                        }),
                        $(go.TextBlock, textStyle(), {
                                row : 2,
                                column : 1,
                                columnSpan : 4,
                                editable : true,
                                isMultiline : false,
                                minSize : new go.Size(10, 14),
                                margin : new go.Margin(0, 0, 0,0)
                            }, 
                            new go.Binding("text", "key").makeTwoWay()
                        ),
                        $(go.TextBlock, "父节点: ", textStyle(), {
                            row : 3,
                            column : 0
                        }),
                        $(go.TextBlock, textStyle(), {
                                row : 3,
                                column : 1,
                                columnSpan : 4,
                                editable : true,
                                isMultiline : false,
                                minSize : new go.Size(10, 14),
                                margin : new go.Margin(0, 0, 0,0)
                            }, 
                            new go.Binding("text", "parent").makeTwoWay()
                        ),
                        $(go.TextBlock, textStyle(),{
                                row : 3,
                                column : 0,
                                columnSpan : 5,
                                font : "italic 9pt sans-serif",
                                wrap : go.TextBlock.WrapFit,
                                editable : true, //默认换行符被允许
                                minSize : new go.Size(10, 14)
                            }, 
                            new go.Binding("text", "comments").makeTwoWay()
                        ) */
                    ) // 结束table面板
                )//结束水平面板 
            ),//结束主面板
            //添加tree展开按钮 下面是主体
            $(go.Panel,{ height: 15 },$("TreeExpanderButton"))
        ); // 结束节点
        //定义连接模版
        myDiagram.linkTemplate = $(go.Link, go.Link.Orthogonal,{ 
                corner: 10,
                relinkableFrom : true,
                relinkableTo : true 
            },
              $(go.Shape,{ strokeWidth:2,stroke : "#00a4a4" }),
              $(go.Shape, { toArrow: "Standard",stroke : "#00a4a4" })
        );
 
        //加载JSON格式数据
        load();
        //缩略图
        //显示在HTML元素上面
        myOverview = $(go.Overview, "myOverview",{ 
            observed: myDiagram 
        }); 
    }
 
    // 当选择一个节点时,允许用户编辑
    function onSelectionChanged(e) {
        var node = e.diagram.selection.first();
        if (node instanceof go.Node) {
            updateProperties(node.data);
        } else {
            updateProperties(null);
            collapsePanel();
        }
    }
 
    // Update the HTML elements for editing the properties of the currently selected node, if any
    function updateProperties(data) {
        expandPanel();
        if (data === null) {
            document.getElementById("propertiesPanel").style.display = "none";
            document.getElementById("name").value = "";
            document.getElementById("title").value = "";
            document.getElementById("comments").value = "";
        } else {
            document.getElementById("propertiesPanel").style.display = "block";
            document.getElementById("name").value = data.name || "";
            document.getElementById("title").value = data.title || "";
            document.getElementById("comments").value = data.comments || "";
        }
    }
 
    //这就是当用户完成文本编辑
    function onTextEdited(e) {
        var tb = e.subject;
        if (tb === null || !tb.name)
            return;
        var node = tb.part;
        if (node instanceof go.Node) {
            updateProperties(node.data);
        }
    }
 
    // 更新字段数据
    function updateData(text, field) {
        var node = myDiagram.selection.first();
        // 最大选择一个节点, 因此这是一个部分的集合
        var data = node.data;
        if (node instanceof go.Node && data !== null) {
            var model = myDiagram.model;
            model.startTransaction("modified " + field);
            if (field === "name") {
                model.setDataProperty(data, "name", text);
            } else if (field === "title") {
                model.setDataProperty(data, "title", text);
            } else if (field === "comments") {
                model.setDataProperty(data, "comments", text);
            }
            model.commitTransaction("modified " + field);
        }
        collapsePanel();
    }
 
    //保存JSON
    function save() {
        document.getElementById("mySavedModel").value = myDiagram.model.toJson();
        myDiagram.isModified = false;
    }
    //加载数据
    function load() {
        myDiagram.model = go.Model.fromJson(document.getElementById("mySavedModel").value);
    }
    
    //自定义布局
    function FlatTreeLayout() {
      //调用基本结构
      go.TreeLayout.call(this);  
    }
    go.Diagram.inherit(FlatTreeLayout, go.TreeLayout);
    FlatTreeLayout.prototype.commitLayout = function() {
          go.TreeLayout.prototype.commitLayout.call(this);  // call base method first
          //找到Y坐标的所有节点
          var y = -Infinity;
          //遍历
          this.network.vertexes.each(function(v) {
            y = Math.max(y, v.node.position.y);
        });
          // 移动所有的节点,但是X坐标不变
          this.network.vertexes.each(function(v) {
              var node = v.node;
              if (node.isTreeLeaf) {
                node.position = new go.Point(node.position.x, y);
              }
          });
    };
    //自定义布局结束
</script>
</head>
<body onload="init()">
    <div id="myOverview" style="border: solid 1px red; width: 180px; height: 80px; position: absolute; top:0px; left: 0px; background-color: aliceblue; z-index: 900"></div>       
    <div id="mainLayout" class="easyui-layout" data-options="fit: true,border:false">
        <div data-options="region: 'north',height:35">
            <div class="easyui-toolbar">
                <a onclick="getAllOrgs();" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true">查询</a>
            <a onclick="goToOrgAddJsp();" href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true">添加</a>
            <a onclick="redo();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'ext-icon-resultset_next'">展开</a>
            <a onclick="undo();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'ext-icon-resultset_previous'">折叠</a>
            <a onclick="toView();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'ext-icon-resultset_previous'">切换到视图显示</a>
            <a onclick="undo();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'ext-icon-resultset_previous'">切换到表格显示</a>
            </div>
        </div>
        <div id="propertiesPanel" title="属性" data-options="region: 'east',border:false,iconCls: 'icon-standard-map', split: true" style="width:200px; padding:0px;">
            <table class="table-edit" width="100%" align="center">
                <tr>
                    <th >名称:</th>
                    <td >
                        <input type="text" id="name" value="" onchange="updateData(this.value, 'name')" />
                    </td>
                </tr>
                <tr>
                    <th > 标题:</th>
                    <td>
                         <input type="text" id="title" value="" onchange="updateData(this.value, 'title')" />
                    </td>
                </tr>
                <tr>
                    <th >备注:</th>
                    <td >
                        <input type="text" id="comments" value="" onchange="updateData(this.value, 'comments')" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: center" colspan="2">
                        <button id="SaveButton" onclick="save()">保存</button>
                        <button onclick="load()">查询</button>
                    </td>
                </tr>
            </table>
        </div>
        <div id="myDiagram" data-options="region: 'center',border:false, split: true" style="padding: 0px;">
            
        </div>
        <div style="display:none;">
            <textarea id="mySavedModel" style="width: 100%; height: 250px">
                { "class": "go.TreeModel",
                  "nodeDataArray": [
                {"key":"1", "name":"上海兰宝传感科技股份有限公司", "title":"执行总监"}, 
                {"key":"2", "name":"研发部", "title":"财务总监", "parent":"11"}, 
                {"key":"3", "name":"销售部", "title":"销售经理", "parent":"11"}, 
                {"key":"4", "name":"人力资源部", "title":"市场部经理", "parent":"11"}, 
                {"key":"5", "name":"信息系统部", "title":"研发部经理", "parent":"11"}, 
                {"key":"6", "name":"生产部", "title":"软件工程师", "parent":"11"}, 
                {"key":"7", "name":"客户服务部", "title":"行政助理", "parent":"11"},
                {"key":"8", "name":"审计会", "title":"办公室主任", "parent":"1"},
                {"key":"9", "name":"生管课", "title":"科长", "parent":"6"},
                {"key":"10", "name":"制造课", "title":"工程师", "parent":"7"},
                {"key":"11", "name":"兰宝传感", "title":"人事部职员", "parent":"1"},
                {"key":"12", "name":"兰宝环保", "title":"软件工程师", "parent":"1"},
                {"key":"13", "name":"项目营销部", "title":"测试人员", "parent":"12"},
                {"key":"14", "name":"项目研发部", "title":"硬件维护", "parent":"10"},
                {"key":"15", "name":"项目工程部", "title":"质量检测", "parent":"5"},
                {"key":"16", "name":"项目服务部", "title":"销售代理", "parent":"3"} 
                 ]
                }
         </textarea>
        </div>
    </div>
</body>
</html>