pandora/static/js/importAnnotations.js

179 lines
5.8 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';
2012-02-14 19:06:25 +00:00
pandora.ui.importAnnotations = function(data) {
2012-02-14 18:48:08 +00:00
var content = Ox.Element().css({margin: '16px'}),
file,
height = 128,
2012-02-16 13:11:27 +00:00
layers = pandora.site.layers.filter(function(layer) {
return layer.canAddAnnotations[pandora.user.level];
}),
2013-01-12 06:55:20 +00:00
layer,
2012-02-16 13:11:27 +00:00
srt = [],
total = 0,
importButton,
selectLayer,
selectFile,
that = Ox.Dialog({
2012-02-14 18:48:08 +00:00
buttons: [
Ox.Button({
id: 'close',
2013-05-09 10:13:58 +00:00
title: Ox._('Close')
2012-02-14 18:48:08 +00:00
}).bindEvent({
click: function() {
that.close();
}
2012-02-16 13:11:27 +00:00
}),
importButton = Ox.Button({
disabled: true,
id: 'import',
2013-05-09 10:13:58 +00:00
title: Ox._('Import')
2012-02-16 13:11:27 +00:00
}).bindEvent({
click: function() {
importButton.hide();
addAnnotations();
2012-02-16 13:11:27 +00:00
}
2012-02-14 18:48:08 +00:00
})
],
closeButton: true,
content: content,
2012-02-14 18:48:08 +00:00
keys: {
'escape': 'close'
},
height: 128,
2012-02-14 19:01:30 +00:00
removeOnClose: true,
width: 368,
2013-05-09 10:13:58 +00:00
title: Ox._('Import Annotations')
2012-02-14 18:48:08 +00:00
})
.bindEvent({
close: function(data) {
2012-02-16 13:11:27 +00:00
that.close();
2012-02-14 18:48:08 +00:00
}
2012-02-16 13:11:27 +00:00
}),
$status = $('<div>').css({
padding: '4px',
paddingBottom: '8px'
}).appendTo(content);
2012-02-16 13:11:27 +00:00
2013-05-09 10:13:58 +00:00
setStatus(Ox._('Please select layer and .srt file'))
function addAnnotations() {
var annotations, task;
2012-06-10 18:31:02 +00:00
if (srt.length > 0) {
2013-05-09 10:13:58 +00:00
setStatus(Ox._('Loading...'));
var annotations = srt.filter(function(data) {
return !Ox.isUndefined(data['in']) && !Ox.isUndefined(data.out) && data.text;
}).map(function(data) {
return {
'in': data['in'],
out: data.out,
value: Ox.sanitizeHTML(data.text)
.replace(/<br[ /]*?>\n/g, '\n')
.replace(/\n\n/g, '<br>\n')
.replace(/\n/g, '<br>\n')
};
});
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) {
$status.html('').append(Ox.LoadingScreen({
2013-05-09 10:13:58 +00:00
text: Ox._('Importing {0} annotations...', [srt.length])
2013-11-20 08:12:59 +00:00
}).start());
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 {
2013-05-09 10:13:58 +00:00
setStatus(Ox._('Importing annotations failed.'));
}
});
2012-02-16 13:11:27 +00:00
} else {
2013-05-09 10:13:58 +00:00
setStatus(Ox._('Importing annotations failed.'));
2012-02-16 13:11:27 +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);
}
2012-02-16 13:11:27 +00:00
selectLayer = Ox.Select({
items: layers,
2013-05-09 10:13:58 +00:00
title: Ox._('Select Layer'),
})
.css({
margin: '4px 2px 4px 4px'
2012-02-14 18:48:08 +00:00
})
.bindEvent({
change: function(data) {
2013-01-12 06:55:20 +00:00
selectLayer.options({
title: null
});
2012-02-16 13:11:27 +00:00
layer = data.value;
2013-01-12 06:55:20 +00:00
total && layer && importButton.options({disabled: false});
2012-02-16 13:11:27 +00:00
}
})
.appendTo(content);
selectFile = Ox.FileButton({
image: 'upload',
lbael: 'File',
2013-05-09 10:13:58 +00:00
title: Ox._('Select SRT...'),
width: 156
2012-02-16 13:11:27 +00:00
})
.css({
margin: '4px 2px 4px 4px'
2012-02-16 13:11:27 +00:00
})
.bindEvent({
click: function(data) {
2013-07-14 17:44:30 +00:00
if (data.files.length) {
2012-02-16 13:11:27 +00:00
var reader = new FileReader();
reader.onloadend = function(event) {
srt = parseSRT(this.result);
2012-02-16 13:11:27 +00:00
total = srt.length;
2013-07-14 17:44:30 +00:00
if (total && layer) {
importButton.options({disabled: false});
selectLayer.hide();
selectFile.hide();
}
setStatus(
2013-05-09 10:13:58 +00:00
Ox._('File contains {0} annotation'
+ (total == 1 ? '' : 's') + '.', [total])
2012-06-10 18:31:02 +00:00
);
2012-02-16 13:11:27 +00:00
};
reader.readAsText(data.files[0]);
2012-02-16 13:11:27 +00:00
} else {
srt = [];
total = 0;
importButton.options({
disabled: true
});
}
2012-02-14 18:48:08 +00:00
}
})
.appendTo(content);
2012-02-14 18:48:08 +00:00
return that;
};