/
defaultui.js
203 lines (169 loc) · 6.12 KB
/
defaultui.js
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
"use strict";
var UI = require('../ui'),
Util = require('../util');
// trim strips whitespace from either end of a string.
//
// This usually exists in native code, but not in IE8.
function trim(s) {
if (typeof String.prototype.trim == 'function') {
return String.prototype.trim.call(s);
} else {
return s.replace(/^[\s\xA0]+|[\s\xA0]+$/g, '');
}
}
// annotationFactory returns a function that can be used to construct an
// annotation from a list of selected ranges.
function annotationFactory(contextEl, ignoreSelector) {
return function (ranges) {
var text = [],
serializedRanges = [];
for (var i = 0, len = ranges.length; i < len; i++) {
var r = ranges[i];
text.push(trim(r.text()));
serializedRanges.push(r.serialize(contextEl, ignoreSelector));
}
return {
quote: text.join(' / '),
ranges: serializedRanges
};
};
}
// maxZIndex returns the maximum z-index of all elements in the provided set.
function maxZIndex(elements) {
var max = -1;
for (var i = 0, len = elements.length; i < len; i++) {
var $el = Util.$(elements[i]);
if ($el.css('position') !== 'static') {
// Use parseFloat since we may get scientific notation for large
// values.
var zIndex = parseFloat($el.css('z-index'));
if (zIndex > max) {
max = zIndex;
}
}
}
return max;
}
// Helper function to inject CSS into the page that ensures Annotator elements
// are displayed with the highest z-index.
function injectDynamicStyle() {
Util.$('#annotator-dynamic-style').remove();
var sel = '*' +
':not(annotator-adder)' +
':not(annotator-outer)' +
':not(annotator-notice)' +
':not(annotator-filter)';
// use the maximum z-index in the page
var max = maxZIndex(Util.$(Util.getGlobal().document.body).find(sel).get());
// but don't go smaller than 1010, because this isn't bulletproof --
// dynamic elements in the page (notifications, dialogs, etc.) may well
// have high z-indices that we can't catch using the above method.
max = Math.max(max, 1000);
var rules = [
".annotator-adder, .annotator-outer, .annotator-notice {",
" z-index: " + (max + 20) + ";",
"}",
".annotator-filter {",
" z-index: " + (max + 10) + ";",
"}"
].join("\n");
Util.$('<style>' + rules + '</style>')
.attr('id', 'annotator-dynamic-style')
.attr('type', 'text/css')
.appendTo('head');
}
// Helper function to remove dynamic stylesheets
function removeDynamicStyle() {
Util.$('#annotator-dynamic-style').remove();
}
// DefaultUI is a function that can be used to construct a plugin that will
// provide Annotator's default user interface.
//
// element - The DOM element which you want to be able to annotate.
//
// Examples
//
// ann = new Annotator.Core.Annotator()
// ann.addPlugin(DefaultUI(document.body, {}))
//
// Returns an Annotator plugin.
function DefaultUI(element) {
// FIXME: restore readOnly mode
//
// options: # Configuration options
// # Start Annotator in read-only mode. No controls will be shown.
// readOnly: false
return function (registry) {
// Local helpers
var makeAnnotation = annotationFactory(element, '.annotator-hl');
// Shared user interface state
var interactionPoint = null;
var adder = new UI.Adder({
onCreate: function (ann) {
registry.annotations.create(ann);
}
});
var editor = new UI.Editor();
var highlighter = new UI.Highlighter(element);
var textSelector = new UI.TextSelector(element, {
onSelection: function (ranges, event) {
if (ranges.length > 0) {
var annotation = makeAnnotation(ranges);
interactionPoint = Util.mousePosition(event);
adder.load(annotation, interactionPoint);
} else {
adder.hide();
}
}
});
var viewer = new UI.Viewer({
onEdit: function (ann) {
registry.annotations.update(ann);
},
onDelete: function (ann) {
registry.annotations['delete'](ann);
},
permitEdit: function (ann) {
return registry.authorizer.permits(
'update',
ann,
registry.identifier.who()
);
},
permitDelete: function (ann) {
return registry.authorizer.permits(
'delete',
ann,
registry.identifier.who()
);
},
autoViewHighlights: element
});
injectDynamicStyle();
return {
onDestroy: function () {
adder.destroy();
editor.destroy();
highlighter.destroy();
textSelector.destroy();
viewer.destroy();
removeDynamicStyle();
},
onAnnotationsLoaded: function (anns) { highlighter.drawAll(anns); },
onAnnotationCreated: function (ann) { highlighter.draw(ann); },
onAnnotationDeleted: function (ann) { highlighter.undraw(ann); },
onAnnotationUpdated: function (ann) { highlighter.redraw(ann); },
onBeforeAnnotationCreated: function (annotation) {
// Editor#load returns a promise that is resolved if editing
// completes, and rejected if editing is cancelled. We return it
// here to "stall" the annotation process until the editing is
// done.
return editor.load(annotation, interactionPoint);
},
onBeforeAnnotationUpdated: function (annotation) {
return editor.load(annotation, interactionPoint);
}
};
};
}
exports.DefaultUI = DefaultUI;