Permalink
Browse files

Some error handling so you don't crash it with the wrong keypress.

  • Loading branch information...
1 parent 7b20ee3 commit 9fb94a35c79798310dc66b4ef5f1b50cf1e70df4 @luqui committed Jun 14, 2011
Showing with 19 additions and 19 deletions.
  1. +9 −17 demo/structural.html
  2. +10 −2 structural.js
View
@@ -55,37 +55,29 @@
var container = elt('div');
$(document.body).append(container);
+var zipper = new Zipper([], Exp_eplus_box.make());
+
var update = function(z) {
+ if (!z) return;
+ zipper = z;
container.empty();
container.append(elt('pre', {}, render_zipper(z)));
};
-var zipper = new Zipper([], Exp_eplus_box.make());
-
update(zipper);
-var unfocus = function() {
- if (typeof(zipper.expr) !== 'string' && zipper.expr.head.unfocus) {
- zipper = zipper.expr.head.unfocus(zipper)
- }
-};
-
$(document.body).keydown(function(e) {
if (37 == e.which) { // left
- unfocus();
- update(zipper = zipper.left());
+ update(zipper.left());
}
else if (38 == e.which) { // up
- unfocus();
- update(zipper = zipper.up());
+ update(zipper.up());
}
else if (39 == e.which) { // right
- unfocus();
- update(zipper = zipper.right());
+ update(zipper.right());
}
else if (40 == e.which) { // down
- unfocus();
- update(zipper = zipper.down(0));
+ update(zipper.down(0));
}
else {
//console.log(e.which, e.charCode);
@@ -97,7 +89,7 @@
var ch = String.fromCharCode(e.charCode);
var tokresult = zipper.expr.head.parse(zipper)(ch);
if (tokresult) {
- update(zipper = tokresult[0]);
+ update(tokresult[0]);
}
}
});
View
@@ -235,10 +235,14 @@ var Zipper = object({
return this.contexts[0].args.length;
},
up: function() {
+ if (this.contexts.length == 0) return null;
+
var cx = this.contexts[0];
return new Zipper(this.contexts.slice(1), cx.fill(this.expr));
},
down: function(n) {
+ if (!(0 <= n && typeof(this.expr) === 'object' && n < this.expr.args.length)) return null;
+
var args = this.expr.args.slice(0);
var focus = args[n];
args[n] = null;
@@ -248,10 +252,14 @@ var Zipper = object({
focus);
},
left: function() {
- return this.up().down(this.position()-1);
+ var u = this.up();
+ if (!u) return null;
+ return u.down(this.position()-1);
},
right: function() {
- return this.up().down(this.position()+1);
+ var u = this.up();
+ if (!u) return null;
+ return u.down(this.position()+1);
},
});

0 comments on commit 9fb94a3

Please sign in to comment.