openmedialibrary/static/js/annotation.js

103 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-01-23 20:44:59 +05:30
'use strict';
2019-01-24 18:19:21 +05:30
oml.ui.annotation = function(annotation, $iframe) {
var $quoteText = Ox.Element()
.addClass('OxSelectable OMLQuote')
.html(Ox.encodeHTMLEntities(annotation.text).replace(/\n/g, '<br/>'))
.on({
click: function(event) {
that.select()
$iframe.postMessage('selectAnnotation', {
id: annotation.id
})
}
});
var $quoteBackground = Ox.Element().addClass('OMLQuoteBackground');
2019-02-01 13:19:11 +05:30
var $quote = Ox.Element()
.addClass('OMLQuoteBox')
.append(
$quoteBackground
).append(
$quoteText
);
2019-01-31 12:37:49 +05:30
var notes = annotation.notes.length ? annotation.notes.map(function(note) {
2019-01-31 22:18:53 +05:30
note.editable = !note.user
2019-01-31 12:37:49 +05:30
return note
}) : [];
2019-01-31 23:02:17 +05:30
var $notes = Ox.ArrayEditable({
2019-01-24 14:50:25 +05:30
editing: true,
2019-01-31 12:37:49 +05:30
items: notes,
2019-01-31 23:02:17 +05:30
placeholder: 'Add note',
2019-01-24 14:50:25 +05:30
type: 'textarea'
}).css({
2019-01-25 15:56:45 +05:30
minHeight: '12px'
2019-01-24 18:19:21 +05:30
}).bindEvent({
2019-02-01 00:12:52 +05:30
doubleclick: addNote,
2019-01-24 18:19:21 +05:30
submit: function(data) {
2019-01-31 12:37:49 +05:30
var note = Ox.getObjectById(annotation.notes, data.id)
if (note) {
note.value = data.value
note.modified = (new Date).toISOString()
2019-01-24 18:19:21 +05:30
} else {
2019-01-31 12:37:49 +05:30
annotation.notes.push({
2019-01-24 18:19:21 +05:30
created: data.created || (new Date).toISOString(),
modified: (new Date).toISOString(),
id: data.id,
2019-01-31 12:37:49 +05:30
user: '',
2019-01-24 18:19:21 +05:30
value: data.value
})
}
that.triggerEvent('change')
}
2019-01-24 14:50:25 +05:30
});
2019-01-23 20:44:59 +05:30
var that = Ox.Element().attr({
2019-01-24 18:19:21 +05:30
id: 'a-' + annotation.id
2019-01-24 11:09:09 +05:30
}).addClass(
2019-01-25 15:56:45 +05:30
'OxSelectable OMLAnnotation'
2019-01-24 11:09:09 +05:30
).css({
borderBottom: '1px solid rgb(208, 208, 208)',
2019-01-31 11:52:19 +05:30
}).bindEvent({
key_delete: function() {
that.triggerEvent('delete', {
id: annotation.id
})
}
2019-01-31 23:02:17 +05:30
}).append($quote).append($notes);
2019-01-29 16:12:46 +05:30
2019-02-01 00:12:52 +05:30
function addNote() {
if (!$notes.options('items').length) {
that.select()
$notes.addItem(0, {
id: 'A',
value: '',
editable: true
}).options({
selected: 'A'
}).editItem()
}
}
2019-01-24 14:50:25 +05:30
that.annotate = function() {
2019-02-01 00:12:52 +05:30
if (oml.user.ui.showAnnotations) {
addNote()
2019-01-24 14:50:25 +05:30
}
2019-01-25 15:56:45 +05:30
}
that.deselect = function() {
that.removeClass('selected')
2019-01-31 12:37:49 +05:30
that.loseFocus()
2019-01-25 15:56:45 +05:30
}
that.select = function () {
2019-02-01 00:12:52 +05:30
$iframe.postMessage('selectAnnotation', {
id: annotation.id
})
2019-01-25 15:56:45 +05:30
let selected = document.querySelector('.OMLAnnotation.selected')
selected && selected.classList.remove('selected')
that.addClass('selected')
2019-01-31 11:52:19 +05:30
that.gainFocus()
that[0].scrollIntoViewIfNeeded()
2019-01-24 14:50:25 +05:30
}
2019-01-23 20:44:59 +05:30
return that;
};