-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathConnectorAnnotationControl.js
More file actions
287 lines (253 loc) · 9.2 KB
/
ConnectorAnnotationControl.js
File metadata and controls
287 lines (253 loc) · 9.2 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
import Rect from './graphics/structs/Rect';
import PaletteItem from './graphics/structs/PaletteItem';
import { ConnectorPlacementType } from './enums';
import createGraphics from './graphics/createGraphics';
import { getFixOfPixelAlignment, getInnerSize, getElementOffset } from './graphics/dom';
import JsonML from './common/jsonml-html';
import Transform from './graphics/Transform';
import PolylinesBuffer from './graphics/structs/PolylinesBuffer';
import ConnectorAnnotationOffsetResolver from './tasks/renders/offsetResolver/ConnectorAnnotationOffsetResolver';
import ConnectorOffbeat from './graphics/shapes/ConnectorOffbeat';
import ConnectorStraight from './graphics/shapes/ConnectorStraight';
import AnnotationLabelTemplate from './templates/html/AnnotationLabelTemplate';
import { isNullOrEmpty } from './common';
import RenderEventArgs from './events/RenderEventArgs';
/**
* Creates JavaScript Connector Annotation Control
* @class Control
* @param {object} element Reference to placeholder `div` element in the DOM. The control renders its content
* inside of that div element.
* @param {ConnectorAnnotationControlConfig} options Connector Annotation Configuration object
*
* @returns {ConnectorAnnotationControl} Returns reference to connector annotation control.
*/
export default function ConnectorAnnotationControl(element, options) {
var _data = {
name: "connectorcontrol",
element: element,
options: options,
placeholder: null,
panelSize: null,
graphics: null,
labelTemplate: null
};
if(!element) {
throw "Control's placeholder element is undefined";
}
if(!options) {
throw "Control's options argument is undefined";
}
function createLayout() {
var viewportSize = getInnerSize(_data.element);
_data.panelSize = new Rect(0, 0, viewportSize.width, viewportSize.height);
var pixelAlignmentFix = getFixOfPixelAlignment(element);
JsonML.appendDOM(_data.element, JsonML.toHTML(
["div", /* root control panel */
{
"tabindex": 0,
"style": {
"position": "relative",
"overflow": "hidden",
"top": "0px",
"left": "0px",
"width": _data.panelSize.width,
"height": _data.panelSize.height,
"padding": "0px",
"marginBottom": "0px",
"marginRight": "0px",
"marginLeft": pixelAlignmentFix.width + "px", /* fixes div pixel alignment */
"marginTop": pixelAlignmentFix.height + "px"
},
"name": "placeholder",
"class": _data.name,
"$": function (element) { _data.placeholder = element; }
}
])
);
_data.graphics = createGraphics(_data.element);
};
function cleanLayout() {
if (_data.graphics !== null) {
_data.graphics.clean();
}
_data.graphics = null;
var placeholder = _data.placeholder;
if (placeholder != null) {
var parent = placeholder.parentNode;
if (parent != null) {
parent.removeChild(placeholder);
}
}
}
function updateLayout() {
var viewportSize = getInnerSize(_data.element),
pixelAlignmentFix = getFixOfPixelAlignment(_data.element);
_data.panelSize = new Rect(0, 0, viewportSize.width, viewportSize.height);
JsonML.applyStyles(_data.placeholder, {
"width": _data.panelSize.width,
"height": _data.panelSize.height,
"marginBottom": "0px",
"marginRight": "0px",
"marginLeft": pixelAlignmentFix.width + "px", /* fixes div pixel alignment */
"marginTop": pixelAlignmentFix.height + "px"
});
}
function update(recreate) {
if (recreate) {
_data.labelTemplate = AnnotationLabelTemplate(_data.options);
cleanLayout();
createLayout();
redraw();
}
else {
updateLayout();
_data.graphics.resize("placeholder", _data.panelSize.width, _data.panelSize.height);
_data.graphics.begin();
redraw();
_data.graphics.end();
}
}
function redraw() {
var annotationConfig = _data.options,
shape,
uiHash,
transform = new Transform(),
panel = _data.graphics.activate("placeholder"),
buffer = new PolylinesBuffer(),
connectorAnnotationOffsetResolver = ConnectorAnnotationOffsetResolver();
transform.size = new Size(_data.panelSize.width, _data.panelSize.height);
transform.setOrientation(annotationConfig.orientationType);
if (annotationConfig.fromRectangle != null && annotationConfig.toRectangle != null) {
var fromRect = annotationConfig.fromRectangle,
toRect = annotationConfig.toRectangle;
/* translate rectangles to Top orientation */
/* from rectangle */
transform.transformRect(fromRect.x, fromRect.y, fromRect.width, fromRect.height, false,
this, function (x, y, width, height) {
fromRect = new Rect(x, y, width, height);
});
/* to rectangle */
transform.transformRect(toRect.x, toRect.y, toRect.width, toRect.height, false,
this, function (x, y, width, height) {
toRect = new Rect(x, y, width, height);
});
switch (annotationConfig.connectorPlacementType) {
case ConnectorPlacementType.Offbeat:
shape = new ConnectorOffbeat();
break;
case ConnectorPlacementType.Straight:
shape = new ConnectorStraight();
break;
}
/* rotate label size to user orientation */
var labelSize;
transform.transformRect(0, 0, annotationConfig.labelSize.width, annotationConfig.labelSize.height, false,
this, function (x, y, width, height) {
labelSize = new Size(width, height);
});
/* rotate panel size to user orientation */
var panelSize = null;
transform.transformRect(0, 0, panel.size.width, panel.size.height, false,
this, function (x, y, width, height) {
panelSize = new Size(width, height);
});
var linePaletteItem = new PaletteItem({
lineColor: annotationConfig.color,
lineWidth: annotationConfig.lineWidth,
lineType: annotationConfig.lineType
});
var hasLabel = !isNullOrEmpty(annotationConfig.label);
/* offset rectangles */
fromRect = new Rect(fromRect).offset(annotationConfig.offset);
toRect = new Rect(toRect).offset(annotationConfig.offset);
var linesOffset = annotationConfig.lineWidth * 6;
/* create connection lines */
shape.draw(buffer, linePaletteItem, fromRect, toRect, linesOffset, 0, labelSize, panelSize,
annotationConfig.connectorShapeType, annotationConfig.labelOffset, annotationConfig.labelPlacementType, hasLabel,
connectorAnnotationOffsetResolver, function (labelPlacement, labelConfig) {
var hasLabel = !isNullOrEmpty(labelConfig.label);
if (hasLabel && labelPlacement != null) {
/* translate result label placement back to users orientation */
transform.transformRect(labelPlacement.x, labelPlacement.y, labelPlacement.width, labelPlacement.height, true,
self, function (x, y, width, height) {
labelPlacement = new Rect(x, y, width, height);
});
uiHash = new RenderEventArgs();
uiHash.context = labelConfig;
/* draw label */
_data.graphics.template(
labelPlacement.x
, labelPlacement.y
, 0
, 0
, 0
, 0
, labelPlacement.width
, labelPlacement.height
, _data.labelTemplate.template()
, _data.labelTemplate.getHashCode()
, _data.labelTemplate.render
, uiHash
, null
);
}
}, annotationConfig);
connectorAnnotationOffsetResolver.resolve();
}
/* translate result polylines back to users orientation */
buffer.transform(transform, true);
/* draw background polylines */
_data.graphics.polylinesBuffer(buffer);
_data.graphics.end();
}
function destroy() {
cleanLayout();
};
/**
* Call this method to update controls configuration. Control uses default Config instance to initialize itself,
* so it sets only options provided in the options parameter.
*
* @param {object} options Options
*/
function setOptions(options) {
for (var option in options) {
if (options.hasOwnProperty(option)) {
_data.options[option] = options[option];
}
}
}
/**
* This method returns current configuration object.
*
* @returns {object} Returns reference to current configuration object
*/
function getOptions() {
return _data.options;
}
/**
* This method returns configuration option by name.
*
* @param {*} option Option name
*/
function getOption(option) {
return _data.options[option];
}
/**
* Sets configuration option of the control by name.
*
* @param {*} option Option name
* @param {*} value Option value
*/
function setOption(option, value) {
return _data.options[option] = value;
}
update(true); /* init control on create */
return {
destroy: destroy,
setOptions: setOptions,
getOptions: getOptions,
setOption: setOption,
getOption: getOption,
update: update
};
};