pandora/static/js/importMediaDialog.js

219 lines
7 KiB
JavaScript
Raw Permalink Normal View History

2016-07-30 00:49:31 +00:00
'use strict';
pandora.ui.importMediaDialog = function(options) {
2016-08-09 12:16:58 +00:00
2016-08-09 13:29:35 +00:00
var help = Ox._('You can import videos from external sites, like YouTube or Vimeo.'),
2016-08-09 12:16:58 +00:00
2016-07-30 00:49:31 +00:00
$content = Ox.Element().css({margin: '16px'}),
2016-08-09 12:16:58 +00:00
$button = Ox.Button({
2016-08-09 13:29:35 +00:00
overlap: 'left',
2016-08-09 12:16:58 +00:00
title: Ox._('Preview'),
width: 128
2016-07-30 00:49:31 +00:00
})
2016-08-09 13:29:35 +00:00
.css({
marginLeft: '-20px',
paddingLeft: '20px',
position: 'absolute',
right: '16px',
top: '16px'
})
2016-07-30 00:49:31 +00:00
.bindEvent({
2016-08-09 12:16:58 +00:00
click: submitURL
2016-08-09 13:29:35 +00:00
})
.appendTo($content),
2016-08-09 12:16:58 +00:00
2016-08-09 13:29:35 +00:00
$input = Ox.Input({
label: Ox._('URL'),
labelWidth: 64,
width: 384
})
.css({
left: '16px',
position: 'absolute',
top: '16px'
})
.bindEvent({
change: submitURL
2016-07-30 00:49:31 +00:00
})
.appendTo($content),
2016-08-09 12:16:58 +00:00
2016-07-30 00:49:31 +00:00
$info = Ox.Element()
2016-08-09 13:29:35 +00:00
.css({
left: '16px',
position: 'absolute',
top: '48px'
})
2016-08-09 12:16:58 +00:00
.html(help)
2016-07-30 00:49:31 +00:00
.appendTo($content),
2016-08-09 12:16:58 +00:00
2016-08-09 13:29:35 +00:00
$loading = Ox.LoadingScreen({
width: 512,
height: 224
}),
2016-07-30 00:49:31 +00:00
that = Ox.Dialog({
buttons: [
Ox.Button({
2016-08-09 12:16:58 +00:00
id: 'close',
title: Ox._('Close')
2016-07-30 00:49:31 +00:00
})
.bindEvent({
click: function() {
that.close();
}
}),
Ox.Button({
disabled: true,
id: 'import',
2016-08-09 12:16:58 +00:00
title: Ox._('Import Video')
2016-07-30 00:49:31 +00:00
}).bindEvent({
click: importMedia
})
],
content: $content,
fixedSize: true,
2016-08-09 12:16:58 +00:00
height: 288,
2016-07-30 00:49:31 +00:00
keys: {
2016-08-09 12:16:58 +00:00
escape: 'close'
2016-07-30 00:49:31 +00:00
},
removeOnClose: true,
2016-08-09 13:29:35 +00:00
title: Ox._('Import Video'),
2016-08-09 12:16:58 +00:00
width: 544
2016-07-30 00:49:31 +00:00
});
2019-01-28 07:05:16 +00:00
function getMediaUrlInfo(url, callback) {
2016-07-30 00:49:31 +00:00
pandora.api.getMediaUrlInfo({url: url}, function(result) {
// FIXME: support playlists / multiple items
var info = result.data.items[0];
var infoKeys = [
'date', 'description', 'id', 'tags',
'title', 'uploader', 'url'
];
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 {
infoKeys.forEach(function(infoKey) {
var infoValue = info[infoKey] || '';
if (key == 'year' && infoKey == 'date') {
infoValue = infoValue.substr(0, 4);
}
if (infoKey == 'tags') {
infoValue = infoValue.join(', ');
2016-08-09 11:17:48 +00:00
}
value = value.replace(
new RegExp('\{' + infoKey + '\}', 'g'), infoValue
);
});
// For example: director -> uploader
if (isArray) {
value = [value];
2016-08-09 11:17:48 +00:00
}
}
return value;
});
2019-01-28 07:05:16 +00:00
callback(values, info)
});
}
function addMedia(url, callback) {
getMediaUrlInfo(url, function(values, info) {
pandora.api.add({title: values.title || info.title}, function(result) {
var edit = Ox.extend(
Ox.filter(values, function(value, key) {
return key != 'title';
}),
{'id': result.data.id}
);
pandora.api.edit(edit, function(result) {
pandora.api.addMediaUrl({
url: info.url,
item: edit.id
}, function(result) {
if (result.data.taskId) {
pandora.wait(result.data.taskId, function(result) {
2016-07-30 00:49:31 +00:00
callback(edit.id);
});
} else {
callback(edit.id);
}
2016-07-30 00:49:31 +00:00
});
});
});
});
};
2016-08-09 12:16:58 +00:00
function getInfo(url, callback) {
pandora.api.getMediaUrlInfo({url: url}, function(result) {
callback(result.data.items);
});
}
2016-07-30 00:49:31 +00:00
function importMedia() {
2016-08-09 12:25:14 +00:00
var url = $input.value();
2016-08-09 13:29:35 +00:00
$input.options({disabled: true});
$button.options({disabled: true});
$info.empty().append($loading.start());
2016-08-09 12:16:58 +00:00
that.disableButton('close');
2016-08-09 13:29:35 +00:00
that.disableButton('import');
2016-07-30 00:49:31 +00:00
addMedia(url, function(item) {
if (item) {
that.close();
Ox.Request.clearCache();
2016-08-09 12:16:58 +00:00
pandora.URL.push('/' + item + '/media');
2016-07-30 00:49:31 +00:00
} else {
2016-08-09 13:29:35 +00:00
$input.options({disabled: false});
$button.options({disabled: false});
$info.empty().html(Ox._('Import failed. Please try again'));
2016-08-09 12:16:58 +00:00
that.enableButton('close');
2016-07-30 00:49:31 +00:00
}
});
}
2016-08-09 12:16:58 +00:00
function submitURL() {
var value = $input.value();
if (value) {
2016-08-09 13:29:35 +00:00
$input.options({disabled: true});
$button.options({disabled: true});
$info.empty().append($loading.start());
that.disableButton('close');
2016-08-09 12:16:58 +00:00
getInfo(value, function(items) {
2016-08-09 13:29:35 +00:00
$input.options({disabled: false});
$button.options({disabled: false});
$loading.stop();
2016-08-09 12:16:58 +00:00
$info.empty();
if (items.length) {
// FIXME: support playlists / multiple items
var info = items[0];
info.thumbnail && $info.append($('<img>').css({
2016-08-09 12:16:58 +00:00
position: 'absolute',
2016-08-09 12:22:46 +00:00
width: '248px'
2016-08-09 12:16:58 +00:00
}).attr('src', info.thumbnail));
$info.append($('<div>').addClass('OxText').css({
height: '192px',
overflow: 'hidden',
position: 'absolute',
2016-08-09 13:29:35 +00:00
left: '264px',
2016-08-09 12:16:58 +00:00
textOverflow: 'ellipsis',
2016-08-09 12:22:46 +00:00
width: '248px'
2016-08-09 12:16:58 +00:00
}).html(
'<span style="font-weight: bold">' + info.title
+ '</span><br/><br/>' + (info.description || '')
2016-08-09 12:16:58 +00:00
));
that.enableButton('import');
} else {
$info.html(help);
}
});
2016-08-09 13:29:35 +00:00
that.enableButton('close');
2016-08-09 12:16:58 +00:00
}
}
2016-07-30 00:49:31 +00:00
return that;
2016-08-09 12:16:58 +00:00
2016-07-30 00:49:31 +00:00
};