pandora/static/js/pandora/embedPanel.js

369 lines
14 KiB
JavaScript
Raw Normal View History

2013-02-18 10:17:19 +00:00
pandora.ui.embedPanel = function() {
2013-02-20 04:39:12 +00:00
2013-02-20 08:58:15 +00:00
var that = Ox.Element(),
2013-02-20 10:07:58 +00:00
options, video,
2013-02-20 08:58:15 +00:00
ui = pandora.user.ui,
$outerPanel, $innerPanel,
$title, $player, $controls, $timeline, $annotations,
$errorBox, $errorLogo, $errorText;
2013-02-20 04:39:12 +00:00
if (pandora.user.ui.item) {
2013-02-20 08:58:15 +00:00
options = getOptions();
2013-02-20 05:58:51 +00:00
2013-02-20 08:58:15 +00:00
pandora.api.get({id: ui.item, keys: [
2013-02-20 05:58:51 +00:00
'duration', 'layers', 'parts', 'posterFrame',
'rightslevel', 'size', 'title', 'videoRatio'
]}, function(result) {
2013-02-20 10:07:58 +00:00
2013-02-20 12:47:27 +00:00
video = Ox.extend(result.data, pandora.getVideoOptions(result.data));
2013-02-20 12:38:51 +00:00
var sizes = getSizes();
options.height = sizes.videoHeight;
2013-02-20 10:21:24 +00:00
2013-02-20 08:58:15 +00:00
if (options.title) {
$title = Ox.Element()
.css({position: 'absolute'})
.append(
$('<div>')
.css({
marginTop: !options.title.match(/\\n/) ? '8px' : '2px',
textAlign: 'center'
})
.html(options.title.replace(/\\n/g, '<br>'))
);
} else {
$title = Ox.Element();
}
2013-02-20 10:07:58 +00:00
$player = Ox.VideoPlayer(Ox.extend({
censored: video.censored,
censoredIcon: pandora.site.cantPlay.icon,
censoredTooltip: pandora.site.cantPlay.text,
2013-02-20 13:12:16 +00:00
controlsBottom: ['play', 'volume', 'scale'].concat(
2013-02-20 18:19:44 +00:00
Ox.Fullscreen.available && options.showCloseButton ? ['fullscreen'] : []
2013-02-20 10:07:58 +00:00
).concat(
['timeline', 'position', 'settings']
),
controlsTooltips: {
close: 'Close',
open: 'Watch on ' + pandora.site.site.name
},
2013-02-20 18:19:44 +00:00
controlsTop: [
options.showCloseButton ? 'close'
: Ox.Fullscreen.available ? 'fullscreen'
: 'space16'
].concat(
2013-02-20 10:07:58 +00:00
['title', 'open']
),
duration: video.duration,
enableFullscreen: Ox.Fullscreen.available,
enableKeyboard: true,
enableMouse: true,
enablePosition: true,
enableSubtitles: true,
enableTimeline: true,
enableVolume: true,
height: options.height,
invertHighlight: options.invertHighlight,
muted: pandora.user.ui.videoMuted,
paused: options.paused,
playInToOut: options.playInToOut,
position: options.position,
poster: '/' + options.item + '/' + '96p' + (
options.position !== void 0 ? options.position
: options['in'] !== void 0 ? options['in']
: video.posterFrame
) +'.jpg',
resolution: pandora.user.ui.videoResolution,
scaleToFill: pandora.user.ui.videoScale == 'fill',
subtitles: video.subtitles,
timeline: options.playInToOut ? function(size, i) {
return '/' + options.item
+ '/timelineantialias'
+ size + 'p' + i + '.jpg'
} : '/' + options.item + '/' + 'timeline16p.png',
timelineType: options.showTimeline
? options.timeline : '',
timelineTypes: options.showTimeline
? pandora.site.timelines : [],
2013-02-20 10:07:58 +00:00
title: video.title,
video: video.video,
volume: pandora.user.ui.videoVolume,
width: options.width
}, options['in'] ? {
'in': options['in']
} : {}, options.out ? {
out: options.out
} : {}))
2013-02-20 08:58:15 +00:00
.bindEvent({
fullscreen: function(data) {
Ox.Fullscreen.toggle();
},
open: function() {
$player.options({paused: true});
var url = document.location.protocol + '//'
+ document.location.hostname + '/'
+ options.item + '/'
+ Ox.formatDuration($player.options('position'));
window.open(url, '_blank');
},
2013-02-20 08:58:15 +00:00
playing: function(data) {
setPosition(data.position, true);
},
position: function(data) {
setPosition(data.position);
2013-02-20 10:07:58 +00:00
},
subtitles: function(data) {
2013-02-20 20:27:43 +00:00
options.showTimeline && $timeline.options({
2013-02-20 10:07:58 +00:00
subtitles: data.subtitles ? video.subtitles : []
});
2013-02-21 08:13:37 +00:00
},
timeline: function(data) {
options.showTimeline && $timeline.options({
type: data.timeline
});
2013-02-20 08:58:15 +00:00
}
});
2013-02-20 10:07:58 +00:00
2013-02-20 08:58:15 +00:00
$controls = Ox.Element();
2013-02-20 10:07:58 +00:00
2013-02-20 08:58:15 +00:00
if (options.showTimeline) {
2013-02-20 10:57:44 +00:00
$timeline = Ox.LargeVideoTimeline(Ox.extend({
2013-02-20 10:07:58 +00:00
duration: video.duration,
2013-02-20 08:58:15 +00:00
getImageURL: function(type, i) {
return '/' + ui.item + '/timeline' + type + '64p' + i + '.jpg';
},
position: options.position,
2013-02-20 10:57:44 +00:00
showInToOut: options.playInToOut,
2013-02-20 10:07:58 +00:00
subtitles: ui.videoSubtitles ? video.subtitles : [],
type: options.timeline,
2013-02-20 08:58:15 +00:00
width: window.innerWidth - 16
2013-02-20 10:57:44 +00:00
}, options['in'] ? {
'in': options['in']
} : {}, options.out ? {
out: options.out
} : {}))
2013-02-20 08:58:15 +00:00
.css({
top: '4px',
left: '4px'
})
.bindEvent({
mousedown: that.gainFocus,
position: changeTimeline
})
.appendTo($controls);
}
2013-02-20 10:07:58 +00:00
if (options.showAnnotations) {
2013-02-21 07:18:17 +00:00
video.annotations = video.annotations.filter(function(layer) {
return Ox.contains(options.showLayers, layer.id);
});
2013-02-20 11:58:28 +00:00
if (options.playInToOut) {
video.annotations.forEach(function(layer) {
2013-02-21 07:18:17 +00:00
var items = [];
layer.items.forEach(function(item) {
if ((
item['in'] >= options['in'] && item['in'] <= options.out
) || (
item.out >= options['in'] && item.out <= options.out
)) {
items.push(item);
}
});
layer.items = items;
2013-02-20 11:58:28 +00:00
});
}
2013-02-20 10:07:58 +00:00
$annotations = Ox.AnnotationPanel(Ox.extend({
font: options.annotationsFont,
layers: video.annotations,
position: options.position,
range: options.annotationsRange,
showLayers: ui.showLayers,
2013-02-20 10:07:58 +00:00
showUsers: true,
2013-02-20 11:58:28 +00:00
sort: options.annotationsSort,
width: window.innerWidth
2013-02-20 10:07:58 +00:00
}, options['in'] ? {
'in': options['in']
} : {}, options.out ? {
out: options.out
} : {}))
.bindEvent({
select: selectAnnotation
})
} else {
$annotations = Ox.Element();
}
2013-02-20 08:58:15 +00:00
$innerPanel = Ox.SplitPanel({
elements: [
{element: $title, size: options.title ? 32 : 0},
{element: $player},
{element: $controls, size: options.showTimeline ? 80 : 0}
],
orientation: 'vertical'
});
2013-02-20 10:07:58 +00:00
2013-02-20 08:58:15 +00:00
$outerPanel = Ox.SplitPanel({
elements: [
2013-02-20 12:38:51 +00:00
{element: $innerPanel, size: sizes.innerHeight},
2013-02-20 08:58:15 +00:00
{element: $annotations}
],
orientation: 'vertical'
});
2013-02-20 10:07:58 +00:00
2013-02-20 08:58:15 +00:00
that.setElement($outerPanel);
2013-02-20 10:07:58 +00:00
2013-02-20 05:58:51 +00:00
});
2013-02-20 04:39:12 +00:00
} else {
2013-02-20 08:58:15 +00:00
that.addClass('OxScreen')
2013-02-20 04:39:12 +00:00
.css({
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
});
2013-02-20 05:22:37 +00:00
2013-02-20 04:39:12 +00:00
$errorBox = $('<div>')
.css({
position: 'absolute',
left: 0,
top: 0,
right: 0,
bottom: 0,
width: '96px',
height: '96px',
padding: '16px',
margin: 'auto'
})
.appendTo(that);
2013-02-20 05:22:37 +00:00
2013-02-20 04:39:12 +00:00
$errorLogo = $('<img>')
.css({width: '96px', opacity: 0})
.one({
load: function() {
$errorLogo.animate({opacity: 1}, 250);
}
})
.attr({src: '/static/png/logo.png'})
.appendTo($errorBox);
2013-02-20 05:22:37 +00:00
2013-02-20 04:39:12 +00:00
$errorText = $('<div>')
.css({marginTop: '4px', fontSize: '9px', textAlign: 'center'})
.html('This view cannot<br>be embedded.')
.appendTo($errorBox);
}
2013-02-20 08:58:15 +00:00
function changeTimeline(data) {
var position = options.playInToOut
? Ox.limit(data.position, options['in'], options.out)
: data.position;
$player.options({position: position});
position != data.position && $timeline.options({position: position});
options.showAnnotations && $annotations.options({position: position});
}
function getOptions() {
var options = {},
defaults = {
2013-02-20 10:07:58 +00:00
annotationsFont: ui.annotationsFont,
annotationsRange: ui.annotationsRange,
annotationsSort: ui.annotationsSort,
2013-02-20 08:58:15 +00:00
invertHighlight: true,
2013-02-20 10:07:58 +00:00
matchRatio: false,
2013-02-20 08:58:15 +00:00
paused: true,
playInToOut: true,
2013-02-20 10:07:58 +00:00
showAnnotations: false,
showCloseButton: false,
showLayers: pandora.site.layers.map(function(layer) {
return layer.id;
}),
2013-02-20 10:07:58 +00:00
showTimeline: false,
timeline: ui.videoTimeline,
2013-02-20 08:58:15 +00:00
width: window.innerWidth
};
ui.hash.query.forEach(function(condition) {
if (condition.key != 'embed') {
options[condition.key] = condition.value;
}
});
options = Ox.extend(
{item: ui.item},
ui.videoPoints[ui.item] || {},
defaults,
options
);
if (!options.position) {
options.position = options['in'] || 0;
}
if (!options['in'] && !options.out) {
options.playInToOut = false;
}
return options;
}
2013-02-20 12:38:51 +00:00
function getSizes() {
var innerHeight,
maxVideoHeight = window.innerHeight
- (options.title ? 32 : 0)
- (options.showTimeline ? 80 : 0)
- (options.showAnnotations ? 128 : 0),
2013-02-20 12:47:27 +00:00
videoHeight;
2013-02-20 12:38:51 +00:00
if (options.matchRatio || options.showAnnotations) {
videoHeight = Math.round(window.innerWidth / video.videoRatio);
2013-02-20 13:08:03 +00:00
options.matchRatio = videoHeight <= maxVideoHeight;
2013-02-20 12:38:51 +00:00
if (!options.matchRatio) {
videoHeight = maxVideoHeight;
}
} else {
videoHeight = window.innerHeight
- (options.title ? 32 : 0)
- (options.showTimeline ? 80 : 0);
}
2013-02-20 12:47:27 +00:00
innerHeight = videoHeight
2013-02-20 12:38:51 +00:00
+ (options.title ? 32 : 0)
+ (options.showTimeline ? 80 : 0);
return {innerHeight: innerHeight, videoHeight: videoHeight};
}
2013-02-20 10:07:58 +00:00
function selectAnnotation(data) {
if (data.id) {
2013-02-20 11:58:28 +00:00
setPosition(Math.max(data['in'], options['in'] || 0));
$annotations.options({selected: ''});
2013-02-20 10:07:58 +00:00
}
}
2013-02-20 08:58:15 +00:00
function setPosition(position, playing) {
!playing && $player.options({position: position});
options.showTimeline && $timeline.options({position: position});
options.showAnnotations && $annotations.options({position: position});
}
2013-02-18 10:17:19 +00:00
that.display = function() {
// fixme: move animation into Ox.App
var animate = $('.OxScreen').length == 0;
Ox.Log('', 'ANIMATE?', animate)
animate && pandora.$ui.body.css({opacity: 0});
that.appendTo(pandora.$ui.body);
animate && pandora.$ui.body.animate({opacity: 1}, 1000);
return that;
};
2013-02-20 04:39:12 +00:00
2013-02-20 12:38:51 +00:00
$(window).on({
resize: function() {
var sizes = getSizes();
2013-02-20 12:47:27 +00:00
$player.options({width: window.innerWidth, height: sizes.videoHeight});
2013-02-20 12:38:51 +00:00
$outerPanel.size(0, sizes.innerHeight);
options.showTimeline && $timeline.options({width: window.innerWidth - 16});
options.showAnnotations && $annotations.options({width: window.innerWidth});
}
});
2013-02-18 10:17:19 +00:00
return that;
2013-02-20 04:39:12 +00:00
2013-02-20 10:46:24 +00:00
};