openmedialibrary/static/js/viewer.js

249 lines
9.5 KiB
JavaScript
Raw Normal View History

2014-05-04 19:26:43 +02:00
'use strict';
oml.ui.viewer = function() {
var ui = oml.user.ui,
2019-01-24 17:22:44 +05:30
annotations = [],
2014-05-04 19:26:43 +02:00
2019-01-23 20:44:59 +05:30
frame = Ox.Element(),
2014-05-04 19:26:43 +02:00
that = Ox.Element()
.bindEvent({
oml_itemview: function(data) {
2014-05-25 14:16:04 +02:00
if (ui.item != item && ui.itemView == 'book') {
that.updateElement(ui.item);
}
2019-01-23 20:44:59 +05:30
},
oml_showannotations: function() {
panel.toggleElement(1);
},
oml_sortannotations: function(data) {
that.renderAnnotations()
},
oml_annotationusers: function(data) {
that.renderAnnotations()
2014-05-04 19:26:43 +02:00
}
}),
2019-01-23 20:44:59 +05:30
panel = Ox.SplitPanel({
elements: [
{
element: frame
},
{
collapsed: !ui.showAnnotations,
collapsible: true,
element: oml.$ui.annotationPanel = oml.ui.annotationPanel(),
2019-01-24 12:52:40 +05:30
resizable: true,
resize: [128, 384],
2019-01-23 20:44:59 +05:30
size: 256,
tooltip: Ox._('Annotations')
+ ' <span class="OxBright">'
+ Ox.SYMBOLS.shift + 'A</span>'
}
],
orientation: 'horizontal'
})
.appendTo(that),
2014-05-25 14:16:04 +02:00
$iframe, item;
2014-05-04 19:26:43 +02:00
2019-01-24 17:22:44 +05:30
function loadAnnotations(callback) {
if (localStorage[item + '.annotations']) {
annotations = JSON.parse(localStorage[item + '.annotations'] || '[]')
var ids = []
annotations.forEach(function(data) {
if (data.comments && !data.notes) {
data.notes = data.comments
delete data.comments
}
if (!Ox.contains(ids, data.id)) {
ids.push(data.id)
var note
if (data.notes && data.notes.length) {
note = data.notes[0]
delete data.notes
}
2019-02-11 00:20:26 +05:30
var a = Ox.extend({}, data)
a.created = a.created || (new Date).toISOString();
a.item = ui.item
oml.api.addAnnotation(a)
if (note) {
data.notes = [note]
} else {
data.notes = []
}
if (data.notes.length) {
var note = data.notes[0]
oml.api.editNote({
item: ui.item,
annotation: data.id,
notes: {
created: note.created,
modified: note.modified,
value: note.value
}
})
}
} else {
console.log('ignore second time', data)
}
})
localStorage[oml.user.ui.item + '.annotations_'] = localStorage[oml.user.ui.item + '.annotations']
delete localStorage[oml.user.ui.item + '.annotations']
callback && callback(annotations)
} else {
oml.api.getAnnotations({
id: ui.item
}, function(result) {
2019-02-11 00:25:00 +05:30
if (!result.data.annotations.length && localStorage[oml.user.ui.item + '.annotations_']) {
localStorage[oml.user.ui.item + '.annotations'] = localStorage[oml.user.ui.item + '.annotations_']
loadAnnotations(callback)
} else {
annotations = result.data.annotations
callback && callback(annotations)
}
})
}
2019-01-24 17:22:44 +05:30
}
function addAnnotation(data, save) {
var a = Ox.extend({}, data)
a.created = a.created || (new Date).toISOString();
a.item = ui.item
if (save !== false) {
oml.api.addAnnotation(a)
2019-01-24 17:22:44 +05:30
}
data.user = a.user || oml.user.id
data.notes = data.notes || [];
annotations.push(data);
2019-01-24 17:22:44 +05:30
}
2019-01-24 17:22:44 +05:30
function removeAnnotation(id) {
oml.api.removeAnnotation({
item: ui.item,
annotation: id
}, function(result) {
annotations = annotations.filter(function(annotation) {
return annotation.id != id
})
2019-01-24 17:22:44 +05:30
})
}
2019-01-24 18:19:21 +05:30
var annotationEvents = {
change: function(data) {
// console.log('change', data)
2019-01-31 11:52:19 +05:30
},
'delete': function(data) {
oml.$ui.annotationFolder.find('#a-' + data.id).remove()
that.postMessage('removeAnnotation', data)
2019-01-24 18:19:21 +05:30
}
}
2014-05-17 13:45:57 +02:00
that.updateElement = function() {
2014-05-25 14:16:04 +02:00
item = ui.item;
2014-05-26 18:02:41 +02:00
if (item && item.length) {
2014-05-25 16:10:38 +02:00
oml.api.get({id: item, keys: ['mediastate']}, function(result) {
if (result.data.mediastate == 'available') {
2019-01-24 12:52:40 +05:30
oml.$ui.annotationFolder.empty()
2019-01-23 20:44:59 +05:30
if ($iframe) {
$iframe.remove()
}
$iframe = Ox.Element('<iframe>').css({
2014-05-25 16:10:38 +02:00
width: '100%',
height: '100%',
border: 0
2019-01-23 20:44:59 +05:30
}).onMessage(function(data, event) {
2019-02-09 17:13:24 +05:30
console.log('got', event, data)
2019-01-23 20:44:59 +05:30
if (event == 'addAnnotation') {
addAnnotation(data);
2019-01-24 18:19:21 +05:30
var $annotation = oml.ui.annotation(data, $iframe).bindEvent(annotationEvents)
2019-01-24 14:50:25 +05:30
oml.$ui.annotationFolder.append($annotation);
$annotation.annotate();
2019-02-02 23:31:55 +05:30
oml.$ui.annotationPanel.updateSelection(false)
oml.$ui.annotationPanel.options({hasAnnotations: true})
2019-01-23 20:44:59 +05:30
} else if (event == 'removeAnnotation') {
2019-01-24 12:52:40 +05:30
oml.$ui.annotationFolder.find('#a-' + data.id).remove()
data.id && removeAnnotation(data.id)
oml.$ui.annotationPanel.options({hasAnnotations: annotations.filter(function(a) {
return a.user == oml.user.id
}).length > 0})
2019-01-23 20:44:59 +05:30
} 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()
}
2019-02-02 23:31:55 +05:30
oml.$ui.annotationPanel.updateSelection(false)
} else if (event == 'selectText') {
oml.$ui.annotationPanel.updateSelection(data)
2019-01-24 17:22:44 +05:30
} else {
console.log('trigger unknwon event', event, data)
2019-01-24 17:22:44 +05:30
that.triggerEvent(event, data);
}
}).bindEvent({
init: function() {
loadAnnotations(function(annotations) {
2019-02-04 14:23:42 +05:30
that.renderAnnotations()
2019-01-24 17:22:44 +05:30
})
2019-01-23 20:44:59 +05:30
}
}).appendTo(frame);
2014-05-25 16:10:38 +02:00
$iframe.attr({
src: '/' + item + '/reader/'
});
}
2014-05-04 19:26:43 +02:00
});
}
return that;
};
2019-01-23 20:44:59 +05:30
that.postMessage = function(event, data) {
2019-01-24 17:22:44 +05:30
$iframe && $iframe.postMessage(event, data)
2019-01-23 20:44:59 +05:30
};
2019-02-01 14:49:43 +05:30
that.getAnnotations = function() {
return annotations;
}
that.renderAnnotations = function(load=false) {
var sortKey = ui.sortAnnotations
if (sortKey == 'date') {
sortKey = 'created'
}
if (sortKey == 'quote') {
sortKey = 'text'
}
if (load) {
loadAnnotations(function() {
that.renderAnnotations()
})
2019-03-05 18:31:21 +01:00
return
}
2019-03-05 19:48:53 +01:00
var map = {}
map[sortKey] = function(value) {
return value.toString();
}
annotations = Ox.sortBy(annotations, sortKey, map)
2019-02-04 14:23:42 +05:30
oml.$ui.annotationFolder.empty();
var visibleAnnotations = [];
var hasAnnotations = false;
2019-02-04 14:23:42 +05:30
annotations.forEach(function(data) {
//that.postMessage('removeAnnotation', {id: data.id})
if (ui.showAnnotationUsers == 'all' || data.user == oml.user.id) {
var $annotation = oml.ui.annotation(data, $iframe).bindEvent(annotationEvents)
oml.$ui.annotationFolder.append($annotation);
visibleAnnotations.push(data)
}
if (data.user == oml.user.id) {
hasAnnotations = true
}
2019-02-04 14:23:42 +05:30
})
oml.$ui.annotationPanel.options({hasAnnotations: hasAnnotations})
// fixme: trigger loaded event from reader instead?
setTimeout(function() {
that.postMessage('addAnnotations', {
annotations: visibleAnnotations,
replace: true
})
}, 500)
2019-02-04 14:23:42 +05:30
}
2014-05-17 13:45:57 +02:00
return that.updateElement();
2014-05-04 19:26:43 +02:00
};