From 3f2536dc75e8d8c183d64b68b1ba8ca49fbc030b Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 16 Feb 2012 18:41:27 +0530 Subject: [PATCH] refactor import annotations dialog --- static/js/pandora/embedDialog.js | 1 - static/js/pandora/importAnnotations.js | 156 ++++++++++++++++--------- 2 files changed, 100 insertions(+), 57 deletions(-) diff --git a/static/js/pandora/embedDialog.js b/static/js/pandora/embedDialog.js index b63a088fa..e8c74bfe8 100644 --- a/static/js/pandora/embedDialog.js +++ b/static/js/pandora/embedDialog.js @@ -22,7 +22,6 @@ pandora.ui.embedDialog = function(data) { keys: { 'escape': 'close' }, - maximizeButton: true, removeOnClose: true, title: 'Embed Video', width: 600 diff --git a/static/js/pandora/importAnnotations.js b/static/js/pandora/importAnnotations.js index 32b0341a4..5b0ec9fbf 100644 --- a/static/js/pandora/importAnnotations.js +++ b/static/js/pandora/importAnnotations.js @@ -4,8 +4,17 @@ pandora.ui.importAnnotations = function(data) { var content = Ox.Element().css({margin: '16px'}), file, - height = 180, - width = 640, + height = 192, + layers = pandora.site.layers.filter(function(layer) { + return layer.canAddAnnotations[pandora.user.level]; + }), + layer = layers[0].id, + width = 384, + srt = [], + total = 0, + importButton, + selectLayer, + selectFile, that = Ox.Dialog({ buttons: [ Ox.Button({ @@ -15,6 +24,18 @@ pandora.ui.importAnnotations = function(data) { click: function() { that.close(); } + }), + importButton = Ox.Button({ + disabled: true, + id: 'import', + title: 'Import', + }).bindEvent({ + click: function() { + importButton.hide(); + selectLayer.hide(); + selectFile.hide(); + addAnnotation(); + } }) ], closeButton: true, @@ -22,7 +43,6 @@ pandora.ui.importAnnotations = function(data) { keys: { 'escape': 'close' }, - maximizeButton: true, height: height, removeOnClose: true, width: width, @@ -30,66 +50,90 @@ pandora.ui.importAnnotations = function(data) { }) .bindEvent({ close: function(data) { + that.close(); } + }), + $status = $('
').css({ + padding: '4px' }); - Ox.Select({ - items: Ox.merge([{id: '', title: 'Select Layer'}], pandora.site.layers), - value: '', + + function addAnnotation() { + if(srt.length>0) { + var data = srt.shift(); + data.text = Ox.parseHTML(data.text); + $status.html(Ox.formatDuration(data['in']) + + ' to ' + Ox.formatDuration(data.out) + '
\n' + + data.text); + pandora.api.addAnnotation({ + 'in': data['in'], + out: data.out, + value: data.text, + item: pandora.user.ui.item, + layer: layer + }, function(result) { + if (result.status.code == 200) { + addAnnotation(); + } else { + content.html('Failed'); + } + }); + } else { + $status.html(total + ' annotations imported.'); + Ox.Request.clearCache(pandora.user.ui.item); + pandora.$ui.contentPanel.replaceElement( + 1, pandora.$ui.item = pandora.ui.item() + ); + } + } + content.append($('
').css({ + padding: '4px', + paddingBottom: '16px' + }).html('Import annotations from .srt file:')); + selectLayer = Ox.Select({ + items: layers, + value: layer, + label: 'Layer', }) .bindEvent({ change: function(data) { - var layer = data.value; - $('') - .attr({ - type: 'file' - }) - .css({ - padding: '8px' - }) - .bind({ - change: function(event) { - file = this.files[0]; - var reader = new FileReader(); - reader.onloadend = function(event) { - var srt = Ox.parseSRT(this.result), - total = srt.length; - function addAnnotation() { - if(srt.length>0) { - var data = srt.shift(); - data.text = Ox.parseHTML(data.text); - content.html('Importing '+total+' '+ layer +':
\n' - + Ox.formatDuration(data['in']) - + ' to ' + Ox.formatDuration(data.out) + '
\n' - + data.text); - pandora.api.addAnnotation({ - 'in': data['in'], - out: data.out, - value: data.text, - item: pandora.user.ui.item, - layer: layer - }, function(result) { - if (result.status.code == 200) { - addAnnotation(); - } else { - content.html('Failed'); - } - }); - } else { - content.html('Done'); - Ox.Request.clearCache(pandora.user.ui.item); - pandora.$ui.contentPanel.replaceElement( - 1, pandora.$ui.item = pandora.ui.item() - ); - } - } - addAnnotation(); - }; - reader.readAsText(file); - } - }) - .appendTo(content); + layer = data.value; } }) .appendTo(content); + + selectFile = $('') + .attr({ + type: 'file' + }) + .css({ + padding: '8px' + }) + .bind({ + change: function(event) { + if(this.files.length) { + file = this.files[0]; + var reader = new FileReader(); + reader.onloadend = function(event) { + srt = Ox.parseSRT(this.result); + total = srt.length; + if(total) { + importButton.options({ + disabled: false + }); + } + $status.html('File contains '+ total + ' annotations.'); + }; + reader.readAsText(file); + } else { + srt = []; + total = 0; + importButton.options({ + disabled: true + }); + } + } + }) + .appendTo(content); + content.append($status); return that; };