forked from wissenbach/oaclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.js
150 lines (114 loc) · 4.71 KB
/
ui.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
YUI().use ('node-base', 'node', 'node-load', 'stylesheet', 'event-base', 'overlay', 'widget-anim',
'editor-base', 'io', 'selector-css3',
function(Y) {
OAClient.getSelectedSpan = function() {
};
OAClient.AnnotateWidget = function(rangeStart, rangeEnd) {
var anchorId = '#charnum' + rangeEnd;
var anchor = Y.one(anchorId);
var overlay = new Y.Overlay({
align: {
node: anchor,
points:[Y.WidgetPositionAlign.TL, Y.WidgetPositionAlign.BR]
},
headerContent: '',
bodyContent:'<div class="annotateWindow"><button id="store_button">Store</button></div>',
footerContent: '',
width : "20em",
height: "20em",
preventOverlap: true,
constrain: true,
plugins: [{fn:Y.Plugin.WidgetAnim, duration: 0.5}],
visible: false,
});
overlay.render();
var editor = new Y.EditorBase({
content: '<strong>Add your <em> annotations </em></strong> here ... '
});
//Add the BiDi plugin
editor.plug(Y.Plugin.EditorBidi);
//Focusing the Editor when the frame is ready..
editor.on('frame:ready', function() {
this.focus();
});
//Rendering the Editor.
editor.render('.annotateWindow');
Y.one('#store_button').on('click', function() {
Y.config.win.getSelection().removeAllRanges();
var annotationBody = editor.getContent();
OAClient.storeAnnotationBody (annotationBody, function(bodyURL){
console.log('The new annotation body is at: ' + bodyURL);
OAClient.newAnnotation(bodyURL, location.href, rangeStart, rangeEnd+1);
});
overlay.destroy(true);
});
overlay.show();
};
OAClient.initGUI = function(){
Y.one('head').append('<link href="' +
OAClient.baseURL + 'oaclient.css" rel="stylesheet" type="text/css">');
Y.one('body').on('mouseup', function(e) {
Y.later(20, this, function() {
if ( Y.config.win.getSelection().rangeCount > 0) {
var selection = Y.config.win.getSelection().getRangeAt(0);
if (selection.toString().length > 0) {
var startElem = Y.one(selection.startContainer).ancestor('pre');
var endElem = Y.one(selection.endContainer).ancestor('pre');
var startId = startElem.get('id');
var rangeStart = parseInt(startId.substring('charnum'.length, startId.length));
var endId = endElem.get('id');
var rangeEnd = parseInt(endId.substring('charnum'.length, endId.length));
OAClient.plaintextSingleton.getHandle(rangeStart, rangeEnd);
new OAClient.AnnotateWidget(rangeStart, rangeEnd);
}
}});
});
};
OAClient.renderAnnotations = function() {
console.log('Rendering annotations.');
OAClient.plaintextSingleton.clearHandles();
Y.each (OAClient.annotations, function(annotation) {
OAClient.plaintextSingleton.getHandle(
annotation.get('posFrom'),
annotation.get('posTo')
)
});
};
function insertAnnotations(annotations) {
OAClient.annotations = new OAClient.AnnotationList();
OAClient.annotations.after(['add', 'remove', 'reset', '*:change'],
OAClient.renderAnnotations, OAClient);
// FIXME quick fix: when there are no annotations yet, the
// variable annotations is of type string and has the value '"[]"'
if (typeof annotations !== 'string')
Y.each(annotations, function(annotation) {
console.log('Adding annotation: ');
var annotationURI = annotation.annotation.uri;
console.log(annotation);
OAClient.getAnnotation(annotationURI, function(response) {
console.log(response);
var constraint =
response.annotation.annotation_target_instances[0].
annotation_target_instance.annotation_constraint.constraint;
var range = OAClient.parseConstraint(constraint);
if (range) {
console.log(range);
var annotationM = new OAClient.AnnotationModel({
uri: annotationURI,
posFrom : range.start,
posTo : range.end - 1
});
console.log('validating annotation...');
var validationError = annotationM.validate(annotationM.getAttrs());
console.log(validationError);
if (!validationError)
OAClient.annotations.add(annotationM);
}
});
});
}
OAClient.plaintextSingleton = new OAClient.Plaintext(function(){
OAClient.queryForAnnotations(location.href, insertAnnotations);
});
OAClient.initGUI();
});