pandora/static/js/addItemDialog.js

274 lines
10 KiB
JavaScript
Raw Normal View History

2016-09-15 15:20:24 +00:00
'use strict';
pandora.ui.addItemDialog = function(options) {
options = options || {};
var input = '';
2017-03-01 11:22:00 +00:00
var selected = options.selected ? options.selected
: !pandora.site.itemRequiresVideo && !pandora.user.ui.item ? 'add' : 'upload';
2016-09-15 15:20:24 +00:00
var $button;
var $panel = Ox.TabPanel({
content: function(id) {
var $content = Ox.Element().css({padding: '8px'});
var $input = Ox.Input({
changeOnKeypress: true,
disabled: id == 'upload',
label: Ox._(id == 'add' ? 'Title' : id == 'upload' ? 'File': 'URL'),
labelWidth: 64,
placeholder: id == 'import' ? Ox._('YouTube, Vimeo, etc.') : '',
width: 512
}).css({
margin: '8px'
}).bindEvent({
change: function(data) {
$button.options({disabled: !data.value});
input = data.value;
}
}).appendTo($content);
return $content;
},
tabs: [
{
id: 'add',
title: Ox._('Add {0}', [pandora.site.itemName.singular]),
disabled: pandora.site.itemRequiresVideo,
selected: selected == 'add'
},
{
id: 'upload',
title: Ox._('Upload Video Files'),
selected: selected == 'upload'
},
{
id: 'import',
title: Ox._('Import Video Files'),
2017-11-04 09:53:27 +00:00
disabled: !pandora.hasCapability('canImportItems'),
2016-09-15 15:20:24 +00:00
selected: selected == 'import'
}
]
}).bindEvent({
change: function(data) {
selected = data.selected;
that.options({buttons: [createButton()]});
}
});
var $screen = Ox.LoadingScreen({
size: 16
});
var that = Ox.Dialog({
buttons: [createButton()],
closeButton: true,
content: $panel,
height: 72,
removeOnClose: true,
title: Ox._('Add {0}', [pandora.site.itemName.singular]),
width: 544
});
function createButton() {
$button = Ox[selected == 'upload' ? 'FileButton' : 'Button']({
disabled: selected != 'upload',
id: selected,
title: selected == 'add'
? Ox._('Add {0} Without Video Files', [pandora.site.itemName.singular])
: selected == 'upload' ? Ox._('Select Video Files')
: Ox._('Import Video Files'),
width: selected == 'add' ? 192 : 128
}).bindEvent({
click: function(data) {
if (selected == 'add') {
that.options({content: $screen.start()});
$button.options({disabled: true});
pandora.api.add({title: input}, function(result) {
Ox.Request.clearCache('find');
$screen.stop();
that.close();
pandora.UI.set({
item: result.data.id,
itemView: 'info'
});
});
} else if (selected == 'upload' && data.files.length > 0) {
that.options({content: $screen.start()});
$button.options({disabled: true});
Ox.serialMap(data.files, function(file, index, files, callback) {
getFileInfo(file, function(info) {
callback(Ox.extend(info, {file: file}));
});
}, onInfo);
} else {
that.options({content: $screen.start()});
$button.options({disabled: true});
pandora.api.getMediaUrlInfo({
url: input
}, function(result) {
if (requslt.data.items.length) {
onInfo(result.data.items.map(getVideoInfo));
} else {
$screen.stop();
that.close();
var error = Ox.Dialog({
buttons: [
{},
Ox.Button({
id: 'close',
title: Ox._('Close')
}).bindEvent({
click: function() {
error.close();
}
})
],
closeButton: true,
content: Ox.Element().css({padding: '32px'}).html('Import failed'),
height: 96,
keys: {escape: 'close'},
removeOnClose: true,
title: Ox._('Import Error'),
width: 192
}).open()
}
2016-09-15 15:20:24 +00:00
});
}
}
});
return $button;
}
function getFileInfo(file, callback) {
2016-09-19 21:12:37 +00:00
var done = false;
2016-09-15 15:20:24 +00:00
Ox.oshash(file, function(oshash) {
var $video = $('<video>'),
url = URL.createObjectURL(file),
info = {
audio: [],
direct: false,
oshash: oshash,
name: file.name,
size: file.size,
title: file.name.split('.').slice(0, -1).join('.'),
video: []
};
$video.one('error', function(event) {
2016-09-19 21:12:37 +00:00
if (!done) {
done = true;
2016-09-23 21:43:28 +00:00
$video[0].src = ''
2016-09-19 21:12:37 +00:00
URL.revokeObjectURL(url);
callback(info);
}
2016-09-15 15:20:24 +00:00
});
$video.one('loadedmetadata', function(event) {
2016-09-19 21:12:37 +00:00
if (!done) {
done = true;
info.duration = $video[0].duration;
if ($video[0].videoHeight > 0) {
info.width = $video[0].videoWidth;
info.height = $video[0].videoHeight;
}
if (info.duration) {
info.bitrate = info.size * 8 / info.duration / 1000;
}
2016-09-23 21:43:28 +00:00
$video[0].src = ''
2016-09-19 21:12:37 +00:00
URL.revokeObjectURL(url);
callback(info);
2016-09-15 15:20:24 +00:00
}
});
2016-09-23 20:59:23 +00:00
$video[0].muted = true;
2016-09-15 15:20:24 +00:00
$video[0].src = url;
});
}
function getVideoInfo(info) {
var values = Ox.map(pandora.site.importMetadata, function(value, key) {
var isArray = Ox.isArray(
Ox.getObjectById(pandora.site.itemKeys, key).type
);
if (isArray && value == '{tags}') {
value = info.tags;
} else {
[
'date', 'description', 'id', 'tags',
'title', 'uploader', 'url'
].forEach(function(infoKey) {
var infoValue = info[infoKey] || '';
if (key == 'year' && infoKey == 'date') {
infoValue = infoValue.substr(0, 4);
}
if (infoKey == 'tags') {
infoValue = infoValue.join(', ');
}
value = value.replace(
new RegExp('\{' + infoKey + '\}', 'g'), infoValue
);
});
// For example: director -> uploader
if (isArray) {
value = [value];
}
}
return value;
});
2016-09-19 17:56:42 +00:00
values.url= info.url;
return Ox.extend(info, values);
2016-09-15 15:20:24 +00:00
}
function onInfo(items) {
// FIXME: what about pending/aborted uploads
pandora.api.findMedia({
keys: ['id', 'item', 'url'],
query: {
conditions: selected == 'upload' ? items.map(function(item) {
return {key: 'oshash', operator: '==', value: item.oshash};
}) : items.map(function(item) {
return {key: 'url', operator: '==', value: item.url};
}),
operator: '|'
}
}, function(result) {
if (result.data.items.length) {
Ox.serialMap(result.data.items, function(item, index, items, callback) {
pandora.api.get({
id: item.item,
keys: ['id', 'title']
}, function(result) {
callback(Ox.extend(item, {
itemID: result.data.id,
itemTitle: result.data.title
}));
})
}, function(media) {
media = media.map(function(media) {
return Ox.extend(media, Ox.getObject(
items,
selected == 'upload' ? 'oshash' : 'url',
media[selected == 'upload' ? 'id' : 'url']
));
});
$screen.stop();
that.close();
pandora.ui.mediaExistsDialog({
action: selected,
media: media,
items: items
}).open();
});
} else {
$screen.stop();
that.close();
pandora.ui.addFilesDialog({
action: selected,
items: items
}).open();
}
})
}
return that;
};