changeset 131:8285784f5ff5

Implement previous and next buttons
author Mike Pavone <pavone@retrodev.com>
date Wed, 07 Aug 2013 10:01:28 -0700
parents 6a1a716bb8c6
children a503a329acfa
files editor.js src/editor.tp
diffstat 2 files changed, 164 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/editor.js	Tue Aug 06 00:53:03 2013 -0700
+++ b/editor.js	Wed Aug 07 10:01:28 2013 -0700
@@ -16,6 +16,24 @@
 	}
 };
 
+object.prototype.getNext = function(curNode) {
+	for (var i = 0; i < this.messages.length-1; i++) {
+		if (this.messages[i] == curNode) {
+			return this.messages[i+1];
+		}
+	}
+	return this.up.getNext(this);
+};
+
+object.prototype.getPrev = function(curNode) {
+	for (var i = 1; i < this.messages.length; i++) {
+		if (this.messages[i] == curNode) {
+			return this.messages[i - 1];
+		}
+	}
+	return this.up.getPrev(this);
+};
+
 lambda.prototype.toHTML = function(node, up) {
 	this.up = up
 	var astNode = this;
@@ -43,6 +61,50 @@
 	node.appendChild(el);
 };
 
+lambda.prototype.getNext = function(curNode) {
+	for (var i = 0; i < this.args.length; i++) {
+		if (this.args[i] == curNode) {
+			if ((i + 1) < this.args.length) {
+				return this.args[i+1];
+			} else if(this.expressions.length) {
+				return this.expressions[0];
+			} else {
+				break;
+			}
+		}
+	}
+	for (var i = 0; i < this.expressions.length-1; i++) {
+		if (this.expressions[i] == curNode) {
+			return this.expressions[i+1];
+		}
+	}
+	return this.up.getNext(this);
+};
+
+lambda.prototype.getPrev = function(curNode) {
+	for (var i = 0; i < this.args.length; i++) {
+		if (this.args[i] == curNode) {
+			if (i > 0) {
+				return this.args[i-1];
+			} else {
+				return this.up.getPrev(this);
+			}
+		}
+	}
+	for (var i = 0; i < this.expressions.length; i++) {
+		if (this.expressions[i] == curNode) {
+			if (i > 0) {
+				return this.expressions[i-1];
+			} else if(this.args.length) {
+				return this.args[this.args.length-1];
+			} else {
+				break;
+			}
+		}
+	}
+	return this.up.getPrev(this);
+};
+
 assignment.prototype.toHTML = function(node, up) {
 	this.up = up;
 	var astNode = this;
@@ -62,6 +124,14 @@
 	this.expression.toHTML(base, this);
 };
 
+assignment.prototype.getNext = function(curNode) {
+	return this.up.getNext(this);
+};
+
+assignment.prototype.getPrev = function(curNode) {
+	return this;
+};
+
 op.prototype.toHTML = function(node, up) {
 	this.up = up;
 	var astNode = this;
@@ -85,6 +155,20 @@
 	node.appendChild(base);
 };
 
+op.prototype.getNext = function(curNode) {
+	if (this.left == curNode) {
+		return this.right;
+	}
+	return this.up.getNext(this);
+};
+
+op.prototype.getPrev = function(curNode) {
+	if (this.right == curNode) {
+		return this.left;
+	}
+	return this.up.getPrev(this);
+};
+
 intlit.prototype.toHTML = function(node, up) {
 	this.up = up;
 	var astNode = this;
@@ -136,6 +220,32 @@
 	node.appendChild(this.domNode);
 };
 
+listlit.prototype.getNext = arraylit.prototype.getNext = function(curNode) {
+	for (var i = 0; i < this.val.length; i++) {
+		if (this.val[i] == curNode) {
+			if ((i + 1) < this.val.length) {
+				return this.val[i+1];
+			} else {
+				break;
+			}
+		}
+	}
+	return this.up.getNext(this);
+};
+
+listlit.prototype.getPrev = arraylit.prototype.getPrev = function(curNode) {
+	for (var i = 0; i < this.val.length; i++) {
+		if (this.val[i] == curNode) {
+			if (i > 0) {
+				return this.val[i-1];
+			} else {
+				break;
+			}
+		}
+	}
+	return this.up.getPrev(this);
+};
+
 arraylit.prototype.toHTML = function(node, up) {
 	this.up = up;
 	this.domNode = newEl('span', {
@@ -178,6 +288,33 @@
 	node.appendChild(base);
 };
 
+funcall.prototype.getNext = function(curNode) {
+	if (this.receiver == curNode && this.args.length) {
+		return this.args[0];
+	}
+	for (var i = 0; i < this.args.length-1; i++) {
+		if (this.args[i] == curNode) {
+			return this.args[i+1];
+		}
+	}
+	return this.up.getNext(this);
+};
+
+funcall.prototype.getPrev = function(curNode) {
+	for (var i = 0; i < this.args.length; i++) {
+		if (this.args[i] == curNode) {
+			if (i > 0) {
+				return this.args[i-1];
+			} else if (this.receiver) {
+				return this.receiver;
+			} else {
+				break;
+			}
+		}
+	}
+	return this.up.getPrev(this);
+};
+
 symbol.prototype.toHTML = function(node, up) {
 	this.up = up;
 	var astNode = this;
--- a/src/editor.tp	Tue Aug 06 00:53:03 2013 -0700
+++ b/src/editor.tp	Wed Aug 07 10:01:28 2013 -0700
@@ -49,18 +49,22 @@
 		stopPropagation <- :Blah {
 		}
 	}
+
 	selection <- #{
 		valid? <- true
 		in <- {
 			(innode domNode) onclick: fakeEvent
 		}
 		out <- {
-			fakeEvent <- #{
-				stopPropagation <- :Blah {
-				}
-			}
 			((astnode up) domNode) onclick: fakeEvent
 		}
+		next <- {
+			(((astnode up) getNext: astnode) domNode) onclick: fakeEvent
+		}
+
+		previous <- {
+			(((astnode up) getPrev: astnode) domNode) onclick: fakeEvent
+		}
 	}
 }
 
@@ -80,6 +84,13 @@
 			}
 			((astnode up) domNode) onclick: fakeEvent
 		}
+		next <- {
+			console log: "selection next"
+			(((astnode up) getNext: astnode) domNode) onclick: fakeEvent
+		}
+		previous <- {
+			(((astnode up) getPrev: astnode) domNode) onclick: fakeEvent
+		}
 	}
 }
 
@@ -321,6 +332,18 @@
 		}
 	}
 
+	(q: "#next") onclick!: :event {
+		if: (selection valid?) {
+			selection next
+		}
+	}
+
+	(q: "#prev") onclick!: :event {
+		if: (selection valid?) {
+			selection previous
+		}
+	}
+
 	path <- (window location) pathname
 	if: (path indexOf: "/edit/") = 0 {
 		editFile: (path substr: 5)