changeset 12:6e4851a204a5

Add ability to load code into editor
author Mike Pavone <pavone@retrodev.com>
date Wed, 21 Mar 2012 23:13:51 -0700
parents 5447cff52da6
children e69f5ab0a453
files editor.css editor.tp index.html jsbackend.js
diffstat 4 files changed, 89 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/editor.css	Wed Mar 21 21:42:38 2012 -0700
+++ b/editor.css	Wed Mar 21 23:13:51 2012 -0700
@@ -27,9 +27,29 @@
 	display: inline-block;
 	padding: 0;
 	margin: 0;
+	white-space: pre;
+	font-family: monospace;
+	overflow: auto;
+	height: 100%;
 }
 
-ul
+#editor
+{
+	display: none;
+	height: 100%;
+}
+
+body.editorMode > #editor
+{
+	display: block;
+}
+
+body.editorMode > #browser
+{
+	display: none;
+}
+
+#editor ul
 {
 	border-style: solid;
 	border-color: black;
@@ -44,22 +64,22 @@
 	overflow: auto;
 }
 
-ul:first-child
+#editor ul:first-child
 {
 	border-bottom-width: 0px;
 }
 
-#operators, .showops > #builtin
+#editor #operators, #editor .showops > #builtin
 {
 	display: none;
 }
 
-.showops > #operators
+#editor .showops > #operators
 {
 	display: block;
 }
 
-li
+#editor li
 {
 	display: inline-block;
 	border-style: solid;
--- a/editor.tp	Wed Mar 21 21:42:38 2012 -0700
+++ b/editor.tp	Wed Mar 21 23:13:51 2012 -0700
@@ -5,6 +5,7 @@
 each <- foreign: :iterable fun {}
 addClass <- foreign: :node className {}
 removeClass <- foreign: :node className {}
+get <- foreign: :url onSuccess onFail onOther {}
 
 //tabletprog JS helpers
 setP <- foreign: :object property val {}
@@ -16,10 +17,26 @@
 }
 
 main <- {
-	each: (qall: "li") :idx el {
+	//bind handlers for file browser links
+	each: (qall: "a") :idx el {
+		el setP: "onclick" :event {
+			console log: "click"
+			link <- foreign: this
+			get: (link getP: "href") :request {
+				addClass: (q: "body") "editorMode"
+				console log: (request getP: "responseText")
+				(q: "#src") setP: "textContent" (request getP: "responseText")
+			}
+			console log: "returning false"
+			foreign: false
+		}
+	}
+	
+	//bind handlers for editor buttons
+	each: (qall: ".controls li") :idx el {
 		el setP: "onclick" :event {
 			srcel <- (q: "#src")
-			srcel setP: "innerHTML" (srcel getP: "innerHTML") + (el getP: "innerHTML")
+			srcel setP: "textContent" (srcel getP: "textContent") + (el getP: "textContent")
 		}
 	}
 	(q: "#ops_button") setP: "onclick" :event {
--- a/index.html	Wed Mar 21 21:42:38 2012 -0700
+++ b/index.html	Wed Mar 21 23:13:51 2012 -0700
@@ -13,29 +13,37 @@
 	<link rel="stylesheet" href="editor.css">
 </head>
 <body>
-	<div class="controls">
-		<ul id="builtin">
-			<li id="ops_button">operators</li>
-			<li>get:</li>
-			<li>set:</li>
-			<li>length</li>
-			<li>if: then: else:</li>
+	<div id="browser">
+		<ul>
+			<li><a href="editor.tp">editor.tp</a></li>
+			<li><a href="samples/fib.tp">samples/fib.tp</a></li>
 		</ul>
-		<ul id="operators">
-			<li id="builtin_button">builtins</li>
-			<li>&lt;</li>
-			<li>&gt;</li>
-			<li>+</li>
-			<li>-</li>
-			<li>*</li>
-			<li>/</li>
-			<li>&&</li>
-			<li>||</li>
-		</ul>
-		<ul id="inscope"></ul>
-	</div><div id="src"></div><div class="controls">
-		<ul></ul>
-		<ul></ul>
+	</div>
+	<div id="editor">
+		<div class="controls">
+			<ul id="builtin">
+				<li id="ops_button">operators</li>
+				<li>get:</li>
+				<li>set:</li>
+				<li>length</li>
+				<li>if: then: else:</li>
+			</ul>
+			<ul id="operators">
+				<li id="builtin_button">builtins</li>
+				<li>&lt;</li>
+				<li>&gt;</li>
+				<li>+</li>
+				<li>-</li>
+				<li>*</li>
+				<li>/</li>
+				<li>&&</li>
+				<li>||</li>
+			</ul>
+			<ul id="inscope"></ul>
+		</div><div id="src"></div><div class="controls">
+			<ul></ul>
+			<ul></ul>
+		</div>
 	</div>
 </body>
 </html>
--- a/jsbackend.js	Wed Mar 21 21:42:38 2012 -0700
+++ b/jsbackend.js	Wed Mar 21 23:13:51 2012 -0700
@@ -62,6 +62,15 @@
 
 funcall.prototype.toJS = function(symbols) {
 	var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
+	if (name == 'foreign') {
+		if ((this.args[0] instanceof lambda) || (this.args[0] instanceof object)) {
+			return null;
+		} else if(this.args[0] instanceof symbol) {
+			return this.args[0].name;
+		} else {
+			throw new Error("Unexpected AST type for foreign:");
+		}
+	}
 	var args = this.args.slice(0, this.args.length);
 	if (this.receiver) {
 		args.splice(0, 0, this.receiver);
@@ -171,15 +180,17 @@
 			break;
 		}
 	}
-	if (this.expression instanceof funcall && this.expression.name == 'foreign:') {
+	var val = this.expression.toJS(symbols);
+	if (val === null) {
 		return null;
 	}
-	return prefix + this.symbol.toJS(symbols) + ' = ' + this.expression.toJS(symbols);
+	return prefix + this.symbol.toJS(symbols) + ' = ' + val;
 };
 assignment.prototype.toJSObject = function(symbols) {
 	symbols.defineMsg(this.symbol.name, this.expression);
-	if (this.expression instanceof funcall && this.expression.name == 'foreign:') {
+	var val = this.expression.toJS(symbols);
+	if (val === null) {
 		return null;
 	}
-	return this.symbol.toJS(symbols) + ': ' + this.expression.toJS(symbols);
+	return this.symbol.toJS(symbols) + ': ' + val;
 };