working on the printing code.

This commit is contained in:
Danny Yoo 2011-09-20 15:59:11 -04:00
parent 3aa90e6b40
commit 6d00f88717
8 changed files with 59 additions and 37 deletions

View File

@ -46,7 +46,7 @@
params.put(this, true);
var parent = document.createElement("span");
parent.appendChild(document.createTextNode('#&'));
parent.appendChild(baselib.format.toDomNode(this.val, params));
parent.appendChild(params.recur(this.val));
return parent;
};

View File

@ -221,11 +221,11 @@
ToDomNodeParameters.prototype.incrementDepth = function() {
return new ToDomNodeParameters({ mode : this.mode,
depth: this.depth + 1,
objectCounter: objectCounter });
objectCounter: this.objectCounter });
};
// getMode: -> (U "print" "display" "write")
// getMode: -> (U "print" "display" "write" "constructor")
ToDomNodeParameters.prototype.getMode = function() {
if (this.mode) {
return this.mode;
@ -336,9 +336,7 @@
};
// toDomNode: scheme-value -> dom-node
var toDomNode = function(x, params) {
var node;
var coerseToParams = function(params) {
if (params === 'write') {
params = new ToDomNodeParameters({'mode' : 'write'});
} else if (params === 'print') {
@ -350,6 +348,14 @@
} else {
params = params || new ToDomNodeParameters({'mode' : 'display'});
}
return params;
};
// toDomNode: scheme-value -> dom-node
var toDomNode = function(x, params) {
var node;
params = coerseToParams(params);
if (baselib.numbers.isSchemeNumber(x)) {
node = numberToDomNode(x, params);
@ -379,7 +385,7 @@
if (x === null) {
node = document.createElement("span");
node.appendChild(document.createTextNode("null"));
node.appendChild(document.createTextNode("#<null>"));
$(node).addClass("null");
return node;
}
@ -387,18 +393,10 @@
if (x === undefined) {
node = document.createElement("span");
node.appendChild(document.createTextNode("#<undefined>"));
$(node).addClass("undefined");
return node;
}
if (typeof(x) === 'object') {
if (params.containsKey(x)) {
node = document.createElement("span");
node.appendChild(document.createTextNode("#" + params.get(x)));
return node;
}
}
if (baselib.functions.isProcedure(x)) {
node = document.createElement("span");
node.appendChild(document.createTextNode('#<procedure: ' + x.displayName + '>'));
@ -406,31 +404,36 @@
return node;
}
if (typeof(x) !== 'object' && typeof(x) !== 'function') {
if (typeof(x) !== 'object') {
node = document.createElement("span");
node.appendChild(document.createTextNode(x.toString()));
return node;
}
// Otherwise, we know the value is an object.
if (params.containsKey(x)) {
node = document.createElement("span");
node.appendChild(document.createTextNode("#" + params.get(x)));
return node;
}
var returnVal;
if (x.nodeType) {
returnVal = x;
returnVal = x;
} else if (x.toDomNode) {
returnVal = x.toDomNode(params);
returnVal = x.toDomNode(params);
} else if (params.getMode() === 'write' && x.toWrittenString) {
node = document.createElement("span");
node.appendChild(document.createTextNode(x.toWrittenString(params)));
returnVal = node;
returnVal = node;
} else if (params.getMode() === 'display' && x.toDisplayedString) {
node = document.createElement("span");
node.appendChild(document.createTextNode(x.toDisplayedString(params)));
returnVal = node;
returnVal = node;
} else {
node = document.createElement("span");
node.appendChild(document.createTextNode(x.toString()));
returnVal = node;
returnVal = node;
}
params.remove(x);
return returnVal;
};

View File

@ -30,6 +30,24 @@
Empty.prototype.toDisplayedString = function (cache) { return "empty"; };
Empty.prototype.toString = function (cache) { return "()"; };
Empty.prototype.toDomNode = function(params) {
if (params.getMode() === "display") {
return $("<span/>").text("()").get(0);
} else if (params.getMode() === "write") {
return $("<span/>").text("()").get(0);
} else if (params.getMode() === "print") {
if (params.getDepth() === 0) {
return $("<span/>").text("'()").get(0);
} else {
return $("<span/>").text("()").get(0);
}
} else if (params.getMode() === "constructor") {
return $("<span/>").text("(list)").get(0);
} else {
return $("<span/>").text("()").get(0);
}
};
// Empty.append: (listof X) -> (listof X)
Empty.prototype.append = function (b) {
@ -131,25 +149,25 @@
Cons.prototype.toDomNode = function (cache) {
cache.put(this, true);
Cons.prototype.toDomNode = function (params) {
params.put(this, true);
var node = document.createElement("span");
node.appendChild(document.createTextNode("("));
var p = this;
while (p instanceof Cons) {
node.appendChild(baselib.format.toDomNode(p.first, cache));
node.appendChild(params.recur(p.first));
p = p.rest;
if (p !== EMPTY) {
node.appendChild(document.createTextNode(" "));
}
if (typeof (p) === 'object' && cache.containsKey(p)) {
if (typeof (p) === 'object' && params.containsKey(p)) {
break;
}
}
if (p !== EMPTY) {
node.appendChild(document.createTextNode("."));
node.appendChild(document.createTextNode(" "));
node.appendChild(baselib.format.toDomNode(p, cache));
node.appendChild(params.recur(p));
}
node.appendChild(document.createTextNode(")"));

View File

@ -38,7 +38,7 @@
$(node).append(document.createTextNode(this._constructorName));
for(i = 0; i < this._fields.length; i++) {
$(node).append(document.createTextNode(" "));
$(node).append(baselib.format.toDomNode(this._fields[i], params));
$(node).append(params.recur(this._fields[i]));
}
$(node).append(document.createTextNode(")"));
return node;

View File

@ -82,12 +82,12 @@
return "#(" + texts.join(" ") + ")";
};
Vector.prototype.toDomNode = function (cache) {
Vector.prototype.toDomNode = function (params) {
var node = document.createElement("span"), i;
cache.put(this, true);
params.put(this, true);
node.appendChild(document.createTextNode("#("));
for (i = 0; i < this.length(); i++) {
node.appendChild(baselib.format.toDomNode(this.ref(i), cache));
node.appendChild(params.recur(this.ref(i)));
if (i !== this.length() - 1) {
node.appendChild(document.createTextNode(" "));
}

View File

@ -225,7 +225,7 @@
this.params = {
// print-as-expression: boolean
'print-as-expression' : true,
'print-as-expression' : false,
// print-mode: (one-of "write" "print" "constructor")
'print-mode' : 'write',

View File

@ -46,7 +46,7 @@ var injectImageMethods = function(r, img) {
installHackToSupportAnimatedGifs(r);
ctx.drawImage(r.animationHackImg, x, y);
};
r.toDomNode = function() {
r.toDomNode = function(params) {
return img.cloneNode(true);
};
};

View File

@ -95,7 +95,7 @@ WorldConfigOption.prototype.configure = function(config) {
};
WorldConfigOption.prototype.toDomNode = function(cache) {
WorldConfigOption.prototype.toDomNode = function(params) {
var span = document.createElement('span');
span.appendChild(document.createTextNode("(" + this.name + " ...)"));
return span;
@ -311,7 +311,7 @@ ToDraw.prototype.toRawHandler = function(MACHINE, toplevelNode) {
v.render(ctx, 0, 0);
success([toplevelNode, reusableCanvasNode]);
} else {
success([toplevelNode, rawJsworld.node_to_tree(plt.baselib.format.toDomNode(v))]);
success([toplevelNode, rawJsworld.node_to_tree(plt.baselib.format.toDomNode(v, MACHINE.params['print-mode']))]);
}
});
};
@ -346,7 +346,8 @@ DefaultDrawingOutput.prototype.toRawHandler = function(MACHINE, toplevelNode) {
var that = this;
var worldFunction = function(world, success) {
success([toplevelNode,
rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world))]);
rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world,
MACHINE.params['print-mode']))]);
//k(rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world)));
};
var cssFunction = function(w, success) { success([]); }