Created
October 15, 2014 22:25
-
-
Save tilgovi/c16123a96231931efd83 to your computer and use it in GitHub Desktop.
annotator-storage-oacg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Annotator = require('annotator'); | |
function dump(data) { | |
var result = {}; | |
result['@context'] = 'http://www.w3.org/ns/oa-context-20130208.json'; | |
if (typeof(data.id) !== 'undefined') { | |
result['@id'] = data.id; | |
} | |
result['@type'] = 'oa:Annotation'; | |
// Bodies: text and tags | |
var body = result['hasBody'] = []; | |
if (typeof(data.tags) !== 'undefined') { | |
body = data.tags.reduce(function (body, tag) { | |
body.push({ | |
'@type': ['oa:ContentAsText', 'oa:Tag'], | |
'chars': tag | |
}); | |
return body; | |
}, body); | |
} | |
if (typeof(data.text) !== 'undefined') { | |
body.push({ | |
'@type': ['cnt:ContentAsText', 'dctypes:Text'], | |
'chars': data.text | |
}); | |
} | |
// Targets: uri, quotes and ranges | |
var target = result['hasTarget'] = []; | |
if (data.uri && typeof(data.ranges) !== 'undefined') { | |
target = data.ranges.reduce(function (target, range) { | |
target.push({ | |
'@type': ['oa:SpecificResource'], | |
'hasSource': data.uri, | |
'hasSelector': { | |
'@type': [ | |
'oa:TextQuoteSelector', | |
'http://annotatorjs.org/ns#XPathRangeSelector' | |
], | |
'exact': data.quote, | |
'startContainer': range.start, | |
'startOffset': range.startOffset, | |
'endContainer': range.end, | |
'endOffset': range.endOffset | |
} | |
}); | |
return target; | |
}, target); | |
} | |
// User | |
if (typeof(data.user) !== 'undefined') { | |
result['annotatedBy'] = data.user; | |
} | |
// Timestamp | |
if (typeof(data.updated) !== 'undefined') { | |
result['annotatedAt'] = data.updated; | |
} | |
return result; | |
} | |
function load(data) { | |
return data; | |
} | |
function OacgStorageAdapter(impl) { | |
function OacStorage() { | |
return { | |
'create': function (annotation) { | |
var data = dump(annotation); | |
return Promise.resolve(impl['create'](data)).then(load); | |
}, | |
'update': function (annotation) { | |
var data = dump(annotation); | |
return Promise.resolve(impl['update'](data)).then(load); | |
}, | |
'delete': function (annotation) { | |
var data = dump(annotation); | |
return Promise.resolve(impl['delete'](data)).then(load); | |
}, | |
'query': function (queryObj) { | |
return Promise.resolve(impl['query'](queryObject)) | |
.then(function (results) { | |
return results.annotations.map(load) | |
}); | |
} | |
} | |
} | |
return OacStorage; | |
} | |
exports.OacgStorageAdapter = OacgStorageAdapter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment