padmaEssay/utils.js

147 lines
4.5 KiB
JavaScript
Raw Normal View History

2010-11-05 22:51:50 +00:00
/*
John Resig's templating utility - http://ejohn.org/blog/javascript-micro-templating/
Not updated code to resolve issue with single quotes from: http://www.west-wind.com/Weblog/posts/509108.aspx
define templates inside <script type="text/html" id="foo"> tags
get html of template with tmpl("foo", json)
*/
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function(){
var cache = {};
this.tmpl = function tmpl(str, data){
// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = !/\W/.test(str) ?
cache[str] = cache[str] ||
tmpl(document.getElementById(str).innerHTML) :
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function("obj",
"var p=[],print=function(){p.push.apply(p,arguments);};" +
// Introduce the data as local variables using with(){}
"with(obj){p.push('" +
// Convert the template into pure JavaScript
str
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t").join("');")
.split("%>").join("p.push('")
.split("\r").join("\\'")
+ "');}return p.join('');");
// Provide some basic currying to the user
return data ? fn( data ) : fn;
};
})();
/**
* Returns time-code in npt format from pos in miliseconds
* @param {Int} pos Position in miliseconds
* @returns {String} Time-code in npt format.
*/
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;
}
/*
Gives a bunch of data parsed from a pad.ma url
Returns:
{
'videoId': string, id of pad.ma video
'type': string, type of link -- info, editor, layer, tcRange, tc or list.
'layerId': string, id of layer, (if type=="layer")
'tcIn': timecode, in npt format (if type == "tcRange")
'tc': timecode, in npt format (if type == 'tc')
}
*/
function parsePadmaUrl(link, padma_url) {
2012-02-15 12:33:47 +00:00
var regex = padmaConfig.data_url;
// var regex = new RegExp("http://.*" + padma_url);
2011-02-21 21:06:27 +00:00
var linkStr = link.replace(regex, "");
2012-02-15 09:35:45 +00:00
// alert(linkStr);
// console.log("linkStr", linkStr);
2010-11-05 22:51:50 +00:00
var r = { };
r.url = link;
2010-11-06 00:56:06 +00:00
// console.log(link);
2012-02-15 12:33:47 +00:00
var idRegex = /([A-Z]+)/;
2012-02-15 09:35:45 +00:00
if (linkStr.match(idRegex)) { // FIXME
2010-11-05 22:51:50 +00:00
var idMatch = linkStr.match(idRegex);
2012-02-15 09:35:45 +00:00
// console.log("match", idMatch);
r.videoId = idMatch[1]
// console.log(r.videoId);
2012-02-15 12:33:47 +00:00
var postIdStr = linkStr.replace(r.videoId, '');
postIdStr = postIdStr.replace("/", ""); //FIXME
2012-02-15 09:35:45 +00:00
// console.log("postId", postIdStr);
2010-11-05 22:51:50 +00:00
if (postIdStr == 'info' || postIdStr == '') {
r.type = 'info';
return r;
} else if (postIdStr == 'editor') {
r.type = 'editor';
return r;
} else if (postIdStr.charAt(0) == 'L') {
r.type = 'layer';
r.layerId = postIdStr;
return r;
2012-02-15 09:35:45 +00:00
} else if (postIdStr.indexOf(",") != -1) {
var tcs = postIdStr.split(",");
2010-11-05 22:51:50 +00:00
r.type = 'tcRange';
r.tcIn = tcs[0];
2012-02-15 09:35:45 +00:00
r.tcOut = tcs[1].replace("/", "");
2010-11-05 22:51:50 +00:00
return r;
2012-02-15 12:33:47 +00:00
} else if (postIdStr.indexOf("-") != -1) {
var tcs = postIdStr.split("-");
r.type = 'tcRange';
r.tcIn = tcs[0];
r.tcOut = tcs[1].replace("/", "");
return r;
2010-11-05 22:51:50 +00:00
} else {
r.type = 'tc';
r.tc = postIdStr;
return r;
}
} else if (linkStr.substring(0,7) == 'find?l=') {
r.type = 'list';
r.listId = linkStr.match(/l\=(L.*?)$/)[1];
return r;
} else {
return false;
}
}