padmaJS/padma.layers.js

229 lines
5.6 KiB
JavaScript
Raw Normal View History

2011-08-10 12:02:08 +00:00
var padmaConfig = {
'background': 'rgba(0,0,0,0.85)',
'color': '#fff',
'links_url': 'pad.ma/',
'data_url': 'http://pad.ma/',
'default_layers': ['transcript', 'description'],
};
(function($) {
var BASE_URL = padmaConfig.data_url;
var padmaVideo = function(videoId) {
this.id = videoId;
this.videoUrl = BASE_URL + videoId + "/video.js";
this.layersUrl = BASE_URL + videoId + "/layers.js";
this.infoUrl = BASE_URL + videoId + "/info";
this.hasVideoData = false;
this.hasLayersData = false;
this.hasAllData = false;
this.video = {};
this.layers = {};
var that = this;
padmaVideo.cache.push(that);
}
padmaVideo.prototype.getAllData = function(callback, data) {
var that = this;
if (!this.hasAllData) {
$.getScript(that.videoUrl, function() {
that.video = video;
that.hasVideoData = true;
$.getScript(that.layersUrl, function() {
that.layers = layers;
that.hasLayersData = true;
that.hasAllData = true;
callback(that, data);
});
});
} else {
callback(that, data);
}
}
//tc in npt
padmaVideo.prototype.getFrame = function(tc, size) {
if (typeof(size) == 'undefined') {
sizeStr = '';
} else {
sizeStr = '.' + size;
}
return BASE_URL + this.id + "/frame/" + tc + sizeStr + ".jpg";
}
padmaVideo.prototype.getPoster = function() {
return this.getFrame(ms2npt(this.video.poster_frame), '320');
}
//tc in npt
padmaVideo.prototype.getVideo = function(tc, size) {
if (!this.hasVideoData) {
return false;
}
if (typeof(size) == 'undefined') {
size = '128';
}
return this.video.urls[size] + "?t=npt:" + tc;
}
padmaVideo.prototype.getDownloadLink = function(tcIn, tcOut) {
return BASE_URL + this.id + "/download/" + tcIn + "-" + tcOut + ".ogv";
}
padmaVideo.prototype.getLayerVideo = function(tcIn, tcOut) {
return this.video.urls['320'] + "?t=" + tcIn + "/" + tcOut;
}
padmaVideo.prototype.noop = function() {
// console.log("I'm done:)");
$.noop();
}
padmaVideo.prototype.getVideoData = function(callback, data) {
var that = this;
if (this.hasVideoData) {
callback(that, data);
} else {
$.getScript(that.videoUrl, function() {
that.video = video;
that.hasVideoData = true;
callback(that, data);
});
}
}
padmaVideo.prototype.getLayersData = function(callback, data) {
var that = this;
if (this.hasLayersData) {
callback(that, data);
} else {
$.getScript(that.layersUrl, function() {
that.layers = layers;
that.hasLayersData = true;
callback(that, data);
});
}
}
padmaVideo.prototype.getLayerById = function(id) {
var that = this;
if (!this.hasLayersData) {
return false;
}
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
if (thisLayer.id == id) {
return thisLayer;
}
}
return null;
}
//timecode in ms
padmaVideo.prototype.getLayersAtTimecode = function(tc) {
var that = this;
if (!this.hasLayersData) {
return false;
}
// console.log(tc);
var layers = [];
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
if (tc > thisLayer.time_in && tc < thisLayer.time_out) {
layers.push(thisLayer);
}
}
return layers;
}
padmaVideo.prototype.getLayerById = function(id) {
var that = this;
var id = $.trim(id);
for (var i=0; i < that.layers.length; i++) {
var thisLayer = that.layers[i];
if (thisLayer.id == id) {
return thisLayer;
}
}
return false;
}
function filterLayersByTracks(layers, tracks) {
var matchedLayers = []
for (var i=0; i < layers.length; i++) {
var layer = layers[i];
// console.log(layer.track);
if ($.inArray(layer.track, tracks) != -1) {
matchedLayers.push(layer);
}
}
return matchedLayers;
}
function filterLayersByContributors(layers, users) {
var matchedLayers = [];
for (var i=0; i < layers.length; i++) {
var layer = layers[i];
if ($.inArray(layer.contributor, users)) {
matchedLayers.push(layer);
}
}
return matchedLayers;
}
padmaVideo.cache = [];
padmaVideo.getFromCache = function(videoId) {
var cache = padmaVideo.cache;
for (var i=0; i<cache.length; i++) {
var v = cache[i];
if (v.id === videoId) {
return v;
}
}
return false;
}
window.padmaVideo = padmaVideo;
window.filterLayersByTracks = filterLayersByTracks;
window.filterLayersByContributors = filterLayersByContributors;
function ms2npt(pos) {
var h = Math.floor(pos / 3600000);
var m = Math.floor(pos % 3600000 / 60000);
var s = Math.floor(pos % 60000 / 1000);
var ms = pos % 1000;
return h.toString().pad('0', 2) + ':' + m.toString().pad('0', 2) + ':' + s.toString().pad('0', 2) + '.' + ms.toString().pad('0', 3);
// return strpad(h.toString(), '0', 2, 'left') + ':' + strpad(m.toString(), '0', 2, 'left') + ':' + strpad(s.toString(), '0', 2, 'left') + '.' + strpad(ms.toString(), '0', 3, 'left');
}
function npt2ms(npt) {
var ms = 0.0;
var p = npt.split(':');
for (i=0; i<p.length; i++) {
ms = ms * 60 + parseFloat(p[i]);
}
return ms * 1000;
}
String.prototype.pad = function(pad, len, dir) {
if (typeof(dir) == 'undefined')
dir = 'left';
var str = this;
while (str.length < len) {
if (dir == 'left')
str = pad + str;
else if (dir == 'right')
str = str + pad;
}
return str;
}
})(jQuery);