'use strict'; oml.ui.viewer = function() { var ui = oml.user.ui, annotations = [], frame = Ox.Element(), that = Ox.Element() .bindEvent({ oml_itemview: function(data) { if (ui.item != item && ui.itemView == 'book') { that.updateElement(ui.item); } }, oml_showannotations: function() { panel.toggleElement(1); }, oml_sortannotations: function(data) { that.renderAnnotations() }, oml_annotationusers: function(data) { that.renderAnnotations() } }), panel = Ox.SplitPanel({ elements: [ { element: frame }, { collapsed: !ui.showAnnotations, collapsible: true, element: oml.$ui.annotationPanel = oml.ui.annotationPanel(), resizable: true, resize: [128, 384], size: 256, tooltip: Ox._('Annotations') + ' ' + Ox.SYMBOLS.shift + 'A' } ], orientation: 'horizontal' }) .appendTo(that), $iframe, item; 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 } addAnnotation(data, false) 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) { console.log(result) annotations = result.data.annotations callback && callback(annotations) }) } } 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) } data.notes = data.notes || []; annotations.push(data); } function removeAnnotation(id) { oml.api.removeAnnotation({ item: ui.item, annotation: id }, function(result) { annotations = annotations.filter(function(annotation) { return annotation.id != id }) }) } var annotationEvents = { change: function(data) { // console.log('change', data) }, 'delete': function(data) { oml.$ui.annotationFolder.find('#a-' + data.id).remove() that.postMessage('removeAnnotation', data) } } that.updateElement = function() { item = ui.item; if (item && item.length) { oml.api.get({id: item, keys: ['mediastate']}, function(result) { if (result.data.mediastate == 'available') { oml.$ui.annotationFolder.empty() if ($iframe) { $iframe.remove() } $iframe = Ox.Element('