pandora/static/js/pandora/mediaView.js

161 lines
6.5 KiB
JavaScript
Raw Normal View History

2011-11-05 17:04:10 +00:00
'use strict';
2011-06-06 18:40:24 +00:00
pandora.ui.mediaView = function() {
var item = pandora.user.ui.item,
view = pandora.user.ui.itemView,
listWidth = 144 + Ox.UI.SCROLLBAR_SIZE,
2011-06-07 03:17:36 +00:00
selectedImage = {};
2011-06-06 18:40:24 +00:00
$preview = Ox.Element(),
that = Ox.SplitPanel({
elements: [
{
element: Ox.Element(),
size: listWidth
},
{
element: $preview
}
],
orientation: 'horizontal'
});
2011-06-07 03:17:36 +00:00
that.resize = function() {
selectedImage.url && renderPreview();
}
2011-06-06 18:40:24 +00:00
pandora.api.get({
id: item,
keys: [view]
}, function(result) {
2011-06-07 03:17:36 +00:00
var images = result.data[view];
selectedImage = images.filter(function(image) {
return image.selected;
})[0];
var $list = Ox.IconList({
2011-06-06 18:40:24 +00:00
item: function(data, sort, size) {
var ratio = data.width / data.height;
size = size || 128;
return {
height: ratio <= 1 ? size : size / ratio,
id: data['id'],
info: data.width + ' x ' + data.height + ' px',
title: view == 'frames' ? Ox.formatDuration(data.position) : data.source,
url: data.url,
width: ratio >= 1 ? size : size * ratio
}
},
items: images,
keys: view == 'frames'
? ['index', 'position', 'width', 'height', 'url']
: ['index', 'source', 'width', 'height', 'url'],
max: 1,
min: 1,
orientation: 'vertical',
2011-06-07 03:17:36 +00:00
selected: [selectedImage['index']],
2011-06-06 18:40:24 +00:00
size: 128,
sort: [{key: 'index', operator: '+'}],
unique: 'index'
})
.css({background: 'rgb(16, 16, 16)'})
.bindEvent({
select: function(event) {
2011-06-07 03:17:36 +00:00
var index = event.ids[0];
selectedImage = images.filter(function(image) {
return image.index == index;
})[0];
renderPreview(selectedImage);
2011-06-06 18:40:24 +00:00
pandora.api[view == 'frames' ? 'setPosterFrame' : 'setPoster'](Ox.extend({
id: item
}, view == 'frames' ? {
2011-06-07 03:17:36 +00:00
position: selectedImage.index // api slightly inconsistent
2011-06-06 18:40:24 +00:00
} : {
2011-06-07 03:17:36 +00:00
source: selectedImage.source
2011-06-06 18:40:24 +00:00
}), function(result) {
2011-06-07 03:17:36 +00:00
var imageRatio = selectedImage.width / selectedImage.height;
2011-06-06 18:40:24 +00:00
$('img[src*="/' + item + '/poster"]').each(function() {
2011-06-07 03:17:36 +00:00
var $this = $(this),
size = Math.max($this.width(), $this.height()),
src = $this.attr('src').split('?')[0] + '?' + Ox.uid();
$('<img>')
.attr({src: src})
.load(function() {
$this.attr({src: src});
view == 'posters' && $this.css(imageRatio < 1 ? {
width: Math.round(size * imageRatio) + 'px',
height: size + 'px'
} : {
width: size + 'px',
height: Math.round(size / imageRatio) + 'px'
});
});
2011-06-06 18:40:24 +00:00
});
});
}
});
that.replaceElement(0, $list);
2011-06-07 03:17:36 +00:00
renderPreview();
2011-06-06 18:40:24 +00:00
});
2011-06-07 03:17:36 +00:00
function renderPreview() {
var previewWidth = pandora.$ui.document.width() - pandora.$ui.mainPanel.size(0) - 1 - listWidth,
previewHeight = pandora.$ui.contentPanel.size(1),
2011-10-29 17:46:46 +00:00
previewRatio = previewWidth / previewHeight,
2011-06-07 03:17:36 +00:00
imageRatio = selectedImage.width / selectedImage.height,
imageWidth = imageRatio > previewRatio ? previewWidth : Math.round(previewHeight * imageRatio),
imageHeight = imageRatio < previewRatio ? previewHeight : Math.round(previewWidth / imageRatio),
imageLeft = Math.floor((previewWidth - imageWidth) / 2),
imageTop = Math.floor((previewHeight - imageHeight) / 2);
$preview.html(
$('<img>')
.attr({
src: selectedImage.url
})
.css({
position: 'absolute',
left: imageLeft + 'px',
top: imageTop + 'px',
width: imageWidth + 'px',
2012-05-26 15:46:24 +00:00
height: imageHeight + 'px'
2011-06-07 03:17:36 +00:00
})
);
if (view == 'frames') {
var left = Math.floor((imageWidth - imageHeight) / 2),
right = Math.ceil((imageWidth - imageHeight) / 2);
$('<div>')
.addClass('OxPosterMarker OxPosterMarkerLeft')
.css({
display: 'block',
left: imageLeft + 'px',
top: imageTop + 'px',
width: left + 'px',
height: imageHeight + 'px'
})
.appendTo($preview.$element);
$('<div>')
.addClass('OxPosterMarker OxPosterMarkerCenter')
.css({
display: 'block',
left: imageLeft + left + 'px',
top: imageTop + 'px',
width: imageHeight - 2 + 'px',
height: imageHeight - 2 + 'px'
})
.appendTo($preview.$element);
$('<div>')
.addClass('OxPosterMarker OxPosterMarkerRight')
.css({
display: 'block',
left: imageLeft + left + imageHeight + 'px',
top: imageTop + 'px',
width: right + 'px',
height: imageHeight + 'px'
})
.appendTo($preview.$element);
}
}
2011-06-06 18:40:24 +00:00
return that;
2012-05-26 15:46:24 +00:00
}