padmaEssay/utils.js
2010-11-06 06:26:06 +05:30

139 lines
4.2 KiB
JavaScript

/*
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) {
// console.log(padma_url);
var linkStr = link.replace(padma_url, "");
// console.log(linkStr);
// console.log(linkStr);
var r = { };
r.url = link;
// console.log(link);
if (linkStr.charAt(0) == 'V') {
// console.log("Hello");
var idRegex = /V(.{1,7})/;
var idMatch = linkStr.match(idRegex);
// console.log("match", idMatch);
r.videoId = idMatch[0];
// console.log(videoId);
var postIdStr = linkStr.replace(idMatch[0] + "/", '');
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;
} else if (postIdStr.indexOf("-") != -1) {
var tcs = postIdStr.split("-");
r.type = 'tcRange';
r.tcIn = tcs[0];
r.tcOut = tcs[1];
return r;
} 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;
}
}