update importAnnotationsDialog

This commit is contained in:
rolux 2014-09-18 14:16:59 +02:00
parent 9408a5f986
commit bee3906d97

View file

@ -1,11 +1,9 @@
// vim: et:ts=4:sw=4:sts=4:ft=javascript // vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict'; 'use strict';
pandora.ui.importAnnotationsDialog = function(data) { pandora.ui.importAnnotationsDialog = function(duration) {
var srt, var layers = pandora.site.layers.filter(function(layer) {
layers = pandora.site.layers.filter(function(layer) {
return layer.canAddAnnotations[pandora.user.level]; return layer.canAddAnnotations[pandora.user.level];
}), }),
@ -26,9 +24,7 @@ pandora.ui.importAnnotationsDialog = function(data) {
marginTop: '16px' marginTop: '16px'
}) })
.bindEvent({ .bindEvent({
change: function() { change: updateLanguageSelect
updateLanguageSelect();
}
}) })
.appendTo($content), .appendTo($content),
@ -55,30 +51,9 @@ pandora.ui.importAnnotationsDialog = function(data) {
}) })
.bindEvent({ .bindEvent({
change: function(data) { change: function(data) {
var reader; that[
if (data.value.length) { data.value.length ? 'enableButton' : 'disableButton'
reader = new FileReader(); ]('import');
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])
);
}
};
reader.readAsText(data.value[0]);
} else {
srt = [];
$importButton.options({
disabled: true
});
}
} }
}) })
.appendTo($content), .appendTo($content),
@ -89,30 +64,24 @@ pandora.ui.importAnnotationsDialog = function(data) {
}) })
.appendTo($content), .appendTo($content),
$dontImportButton = Ox.Button({
id: 'dontImport',
title: Ox._('Don\'t Import')
})
.bindEvent({
click: function() {
that.close();
}
}),
$importButton = Ox.Button({
disabled: true,
id: 'import',
title: Ox._('Import')
}).bindEvent({
click: function() {
addAnnotations();
}
}),
that = Ox.Dialog({ that = Ox.Dialog({
buttons: [ buttons: [
$dontImportButton, Ox.Button({
$importButton id: 'dontImport',
title: Ox._('Don\'t Import')
})
.bindEvent({
click: function() {
that.close();
}
}),
Ox.Button({
disabled: true,
id: 'import',
title: Ox._('Import')
}).bindEvent({
click: addAnnotations
})
], ],
closeButton: true, closeButton: true,
content: $content, content: $content,
@ -125,71 +94,96 @@ pandora.ui.importAnnotationsDialog = function(data) {
width: 416 width: 416
}); });
updateLanguageSelect() updateLanguageSelect();
function addAnnotations() { function addAnnotations() {
var annotations,
var annotations = [],
language = $languageSelect.value(), language = $languageSelect.value(),
layer = $layerSelect.value(), layer = $layerSelect.value(),
task; file = $fileInput.value()[0],
if (srt.length > 0) { reader = new FileReader();
setStatus(Ox._('Importing {0} annotations...', [srt.length]));
annotations = srt.filter(function(data) { disableButtons();
return !Ox.isUndefined(data['in'])
&& !Ox.isUndefined(data.out) reader.onloadend = function(e) {
&& data.text; if (this.result) {
}).map(function(data) { annotations = parseSRT(this.result);
var value = Ox.sanitizeHTML(data.text) }
.replace(/<br[ /]*?>\n/g, '\n') if (annotations.length) {
.replace(/\n\n/g, '<br>\n') setStatus(Ox._(
.replace(/\n/g, '<br>\n'); 'Importing {0} annotation'
if (language != pandora.site.language) { + (annotations.length == 1 ? '' : 's') + '...',
value = '<span lang="' + language + '">' + value + '</span>'; [annotations.length]
} ));
return { annotations = annotations.map(function(annotation) {
'in': data['in'], var value = Ox.sanitizeHTML(annotation.text)
out: data.out, .replace(/<br[ /]*?>\n/g, '\n')
value: value .replace(/\n\n/g, '<br>\n')
}; .replace(/\n/g, '<br>\n');
}); if (language != pandora.site.language) {
pandora.api.addAnnotations({ value = '<span lang="' + language + '">'
annotations: annotations, + value + '</span>';
item: pandora.user.ui.item, }
layer: layer return {
}, function(result) { 'in': annotation['in'],
if (result.data.taskId) { out: annotation.out,
setStatus(Ox._('Importing {0} annotations...', [srt.length])); value: value
pandora.wait(result.data.taskId, function(result) { };
if (result.data.status == 'SUCCESS') { });
setStatus(Ox._('{0} annotations imported.', [annotations.length])); pandora.api.addAnnotations({
Ox.Request.clearCache(pandora.user.ui.item); annotations: annotations,
pandora.$ui.contentPanel.replaceElement( item: pandora.user.ui.item,
1, pandora.$ui.item = pandora.ui.item() layer: layer
); }, function(result) {
} else { if (result.data.taskId) {
setStatus(Ox._('Import failed.')); pandora.wait(result.data.taskId, function(result) {
} if (result.data.status == 'SUCCESS') {
}); setStatus(Ox._('Import succeeded.'));
} else { Ox.Request.clearCache(pandora.user.ui.item);
setStatus(Ox._('Import failed.')); pandora.$ui.contentPanel.replaceElement(
} 1, pandora.$ui.item = pandora.ui.item()
}); );
} } else {
setStatus(Ox._('Import failed.'));
}
enableButtons();
});
} else {
setStatus(Ox._('Import failed.'));
enableButtons();
}
});
} else {
setStatus(Ox._('No valid annotations found.'));
enableButtons();
}
};
setTimeout(function() {
reader.readAsText(file);
}, 250);
} }
function parseSRT(data) { function disableButtons() {
var srt = Ox.parseSRT(data), that.disableButtons();
length = srt.length - 1; that.disableCloseButton();
//pandora layers include outpoint, }
//speedtrans right now sets in to out,
//to avoid one frame overlaps, function enableButtons() {
//move outpoint by 0.001 seconds that.enableButtons();
for (var i=0; i < length; i++) { that.enableCloseButton();
if (srt[i].out == srt[i+1]['in']) { }
srt[i].out = srt[i].out - 0.001;
} function parseSRT(srt) {
} return Ox.parseSRT(srt).filter(function(annotation) {
return srt; return !Ox.isUndefined(annotation['in'])
&& !Ox.isUndefined(annotation.out)
&& annotation['in'] <= annotation.out
&& annotation.out <= duration
&& annotation.text;
});
} }
function setStatus(status) { function setStatus(status) {