Skip to content

Instantly share code, notes, and snippets.

@abuggia
Created April 1, 2011 14:17
Show Gist options
  • Save abuggia/898210 to your computer and use it in GitHub Desktop.
Save abuggia/898210 to your computer and use it in GitHub Desktop.
/* *** Javascript Library */
/*
---------- base.js -----------
*/
var pageHasLoaded = false;
var $break = new Object();
var $continue = new Object();
var $browser;
new function() {
var b = navigator.userAgent.toLowerCase();
var msie = /msie/.test(b) && !/opera/.test(b);
$browser = {
msie: msie,
mozilla: !msie && /mozilla/.test(b) && !/compatible/.test(b),
boxModel: !msie || document.compatMode == "CSS1Compat"
};
}
function doCtOnload(f) {
if (f) {
f();
}
pageHasLoaded = true;
}
function $(id){
return (typeof id == 'string') ? document.getElementById(id) : id
}
function extend(obj,prop) {
for ( var i in prop ) obj[i] = prop[i];
return obj;
}
var Try = {
times: function(f, times, seconds) {
var funcs = new Array();
for(var i=0;i<times;i++) {
funcs[i] = f;
}
return this.these(funcs, seconds)
},
these: function(funcs, seconds) {
seconds = seconds ? seconds : .5;
var interval = seconds * 1000;
funcs.reverse();
return this._do(funcs.pop(), funcs, interval);
},
_do: function(f, funcs, interval) {
try {
f();
} catch (e) {
if (e == $continue) {
if (funcs.length == 0) {
return false;
}
return setTimeout(function() {Try._do(funcs.pop(), funcs, interval)}, interval)
} else {
throw e;
}
}
return true;
}
}
/*
---------- dom.js -----------
*/
var $D = {}
extend($D, {
append: function (e, c) {
$(e).appendChild(c);
},
getRowWidths: function (row) {
var widths = new Array();
if (row) {
var cells = row.cells;
var numCells = cells.length;
for (var i = 0; i < numCells; i++) {
var diff = (i == (widths.length - 1)) ? 1 : 0;
widths[i] = cells[i].offsetWidth - diff;
}
}
return widths
},
setRowWidths: function (row, widths) {
if (row) {
var cells = row.cells;
var numCells = widths.length;
for (var i = 0; i < widths.length; i++) {
this.setWidth(cells[i], widths[i]);
}
}
},
setWidth: function (e, width) {
e.style.width = (width - this.getPadding(e, "paddingRight") - this.getPadding(e, "paddingLeft")) + 'px';
},
getPadding: function (e, padding) {
return parseInt(e.currentStyle ? e.currentStyle[padding] : e.style[padding]);
},
getWindowWidth: function () {
return (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.clientWidth;
},
doWhenFrameLoads: function(func, frame) {
var f = function() {
if (frame.pageHasLoaded) {
func();
} else {
throw $continue;
}
};
if (!Try.times(f, 5)) {
return Try.times(f, 5, 1.5);
}
return true;
}
});
/*
---------- form.js -----------
*/
var $F = {};
extend($F, {
postParam: undefined, form: undefined,
init: function(form, postParam) {
this.form = form;
this.postParam = postParam;
},
setParam: function(name, value) {
p = $(name)
return p ? p.value = value : this.addHidden(name, value);
},
post: function(method) {
this.setParam(this.postParam, method);
this.form.submit();
},
addHidden: function(name, value) {
var i;
if($browser.msie){
i = document.createElement("<input type='hidden' name='"+name+"' value='"+value+"'>");
} else {
i = document.createElement("input");
i.type = "hidden";
i.name = name;
i.value = value;
}
$D.append(this.form, i);
}
});
/*
---------- lang.js -----------
*/
function $A(a) {
extend(a, ARRAY)
return a;
}
var ARRAY = {
getTotal: function() {
var t = 0;
for(var i=0;i<this.length;i++) {
t += this[i];
}
return t;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment