pandora/static/js/exportAnnotationsDialog.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-09-18 10:40:41 +00:00
'use strict';
pandora.ui.exportAnnotationsDialog = function(options) {
var annotations = pandora.$ui.editor.getCurrentAnnotations(),
layers = pandora.site.layers.map(function(layer) {
return {
disabled: annotations[layer.id].length == 0,
id: layer.id,
title: layer.title
}
}),
enabledLayers = layers.filter(function(layer) {
return layer.disabled == false;
}),
layer = (enabledLayers.length ? enabledLayers : layers)[0].id,
$content = Ox.Element().css({margin: '16px'}),
$layerSelect = Ox.Select({
items: layers,
label: Ox._('Layer'),
labelWidth: 128,
value: layer,
width: 384
})
.css({
marginTop: '16px'
})
.bindEvent({
change: function() {
updateStatus();
that.enableButton('export');
2014-09-18 15:24:58 +00:00
!$link && addLink();
updateLink();
}
})
.appendTo($content),
$status = Ox.$('<div>')
.css({
marginTop: '16px'
})
.appendTo($content),
2014-09-18 15:24:58 +00:00
$link,
that = Ox.Dialog({
buttons: [
Ox.Button({
2014-09-18 15:24:58 +00:00
id: 'dontExport',
title: Ox._('Don\'t Export')
})
.bindEvent({
click: function() {
that.close();
}
}),
Ox.Button({
2014-09-18 15:24:58 +00:00
disabled: enabledLayers.length == 0,
id: 'export',
title: Ox._('Export')
})
],
closeButton: true,
content: $content,
fixedSize: true,
height: 80,
removeOnClose: true,
title: Ox._('Export Annotations'),
width: 416
});
updateStatus();
2014-09-18 15:24:58 +00:00
if (enabledLayers.length) {
addLink();
updateLink();
}
function addLink() {
var layer = $layerSelect.value();
$link = $('<a>').attr({
target: '_blank'
});
updateLink();
$(that.find('.OxButton')[3]).wrap($link);
}
function updateLink() {
var layer = $layerSelect.value();
$link.attr({
download: options.title + ' - '
+ Ox.getObjectById(layers, layer).title + '.srt',
href: 'data:text/plain;base64,' + btoa(
Ox.formatSRT(annotations[layer].map(function(annotation) {
return {
'in': annotation['in'],
out: annotation.out,
text: annotation.value
.replace(/\n/g, ' ')
.replace(/\s+/g, ' ')
.replace(/<br>\s+?/g, '\n')
};
}))
)
})
}
function updateStatus() {
$status.html(Ox._('All {0} currently shown will be exported.', [
Ox.getObjectById(layers, $layerSelect.value()).title.toLowerCase()
]));
}
return that;
2014-09-18 10:40:41 +00:00
};