jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
/*
Graph类来管理整个流程图,包括所有部件、上下文菜单等等都由Graph来统一管理和调度
*/
var HH = {};
HH.Libra = {};
HH.Libra.Graph = function (type, options) {
    var graph = this,
        activities = {},
        transitions = {};
    graph.contextMenuItems = [];
    //  graph.contextMenuItems.push(new HH.Libra.ContextMenuExecuteItem({ text: '新增文本', url: this }));
 
    var zrenderInstance,
        contextMenuContainer;
    this.type = type;
    //1. 新增元素
    this.addEventListener = function (name, handler) {
        this.addEventListener(name, handler);
    };
 
 
    this.addActivity = function (activity) {
        activity.graph = graph;
        activities[activity.id] = { object: activity };
    };
    //1. 新增元素
    this.delActivity = function (activity) {
        activity.graph = graph;
        activities[activity.id] = { object: null };
    };
    // 2. 更新元素
    this.UpdateActivity = function (id, activity) {
        activity.graph = graph;
        activities[id] = { object: activity };
    };
    //3.根据ID,得到元素实体
    this.getActivity = function (id) { return activities[id].object; };
    //4.获取所有元素的集合
    this.getAllActivity = function () { return activities; };
    //5.线条对象
    this.addTransition = function (transition) {
        transition.graph = graph;
        transitions[transition.id] = { object: transition };
    };
    //6.更新元素视图
    function modElements(shapes) {
        shapes.each(function (shape) { zrenderInstance.modElement(shape); });
        return shapes;
    }
 
    var dragingActivity = null;
 
    // 当前正在拖放的节点
    // 活动节点拖放开始
    this.onActivityDragStart = function (activity) {
        dragingActivity = activity;
 
    };
    // 活动节点拖放结束
    this.onActivityDragEnd = function () {
        if (dragingActivity)
            refreshActivityTransitions(dragingActivity);
        dragingActivity = null;
    };
 
    //单元素实时刷新
    this.refreshActivity = function (shape) {
        zrenderInstance.modElement(shape);
    }
 
    // 拖动过程处理
    function zrenderInstanceOnMouseMove() {
        if (dragingActivity != null)
            refreshActivityTransitions(dragingActivity);
    }
 
    function onClickTest(obj) {
      //  console.log(obj);
        if (obj.target == undefined) {
            addText(obj.event.offsetX, obj.event.offsetY);
        }
    }
    // 刷新活动相关的所有连接弧
    function refreshActivityTransitions(activity) {
        var activityId = activity.id;
        for (var key in transitions) {
            var transition = transitions[key].object;
            if (transition.from === activityId || transition.to == activityId) {
                zrenderInstance.refreshShapes(modElements(transition.refresh(graph)));
            }
        }
    }
 
    // 当前选中的部件
    var selectedUnit = null;
    this.onUnitSelect = function (unit) {
        if (selectedUnit) zrenderInstance.refreshShapes(modElements(selectedUnit.unselect(graph)));
        zrenderInstance.refreshShapes(modElements(unit.select(graph)));
        selectedUnit = unit;
 
    };
    //记录当前鼠标在哪个部件上,可以用来生成上下文相关菜单
    var currentUnit = null;
    this.onUnitMouseOver = function (unit) {
        currentUnit = unit;
    };
    this.onUnitMouseOut = function (unit) {
        if (currentUnit === unit) currentUnit = null;
    };
    //上下文菜单事件响应
    function onContextMenu(event) {
        Event.stop(event);
        if (currentUnit) {
            currentUnit.showContextMenu(event, contextMenuContainer, graph);
        }
 
    }
 
    //增加新的对象元素
    this.addShape = function (shape) {
        zrenderInstance.addShape(shape);
    };
 
 
    //删除对象元素
    this.delShape = function (shape) {
        zrenderInstance.delShape(shape);
        contextMenuContainer.hide();
    };
 
    // 增加新的对象元素
    this.modShape = function (_activity, x, y) {
        // 更新元素的坐标信息
        //alert(_activity.id)
        //alert(x)
        //alert(y)
        for (var key in activities) {
            if (activities[key].object != null) {
                if (activities[key].object.id == _activity.id) {
                    activities[key].object.position.x = x;
                    activities[key].object.position.y = y;
                    //                alert(_activity.position.y);
                    break;
                }
            }
        }
    };
 
    // 初始化 
    this.init = function (widthstr, heightstr) {
       
        if (widthstr == "" || widthstr == null || widthstr == undefined) widthstr = "500";
        if (heightstr == "" || heightstr == null || heightstr == undefined) heightstr = "350";
        var canvasElement = options.canvas.element;
        try {
            canvasElement.empty();
        } catch (exception) {
            window.location.reload();
            return false;
        }
        //canvasElement.setStyle({ height: document.viewport.getHeight() + 'px' });
        canvasElement.setStyle({ height: heightstr + 'px', width: widthstr + 'px' });
        zrenderInstance = graph.type.zrender.init(document.getElementById(canvasElement.identify()));
 
        // 创建上下文菜单容器
        contextMenuContainer = new Element('div', { 'class': 'context-menu' });
        contextMenuContainer.hide();
        document.body.appendChild(contextMenuContainer);
 
        Event.observe(contextMenuContainer, 'mouseout', function (event) {
            // 关闭时,应判断鼠标是否已经移出菜单容器
            if (!Position.within(contextMenuContainer, event.clientX, event.clientY)) {
                contextMenuContainer.hide();
            }
        });
        zrenderInstance.on('dblclick', onClickTest);
        //alert(1);
        // 侦听拖动过程
        zrenderInstance.on('mousemove', zrenderInstanceOnMouseMove);
        //  上下文菜单
        Event.observe(document, 'contextmenu', onContextMenu);
    };
 
 
    // 呈现或刷新呈现
    this.render = function () {
        var canvasElement = options.canvas.element;
        canvasElement.setStyle({ height: document.viewport.getHeight() + 'px' });
        zrenderInstance.render();
    };
 
    //获取二进制流
    this.GetImgUrl = function () {
        var ShapeURL = zrenderInstance.toDataURL();
        return ShapeURL;
    };
};
 
/*
 * 部件(包括活动和连接弧): 定义了Unit(部件基类),所有的部件都继承自这个基类
 */
HH.Libra.Unit = Class.create({
    id: null,
    title: null,
    graph: null,
    // 当前是否被选中
    selected: false,
    // 上下文菜单项集合
    contextMenuItems: [],
 
    initialize: function (options) {
        var _this = this;
        _this.id = options.id;
        _this.title = options.title;
      
    },
    createShapeOptions: function () {
        var _this = this;
        return {
            hoverable: true,
            clickable: true,
            onclick: function (params) {
                // 选中并高亮
                _this.graph.onUnitSelect(_this);
            },
            onmouseover: function (params) { _this.graph.onUnitMouseOver(_this); },
            onmouseout: function (params) { _this.graph.onUnitMouseOut(_this); }
        };
    },
 
    addTo: function (graph) { },
    // 刷新显示
    refresh: function (graph) { return []; },
    // 选中
    select: function (graph) {
        this.selected = true;
        return this.refresh(graph);
    },
    // 取消选中
    unselect: function (graph) {
        this.selected = false;
        return this.refresh(graph);
    },
    // 显示上下文菜单
    showContextMenu: function (event, container, graph) {
 
        container.hide();
        container.innerHTML = '';
 
        var ul = new Element('ul');
        container.appendChild(ul);
        this.buildContextMenuItems(ul, graph);
 
        // 加偏移,让鼠标位于菜单内
        var offset = -5;
        var rightEdge = document.body.clientWidth - event.clientX;
        var bottomEdge = document.body.clientHeight - event.clientY;
        if (rightEdge < container.offsetWidth)
            container.style.left = document.body.scrollLeft + event.clientX - container.offsetWidth + offset;
        else
            container.style.left = document.body.scrollLeft + event.clientX + offset;
 
        if (bottomEdge < container.offsetHeight)
            container.style.top = document.body.scrollTop + event.clientY - container.offsetHeight + offset;
        else
            container.style.top = document.body.scrollTop + event.clientY + offset;
 
        container.show();
    },
 
    // 创建上下文菜单项
    buildContextMenuItems: function (container, graph) {
        var unit = this;
        unit.contextMenuItems.each(function (item) {
 
            item.addTo(container);
        });
    }
});
 
/*
 * 上下文菜单项基类
 */
HH.Libra.ContextMenuItem = Class.create({
    options: null,
    // 菜单文本
    text: null,
    initialize: function (options) {
        this.options = options;
        this.text = options.text;
    },
    //
    addTo: function (container) {
        var _this = this;
        var li = new Element('li');
        container.appendChild(li);
        var a = new Element('a', { href: 'javascript:;' });
        li.appendChild(a);
        a.insert(_this.text);
        Event.observe(a, 'click', function () {
            _this.onClick();
        });
    }
 
});
 
/*
 * 上下文菜单项(直接执行)
 */
HH.Libra.ContextMenuExecuteItem = Class.create(HH.Libra.ContextMenuItem, {
    url: null,
 
    initialize: function ($super, options) {
        $super(options);
        this.url = options.url;//此处传递的是右键的对象
    },
 
    onClick: function () {
        var shape = this.url.shape;
        graph.delShape(shape);
        graph.delActivity(this.url);
      //  this.url.shape = shape;
        
        var tagActivities = graph.getAllActivity();
        
        var has = false;
        var delHot =this.url.shape.style.text;
        for (var key in tagActivities) {
            if (tagActivities[key].object != null) {
                var hotname = tagActivities[key].object.shape.style.text;
                if(hotname==delHot)
                    has=true;
            }
        }
        //左侧有重复热点情况下,删除其中一个热点时,需要保持右侧列表剩余热点的行“已存在”颜色
        if(!has)
            changeRowColor(delHot, 0);
    }
});