This repository has been archived by the owner on Jun 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
item.js
336 lines (285 loc) · 9 KB
/
item.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*******************************************************************************
Accurator Item
This code allows the item page to be setup according to the locale, domain
and user settings. The main functionallity regards annoting the item, for which
it relies upon the following files:
- in web/js/components
* field.js - field objects allowing users to add annotations
* annotations.js - list of annotations
* deniche-plugin.js - plugin for annotorious embedding field objects in the
editor div and manages the adding and removal of annotations in the annotation
list
- in web/js/lib
* annotorious.min.js - annotorious editor used for annotating fragments of images
*******************************************************************************/
"use strict";
var page = {
showMetadata: true,
showAnnotations: true,
imageId: null, // Set on init image
fragmentFieldsId: "itemDivAnnotoriousFields", // Id container containing fields
wholeFieldsId: "itemDivFields"
}
function itemInit() {
var locale = getLocale();
var domain = getDomain();
var uri = getParameterByName("uri");
populateFlags(locale);
userLoggedIn()
.then(function(userData) {
// user is logged in, so draw page
drawPage(userData);
}, function() {
// user is not logged in, show modal
var onDismissal = function() {
document.location.href = "intro.html"
};
login(drawPage, onDismissal);
});
function drawPage(userData) {
var ui, annotation_ui, labels;
var user = userData.user;
var userName = getUserName(userData.user);
setLinkLogo("profile");
populateNavbar(userName, [{link:"profile.html", name:"Profile"}], locale);
domainSettings(domain)
.then(function(domainData) {
ui = domainData.hasUI + "item";
annotation_ui = domainData.hasAnnotationUI;
return getLabels(locale, ui);
})
.then(function(labelData){
labels = initLabels(labelData);
// only show path and navigation when cluster is available
if ((localStorage.getItem("uris") !== null) &&
(localStorage.getItem("uris") !== "undefined")) {
addNavigation(uri);
}
addButtonEvents(user);
return events(user, locale, labels);
})
.then(function() {
return setImage(uri);
})
.then(function(metadata) {
displayMetadata(uri);
displayAnnotations(uri);
return addAnnotationFields(metadata, user, uri, locale, domain, annotation_ui, labels);
});
}
}
function setImage(uri) {
return $.getJSON("metadata", {uri:uri})
.then(function(metadata){
// set id image
page.imageId = "itemImg" + generateIdFromUri(uri);
$(".itemImg").attr("id", page.imageId);
// return info for anotorious
return metadata;
});
}
function initLabels(labelData) {
document.title = labelData.title;
$("#itemBtnPrevious").append(labelData.itemBtnPrevious);
$("#itemBtnNext").prepend(labelData.itemBtnNext);
$("#navbarBtnRecommend").append(labelData.navbarBtnRecommend);
$("#navbarBtnSearch").append(labelData.navbarBtnSearch);
var labels = {
itemHdrFirst: labelData.itemHdrFirst,
itemTxtFirst: labelData.itemTxtFirst,
annoBtnCancel: labelData.annoBtnCancel,
annoBtnDone: labelData.annoBtnDone
};
return labels;
}
function events(user, locale, labels) {
return $.getJSON("annotations", {uri:user, type:"user"})
.then(function(annotations) {
if (annotations.length === 0) {
alertMessage(labels.itemHdrFirst, labels.itemTxtFirst, 'success');
}
if (annotations.length === 5) {
showForm(
"formPersonal",
["country",
"language",
"education",
"gender",
"birthDate"],
locale
);
}
if (annotations.length === 10) {
showForm(
"formInternet",
["socialNetwork",
"taggingSites",
"taggingExperience",
"mail",
"mailCheck"],
locale
);
}
});
}
function addNavigation(uri) {
var uris = JSON.parse(localStorage.getItem("uris"));
var pathArray = JSON.parse(localStorage.getItem("path"));
// show path if not random
if (!(pathArray == "random")) {
var path = new Path(pathArray, "itemDivPath");
path.enrich()
.then(function() {
$("#itemDivPath").append(path.node);
});
}
addNavigationButtonEvents(uris, uri);
}
function addButtonEvents(user) {
$("#navbarBtnRecommend").click(function() {
document.location.href = "results.html?user=" + user;
});
// Search on pressing enter
$("#navbarInpSearch").keypress(function(event) {
if (event.which == 13) {
var query = encodeURIComponent($("#navbarInpSearch").val());
document.location.href = "results.html?query=" + query;
}
});
$("#navbarBtnSearch").click(function() {
var query = encodeURIComponent($("#navbarInpSearch").val());
document.location.href = "results.html?query=" + query;
});
}
function addNavigationButtonEvents(uris, uri) {
var index = uris.indexOf(uri);
if (index === 0) {
$("#itemBtnPrevious").attr("disabled", "disabled");
} else {
$("#itemBtnPrevious").click(function() {
document.location.href = "item.html?uri=" + uris[index -1];
});
}
if(index === uris.length-1) {
$("#itemBtnNext").attr("disabled", "disabled");
} else {
$("#itemBtnNext").click(function() {
document.location.href = "item.html?uri=" + uris[index + 1];
});
}
}
function addAnnotationFields(metadata, user, uri, locale, domain, annotation_ui, labels) {
// Retrieve the fields that should be added (based on save_user_info)
return $.getJSON("annotation_fields", {
locale:locale,
domain:domain,
annotation_ui:annotation_ui
})
.then(function(fields) {
// context object for field
var context = {
target: uri,
targetImage: metadata.image_uri,
user: user,
locale: locale,
imageId: page.imageId,
};
// add fields whole image
for (var i = 0; i < fields.whole_fields.length; i++) {
context.id = "whole" + generateIdFromUri(fields.whole_fields[i].uri);
context.fragment = false;
context.fieldsId = page.wholeFieldsId;
// create new field object
var wholeField = createField(fields.whole_fields[i], context);
// add the field to div
$("#" + wholeField.fieldsId).append(wholeField.node);
// show existing annotations
if (wholeField.showAnnotations) wholeField.getAnnotations();
}
// add hidden container for fields if there are fragment fields
if (fields.fragment_fields.length > 0) {
$(".itemDivHidden").append($.el.div({'id':page.fragmentFieldsId}));
// set fields attribute image for annotorious deniche
$("#" + page.imageId).attr("fields", page.fragmentFieldsId);
}
// add fields to hidden dom elements for annotorious
for (var i = 0; i < fields.fragment_fields.length; i++) {
context.id = "fragment" + generateIdFromUri(fields.fragment_fields[i].uri);
context.fragment = true;
context.fieldsId = page.fragmentFieldsId;
// create new field object
var fragmentField = createField(fields.fragment_fields[i], context);
// append the field to div which will be embedded in annotorious
$("#" + fragmentField.fieldsId).append(fragmentField.node);
}
// add the deniche plugin, which embeds the fields in annotorious
anno.addPlugin("DenichePlugin", labels);
});
}
function createField(field, context) {
switch (field.type) {
case "DropdownField":
return new DropdownField(field, context);
case "TextField":
return new TextField(field, context);
}
}
function displayMetadata(uri) {
if(page.showMetadata){
// Get metadata from server
$.getJSON("metadata", {uri:uri})
.done(function(metadata){
appendMetadataWell(metadata);
});
}
}
function appendMetadataWell(metadata) {
$("#itemDivMetadata").append(
$.el.div({'class':'row'},
$.el.div({'class':'col-md-10 col-md-offset-1'},
$.el.div({'class':'well well-sm'},
$.el.h4('Showing metadata for ' + metadata.title),
$.el.dl({'class':'dl-horizontal',
'id':'itemLstMetadata'})))));
for(var i = 0; i < metadata.properties.length; i++) {
var encodedQuery = encodeURIComponent(metadata.properties[i].object_label);
$("#itemLstMetadata").append(
$.el.dt(metadata.properties[i].predicate_label));
$("#itemLstMetadata").append(
$.el.dd(
$.el.a({'class':'r_undef',
'href':'results.html?query=' + encodedQuery},
metadata.properties[i].object_label)));
}
}
function displayAnnotations(uri) {
// Get annotations from server for projecting in well
if(page.showAnnotations){
$.getJSON("annotations", {uri:uri, type:"object", enrich:"true"})
.done(function(annotations){
if(annotations.length > 0){
$("#itemDivMetadata").append(annotationWell(annotations));
}
});
}
}
function annotationWell(annotations) {
$("#itemDivMetadata").append(
$.el.div({'class':'row'},
$.el.div({'class':'col-md-10 col-md-offset-1'},
$.el.div({'class':'well well-sm'},
$.el.h4('Annotations'),
$.el.dl({'class':'dl-horizontal',
'id':'itemLstAnnotations'})))));
for(var i = 0; i < annotations.length; i++) {
var encodedQuery = encodeURIComponent(annotations.label);
var field = generateIdFromUri(annotations[i].field);
$("#itemLstAnnotations").append(
$.el.dt(field));
$("#itemLstAnnotations").append(
$.el.dd(
$.el.a({'class':'r_undef',
'href':'results.html?query=' + encodedQuery},
annotations[i].label)));
}
}