Skip to content

Instantly share code, notes, and snippets.

@jbaiter
Created February 6, 2018 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbaiter/121a042fef59eb4573228bb5263bf07d to your computer and use it in GitHub Desktop.
Save jbaiter/121a042fef59eb4573228bb5263bf07d to your computer and use it in GitHub Desktop.
(function ($){
$.ElucidateEndpoint = function (options) {
jQuery.extend(this, {
token: null,
container: options.container,
url: options.url,
dfd: null,
// OA list for Mirador use
annotationsList: [],
// internal list for module use to map id to URI
idMapper: {}
}, options);
this.init();
};
$.ElucidateEndpoint.prototype = {
// Any set up for this endpoint, and triggers a search of the URI passed to object
init: function () {
},
// Search endpoint for all annotations with a given URI
search: function (options, successCallback, errorCallback) {
var _this = this;
this.annotationsList = []; // clear out current list
jQuery.ajax({
url: _this.url + '/oa/services/search/target',
cache: false,
type: 'GET',
dataType: 'text',
headers: {},
data: {
fields: 'source',
value: options.uri,
strict: true
},
success: function(data) {
var result = _this.cleanJsonLd(data);
var annos = result['as:first']['as:items']['@list'];
_this.annotationsList = annos
.map(_this.formatAnno)
.map(function(anno) {
anno.endpoint = _this;
return anno;
});
if (typeof successCallback === 'function') {
successCallback(_this.annotationsList);
} else {
_this.dfd.resolve(true);
}
},
error: function(xhr, statusText, err) {
if (typeof errorCallback === 'function') {
errorCallback();
} else {
_this.dfd.reject();
console.log('The request for annotations has caused an error for endpoint: ' + options.uri + ' due to ' + statusText);
}
}
});
},
cleanJsonLd: function(json) {
return JSON.parse(json
.replace(/http:\/\/www.w3.org\/ns\/activitystreams#/gi, 'as:')
.replace(/"type"\s*:/gi, '"@type":')
.replace(/"id"\s*:/gi, '"@id":')
);
},
formatAnno: function(anno) {
var converted = {
'@context': 'http://iiif.io/api/presentation/2/context.json',
'@type': anno['@type'],
'@id': anno['@id'],
'motivation': anno.motivatedBy,
'resource': anno.hasBody,
'on': [{
'@type': anno.hasTarget['@type'],
'full': anno.hasTarget.hasSource,
'selector': anno.hasTarget.hasSelector,
'within': {
'@id': anno.hasTarget['dcterms:isPartOf'].id,
'@type': 'sc:Manifest'
}
}]
};
return converted;
},
deleteAnnotation: function (annotationID, returnSuccess, returnError) {
var _this = this;
jQuery.ajax({
url: annotationID,
type: 'DELETE',
dataType: 'json',
headers: {
// 'x-annotator-auth-token': this.token
},
success: function(data) {
if (typeof returnSuccess === 'function') {
returnSuccess();
}
},
error: function(xhr, statusText, err) {
if (typeof returnError === 'function') {
returnError();
} else {
console.log('Failed to delete annotation ' + annotationID + ' due to ' + statusText);
}
}
});
},
update: function (oaAnnotation, returnSuccess, returnError) {
var annotation = oaAnnotation;
var _this = this;
delete annotation.endpoint;
jQuery.ajax({
url: annotation['@id'],
type: 'PUT',
dataType: 'text',
headers: {
// 'x-annotator-auth-token': this.token
},
data: JSON.stringify(annotation),
contentType: 'application/ld+json',
success: function(data) {
if (typeof returnSuccess === 'function') {
var anno = _this.formatAnno(_this.cleanJsonLd(data));
anno.endpoint = _this;
returnSuccess(anno);
}
},
error: function(xhr, statusText, err) {
if (typeof returnError === 'function') {
returnError();
} else {
console.log('Failed to update annotation: ' + oaAnnotation['@id'] + ' due to ' + statusText);
}
}
});
// this is what updates the viewer
annotation.endpoint = _this;
},
create: function (oaAnnotation, returnSuccess, returnError) {
var annotation = oaAnnotation;
var _this = this;
jQuery.ajax({
url: _this.url + '/oa/' + _this.container + '/',
type: 'POST',
dataType: 'text',
headers: {
// 'x-annotator-auth-token': this.token
},
data: JSON.stringify(annotation),
contentType: 'application/ld+json',
success: function(data) {
var anno = _this.formatAnno(_this.cleanJsonLd(data));
anno.endpoint = _this;
if (typeof returnSuccess === 'function') {
returnSuccess(anno);
}
},
error: function(xhr, statusText, err) {
if (typeof returnError === 'function') {
returnError();
} else {
console.log('Failed to create annotation: ' + oaAnnotation['@id'] + ' due to ' + statusText);
}
}
});
},
set: function (prop, value, options) {
if (options) {
this[options.parent][prop] = value;
} else {
this[prop] = value;
}
},
userAuthorize: function (action, annotation) {
return true; // allow all
}
};
}(Mirador));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment