forked from wissenbach/oaclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.js
195 lines (164 loc) · 4.83 KB
/
protocol.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
YUI().use ('io', 'json', 'oop', function (Y) {
var oacServer = "http://demo.interedition.eu/raxld";
/** Take the same config object as Y.io() **/
OAClient.proxy = function (url, cfg) {
var encodedURL = window.encodeURIComponent(url);
// var requestURL = OAClient.baseURL + 'ba-simple-proxy-original.php?url=' + encodedURL;
var requestURL = OAClient.baseURL + 'ba-simple-proxy.php?url=' + encodedURL;
function unwrapConfig (config) {
var resultConfig = {
responseText : Y.JSON.stringify(Y.JSON.parse(config.responseText).contents)
};
return resultConfig;
};
var proxyCfg = Y.clone(cfg);
proxyCfg.on = {
success: function(transaction, config) {
console.log('proxy success');
var status = parseInt(Y.JSON.parse(config.responseText).status.http_code);
if (status < 400)
if (cfg.on && cfg.on.success)
cfg.on.success (transaction, unwrapConfig(config));
else
if (cfg.on && cfg.on.failure)
cfg.on.failure (transaction, unwrapConfig(config));
},
failure: function (transaction, config) {
console.log('proxy fail');
if (cfg.on && cfg.on.failure)
cfg.on.failure (transaction, unwrapConfig(config));
}
};
Y.io(requestURL, proxyCfg);
//$.ajax({url: requestURL, dataType: 'json'});
};
/**
* The callback is called with the the URI of the annotation body as
* the only parameter
*/
OAClient.storeAnnotationBody = function (content, callback) {
var url = oacServer + '/annotation_bodies';
var body = {
'mime_type' : 'text/html',
'content' : content
}
var cfg = {
method: 'POST',
data: Y.JSON.stringify(body),
headers: {
'Content-Type' : 'application/json',
'Accept' : 'application/json'
},
on: {
success: function(transaction, config) {
var createdBodyURI = Y.JSON.parse(config.responseText).annotation_body.uri;
console.log('successfully created annotation body at ' + createdBodyURI);
if (callback)
callback(createdBodyURI);
},
failure: function (transaction, config) {
console.log('failed to create annotation body!');
}
}
};
OAClient.proxy(url, cfg);
//Y.io(url, cfg);
};
OAClient.newAnnotation = function(bodyURI, targetURI, rangeStart, rangeEnd, callback) {
var url = oacServer + '/annotations';
var constraint = ({
'checksum' : "",
'position' : 'char=' + rangeStart + ',' + rangeEnd,
'context' : ""
});
var data = {
'author_uri' : 'http://authored.by.oaclient',
'body_uri' : bodyURI,
'targets' : [{
'uri' : targetURI,
'constraint' : {
'constraint_type' : "RFC_5147",
'constraint' : Y.JSON.stringify(constraint)
}
}]
};
var cfg = {
method: 'POST',
data: Y.JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
'Accept' : 'application/json'
},
on: {
success: function(transaction, config) {
var annotationURI = Y.JSON.parse(config.responseText).annotation.uri;
console.log('successfully created annotation at '
+ rangeStart + ', ' + rangeEnd + ': ' + annotationURI );
console.log(config);
},
failure: function(transaction, config) {
console.log('failed to create anootation: ' + config);
}
}
};
console.log('sending data:');
console.log(data);
OAClient.proxy(url, cfg);
//Y.io(url, cfg);
};
OAClient.getAnnotation = function(uri, callback) {
var cfg = {
method: 'GET',
headers: {
'Accept' : 'application/json'
},
on: {
success: function(transaction, config) {
console.log('successfully retrieved annotation:');
var response = Y.JSON.parse(config.responseText);
if (callback)
callback(response);
},
failure: function (transaction, config) {
console.log('failed to retrieve annotation!');
}
}
};
OAClient.proxy(uri, cfg);
//Y.io(url, cfg);
};
OAClient.queryForAnnotations = function(targetURI, callback) {
var encodedTargetURI = window.encodeURIComponent(targetURI);
var url = oacServer + '/annotations/query?q=' + encodedTargetURI;
var cfg = {
method: 'GET',
headers: {
//'Content-Type' : 'application/json',
'Accept' : 'application/json'
},
on: {
success: function(transaction, config) {
console.log('successful query for annotations:');
var response = Y.JSON.parse(config.responseText);
if (callback)
callback(response);
},
failure: function (transaction, config) {
console.log('failed to query for annotations!');
}
}
};
OAClient.proxy(url, cfg);
//Y.io(url, cfg);
};
OAClient.parseConstraint = function(constraint) {
var position = Y.JSON.parse(constraint).position;
if (position.slice(0,5) !== 'char=')
return null;
var split = position.slice(5).split(',');
return {
start: parseInt(split[0]),
end: parseInt(split[1])
};
};
});