-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanno-elasticsearch-plugin.js
More file actions
162 lines (138 loc) · 4.03 KB
/
anno-elasticsearch-plugin.js
File metadata and controls
162 lines (138 loc) · 4.03 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
/**
* A simple storage connector plugin to the ElasticSearch REST interface.
*
* Note: the plugin requires jQuery to be linked into the host page.
*
* THIS PLUGIN IS FOR DEMO PURPOSES ONLY - DON'T USE IN A PRODUCTION
* ENVIRONMENT.
*/
annotorious.plugin.ElasticSearch = function(opt_config_options) {
/** @private **/
this._STORE_URI = 'http://' + window.location.hostname + ':9200/annotations/';
/** @private **/
this._annotations = [];
/** @private **/
this._loadIndicators = [];
}
annotorious.plugin.ElasticSearch.prototype.initPlugin = function(anno) {
var self = this;
anno.addHandler('onAnnotationCreated', function(annotation) {
self._create(annotation);
});
anno.addHandler('onAnnotationUpdated', function(annotation) {
self._update(annotation);
});
anno.addHandler('onAnnotationRemoved', function(annotation) {
self._delete(annotation);
});
self._loadAnnotations(anno);
}
annotorious.plugin.ElasticSearch.prototype.onInitAnnotator = function(annotator) {
var spinner = this._newLoadIndicator();
annotator.element.appendChild(spinner);
this._loadIndicators.push(spinner);
}
annotorious.plugin.ElasticSearch.prototype._newLoadIndicator = function() {
var outerDIV = document.createElement('div');
outerDIV.className = 'annotorious-es-plugin-load-outer';
var innerDIV = document.createElement('div');
innerDIV.className = 'annotorious-es-plugin-load-inner';
outerDIV.appendChild(innerDIV);
return outerDIV;
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._showError = function(error) {
// TODO proper error handling
window.alert('ERROR');
console.log(error);
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._loadAnnotations = function(anno) {
// TODO A fixed size value of 10000 might not be good
var self = this;
var context = window.location.pathname
var query = {
"query" : {
//"constant_score" : {
// "filter" : {
"term" : {
"context" : context
}
// }
//}
},
"size": 10000
};
jQuery.post(this._STORE_URI + '_search', JSON.stringify(query), function(data) {
try {
jQuery.each(data.hits.hits, function(idx, hit) {
var annotation = hit['_source'];
annotation.id = hit['_id'];
if (jQuery.inArray(annotation.id, self._annotations) < 0) {
self._annotations.push(annotation.id);
if (!annotation.shape && annotation.shapes[0].geometry)
anno.addAnnotation(annotation);
}
});
} catch (e) {
self._showError(e);
}
// Remove all load indicators
jQuery.each(self._loadIndicators, function(idx, spinner) {
jQuery(spinner).remove();
});
});
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._create = function(annotation) {
var self = this;
annotation['context'] = self._remove_params(self._remove_domain(annotation['context']));
annotation['src'] = self._remove_domain(annotation['src']);
jQuery.post(this._STORE_URI + 'annotation/', JSON.stringify(annotation), function(response) {
// TODO error handling if response status != 201 (CREATED)
var id = response['_id'];
annotation.id = id;
});
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._update = function(annotation) {
var self = this;
jQuery.ajax({
url: this._STORE_URI + 'annotation/' + annotation.id,
type: 'PUT',
data: JSON.stringify(annotation)
});
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._delete = function(annotation) {
jQuery.ajax({
url: this._STORE_URI + 'annotation/' + annotation.id,
type: 'DELETE'
});
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._remove_domain = function(url) {
return url.replace(/^.*\/\/[^\/]+/, '');
}
/**
* @private
*/
annotorious.plugin.ElasticSearch.prototype._remove_params = function(url) {
if (url.indexOf('?') != -1) {
return url.substr(0, url.indexOf('?'));
} else {
return url;
}
}