openmedialibrary/static/js/annotation.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-01-23 15:14:59 +00:00
'use strict';
2019-01-24 12:49:21 +00:00
oml.ui.annotation = function(annotation, $iframe) {
2019-01-25 10:26:45 +00:00
var $quote = Ox.Element().addClass('OxSelectable OMLQuote').css({
padding: '8px'
2019-01-29 10:42:46 +00:00
}).html(Ox.encodeHTMLEntities(annotation.text).replace(/\n/g, '<br/>')).on({
2019-01-25 10:26:45 +00:00
click: function(event) {
that.select()
$iframe.postMessage('selectAnnotation', {
2019-01-29 10:42:46 +00:00
id: annotation.id
2019-01-25 10:26:45 +00:00
})
}
})
2019-01-31 07:07:49 +00:00
var notes = annotation.notes.length ? annotation.notes.map(function(note) {
note.editable = note.user == ''
return note
}) : [{
id: 'A',
placeholder: 'Add Note',
value: '',
editable: true
}];
console.log(annotation.notes)
2019-01-29 10:42:46 +00:00
var $note = Ox.ArrayEditable({
2019-01-24 09:20:25 +00:00
editing: true,
2019-01-31 07:07:49 +00:00
items: notes,
2019-01-24 09:20:25 +00:00
type: 'textarea'
}).css({
2019-01-25 10:26:45 +00:00
margin: '2px',
minHeight: '12px'
2019-01-24 12:49:21 +00:00
}).bindEvent({
submit: function(data) {
2019-01-31 07:07:49 +00:00
var note = Ox.getObjectById(annotation.notes, data.id)
if (note) {
note.value = data.value
note.modified = (new Date).toISOString()
2019-01-24 12:49:21 +00:00
} else {
2019-01-31 07:07:49 +00:00
annotation.notes.push({
2019-01-24 12:49:21 +00:00
created: data.created || (new Date).toISOString(),
modified: (new Date).toISOString(),
id: data.id,
2019-01-31 07:07:49 +00:00
user: '',
2019-01-24 12:49:21 +00:00
value: data.value
})
}
that.triggerEvent('change')
}
2019-01-24 09:20:25 +00:00
});
2019-01-23 15:14:59 +00:00
var that = Ox.Element().attr({
2019-01-24 12:49:21 +00:00
id: 'a-' + annotation.id
2019-01-24 05:39:09 +00:00
}).addClass(
2019-01-25 10:26:45 +00:00
'OxSelectable OMLAnnotation'
2019-01-24 05:39:09 +00:00
).css({
borderBottom: '1px solid rgb(208, 208, 208)',
2019-01-31 06:22:19 +00:00
}).bindEvent({
key_delete: function() {
that.triggerEvent('delete', {
id: annotation.id
})
}
2019-01-25 10:26:45 +00:00
}).append($quote).append($note);
2019-01-29 10:42:46 +00:00
2019-01-24 09:20:25 +00:00
that.annotate = function() {
var item = {
id: 'note', value: '', editable: true
}
2019-01-25 10:26:45 +00:00
}
that.deselect = function() {
that.removeClass('selected')
2019-01-31 07:07:49 +00:00
that.loseFocus()
2019-01-25 10:26:45 +00:00
}
that.select = function () {
let selected = document.querySelector('.OMLAnnotation.selected')
selected && selected.classList.remove('selected')
that.addClass('selected')
2019-01-31 06:22:19 +00:00
that.gainFocus()
2019-01-24 09:20:25 +00:00
}
2019-01-23 15:14:59 +00:00
return that;
};