pandora/static/js/importAnnotationsDialog.js

217 lines
6.5 KiB
JavaScript
Raw Normal View History

2012-02-14 18:48:08 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
2014-09-17 13:10:17 +00:00
pandora.ui.importAnnotationsDialog = function(data) {
2014-09-17 14:42:44 +00:00
var srt,
layers = pandora.site.layers.filter(function(layer) {
2012-02-16 13:11:27 +00:00
return layer.canAddAnnotations[pandora.user.level];
}),
2014-09-17 13:10:17 +00:00
languages = Ox.sortBy(Ox.LANGUAGES.map(function(language) {
return {id: language.code, title: language.name};
2014-09-17 13:25:48 +00:00
}), 'title'),
2014-09-17 14:42:44 +00:00
2014-09-17 13:10:17 +00:00
$content = Ox.Element()
.css({margin: '16px'}),
$layerSelect = Ox.Select({
items: layers,
label: Ox._('Layer'),
2014-09-17 13:25:48 +00:00
labelWidth: 128,
2014-09-17 13:10:17 +00:00
width: 384
})
.css({
marginTop: '16px'
})
2014-09-17 14:42:44 +00:00
.bindEvent({
change: function() {
updateLanguageSelect();
}
})
2014-09-17 13:10:17 +00:00
.appendTo($content),
$languageSelect = Ox.Select({
2014-09-17 13:25:48 +00:00
items: languages,
2014-09-17 13:10:17 +00:00
label: Ox._('Language'),
2014-09-17 13:25:48 +00:00
labelWidth: 128,
2014-09-17 14:42:44 +00:00
value: pandora.site.language,
2014-09-17 13:10:17 +00:00
width: 384
})
.css({
marginTop: '16px'
})
.appendTo($content),
$fileInput = Ox.FileInput({
2014-09-17 14:42:44 +00:00
label: Ox._('SRT File'),
labelWidth: 128,
2014-09-17 13:25:48 +00:00
maxFiles: 1,
2014-09-17 13:10:17 +00:00
width: 384
})
.css({
marginTop: '16px'
})
.bindEvent({
change: function(data) {
2014-09-17 13:25:48 +00:00
var reader;
if (data.value.length) {
reader = new FileReader();
2014-09-17 14:42:44 +00:00
reader.onloadend = function(e) {
if (this.result) {
srt = parseSRT(this.result);
if (srt.length) {
$importButton.options({disabled: false});
//selectLayer.hide();
//selectFile.hide();
}
setStatus(
Ox._('File contains {0} annotation'
+ (srt.length == 1 ? '' : 's') + '.', [srt.length])
);
2014-09-17 13:10:17 +00:00
}
};
2014-09-17 13:25:48 +00:00
reader.readAsText(data.value[0]);
2014-09-17 13:10:17 +00:00
} else {
srt = [];
2014-09-17 14:42:44 +00:00
$importButton.options({
2014-09-17 13:10:17 +00:00
disabled: true
});
}
}
})
.appendTo($content),
$status = Ox.$('<div>')
.css({
2014-09-17 14:42:44 +00:00
marginTop: '48px' // FIXME
2014-09-17 13:10:17 +00:00
})
.appendTo($content),
2014-09-17 14:42:44 +00:00
$dontImportButton = Ox.Button({
id: 'dontImport',
title: Ox._('Don\'t Import')
2014-09-17 13:10:17 +00:00
})
.bindEvent({
click: function() {
that.close();
}
}),
$importButton = Ox.Button({
disabled: true,
id: 'import',
title: Ox._('Import')
}).bindEvent({
click: function() {
2014-09-17 14:42:44 +00:00
addAnnotations();
2014-09-17 13:10:17 +00:00
}
}),
that = Ox.Dialog({
2012-02-14 18:48:08 +00:00
buttons: [
2014-09-17 14:42:44 +00:00
$dontImportButton,
2014-09-17 13:10:17 +00:00
$importButton
2012-02-14 18:48:08 +00:00
],
2014-09-17 14:42:44 +00:00
closeButton: true,
2014-09-17 13:10:17 +00:00
content: $content,
height: 144,
2012-02-14 18:48:08 +00:00
keys: {
2014-09-17 14:42:44 +00:00
escape: 'dontImport'
2012-02-14 18:48:08 +00:00
},
2012-02-14 19:01:30 +00:00
removeOnClose: true,
2014-09-17 13:10:17 +00:00
title: Ox._('Import Annotations'),
width: 416
});
2012-02-16 13:11:27 +00:00
2014-09-17 14:42:44 +00:00
updateLanguageSelect()
function addAnnotations() {
2014-09-17 14:42:44 +00:00
var annotations,
language = $languageSelect.value(),
layer = $layerSelect.value(),
task;
2012-06-10 18:31:02 +00:00
if (srt.length > 0) {
2014-09-17 14:42:44 +00:00
setStatus(Ox._('Importing {0} annotations...', [srt.length]));
annotations = srt.filter(function(data) {
2014-09-17 13:10:17 +00:00
return !Ox.isUndefined(data['in'])
&& !Ox.isUndefined(data.out)
&& data.text;
}).map(function(data) {
2014-09-17 14:42:44 +00:00
var value = Ox.sanitizeHTML(data.text)
.replace(/<br[ /]*?>\n/g, '\n')
.replace(/\n\n/g, '<br>\n')
.replace(/\n/g, '<br>\n');
if (language != pandora.site.language) {
value = '<span lang="' + language + '">' + value + '</span>';
}
return {
'in': data['in'],
out: data.out,
2014-09-17 14:42:44 +00:00
value: value
};
});
pandora.api.addAnnotations({
annotations: annotations,
2012-02-16 13:11:27 +00:00
item: pandora.user.ui.item,
layer: layer
}, function(result) {
if (result.data.taskId) {
2014-09-17 14:42:44 +00:00
setStatus(Ox._('Importing {0} annotations...', [srt.length]));
pandora.wait(result.data.taskId, function(result) {
2013-07-14 17:44:30 +00:00
if (result.data.status == 'SUCCESS') {
2013-05-09 10:13:58 +00:00
setStatus(Ox._('{0} annotations imported.', [annotations.length]));
Ox.Request.clearCache(pandora.user.ui.item);
pandora.$ui.contentPanel.replaceElement(
1, pandora.$ui.item = pandora.ui.item()
);
} else {
2014-09-17 14:42:44 +00:00
setStatus(Ox._('Import failed.'));
}
});
2012-02-16 13:11:27 +00:00
} else {
2014-09-17 14:42:44 +00:00
setStatus(Ox._('Import failed.'));
2012-02-16 13:11:27 +00:00
}
});
}
}
2014-09-17 13:10:17 +00:00
function parseSRT(data) {
var srt = Ox.parseSRT(data),
length = srt.length - 1;
//pandora layers include outpoint,
//speedtrans right now sets in to out,
//to avoid one frame overlaps,
//move outpoint by 0.001 seconds
for (var i=0; i < length; i++) {
if (srt[i].out == srt[i+1]['in']) {
srt[i].out = srt[i].out - 0.001;
}
}
return srt;
}
function setStatus(status) {
$status.html(status);
}
2014-09-17 14:42:44 +00:00
function updateLanguageSelect() {
var layerType = Ox.getObjectById(
pandora.site.layers, $layerSelect.value()
).type;
if (layerType != 'text') {
$languageSelect.value(pandora.site.language);
}
languages.forEach(function(language) {
$languageSelect[
language.id == pandora.site.language || layerType == 'text'
? 'enableItem' : 'disableItem'
](language.id);
});
}
2012-02-14 18:48:08 +00:00
return that;
2014-09-17 13:10:17 +00:00
2012-02-14 18:48:08 +00:00
};