-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathShapeAnnotationControl.js
More file actions
171 lines (154 loc) · 4.86 KB
/
ShapeAnnotationControl.js
File metadata and controls
171 lines (154 loc) · 4.86 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
import Rect from './graphics/structs/Rect';
import createGraphics from './graphics/createGraphics';
import { getFixOfPixelAlignment, getInnerSize } from './graphics/dom';
import JsonML from './common/jsonml-html';
import ShapeAnnotation from './graphics/annotations/ShapeAnnotation';
import AnnotationLabelTemplate from './templates/html/AnnotationLabelTemplate';
/**
* Creates JavaScript Shape 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 {ShapeAnnotationControlConfig} options Shape Annotation Configuration object
*
* @returns {ShapeAnnotationControl} Returns reference to shape annotation control.
*/
export default function ShapeAnnotationControl(element, options) {
var _data = {
name: "shapecontrol",
element: element,
options: options,
placeholder: null,
panelSize: null,
graphics: null,
labelTemplate: null,
shape: 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);
_data.shape = new ShapeAnnotation();
};
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();
_data.shape.draw(_data.options, _data.graphics, _data.panelSize, _data.labelTemplate);
}
else {
updateLayout();
_data.graphics.resize("placeholder", _data.panelSize.width, _data.panelSize.height);
_data.graphics.begin();
_data.shape.draw(_data.options, _data.graphics, _data.panelSize, _data.labelTemplate);
_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
};
};