pandora/static/js/importMediaDialog.js

189 lines
6 KiB
JavaScript
Raw 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
var help = Ox._('Enter a URL from an external site (like YouTube or Vimeo).'),
2016-07-30 00:49:31 +00:00
$content = Ox.Element().css({margin: '16px'}),
2016-08-09 12:16:58 +00:00
$input = Ox.Input({
label: Ox._('URL'),
labelWidth: 128,
2016-07-30 00:49:31 +00:00
width: 384
})
2016-08-09 12:16:58 +00:00
.bindEvent({
change: submitURL
})
$button = Ox.Button({
title: Ox._('Preview'),
width: 128
2016-07-30 00:49:31 +00:00
})
.bindEvent({
2016-08-09 12:16:58 +00:00
click: submitURL
}),
$form = Ox.FormElementGroup({
elements: [$input, $button]
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 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-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
})
],
closeButton: true,
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,
title: Ox._('Import Media'),
2016-08-09 12:16:58 +00:00
width: 544
2016-07-30 00:49:31 +00:00
});
function addMedia(url, callback) {
pandora.api.getMediaUrlInfo({url: url}, function(result) {
result.data.items.forEach(function(info) {
2016-08-09 11:17:48 +00:00
var infoKeys = [
2016-08-09 12:16:58 +00:00
'date', 'description', 'id', 'tags',
'title', 'uploader', 'url'
2016-08-09 11:17:48 +00:00
];
var values = Ox.map(pandora.site.importMetadata, function(value, key) {
2016-08-09 11:26:51 +00:00
var type = Ox.getObjectById(pandora.site.itemKeys, key).type;
2016-08-09 11:17:48 +00:00
infoKeys.forEach(function(infoKey) {
2016-08-09 11:30:31 +00:00
var infoValue = info[infoKey];
2016-08-09 11:28:51 +00:00
if (key == 'year' && infoKey == 'date') {
2016-08-09 11:17:48 +00:00
infoValue = infoValue.substr(0, 4);
}
if (infoKey == 'tags' && Ox.isArray(type)) {
infoValue = infoValue.join(', ');
}
value = value.replace(
2016-08-09 11:21:00 +00:00
new RegExp('\{' + infoKey + '\}', 'g'), infoValue
2016-08-09 11:17:48 +00:00
);
});
if (Ox.isArray(type)) {
value = [value];
}
return value;
});
pandora.api.add({title: values.title || info.title}, function(result) {
var edit = Ox.extend(
Ox.filter(values, function(value, key) {
2016-08-09 11:21:00 +00:00
return key != 'title';
2016-08-09 11:17:48 +00:00
}),
{'id': result.data.id}
);
2016-07-30 00:49:31 +00:00
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) {
callback(edit.id);
});
} else {
callback(edit.id);
}
});
});
});
});
});
};
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() {
var url = $url.value();
$info.empty();
$info.append(loadingIcon());
that.disableButton('import');
2016-08-09 12:16:58 +00:00
that.disableButton('close');
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 12:16:58 +00:00
$info.empty().html('Import failed.');
that.enableButton('close');
2016-07-30 00:49:31 +00:00
}
});
}
function loadingIcon() {
return Ox.LoadingIcon().css({
margin: 'auto',
marginTop: '64px',
width: '100%'
}).start();
}
2016-08-09 12:16:58 +00:00
function submitURL() {
var value = $input.value();
if (value) {
$info.empty();
$info.append(loadingIcon());
getInfo(value, function(items) {
$info.empty();
if (items.length) {
// FIXME: support playlists / multiple items
var info = items[0];
$info.append($('<img>').css({
position: 'absolute',
left: '16px',
top: '48px',
width: '128px'
}).attr('src', info.thumbnail));
$info.append($('<div>').addClass('OxText').css({
height: '192px',
overflow: 'hidden',
position: 'absolute',
right: '16px',
textOverflow: 'ellipsis',
top: '48px',
width: '240px'
}).html(
'<span style="font-weight: bold">' + info.title
+ '</span><br/><br/>' + info.description
));
that.enableButton('import');
} else {
$info.html(help);
}
});
}
}
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
};