pandora/static/js/similarClipsDialog.js

401 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2012-06-06 13:18:03 +00:00
'use strict';
pandora.ui.similarClipsDialog = function() {
2012-06-06 13:18:03 +00:00
2012-06-16 12:00:42 +00:00
var dialogSize = {
height: Math.round((window.innerHeight - 48) * 0.9),
width: Math.round(window.innerWidth * 0.9)
},
itemTitleKeys = pandora.site.itemTitleKeys,
2012-06-16 12:00:42 +00:00
left = getLeft(),
2013-07-15 14:45:54 +00:00
fixedRatio = pandora.site.video.previewRatio,
2012-06-16 10:10:46 +00:00
fps = 25,
item = pandora.getItemIdAndPosition(),
2012-06-16 12:00:42 +00:00
itemWidth = 160,
2012-06-16 10:10:46 +00:00
sequence,
2012-06-16 12:00:42 +00:00
2012-06-16 10:10:46 +00:00
$item,
2012-06-16 12:00:42 +00:00
$list,
2012-06-16 10:10:46 +00:00
2012-06-16 12:00:42 +00:00
$innerPanel = Ox.SplitPanel({
elements: [
{element: Ox.Element(), size: itemWidth},
{element: Ox.Element()}
2012-06-06 13:18:03 +00:00
],
2012-06-16 12:00:42 +00:00
orientation: 'horizontal'
2012-06-15 17:20:02 +00:00
}),
2012-06-16 10:10:46 +00:00
2012-06-16 12:00:42 +00:00
$toolbar = Ox.Bar({size: 24}),
2012-06-16 10:10:46 +00:00
$clipButtons = Ox.ButtonGroup({
buttons: [
{id: 'previous', title: 'left', tooltip: Ox._('Previous Clip'), disabled: true},
{id: 'next', title: 'right', tooltip: Ox._('Next Clip'), disabled: true}
2012-06-16 10:10:46 +00:00
],
type: 'image'
})
.css({float: 'left', width: '32px', margin: '4px'})
.bindEvent({
click: function(data) {
item.position = data.id == 'previous'
? sequence['in'] - 1 / fps : sequence.out;
2012-06-16 12:00:42 +00:00
getClips();
2012-06-16 10:10:46 +00:00
}
})
.appendTo($toolbar),
2012-06-16 12:00:42 +00:00
$modeButtons = Ox.ButtonGroup({
buttons: [
2013-05-09 10:13:58 +00:00
{id: 'shape', title: Ox._('Similar Shapes')},
{id: 'color', title: Ox._('Similar Colors')}
2012-06-16 12:00:42 +00:00
],
selectable: true,
value: pandora.user.ui.sequenceMode
})
.bindEvent({
change: function(data) {
pandora.UI.set({sequenceMode: data.value});
getClips();
}
})
.css({
position: 'absolute',
left: left,
top: '4px',
width: '256px',
textAlign: 'center'
})
.appendTo($toolbar),
2012-06-15 17:20:02 +00:00
$sortSelect = Ox.Select({
items: itemTitleKeys.concat(['position', 'duration']).map(function(id) {
2013-05-09 10:13:58 +00:00
var item = Ox.getObjectById(pandora.site.itemKeys, id)
|| Ox.getObjectById(pandora.site.clipKeys, id),
title = Ox._(item ? item.title : Ox.toTitleCase(id));
2012-06-16 10:43:48 +00:00
return {
id: id,
2013-05-09 10:13:58 +00:00
title: Ox._('Sort by {0}', [title])
2012-06-16 10:43:48 +00:00
};
2012-06-15 17:20:02 +00:00
}),
2012-06-16 10:43:48 +00:00
value: pandora.user.ui.sequenceSort[0].key,
2012-06-15 17:20:02 +00:00
width: 128
})
.bindEvent({
change: function(data) {
var key = data.value;
pandora.UI.set({sequenceSort: [{
key: key,
operator: pandora.getSortOperator(key)
}]});
2012-06-16 10:10:46 +00:00
updateOrderButton();
2012-06-16 10:43:48 +00:00
$list.options({sort: pandora.user.ui.sequenceSort});
2012-06-15 17:20:02 +00:00
}
}),
$orderButton = Ox.Button({
overlap: 'left',
title: getTitle(),
tooltip: getTooltip(),
type: 'image'
})
.bindEvent({
click: function() {
pandora.UI.set({sequenceSort: [{
key: pandora.user.ui.sequenceSort[0].key,
operator: pandora.user.ui.sequenceSort[0].operator == '+' ? '-' : '+'
}]});
updateOrderButton();
$list.options({sort: pandora.user.ui.sequenceSort});
}
}),
$sortElement = Ox.FormElementGroup({
elements: [$sortSelect, $orderButton],
float: 'right'
})
.css({
float: 'right',
margin: '4px'
2012-06-15 17:20:02 +00:00
})
.appendTo($toolbar),
2012-06-16 10:10:46 +00:00
2012-06-16 12:00:42 +00:00
$outerPanel = Ox.SplitPanel({
elements: [
{element: $toolbar, size: 24},
{element: $innerPanel}
],
orientation: 'vertical'
}),
$dialog = Ox.Dialog({
buttons: [
Ox.Button({
disabled: true,
id: 'open',
2013-05-09 10:13:58 +00:00
title: Ox._('Open Selected Clip'),
2012-06-16 12:00:42 +00:00
width: 128
}).bindEvent({
click: openClip
}),
Ox.Button({
id: 'close',
2013-05-09 10:13:58 +00:00
title: Ox._('Close'),
2012-06-16 12:00:42 +00:00
width: 64
}).bindEvent({
click: function() {
$dialog.close();
}
})
],
closeButton: true,
content: $outerPanel,
height: dialogSize.height,
keys: {escape: 'close'},
maximizeButton: true,
padding: 0,
removeOnClose: true,
2013-05-09 10:13:58 +00:00
title: Ox._('Similar Clips'),
2012-06-16 12:00:42 +00:00
width: dialogSize.width
})
.bindEvent({
resize: function(data) {
dialogSize = data;
left = getLeft();
$modeButtons.css({left: left});
$status.css({left: left});
}
}),
2012-06-15 17:20:02 +00:00
$statusbar = $dialog.children('.OxButtonsbar'),
2012-06-16 10:10:46 +00:00
$image = $('<img>')
.attr({src: getImageURL()})
.css({
float: 'left',
width: '24px',
height: '16px',
borderRadius: '2px',
margin: '4px 4px 4px 8px',
boxShadow: '0 0 1px rgb(128, 128, 128)'
})
.appendTo($statusbar),
2012-06-15 17:20:02 +00:00
$status = Ox.Element()
.css({
2012-06-16 12:00:42 +00:00
position: 'absolute',
left: left,
top: '6px',
width: '256px',
2012-06-15 17:20:02 +00:00
fontSize: '9px',
textAlign: 'center'
})
2013-05-09 10:13:58 +00:00
.html(Ox._('Loading...'))
2012-06-15 17:20:02 +00:00
.appendTo($statusbar);
2012-06-16 12:00:42 +00:00
getClips();
2012-06-16 10:10:46 +00:00
function changeClip(data) {
var split = data.ids[0].replace('-', '/').split('/');
item.id = split[0];
item.position = parseFloat(split[1]);
2012-06-16 12:00:42 +00:00
$item = null;
getClips();
2012-06-16 10:10:46 +00:00
}
2012-06-16 12:00:42 +00:00
function getClips() {
$dialog && $dialog.disableButton('open');
2013-05-09 10:13:58 +00:00
$status && $status.html(Ox._('Loading...'));
2012-06-16 12:00:42 +00:00
pandora.api.get({
id: item.id,
keys: itemTitleKeys.concat(['duration', 'videoRatio'])
2012-06-16 12:00:42 +00:00
}, function(result) {
Ox.extend(item, result.data);
pandora.api.getSequence({
id: item.id,
mode: pandora.user.ui.sequenceMode,
position: item.position
}, function(result) {
// result.data: {hash, id, in, out}
2012-06-16 14:21:32 +00:00
if (Ox.isEmpty(result.data)) {
result.data['in'] = item.position;
result.data.out = item.position + 1 / fps;
result.data.id = item.id + '/'
+ Ox.formatNumber(result.data['in'], 3) + '-'
+ Ox.formatNumber(result.data.out, 3)
}
2012-06-16 12:00:42 +00:00
sequence = Ox.extend({}, item, result.data);
$clipButtons[sequence['in'] > 0 ? 'enableButton' : 'disableButton']('previous');
$clipButtons[sequence.out < item.duration ? 'enableButton' : 'disableButton']('next');
2012-06-16 16:39:01 +00:00
$item = Ox.IconList({
fixedRatio: fixedRatio,
item: getItem,
/*
items: [item],
*/
///*
items: function(data, callback) {
// FIXME: witout timeout, list layout is wrong
setTimeout(function() {
callback({
data: {items: data.keys ? [sequence] : 1},
status: {code: 200, text: 'ok'}
});
}, 25);
},
//*/
max: 0,
orientation: 'both',
size: 128,
sort: [{key: 'id', operator: '+'}],
unique: 'id'
});
$innerPanel.replaceElement(0, $item);
2012-06-16 14:21:32 +00:00
if (sequence.hash) {
$list = Ox.IconList({
2012-06-16 12:00:42 +00:00
fixedRatio: fixedRatio,
item: getItem,
items: function(data, callback) {
2012-06-16 14:21:32 +00:00
pandora.api.findSequences(Ox.extend(data, {
query: {
conditions: [
{key: 'id', value: sequence.id, operator: '!='},
{key: 'mode', value: pandora.user.ui.sequenceMode, operator: '=='},
{key: 'hash', value: sequence.hash, operator: '=='}
],
operator: '&'
}
}), callback);
2012-06-16 12:00:42 +00:00
},
keys: itemTitleKeys.concat(['id', 'videoRatio']),
2012-06-16 14:21:32 +00:00
max: 1,
2012-06-16 12:00:42 +00:00
orientation: 'both',
size: 128,
2012-06-16 14:21:32 +00:00
sort: Ox.clone(pandora.user.ui.sequenceSort),
2012-06-16 12:00:42 +00:00
unique: 'id'
2012-06-16 14:21:32 +00:00
})
.bindEvent({
init: function(data) {
$status.html(
2013-08-05 22:46:10 +00:00
Ox.toTitleCase(Ox.formatCount(data.items, 'clip'))
2012-06-16 14:21:32 +00:00
);
},
open: changeClip,
select: function(data) {
$dialog[
data.ids.length ? 'enableButton' : 'disableButton'
]('open');
}
2012-06-16 12:00:42 +00:00
});
2012-06-16 14:21:32 +00:00
} else {
$list = Ox.Element();
2013-08-05 22:46:10 +00:00
$status.html('No Clips');
2012-06-16 12:00:42 +00:00
}
$innerPanel.replaceElement(1, $list);
$image.attr({src: getImageURL(sequence.hash)});
});
});
}
function getImageURL(hash) {
2012-06-16 10:10:46 +00:00
var canvas = $('<canvas>').attr({width: 8, height: 8})[0],
context = canvas.getContext('2d'),
imageData = context.getImageData(0, 0, 8, 8),
data = imageData.data;
2012-06-16 12:00:42 +00:00
if (hash) {
if (pandora.user.ui.sequenceMode == 'shape') {
Ox.loop(8, function(y) {
var value = parseInt(hash.substr(14 - y * 2, 2), 16);
Ox.loop(8, function(x) {
var color = value & Math.pow(2, x) ? 255 : 0,
index = y * 32 + x * 4;
Ox.loop(3, function(i) {
data[index + i] = color;
});
data[index + 3] = 255;
2012-06-16 10:10:46 +00:00
});
});
2012-06-16 12:00:42 +00:00
} else if (pandora.user.ui.sequenceMode == 'color') {
Ox.loop(8, function(part) {
var x = part % 4 * 2,
y = Math.floor(part / 4) * 4,
value = parseInt(hash.substr(14 - part * 2, 2), 16),
// RRRGGGBB
color = [
(value >> 5) * 32,
(value >> 2 & 7) * 32,
(value & 3) * 64
];
Ox.loop(4, function(dy) {
Ox.loop(2, function(dx) {
var index = (y + dy) * 32 + (x + dx) * 4;
Ox.loop(3, function(i) {
data[index + i] = color[i];
});
data[index + 3] = 255;
2012-06-16 10:10:46 +00:00
});
2012-06-16 12:00:42 +00:00
});
2012-06-16 10:10:46 +00:00
});
2012-06-16 12:00:42 +00:00
}
context.putImageData(imageData, 0, 0);
2012-06-16 10:10:46 +00:00
}
return canvas.toDataURL();
}
function getItem(data, sort, size) {
var ratio = data.videoRatio,
width = ratio > fixedRatio ? size : Math.round(size * ratio / fixedRatio),
height = Math.round(width / ratio);
return {
height: height,
id: data.id,
info: Ox.formatDuration(data['in'], 2) + '-' + Ox.formatDuration(data.out, 2),
title: pandora.getItemTitle(data),
url: pandora.getMediaURL('/' + data.id.split('/')[0] + '/' + height + 'p' + data['in'] + '.jpg'),
2012-06-16 10:10:46 +00:00
width: width
};
}
2012-06-15 17:47:36 +00:00
2012-06-16 12:00:42 +00:00
function getLeft() {
return Math.floor((dialogSize.width - 256) / 2) + 'px';
}
2012-06-15 17:20:02 +00:00
function getTitle() {
2013-05-10 14:54:30 +00:00
return pandora.user.ui.sequenceSort[0].operator == '+' ? Ox._('up') : Ox._('down');
2012-06-15 17:20:02 +00:00
}
2012-06-16 10:10:46 +00:00
2012-06-15 17:20:02 +00:00
function getTooltip() {
2013-05-10 14:54:30 +00:00
return pandora.user.ui.sequenceSort[0].operator == '+' ? Ox._('Ascending') : Ox._('Descending');
2012-06-15 17:20:02 +00:00
}
2012-06-16 10:10:46 +00:00
2012-06-15 17:20:02 +00:00
function openClip() {
var selected = $list.options('selected')[0],
split = selected.replace('-', '/').split('/'),
item = split[0],
2012-06-16 10:10:46 +00:00
inPoint = parseFloat(split[1]),
outPoint = parseFloat(split[2]),
2012-06-15 17:20:02 +00:00
set = {
2012-06-16 10:10:46 +00:00
item: item,
2012-06-15 17:20:02 +00:00
itemView: pandora.user.ui.videoView
};
set['videoPoints.' + split[0]] = {
annotation: '',
'in': inPoint,
out: outPoint,
position: inPoint
};
$dialog.close();
pandora.UI.set(set)
}
2012-06-16 10:10:46 +00:00
function updateOrderButton() {
2012-06-15 17:47:36 +00:00
$orderButton.options({
2012-06-15 17:20:02 +00:00
title: getTitle(),
tooltip: getTooltip()
2012-06-06 13:18:03 +00:00
});
2012-06-15 17:20:02 +00:00
}
2012-06-06 13:18:03 +00:00
return $dialog;
2012-06-06 19:49:32 +00:00
};