pandora/static/js/metadataDialog.js

387 lines
13 KiB
JavaScript
Raw Normal View History

'use strict';
pandora.ui.metadataDialog = function(data) {
var keys = [
2013-02-26 07:51:21 +00:00
'title', 'alternativeTitles', 'director',
2013-02-26 15:54:58 +00:00
'country', 'year', 'language', 'runtime', 'color', 'sound',
2013-02-25 16:36:38 +00:00
'productionCompany',
2013-02-26 07:51:21 +00:00
'producer', 'writer', 'cinematographer', 'editor', 'composer', 'actor',
2018-07-09 13:21:32 +00:00
'lyricist', 'singer',
2013-02-26 15:54:58 +00:00
'genre', 'keyword', 'summary'
],
2017-03-03 11:16:34 +00:00
mapKeys = {
'keyword': 'topic'
},
updateKeys,
dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
dialogWidth = Math.round(window.innerWidth * 0.9),
2013-02-25 10:18:58 +00:00
formWidth = getFormWidth(),
imdb,
2013-02-25 10:18:58 +00:00
$confirmDialog,
2013-02-27 12:58:45 +00:00
$selectAllButton,
$selectNoneButton,
2013-02-25 10:18:58 +00:00
$label = {},
$input = {},
2013-02-25 10:18:58 +00:00
that = data.imdbId ? updateDialog() : idDialog();
keys = keys.filter(function(key) {
2018-08-09 18:54:06 +00:00
return key == 'alternativeTitles' || getItemKey(key);
});
2013-02-25 10:18:58 +00:00
data.imdbId && getMetadata();
function idDialog() {
2013-03-03 05:34:44 +00:00
return pandora.ui.iconDialog({
2013-02-25 10:18:58 +00:00
buttons: [
Ox.Button({
id: 'close',
2013-05-09 10:13:58 +00:00
title: Ox._('Not Now')
2013-02-25 10:18:58 +00:00
})
.bindEvent({
click: function() {
that.close();
}
2013-02-28 06:28:28 +00:00
}),
Ox.Button({
distabled: true,
id: 'update',
2013-05-09 10:13:58 +00:00
title: Ox._('Update IMDb ID...')
2013-02-28 06:28:28 +00:00
})
.bindEvent({
click: function() {
that.close();
pandora.$ui.idDialog = pandora.ui.idDialog(data).open();
}
2013-02-25 10:18:58 +00:00
})
],
content: Ox._(
'To update the metadata for this {0}, please enter its IMDb ID.',
[pandora.site.itemName.singular.toLowerCase()]
),
2013-02-28 06:28:28 +00:00
keyboard: {enter: 'update', escape: 'close'},
2013-05-09 10:13:58 +00:00
title: Ox._('Update Metadata')
2013-02-25 10:18:58 +00:00
});
}
2018-05-09 09:28:09 +00:00
2013-02-25 10:18:58 +00:00
function updateDialog() {
return Ox.Dialog({
buttons: [
2013-02-28 06:28:28 +00:00
Ox.Button({
id: 'switch',
2013-05-09 10:13:58 +00:00
title: Ox._('Update IMDb ID...')
2013-02-28 06:28:28 +00:00
})
.bindEvent({
click: function() {
that.close();
pandora.$ui.idDialog = pandora.ui.idDialog(data).open();
}
}),
2013-02-28 06:28:28 +00:00
{},
2013-02-25 10:18:58 +00:00
Ox.Button({
id: 'cancel',
2013-05-09 10:13:58 +00:00
title: Ox._('Don\'t Update')
2013-02-25 10:18:58 +00:00
})
.bindEvent({
click: function() {
that.close();
}
}),
Ox.Button({
disabled: true,
id: 'update',
2013-05-09 10:13:58 +00:00
title: Ox._('Update Metadata...')
2013-02-25 10:18:58 +00:00
})
.bindEvent({
click: function() {
$confirmDialog = confirmDialog().open();
}
})
],
closeButton: true,
2013-11-20 08:12:59 +00:00
content: Ox.LoadingScreen().start(),
2013-02-25 10:18:58 +00:00
height: dialogHeight,
maximizeButton: true,
minHeight: 256,
minWidth: 512,
removeOnClose: true,
2013-05-09 10:13:58 +00:00
title: Ox._('Update Metadata'),
2013-02-25 10:18:58 +00:00
width: dialogWidth
})
.bindEvent({
resize: setSize
});
}
function confirmDialog() {
2013-03-03 05:34:44 +00:00
return pandora.ui.iconDialog({
2013-02-25 10:18:58 +00:00
buttons: [
Ox.Button({
id: 'cancel',
2013-05-09 10:13:58 +00:00
title: Ox._('Don\'t Update')
2013-02-25 10:18:58 +00:00
})
.bindEvent({
click: function() {
$confirmDialog.close();
}
}),
Ox.Button({
id: 'update',
2013-05-09 10:13:58 +00:00
title: Ox._('Update')
2013-02-25 10:18:58 +00:00
})
.bindEvent({
click: function() {
$confirmDialog.close();
updateMetadata();
}
})
],
content: Ox._('Are you sure you want to update the value'
2013-03-03 05:34:44 +00:00
+ (updateKeys.length == 1 ? '' : 's')
2013-05-09 10:13:58 +00:00
+ ' for {0}?', [updateKeys.map(function(key, index) {
2013-03-03 05:34:44 +00:00
return (
index == 0 ? ''
: index < updateKeys.length - 1 ? ', '
2013-05-09 10:13:58 +00:00
: ' ' + Ox._('and') + ' '
2013-03-03 05:34:44 +00:00
) + getTitle(key)
}).join('')]
),
height: 192,
keyboard: {enter: 'update', escape: 'cancel'},
2013-05-09 10:13:58 +00:00
title: Ox._('Update Metadata')
2013-02-25 10:18:58 +00:00
});
}
2013-02-28 08:40:35 +00:00
function formatValue(key, value) {
return !value ? ''
: key == 'alternativeTitles' ? value.map(function(v) {
return v[0];
}).join('; ')
: key == 'runtime' ? Math.round(value / 60) + ' min'
: key == 'productionCompany' ? value.join('; ')
2013-02-28 08:40:35 +00:00
: Ox.isArray(
2017-03-03 11:16:34 +00:00
getItemKey(key).type
2013-02-28 08:40:35 +00:00
) ? value.join(', ')
: value;
}
function getFormWidth() {
2014-09-26 12:12:25 +00:00
return dialogWidth - 32 - Ox.UI.SCROLLBAR_SIZE;
2013-02-28 08:40:35 +00:00
}
2017-03-03 11:16:34 +00:00
function getItemKey(key) {
return Ox.getObjectById(pandora.site.itemKeys, getKey(key));
}
function getKey(key) {
return mapKeys[key] || key;
}
2013-02-28 08:40:35 +00:00
function getMetadata() {
2015-04-20 08:13:56 +00:00
pandora.api.getMetadata({
id: data.imdbId,
keys: keys.concat(['originalTitle'])
}, function(result) {
2013-02-27 12:58:45 +00:00
var $bar = Ox.Bar({size: 24}),
2013-02-28 08:40:35 +00:00
$data = Ox.Element()
.css({padding: '13px', overflowY: 'auto'}),
2013-02-27 12:58:45 +00:00
$content = Ox.SplitPanel({
elements: [
{element: $bar, size: 24},
{element: $data}
],
orientation: 'vertical'
});
$selectNoneButton = Ox.Button({
2013-05-09 10:13:58 +00:00
title: Ox._('Select No Updates'),
2013-02-27 12:58:45 +00:00
})
.css({float: 'left', margin: '4px 2px 4px 4px'})
.bindEvent({
click: function() {
selectAll(0)
}
})
.appendTo($bar),
$selectAllButton = Ox.Button({
2013-05-09 10:13:58 +00:00
title: Ox._('Select All Updates'),
2013-02-27 12:58:45 +00:00
})
.css({float: 'left', margin: '4px 2px 4px 2px'})
.bindEvent({
click: function() {
selectAll(1);
}
})
.appendTo($bar);
if (result.data) {
2013-02-27 12:58:45 +00:00
imdb = Ox.clone(result.data, true);
2013-02-26 16:24:52 +00:00
if (imdb.originalTitle) {
2013-02-26 16:35:02 +00:00
imdb.alternativeTitles = [[imdb.title, []]].concat(imdb.alternativeTitles || []);
2013-02-26 16:24:52 +00:00
imdb.title = imdb.originalTitle;
}
keys.forEach(function(key, index) {
2018-05-09 09:28:09 +00:00
var isEqual = Ox.isEqual(data[getKey(key)], imdb[key]) || (
isEmpty(data[getKey(key)]) && isEmpty(imdb[key])
2013-02-27 12:58:45 +00:00
),
checked = isEqual ? [true, true]
2018-05-09 09:28:09 +00:00
: isEmpty(data[getKey(key)]) && !isEmpty(imdb[key]) ? [false, true]
2013-02-28 05:08:20 +00:00
: [true, false];
if (index > 0) {
$('<div>')
.css({width: '1px', height: '8px'})
2013-02-27 12:58:45 +00:00
.appendTo($data);
}
2013-02-25 10:18:58 +00:00
$label[key] = Ox.Label({
title: getTitle(key),
width: formWidth
})
2015-04-20 08:13:56 +00:00
.css({
display: 'inline-block',
margin: '3px 3px 5px 3px'
})
2013-02-27 12:58:45 +00:00
.appendTo($data);
2018-05-09 09:28:09 +00:00
$input[key] = [data[getKey(key)], imdb[key]].map(function(v, i) {
return Ox.InputGroup({
inputs: [
Ox.Checkbox({
disabled: isEqual,
value: checked[i],
width: 16
})
.bindEvent({
2013-02-27 12:58:45 +00:00
change: function() {
toggle(key);
}
}),
Ox.Input({
disabled: true,
2013-02-25 10:18:58 +00:00
value: formatValue(key, v),
width: formWidth - 80
})
.bindEvent({
change: function() {
// on change, revert to initial value
// (if above you remove disabled: true)
$input[key][i].options({value: [
$input[key][i].options('value')[0],
2013-02-25 10:18:58 +00:00
formatValue(key, v)
]});
}
})
],
separators: [
2015-04-20 08:13:56 +00:00
{
title: [Ox._('Current'), Ox._('Update')][i],
width: 64
}
]
})
.css({display: 'inline-block', margin: '3px'})
2013-02-27 12:58:45 +00:00
.appendTo($data);
});
});
2013-02-28 08:40:35 +00:00
that.options({content: $content});
updateKeys = getUpdateKeys();
2013-02-27 12:58:45 +00:00
updateButtons();
} else {
// ...
}
});
}
2013-02-25 10:18:58 +00:00
function getTitle(key) {
2013-07-02 11:18:48 +00:00
return Ox._(
key == 'alternativeTitles' ? 'Alternative Titles'
2017-03-03 11:16:34 +00:00
: getItemKey(key).title
2013-05-09 10:13:58 +00:00
);
}
function getUpdateKeys() {
return keys.filter(function(key) {
2013-02-25 16:36:38 +00:00
return $input[key][0].options('inputs')[0].options('value') === false;
});
}
function isEmpty(value) {
return Ox.isEmpty(value) || Ox.isUndefined(value);
}
2013-02-27 12:58:45 +00:00
function selectAll(i) {
keys.forEach(function(key) {
var $checkbox = $input[key][1].options('inputs')[0];
if (!$checkbox.options('disabled')) {
if (
(i == 0 && $checkbox.options('value'))
|| (i == 1 && !$checkbox.options('value'))
) {
toggle(key);
}
}
})
}
2013-02-25 10:18:58 +00:00
function setSize(data) {
dialogHeight = data.height;
dialogWidth = data.width;
formWidth = getFormWidth();
Ox.forEach($input, function($inputs, key) {
$label[key].options({width: formWidth});
$inputs.forEach(function($element) {
$element.options('inputs')[1].options({width: formWidth - 80});
});
});
}
2013-02-27 12:58:45 +00:00
function toggle(key) {
var $checkbox = $input[key][1].options('inputs')[0];
if (!$checkbox.options('disabled')) {
Ox.loop(2, function(i) {
var value = $input[key][i].options('value');
$input[key][i].options({
value: [!value[0], value[1]]
});
})
}
updateKeys = getUpdateKeys();
updateButtons();
}
function updateButtons() {
var checked = [0, 0];
keys.forEach(function(key) {
var $checkbox = $input[key][1].options('inputs')[0];
if (!$checkbox.options('disabled')) {
checked[$checkbox.options('value') ? 1 : 0]++;
}
});
$selectNoneButton.options({disabled: checked[1] == 0});
$selectAllButton.options({disabled: checked[0] == 0})
2013-02-25 10:18:58 +00:00
that[updateKeys.length ? 'enableButton' : 'disableButton']('update');
}
function updateMetadata() {
2013-02-25 18:13:36 +00:00
var edit = {id: data.id}, type;
2013-02-25 10:18:58 +00:00
updateKeys.forEach(function(key) {
2013-02-26 16:35:02 +00:00
type = key == 'alternativeTitles' ? []
2017-03-03 11:16:34 +00:00
: getItemKey(key).type;
edit[getKey(key)] = imdb[key] || (
2013-02-25 16:36:38 +00:00
Ox.isArray(type) ? [] : ''
);
2013-02-25 10:18:58 +00:00
});
2013-11-20 08:12:59 +00:00
that.options({content: Ox.LoadingScreen().start()}).disableButtons();
2013-02-25 10:18:58 +00:00
pandora.api.edit(edit, function(result) {
that.close();
pandora.updateItemContext();
pandora.$ui.contentPanel.replaceElement(1,
pandora.$ui.item = pandora.ui.item()
);
});
}
return that;
2013-02-25 18:13:36 +00:00
};