forked from Ericsson/CodeCompass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagram.js
More file actions
316 lines (258 loc) · 8.89 KB
/
Copy pathdiagram.js
File metadata and controls
316 lines (258 loc) · 8.89 KB
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
require([
'dojo/_base/declare',
'dojo/dom-attr',
'dojo/dom-construct',
'dojo/query!css2',
'dojo/topic',
'dijit/layout/BorderContainer',
'dijit/layout/ContentPane',
'dijit/form/Button',
'dijit/Dialog',
'codecompass/urlHandler',
'codecompass/viewHandler',
'codecompass/model'],
function (declare, attr, dom, query, topic, BorderContainer, ContentPane,
Button, Dialog, urlHandler, viewHandler, model) {
var Diagram = declare(BorderContainer, {
constructor : function () {
this.gutters = false;
this.style = 'height: 100%';
this._subscribeTopics();
},
postCreate : function () {
var that = this;
//--- Diagram region ---//
this._diagCont = new ContentPane({
region : 'top',
splitter : true,
style : 'height: 70%; width: 100%;'
});
this.addChild(this._diagCont);
//--- Text region ---//
viewHandler.getModuleAsync('text').then(function (text) {
that._textModule = text;
that._textCont = new ContentPane({
region : 'center',
content : that._textModule,
style : 'padding: 0; border-top: 1px solid gray'
});
that.addChild(that._textCont);
});
//--- Update on resize ---//
require(['dojo/aspect'], function (aspect) {
aspect.after(that._diagCont, 'resize', function () {
if (that._svgPanZoom)
that._svgPanZoom.resize();
});
});
},
loadDiagram : function (handlerId, diagramType, node) {
var that = this;
var handler = viewHandler.getModule(handlerId);
if (!handler) {
console.warn(handlerId + ' diagram is not registrated.');
return;
}
this._handler = handler;
this._handlerId = handlerId;
this._diagramType = diagramType;
if (gtag) {
gtag ('event', 'load_diagram: ' + diagramType.toString(), {
'event_category' : urlHandler.getState('wsid'),
'event_label' : urlHandler.getFileInfo().name
});
}
try {
this._diagCont.set('content', dom.create('span', {
class : 'diagram-loading'
}));
this._handler.getDiagram(diagramType, node, function (svg) {
if (svg) {
var svgDom = dom.toDom(svg);
// Remove default browser tooltips.
// TODO: Sometimes the default browser tooltips are in an <a> tag
// which contains an xlink:title attribute. This is the case for
// example in "provides" relation where the provided functions are
// listed in the tooltip. These should also be placed in a Dojo
// Tooltip so these can also be removed from the diagram.
query('.node title', svgDom).forEach(function (node) {
dom.destroy(node);
});
query('.edge title', svgDom).forEach(function (node) {
dom.destroy(node);
});
query('.graph title', svgDom).forEach(function (node) {
dom.destroy(node);
});
that._diagCont.set('content', svgDom);
that._svg = svg;
that._setDiagramZoomable();
that._setMouseEvents();
} else {
that._diagCont.set('content', dom.create('div', {
innerHTML : 'No diagram',
style : 'margin: 100px 0px 0px 10px;\
text-align: center;\
font-weight: bold'
}));
}
});
} catch (ex) {
console.error(ex);
// TODO: Display an error dialog.
}
},
setState : function (state) {
if ( state.center !== this.id ||
!state.diagNode ||
!state.diagType ||
!state.diagHandler)
return;
this.loadDiagram(state.diagHandler, state.diagType, state.diagNode);
},
_subscribeTopics : function () {
var that = this;
// Opens a diagram and sets the center module to this. Message attributes:
// - handler: A view handler ID of type Diagram.
// - diagramType: A diagram type ID. This will be passed to the handler
// when fetching the diagram.
// - node: A node ID to which the diagram belongs.
topic.subscribe('codecompass/openDiagram', function (message) {
that.loadDiagram(message.handler, message.diagramType, message.node);
if (urlHandler.getState('center') !== that.id)
that._textCont.set('content', that._textModule);
topic.publish('codecompass/setCenterModule', that.id);
urlHandler.setStateValue({
diagNode : message.node,
diagType : message.diagramType,
diagHandler : message.handler,
center : that.id
});
});
},
_setDiagramZoomable : function () {
var svgDom = query('svg')[0];
attr.set(svgDom, { width : '100%', height : '100%' });
this._svgPanZoom = svgPanZoom(svgDom, {
fit : false,
center : true,
maxZoom : Infinity,
minZoom : 0.1,
dblClickZoomEnabled : false,
controlIconsEnabled : true
});
},
_setMouseEvents : function () {
var that = this;
var clickPos = { x : 0, y : 0 };
var isMouseOverSVGElement = false;
function onMouseOver(svgElement) {
isMouseOverSVGElement = true;
setTimeout(function () {
if (!isMouseOverSVGElement)
return;
if (svgElement.id === that._lastMouseoverNodeId)
return;
that._lastMouseoverNodeId = svgElement.id;
var info
= that._handler.mouseOverInfo(that._diagramType, svgElement.id);
that._textModule.loadFile(info.fileId);
that._textModule.set('selection', info.selection);
that._textModule.jumpToPos(info.selection[0]);
}, 1000);
}
function onMouseOut(svgElement) {
isMouseOverSVGElement = false;
}
//--- Events on nodes ---//
var nodes = query('.node', this._diagCont.domNode);
nodes.on('mouseover', function () { onMouseOver(this); });
nodes.on('mouseout', function () { onMouseOut(this); });
nodes.on('click', function (event) {
if (clickPos.x !== event.clientX || clickPos.y !== event.clientY)
return;
topic.publish('codecompass/openDiagram', {
handler : that._handlerId,
diagramType : that._diagramType,
node : this.id
});
});
nodes.on('mousedown', function (event) {
clickPos = { x : event.clientX, y : event.clientY };
});
}
});
var diagram = new Diagram({ id : 'diagram' });
viewHandler.registerModule(diagram, {
type : viewHandler.moduleType.Center
});
var legendButton = new Button({
label : 'Legend',
render : function () { return this; },
onClick : function () {
if (!urlHandler.getLanguageService())
return;
var diagramType = urlHandler.getState('diagType');
var handlerId = urlHandler.getState('diagHandler');
var handler = viewHandler.getModule(handlerId);
var legend = handler.getDiagramLegend(diagramType);
new Dialog({
title : 'Diagram Legend',
content : legend || '<b>No Legend</b>',
style : legend ? '' : 'width: 200px'
}).show();
}
});
viewHandler.registerModule(legendButton, {
type : viewHandler.moduleType.ContextButton,
priority : 20,
center : 'diagram'
});
var textViewButton = new Button({
label : 'Text View',
render : function () { return this; },
onClick : function () {
var fileInfo = urlHandler.getFileInfo();
topic.publish('codecompass/openFile', {
fileId : fileInfo.id
});
}
});
viewHandler.registerModule(textViewButton, {
type : viewHandler.moduleType.ContextButton,
priority : 25,
center : 'diagram'
});
var exportToSvgButton = new Button({
label : 'Export SVG',
render : function () { return this; },
onClick : function () {
var w = window.open();
w.document.body.innerHTML = diagram._svg;
w.document.title = "CodeCompass Export SVG";
}
});
viewHandler.registerModule(exportToSvgButton, {
type : viewHandler.moduleType.ContextButton,
priority : 30,
center : 'diagram'
});
var downloadImageButton = new Button({
label : 'Download image',
render : function () { return this; },
onClick : function () {
var element = document.createElement('a');
element.setAttribute('href',
'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(diagram._svg));
element.setAttribute('download', 'diagram.svg');
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
});
viewHandler.registerModule(downloadImageButton, {
type : viewHandler.moduleType.ContextButton,
priority : 40,
center : 'diagram'
});
});