pandora/static/js/pandora/importAnnotations.js

179 lines
5.7 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',
title: 'Close'
}).bindEvent({
click: function() {
that.close();
}
2012-02-16 13:11:27 +00:00
}),
importButton = Ox.Button({
disabled: true,
id: 'import',
2012-05-26 15:46:24 +00:00
title: '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,
2012-05-26 15:46:24 +00:00
title: '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
setStatus('Please select layer and .srt file')
function addAnnotations() {
var annotations, task;
2012-06-10 18:31:02 +00:00
if (srt.length > 0) {
setStatus('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({
text: 'Importing ' + srt.length + ' annotations...'
}));
pandora.wait(result.data.taskId, function(result) {
if(result.data.status == 'SUCCESS') {
setStatus(annotations.length + ' annotations imported.');
Ox.Request.clearCache(pandora.user.ui.item);
pandora.$ui.contentPanel.replaceElement(
1, pandora.$ui.item = pandora.ui.item()
);
} else {
setStatus('Importing annotations failed.');
}
});
2012-02-16 13:11:27 +00:00
} else {
setStatus('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,
title: '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',
title: '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) {
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;
if(total && layer) {
importButton.options({disabled: false});
selectLayer.hide();
selectFile.hide();
}
setStatus(
2012-06-10 18:31:02 +00:00
'File contains ' + total + ' annotation'
+ (total == 1 ? '' : 's') + '.'
);
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;
};