websockify/include/util.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

if ((!window.console) || (! /__debug__$/i.test(document.location.href))) {
// non-debug mode, an empty function
window.console = window.console || {};
window.console.log = function(message) {};
window.console.warn = function(message) {};
window.console.error = function(message) {};
}
2010-04-18 22:28:54 +01:00
function dirObj(obj, depth, parent) {
var msg = "";
var val = "";
if (! depth) { depth=2; }
if (! parent) { parent= ""; }
// Print the properties of the passed-in object
for (var i in obj) {
if ((depth > 1) && (typeof obj[i] == "object")) {
// Recurse attributes that are objects
2010-04-18 22:28:54 +01:00
msg += dirObj(obj[i], depth-1, parent + "." + i);
} else {
val = new String(obj[i]).replace("\n", " ");
if (val.length > 30) {
val = val.substr(0,30) + "...";
}
msg += parent + "." + i + ": " + val + "\n";
}
}
return msg;
}
/*
* Cross-browser positioning
*/
// Get DOM element position on page
function getPosition(obj) {
var x = 0, y = 0;
if (obj.offsetParent) {
do {
x += obj.offsetLeft;
y += obj.offsetTop;
} while (obj = obj.offsetParent);
}
return {'x': x, 'y': y};
}
// Get mouse event position in DOM element
function getEventPosition(e, obj) {
var evt, docX, docY, pos;
//if (!e) evt = window.event;
evt = e.event || window.event;
if (evt.pageX || evt.pageY) {
docX = evt.pageX;
docY = evt.pageY;
} else if (evt.clientX || evt.clientY) {
docX = evt.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
docY = evt.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}
pos = getPosition(obj);
return {'x': docX - pos.x, 'y': docY - pos.y};
}
/*
* Make arrays quack
*/
Array.prototype.shift8 = function () {
return this.shift();
};
Array.prototype.push8 = function (num) {
this.push(num & 0xFF);
};
Array.prototype.shift16 = function () {
return (this.shift() << 8) +
(this.shift() );
};
Array.prototype.push16 = function (num) {
this.push((num >> 8) & 0xFF,
(num ) & 0xFF );
};
Array.prototype.shift32 = function () {
return (this.shift() << 24) +
(this.shift() << 16) +
(this.shift() << 8) +
(this.shift() );
};
Array.prototype.get32 = function (off) {
return (this[off ] << 24) +
(this[off + 1] << 16) +
(this[off + 2] << 8) +
(this[off + 3] );
};
Array.prototype.push32 = function (num) {
this.push((num >> 24) & 0xFF,
(num >> 16) & 0xFF,
(num >> 8) & 0xFF,
(num ) & 0xFF );
};
Array.prototype.shiftStr = function (len) {
var arr = this.splice(0, len);
return arr.map(function (num) {
return String.fromCharCode(num); } ).join('');
};
Array.prototype.pushStr = function (str) {
var i, n = str.length;
for (i=0; i < n; i++) {
this.push(str.charCodeAt(i));
}
};
Array.prototype.shiftBytes = function (len) {
return this.splice(0, len);
};