openmedialibrary/static/js/viewer.js

151 lines
5.8 KiB
JavaScript
Raw Normal View History

2014-05-04 17:26:43 +00:00
'use strict';
oml.ui.viewer = function() {
var ui = oml.user.ui,
2019-01-24 11:52:44 +00:00
annotations = [],
2014-05-04 17:26:43 +00:00
2019-01-23 15:14:59 +00:00
frame = Ox.Element(),
2014-05-04 17:26:43 +00:00
that = Ox.Element()
.bindEvent({
oml_itemview: function(data) {
2014-05-25 12:16:04 +00:00
if (ui.item != item && ui.itemView == 'book') {
that.updateElement(ui.item);
}
2019-01-23 15:14:59 +00:00
},
oml_showannotations: function() {
panel.toggleElement(1);
2014-05-04 17:26:43 +00:00
}
}),
2019-01-23 15:14:59 +00:00
panel = Ox.SplitPanel({
elements: [
{
element: frame
},
{
collapsed: !ui.showAnnotations,
collapsible: true,
element: oml.$ui.annotationPanel = oml.ui.annotationPanel(),
2019-01-24 07:22:40 +00:00
resizable: true,
resize: [128, 384],
2019-01-23 15:14:59 +00:00
size: 256,
tooltip: Ox._('Annotations')
+ ' <span class="OxBright">'
+ Ox.SYMBOLS.shift + 'A</span>'
}
],
orientation: 'horizontal'
})
.appendTo(that),
2014-05-25 12:16:04 +00:00
$iframe, item;
2014-05-04 17:26:43 +00:00
2019-01-24 11:52:44 +00:00
function loadAnnotations(callback) {
annotations = JSON.parse(localStorage[item + '.annotations'] || '[]')
2019-01-31 07:07:49 +00:00
annotations.forEach(function(data) {
if (data.comments && !data.notes) {
data.notes = data.comments
delete data.comments
}
data.notes = data.notes || [];
})
2019-01-24 11:52:44 +00:00
callback && callback(annotations)
}
function saveAnnotations(data) {
if (data) {
data.created = data.created || (new Date).toISOString();
2019-01-31 07:07:49 +00:00
if (data.comments && !data.notes) {
data.notes = data.comments
delete data.comments
}
data.notes = data.notes || [];
2019-01-24 11:52:44 +00:00
annotations.push(data);
}
localStorage[item + '.annotations'] = JSON.stringify(annotations)
}
function removeAnnotation(id) {
annotations = annotations.filter(function(annotation) {
return annotation.id != id
})
saveAnnotations()
}
2019-01-24 12:49:21 +00:00
var annotationEvents = {
change: function() {
saveAnnotations()
2019-01-31 06:22:19 +00:00
},
'delete': function(data) {
oml.$ui.annotationFolder.find('#a-' + data.id).remove()
that.postMessage('removeAnnotation', data)
2019-01-24 12:49:21 +00:00
}
}
2014-05-17 11:45:57 +00:00
that.updateElement = function() {
2014-05-25 12:16:04 +00:00
item = ui.item;
2014-05-26 16:02:41 +00:00
if (item && item.length) {
2014-05-25 14:10:38 +00:00
oml.api.get({id: item, keys: ['mediastate']}, function(result) {
if (result.data.mediastate == 'available') {
2019-01-24 07:22:40 +00:00
oml.$ui.annotationFolder.empty()
2019-01-23 15:14:59 +00:00
if ($iframe) {
$iframe.remove()
}
$iframe = Ox.Element('<iframe>').css({
2014-05-25 14:10:38 +00:00
width: '100%',
height: '100%',
border: 0
2019-01-23 15:14:59 +00:00
}).onMessage(function(data, event) {
if (event == 'addAnnotation') {
2019-01-24 11:52:44 +00:00
saveAnnotations(data);
2019-01-24 12:49:21 +00:00
var $annotation = oml.ui.annotation(data, $iframe).bindEvent(annotationEvents)
2019-01-24 09:20:25 +00:00
oml.$ui.annotationFolder.append($annotation);
$annotation.annotate();
2019-01-23 15:14:59 +00:00
} else if (event == 'removeAnnotation') {
2019-01-24 07:22:40 +00:00
oml.$ui.annotationFolder.find('#a-' + data.id).remove()
2019-01-24 11:52:44 +00:00
removeAnnotation(data.id)
2019-01-23 15:14:59 +00:00
} else if (event == 'selectAnnotation') {
if (data.id) {
var $annotation = oml.$ui.annotationFolder.find('#a-' + data.id)
$annotation.length && $annotation.select()
} else {
var $annotation = oml.$ui.annotationFolder.find('.OMLAnnotation.selected')
$annotation.length && $annotation.deselect()
}
} else if (event == 'selectText') {
oml.$ui.annotationPanel.updateSelection(data)
2019-01-24 11:52:44 +00:00
} else {
2019-01-31 18:42:52 +00:00
console.log('got', event, data)
2019-01-24 11:52:44 +00:00
that.triggerEvent(event, data);
}
}).bindEvent({
init: function() {
loadAnnotations(function(annotations) {
annotations.forEach(function(data) {
2019-01-24 12:49:21 +00:00
var $annotation = oml.ui.annotation(data, $iframe).bindEvent(annotationEvents)
2019-01-24 11:52:44 +00:00
oml.$ui.annotationFolder.append($annotation);
})
// fixme: trigger loaded event from reader instead?
setTimeout(function() {
that.postMessage('addAnnotations', {
annotations: annotations
})
}, 500)
})
2019-01-23 15:14:59 +00:00
}
}).appendTo(frame);
2014-05-25 14:10:38 +00:00
$iframe.attr({
src: '/' + item + '/reader/'
});
}
2014-05-04 17:26:43 +00:00
});
}
return that;
};
2019-01-23 15:14:59 +00:00
that.postMessage = function(event, data) {
2019-01-24 11:52:44 +00:00
$iframe && $iframe.postMessage(event, data)
2019-01-23 15:14:59 +00:00
};
2019-02-01 09:19:43 +00:00
that.getAnnotations = function() {
return annotations;
}
2014-05-17 11:45:57 +00:00
return that.updateElement();
2014-05-04 17:26:43 +00:00
};