pandora/static/js/pandora/idDialog.js

253 lines
8.7 KiB
JavaScript
Raw Normal View History

2013-02-28 08:40:43 +00:00
'use strict';
pandora.ui.idDialog = function(data) {
data = Ox.clone(data, true);
var originalData = Ox.clone(data, true),
dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
2013-02-28 08:40:43 +00:00
dialogWidth = Math.round(window.innerWidth * 0.9),
formWidth = getFormWidth(),
$content,
$input = [],
2013-02-28 08:40:43 +00:00
$checkboxGroup,
that = Ox.Dialog({
buttons: [
Ox.Button({
distabled: true,
id: 'switch',
2013-05-09 10:13:58 +00:00
title: Ox._('Update Metadata...')
2013-02-28 08:40:43 +00:00
})
.bindEvent({
click: function() {
that.close();
pandora.$ui.metadataDialog = pandora.ui.metadataDialog(originalData).open();
2013-02-28 08:40:43 +00:00
}
}),
2013-02-28 08:40:43 +00:00
{},
Ox.Button({
id: 'cancel',
2013-05-09 10:13:58 +00:00
title: Ox._('Don\'t Update')
2013-02-28 08:40:43 +00:00
})
.bindEvent({
click: function() {
that.close();
}
}),
Ox.Button({
disabled: true,
id: 'update',
2013-05-09 10:13:58 +00:00
title: Ox._('Update IMDb ID')
2013-02-28 08:40:43 +00:00
})
.bindEvent({
click: updateId
})
],
closeButton: true,
2013-03-08 08:51:35 +00:00
content: Ox.LoadingScreen(),
2013-02-28 08:40:43 +00:00
height: dialogHeight,
maximizeButton: true,
minHeight: 256,
minWidth: 512,
removeOnClose: true,
2013-05-09 10:13:58 +00:00
title: Ox._('Update IMDb ID'),
2013-02-28 08:40:43 +00:00
width: dialogWidth
})
.bindEvent({
resize: setSize
});
getIds();
function getFormWidth() {
return dialogWidth - 32 - Ox.UI.SCROLLBAR_SIZE;
}
function getIds() {
var get = {};
['title', 'director', 'year'].forEach(function(key) {
if (!Ox.isEmpty(data[key])) {
get[key] = data[key];
2013-02-28 08:40:43 +00:00
}
});
pandora.api.getIds(get, function(result) {
var checkboxes = [];
2013-03-02 10:01:50 +00:00
if (!data.imdbId) {
checkboxes.push(
{
id: 'none',
title: getTitle({
id: data.imdbId,
title: data.title,
director: data.director,
year: data.year
})
}
);
}
$content = Ox.Element()
.css({padding: '13px', overflowY: 'auto'});
2013-03-02 10:01:50 +00:00
['title', 'director', 'year'].forEach(function(key) {
$input.push(
Ox.Input({
label: Ox.toTitleCase(key),
labelWidth: 128,
value: Ox.decodeHTMLEntities(key == 'director' && data[key]
2013-03-02 10:01:50 +00:00
? data[key].join(', ')
: data[key]),
2013-03-02 10:01:50 +00:00
width: formWidth
})
.css({display: 'inline-block', margin: '3px'})
.bindEvent({
change: function(data_) {
data[key] = key == 'director'
? data_.value.split(', ')
: data_.value;
2013-03-08 08:51:35 +00:00
that.options({content: Ox.LoadingScreen()});
2013-03-02 10:01:50 +00:00
getIds();
}
})
.appendTo($content)
);
});
$('<div>')
.css({width: '1px', height: '8px'})
.appendTo($content);
(
data.imdbId
&& Ox.getIndexById(result.data.items, data.imdbId) == -1
? pandora.api.findId
: Ox.noop
)({id: data.imdbId}, function(result_) {
if (result_ && result_.data.items.length) {
2013-02-28 08:40:43 +00:00
checkboxes.push(
{
2013-03-02 10:01:50 +00:00
id: data.imdbId,
title: getTitle(result_.data.items[0])
}
2013-03-02 10:01:50 +00:00
)
2013-02-28 08:40:43 +00:00
}
2013-03-02 10:01:50 +00:00
checkboxes = checkboxes.concat(
result.data.items.map(function(item) {
return {
id: item.id,
title: getTitle(item)
};
})
);
$checkboxGroup = Ox.CheckboxGroup({
checkboxes: checkboxes,
type: 'list',
width: formWidth,
value: !data.imdbId ? 'none' : data.imdbId
})
.css({display: 'inline-block', margin: '3px'})
.bindEvent({
change: updateButton
})
.appendTo($content);
var elements = $checkboxGroup.find('.OxCheckbox:not(.OxButton)');
checkboxes.forEach(function(checkbox, index) {
if (checkbox.id != 'none') {
Ox.Button({
title: 'arrowRight',
2013-05-09 10:13:58 +00:00
tooltip: Ox._('View on IMDb'),
2013-03-02 10:01:50 +00:00
type: 'image'
})
.css({
float: 'right',
marginTop: '-18px'
})
.bindEvent({
click: function() {
window.open('/url=' + encodeURIComponent(
'http://imdb.com/title/tt'
+ checkbox.id + '/combined'
), '_blank');
}
})
.appendTo(elements[index]);
2013-02-28 08:40:43 +00:00
}
});
2013-03-02 10:01:50 +00:00
that.options({content: $content});
updateButton();
});
2013-02-28 08:40:43 +00:00
});
}
function getTitle(item) {
2013-03-02 10:01:50 +00:00
item = Ox.clone(item, true);
if (item.id && item.id == data.imdbId) {
item.id = highlightTitle(item.id);
}
if (item.title == data.title) {
item.title = highlightTitle(item.title);
}
if (item.originalTitle == data.title) {
item.originalTitle = highlightTitle(item.originalTitle);
}
if (item.director) {
item.director.forEach(function(director, i) {
if (Ox.contains(data.director, director)) {
item.director[i] = highlightTitle(director);
}
})
}
if (item.year && item.year == data.year) {
item.year = highlightTitle(item.year);
}
2013-02-28 08:40:43 +00:00
return Ox.filter([
item.id,
item.title + (item.originalTitle ? ' (' + item.originalTitle + ')' : ''),
item.director ? item.director.join(', ') : '',
item.year
2013-02-28 08:40:43 +00:00
]).join(' &mdash; ');
}
function highlightTitle(value) {
return '<span style="font-weight: bold">' + value + '</span>';
}
function isEmpty(value) {
return Ox.isEmpty(value) || Ox.isUndefined(value);
}
function setSize(data) {
2013-02-28 08:40:43 +00:00
dialogHeight = data.height;
dialogWidth = data.width;
formWidth = getFormWidth();
$input.forEach(function($element) {
$element.options({width: formWidth});
});
2013-02-28 08:40:43 +00:00
$checkboxGroup.options({width: formWidth});
}
function updateButton() {
that[
$checkboxGroup.options('value') == data.imdbId || (
$checkboxGroup.options('value') == 'none' && !data.imdbId
) ? 'disableButton' : 'enableButton'
2013-02-28 08:40:43 +00:00
]('update');
}
function updateId() {
2013-03-08 08:51:35 +00:00
that.options({content: Ox.LoadingScreen()}).disableButtons();
pandora.api.edit({
id: data.id,
imdbId: $checkboxGroup.options('value')
}, function(result) {
2013-02-28 08:40:43 +00:00
that.close();
pandora.updateItemContext();
pandora.$ui.contentPanel.replaceElement(1,
pandora.$ui.item = pandora.ui.item()
);
});
}
return that;
2013-05-09 10:13:58 +00:00
};