Skip to content

Commit

Permalink
Merge branch 'bug17', remote branch 'mike/17-errorConsole' into bug17
Browse files Browse the repository at this point in the history
  • Loading branch information
Andor committed Jul 24, 2010
2 parents 1a22ee1 + aaaf134 commit 4a2f33b
Show file tree
Hide file tree
Showing 2 changed files with 286 additions and 1 deletion.
1 change: 1 addition & 0 deletions demos/acorn/acorn.js
Expand Up @@ -50,6 +50,7 @@ function mouseReleased(){

function keyDown(){
document.getElementById('key').innerHTML = key;
ps.println(key);
}

function render() {
Expand Down
286 changes: 285 additions & 1 deletion psapi.js
Expand Up @@ -19,6 +19,7 @@ function PointStream(){

// defaults
var attn = [0.01, 0.0, 0.003];
var logBuffer = [];

// browser detection to handle differences such as mouse scrolling
var browser = -1;
Expand Down Expand Up @@ -459,7 +460,267 @@ function PointStream(){
key = keyCodeMap(evt.keyCode, evt.shiftKey);
}
type();
}
};

// tinylog lite JavaScript library
// http://purl.eligrey.com/tinylog/lite
/*global tinylog,print*/
var tinylogLite = (function() {
"use strict";

var tinylogLite = {},
undef = "undefined",
func = "function",
False = !1,
True = !0,
logLimit = 512,
log = "log";

if (typeof tinylog !== undef && typeof tinylog[log] === func) {
// pre-existing tinylog present
tinylogLite[log] = tinylog[log];
} else if (typeof document !== undef && !document.fake) {
(function() {
// DOM document
var doc = document,

$div = "div",
$style = "style",
$title = "title",

containerStyles = {
zIndex: 10000,
position: "fixed",
bottom: "0px",
width: "100%",
height: "15%",
fontFamily: "sans-serif",
color: "#ccc",
backgroundColor: "black"
},
outputStyles = {
position: "relative",
fontFamily: "monospace",
overflow: "auto",
height: "100%",
paddingTop: "5px"
},
resizerStyles = {
height: "5px",
marginTop: "-5px",
cursor: "n-resize",
backgroundColor: "darkgrey"
},
closeButtonStyles = {
position: "absolute",
top: "5px",
right: "20px",
color: "#111",
MozBorderRadius: "4px",
webkitBorderRadius: "4px",
borderRadius: "4px",
cursor: "pointer",
fontWeight: "normal",
textAlign: "center",
padding: "3px 5px",
backgroundColor: "#333",
fontSize: "12px"
},
entryStyles = {
//borderBottom: "1px solid #d3d3d3",
minHeight: "16px"
},
entryTextStyles = {
fontSize: "12px",
margin: "0 8px 0 8px",
maxWidth: "100%",
whiteSpace: "pre-wrap",
overflow: "auto"
},

view = doc.defaultView,
docElem = doc.documentElement,
docElemStyle = docElem[$style],

setStyles = function() {
var i = arguments.length,
elemStyle, styles, style;

while (i--) {
styles = arguments[i--];
elemStyle = arguments[i][$style];

for (style in styles) {
if (styles.hasOwnProperty(style)) {
elemStyle[style] = styles[style];
}
}
}
},

observer = function(obj, event, handler) {
if (obj.addEventListener) {
obj.addEventListener(event, handler, False);
} else if (obj.attachEvent) {
obj.attachEvent("on" + event, handler);
}
return [obj, event, handler];
},
unobserve = function(obj, event, handler) {
if (obj.removeEventListener) {
obj.removeEventListener(event, handler, False);
} else if (obj.detachEvent) {
obj.detachEvent("on" + event, handler);
}
},
clearChildren = function(node) {
var children = node.childNodes,
child = children.length;

while (child--) {
node.removeChild(children.item(0));
}
},
append = function(to, elem) {
return to.appendChild(elem);
},
createElement = function(localName) {
return doc.createElement(localName);
},
createTextNode = function(text) {
return doc.createTextNode(text);
},

createLog = tinylogLite[log] = function(message) {
// don't show output log until called once
var uninit,
originalPadding = docElemStyle.paddingBottom,
container = createElement($div),
containerStyle = container[$style],
resizer = append(container, createElement($div)),
output = append(container, createElement($div)),
closeButton = append(container, createElement($div)),
resizingLog = False,
previousHeight = False,
previousScrollTop = False,
messages = 0,

updateSafetyMargin = function() {
// have a blank space large enough to fit the output box at the page bottom
docElemStyle.paddingBottom = container.clientHeight + "px";
},
setContainerHeight = function(height) {
var viewHeight = view.innerHeight,
resizerHeight = resizer.clientHeight;

// constrain the container inside the viewport's dimensions
if (height < 0) {
height = 0;
} else if (height + resizerHeight > viewHeight) {
height = viewHeight - resizerHeight;
}

containerStyle.height = height / viewHeight * 100 + "%";

updateSafetyMargin();
},
observers = [
observer(doc, "mousemove", function(evt) {
if (resizingLog) {
setContainerHeight(view.innerHeight - evt.clientY);
output.scrollTop = previousScrollTop;
}
}),

observer(doc, "mouseup", function() {
if (resizingLog) {
resizingLog = previousScrollTop = False;
}
}),

observer(resizer, "dblclick", function(evt) {
evt.preventDefault();

if (previousHeight) {
setContainerHeight(previousHeight);
previousHeight = False;
} else {
previousHeight = container.clientHeight;
containerStyle.height = "0px";
}
}),

observer(resizer, "mousedown", function(evt) {
evt.preventDefault();
resizingLog = True;
previousScrollTop = output.scrollTop;
}),

observer(resizer, "contextmenu", function() {
resizingLog = False;
}),

observer(closeButton, "click", function() {
uninit();
})
];

uninit = function() {
// remove observers
var i = observers.length;

while (i--) {
unobserve.apply(tinylogLite, observers[i]);
}

// remove tinylog lite from the DOM
docElem.removeChild(container);
docElemStyle.paddingBottom = originalPadding;

clearChildren(output);
clearChildren(container);

tinylogLite[log] = createLog;
};

setStyles(
container, containerStyles, output, outputStyles, resizer, resizerStyles, closeButton, closeButtonStyles);

closeButton[$title] = "Close Log";
append(closeButton, createTextNode("\u2716"));

resizer[$title] = "Double-click to toggle log minimization";

docElem.insertBefore(container, docElem.firstChild);

tinylogLite[log] = function(message) {
if (messages === logLimit) {
output.removeChild(output.firstChild);
} else {
messages++;
}

var entry = append(output, createElement($div)),
entryText = append(entry, createElement($div));

entry[$title] = (new Date()).toLocaleTimeString();

setStyles(
entry, entryStyles, entryText, entryTextStyles);

append(entryText, createTextNode(message));
output.scrollTop = output.scrollHeight;
};

tinylogLite[log](message);
};
}());
} else if (typeof print === func) { // JS shell
tinylogLite[log] = print;
}

return tinylogLite;
}());

/**
*/
Expand Down Expand Up @@ -709,6 +970,29 @@ function PointStream(){
}
},

/**
*/
console: window.console || tinylogLite,

println: function(message) {
var bufferLen = logBuffer.length;
if (bufferLen) {
tinylogLite.log(logBuffer.join(""));
logBuffer.length = 0; // clear log buffer
}

if (arguments.length === 0 && bufferLen === 0) {
tinylogLite.log("");
} else if (arguments.length !== 0) {
tinylogLite.log(message);
}
},

print: function(message) {
logBuffer.push(message);
},

/**
cvs
renderCB
Expand Down

0 comments on commit 4a2f33b

Please sign in to comment.