unify annotations local storage

This commit is contained in:
j 2019-01-24 17:22:44 +05:30
commit d4b89137db
4 changed files with 61 additions and 40 deletions

View file

@ -1,7 +1,6 @@
'use strict';
oml.ui.annotation = function(data, $iframe) {
data.created = data.created || (new Date).toISOString();
var $arrayEditable = Ox.ArrayEditable({
editing: true,
type: 'textarea'

View file

@ -3,6 +3,7 @@
oml.ui.viewer = function() {
var ui = oml.user.ui,
annotations = [],
frame = Ox.Element(),
that = Ox.Element()
@ -38,6 +39,24 @@ oml.ui.viewer = function() {
.appendTo(that),
$iframe, item;
function loadAnnotations(callback) {
annotations = JSON.parse(localStorage[item + '.annotations'] || '[]')
callback && callback(annotations)
}
function saveAnnotations(data) {
if (data) {
data.created = data.created || (new Date).toISOString();
annotations.push(data);
}
localStorage[item + '.annotations'] = JSON.stringify(annotations)
}
function removeAnnotation(id) {
annotations = annotations.filter(function(annotation) {
return annotation.id != id
})
saveAnnotations()
}
that.updateElement = function() {
item = ui.item;
if (item && item.length) {
@ -55,18 +74,36 @@ oml.ui.viewer = function() {
console.log('got', event, data)
if (event == 'addAnnotation') {
console.log('adding', data.id)
saveAnnotations(data);
var $annotation = oml.ui.annotation(data, $iframe)
oml.$ui.annotationFolder.append($annotation);
$annotation.annotate();
} else if (event == 'removeAnnotation') {
console.log('do it ...', data)
oml.$ui.annotationFolder.find('#a-' + data.id).remove()
removeAnnotation(data.id)
} else if (event == 'selectAnnotation') {
console.log('select', data)
} else if (event == 'deselectAnnotation') {
console.log('deselect', data)
} else {
that.triggerEvent(event, data);
}
}).bindEvent({
init: function() {
loadAnnotations(function(annotations) {
annotations.forEach(function(data) {
var $annotation = oml.ui.annotation(data, $iframe)
oml.$ui.annotationFolder.append($annotation);
})
// fixme: trigger loaded event from reader instead?
setTimeout(function() {
that.postMessage('addAnnotations', {
annotations: annotations
})
}, 500)
})
}
that.triggerEvent(event, data);
}).appendTo(frame);
$iframe.attr({
src: '/' + item + '/reader/'
@ -77,7 +114,7 @@ oml.ui.viewer = function() {
return that;
};
that.postMessage = function(event, data) {
$iframe && $iframe.postMesage(event, data)
$iframe && $iframe.postMessage(event, data)
};
return that.updateElement();