pandora/static/js/embed/pandora.js

170 lines
7.4 KiB
JavaScript
Raw Normal View History

2012-02-15 06:26:41 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
'use strict';
2011-10-22 11:24:26 +00:00
Ox.load('UI', {
debug: false,
hideScreen: false,
loadImages: true,
showScreen: true,
theme: 'modern'
}, function() {
window.pandora = new Ox.App({url: '/api/'}).bindEvent({
load: function(data) {
Ox.extend(pandora, {
site: data.site,
user: data.user.level == 'guest' ? Ox.clone(data.site.user) : data.user,
ui: {},
2012-02-15 06:26:41 +00:00
clip: function(options) {
var that = Ox.Element(),
2012-02-17 09:19:15 +00:00
keys = [ 'duration', 'layers', 'parts', 'posterFrame', 'rightslevel', 'size', 'title', 'videoRatio' ];
pandora.user.ui.item = options.item;
pandora.api.get({id: options.item, keys: keys}, function(result) {
2012-02-17 09:19:15 +00:00
var videoOptions = getVideoOptions(result.data);
2011-10-22 11:24:26 +00:00
that.append(pandora.player = Ox.VideoPlayer({
censored: videoOptions.censored,
controlsBottom: ['play', 'volume', 'scale', 'timeline', 'settings'],
duration: result.data.duration,
2011-10-22 11:24:26 +00:00
enableFind: false,
enableFullscreen: true,
2011-12-29 12:41:28 +00:00
enableKeyboard: true,
enableMouse: true,
enableTimeline: true,
2011-10-22 11:24:26 +00:00
enableVolume: true,
externalControls: false,
height: window.innerHeight,
2012-02-15 06:26:41 +00:00
'in': options['in'],
out: options.out,
paused: options.paused,
position: options['in'],
poster: '/' + options.item + '/' + '96p' + options['in'] +'.jpg',
resolution: pandora.user.ui.videoResolution,
2011-12-29 12:41:28 +00:00
showMarkers: false,
showMilliseconds: 0,
subtitles: videoOptions.subtitles,
2012-02-15 06:26:41 +00:00
timeline: '/' + options.item + '/' + 'timeline16p.png',
2011-10-22 11:24:26 +00:00
title: result.data.title,
video: videoOptions.video,
width: window.innerWidth
2011-10-22 11:24:26 +00:00
})
.bindEvent({
playing: checkRange,
position: checkRange,
resolution: function(data) {
pandora.api.setUI({'videoResolution': data.resolution});
},
2011-10-22 11:24:26 +00:00
})
);
Ox.UI.hideLoadingScreen();
function checkRange(data) {
2012-06-10 18:31:02 +00:00
if (
data.position < options['in'] - 0.04
|| data.position > options.out
) {
if (!pandora.player.options('paused')) {
pandora.player.togglePaused();
}
pandora.player.options({
2012-02-17 09:19:15 +00:00
position: options['in']
});
}
}
2011-10-22 11:24:26 +00:00
});
return that;
2012-02-15 06:26:41 +00:00
}
2011-10-22 11:24:26 +00:00
});
Ox.extend(pandora.user, {
videoFormat: Ox.UI.getVideoFormat(pandora.site.video.formats)
});
2012-02-15 06:26:41 +00:00
var options = parseQuery();
2012-06-25 14:49:39 +00:00
if (['video', 'player'].indexOf(options.view) > -1) {
2012-02-15 06:26:41 +00:00
pandora.ui.info = pandora.clip(options)
.css({width: '100%', height: '100%'})
.appendTo(document.body);
}
2011-10-22 11:24:26 +00:00
}
});
2012-03-03 18:58:07 +00:00
function getVideoUrl(id, resolution, part) {
var prefix = pandora.site.site.videoprefix
.replace('{id}', id)
.replace('{part}', part)
.replace('{resolution}', resolution)
.replace('{uid}', Ox.uid());
return prefix + '/' + id + '/' + resolution + 'p' + part + '.' + pandora.user.videoFormat;
}
2012-02-17 09:19:15 +00:00
function getVideoOptions(data) {
var canPlayClips = data.editable || pandora.site.capabilities.canPlayClips[pandora.user.level] >= data.rightslevel,
canPlayVideo = data.editable || pandora.site.capabilities.canPlayVideo[pandora.user.level] >= data.rightslevel,
options = {};
options.subtitles = data.layers.subtitles
? data.layers.subtitles.map(function(subtitle) {
return {'in': subtitle['in'], out: subtitle.out, text: subtitle.value};
})
: [];
options.censored = canPlayVideo ? []
: canPlayClips ? (
options.subtitles.length
2012-05-24 08:22:56 +00:00
? options.subtitles.map(function(subtitle, i) {
return {
'in': i == 0 ? 0 : options.subtitles[i - 1].out,
out: subtitle['in']
};
}).concat(
[{'in': Ox.last(options.subtitles).out, out: data.duration}]
2012-05-24 08:22:56 +00:00
).filter(function(censored) {
// don't include gaps shorter than one second
return censored.out - censored['in'] >= 1;
})
: Ox.range(0, data.duration - 5, 60).map(function(position) {
return {
'in': position + 5,
out: Math.min(position + 60, data.duration)
};
})
)
: [{'in': 0, out: data.duration}];
options.video = {};
pandora.site.video.resolutions.forEach(function(resolution) {
options.video[resolution] = Ox.range(data.parts).map(function(i) {
2012-03-03 18:58:07 +00:00
return getVideoUrl(data.item || pandora.user.ui.item, resolution, i + 1);
});
});
options.layers = [];
pandora.site.layers.forEach(function(layer, i) {
options.layers[i] = Ox.extend({}, layer, {
items: data.layers[layer.id].map(function(annotation) {
annotation.duration = Math.abs(annotation.out - annotation['in']);
annotation.editable = annotation.editable
|| annotation.user == pandora.user.username
|| pandora.site.capabilities['canEditAnnotations'][pandora.user.level];
return annotation;
})
});
});
return options;
}
2012-02-17 09:19:15 +00:00
function parseQuery() {
var vars = window.location.search.length
2012-05-24 09:56:52 +00:00
? window.location.search.slice(1).split('&')
2012-02-17 09:19:15 +00:00
: [],
query = {
2012-05-24 09:56:52 +00:00
item: window.location.pathname.slice(1).split('/')[0]
2012-02-17 09:19:15 +00:00
},
defaults = {
view: 'video',
'in': 0,
out: 10,
paused: true,
item: ''
};
vars.forEach(function(v) {
2012-05-24 09:56:52 +00:00
v = v.split('=');
2012-02-17 09:19:15 +00:00
query[v[0]] = decodeURIComponent(v[1]);
});
return Ox.extend({}, defaults, query);
}
2011-10-22 11:24:26 +00:00
});