Update textfield example to use Actor

This commit is contained in:
Tony Garnock-Jones 2014-08-25 11:44:59 -07:00
parent a0a6a3dbfe
commit f5e59bfcd8
1 changed files with 109 additions and 127 deletions

View File

@ -1,7 +1,7 @@
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// GUI // GUI
var Route = Minimart.Route; var Actor = Minimart.Actor;
var World = Minimart.World; var World = Minimart.World;
var sub = Minimart.sub; var sub = Minimart.sub;
var pub = Minimart.pub; var pub = Minimart.pub;
@ -17,38 +17,12 @@ function piece(text, pos, lo, hi, cls) {
} }
function spawnGui() { function spawnGui() {
World.spawn({ World.spawn(new Actor(function () {
boot: function () { Actor.subscribe(
World.updateRoutes([sub(["jQuery", "#inputRow", "+keypress", __]), function () { return ["jQuery", "#inputRow", "+keypress", _$("event")]; },
sub(["fieldContents", __, __], 0, 1), function (event) {
sub(["highlight", __], 0, 1)]); var keycode = event.keyCode;
}, var character = String.fromCharCode(event.charCode);
fieldContentsSpec: Route.compileProjection(["fieldContents", _$, _$]),
highlightSpec: Route.compileProjection(["highlight", _$]),
handleEvent: function (e) {
switch (e.type) {
case "routes":
var text = "", pos = 0, highlight = false;
// BUG: escape text!
var fc = Route.matcherKeys(e.gestalt.project(this.fieldContentsSpec, true));
if (fc.length > 0) {
text = fc[0][0];
pos = fc[0][1];
}
var hl = Route.matcherKeys(e.gestalt.project(this.highlightSpec, true));
if (hl.length > 0) {
highlight = hl[0][0];
}
$("#fieldContents")[0].innerHTML = highlight
? piece(text, pos, 0, highlight[0], "normal") +
piece(text, pos, highlight[0], highlight[1], "highlight") +
piece(text, pos, highlight[1], text.length + 1, "normal")
: piece(text, pos, 0, text.length + 1, "normal");
break;
case "message":
if (e.message[0] === "jQuery") { // it's a keypress event
var keycode = e.message[3].keyCode;
var character = String.fromCharCode(e.message[3].charCode);
if (keycode === 37 /* left */) { if (keycode === 37 /* left */) {
World.send(["fieldCommand", "cursorLeft"]); World.send(["fieldCommand", "cursorLeft"]);
} else if (keycode === 39 /* right */) { } else if (keycode === 39 /* right */) {
@ -60,10 +34,30 @@ function spawnGui() {
} else if (character) { } else if (character) {
World.send(["fieldCommand", ["insert", character]]); World.send(["fieldCommand", ["insert", character]]);
} }
}
}
}
}); });
Actor.observeAdvertisers(
function () { return ["fieldContents", _$("text"), _$("pos")]; },
{ singleton: "field" },
updateDisplay);
Actor.observeAdvertisers(
function () { return ["highlight", _$("state")]; },
{ singleton: "highlight" },
updateDisplay);
function updateDisplay() {
// BUG: escape text!
var text = this.field ? this.field.text : "";
var pos = this.field ? this.field.pos : 0;
var highlight = this.highlight ? this.highlight.state : false;
$("#fieldContents")[0].innerHTML = highlight
? piece(text, pos, 0, highlight[0], "normal") +
piece(text, pos, highlight[0], highlight[1], "highlight") +
piece(text, pos, highlight[1], text.length + 1, "normal")
: piece(text, pos, 0, text.length + 1, "normal");
}
}));
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -71,92 +65,80 @@ function spawnGui() {
function spawnModel() { function spawnModel() {
var initialContents = "initial"; var initialContents = "initial";
World.spawn({ World.spawn(new Actor(function () {
fieldContents: initialContents, this.fieldContents = initialContents;
cursorPos: initialContents.length, /* positions address gaps between characters */ this.cursorPos = initialContents.length; /* positions address gaps between characters */
boot: function () {
World.updateRoutes(this.subscriptions()); Actor.advertise(
}, function () { return ["fieldContents", this.fieldContents, this.cursorPos]; });
subscriptions: function () {
return [sub(["fieldCommand", __]), Actor.subscribe(
pub(["fieldContents", this.fieldContents, this.cursorPos])]; function () { return ["fieldCommand", _$("command")]; },
}, function (command) {
handleEvent: function (e) { if (command === "cursorLeft") {
switch (e.type) {
case "message":
if (e.message[1] === "cursorLeft") {
this.cursorPos--; this.cursorPos--;
if (this.cursorPos < 0) if (this.cursorPos < 0)
this.cursorPos = 0; this.cursorPos = 0;
} else if (e.message[1] === "cursorRight") { } else if (command === "cursorRight") {
this.cursorPos++; this.cursorPos++;
if (this.cursorPos > this.fieldContents.length) if (this.cursorPos > this.fieldContents.length)
this.cursorPos = this.fieldContents.length; this.cursorPos = this.fieldContents.length;
} else if (e.message[1] === "backspace" && this.cursorPos > 0) { } else if (command === "backspace" && this.cursorPos > 0) {
this.fieldContents = this.fieldContents =
this.fieldContents.substring(0, this.cursorPos - 1) + this.fieldContents.substring(0, this.cursorPos - 1) +
this.fieldContents.substring(this.cursorPos); this.fieldContents.substring(this.cursorPos);
this.cursorPos--; this.cursorPos--;
} else if (e.message[1].constructor === Array && e.message[1][0] === "insert") { } else if (command.constructor === Array && command[0] === "insert") {
var newText = e.message[1][1]; var newText = command[1];
this.fieldContents = this.fieldContents =
this.fieldContents.substring(0, this.cursorPos) + this.fieldContents.substring(0, this.cursorPos) +
newText + newText +
this.fieldContents.substring(this.cursorPos); this.fieldContents.substring(this.cursorPos);
this.cursorPos += newText.length; this.cursorPos += newText.length;
} }
World.updateRoutes(this.subscriptions()); this.updateRoutes();
break;
}
}
}); });
}));
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Search engine // Search engine
function spawnSearch() { function spawnSearch() {
World.spawn({ World.spawn(new Actor(function () {
fieldContents: "", var self = this;
highlight: false, self.fieldContents = "";
fieldContentsSpec: Route.compileProjection(["fieldContents", _$, _$]), self.highlight = false;
boot: function () {
World.updateRoutes(this.subscriptions()); Actor.advertise(
}, function () { return ["highlight", self.highlight]; });
subscriptions: function () {
return [sub(["jQuery", "#searchBox", "input", __]), Actor.subscribe(
sub(["fieldContents", __, __], 0, 1), function () { return ["jQuery", "#searchBox", "input", _$("event")]; },
pub(["highlight", this.highlight])]; search);
},
search: function () { Actor.observeAdvertisers(
var searchtext = $("#searchBox")[0].value; function () { return ["fieldContents", _$("text"), _$("pos")]; },
var oldHighlight = this.highlight; { singleton: "field" },
if (searchtext) { function () {
var pos = this.fieldContents.indexOf(searchtext); self.fieldContents = self.field ? self.field.text : "";
this.highlight = (pos !== -1) && [pos, pos + searchtext.length]; search();
} else {
this.highlight = false;
}
if (JSON.stringify(oldHighlight) !== JSON.stringify(this.highlight)) {
World.updateRoutes(this.subscriptions());
}
},
handleEvent: function (e) {
switch (e.type) {
case "routes":
var fc = Route.matcherKeys(e.gestalt.project(this.fieldContentsSpec, true));
if (fc.length > 0) {
this.fieldContents = fc[0][0];
}
this.search();
break;
case "message":
if (e.message[0] === "jQuery") { // it's a search box input event
this.search();
}
}
}
}); });
function search() {
var searchtext = $("#searchBox")[0].value;
var oldHighlight = self.highlight;
if (searchtext) {
var pos = self.fieldContents.indexOf(searchtext);
self.highlight = (pos !== -1) && [pos, pos + searchtext.length];
} else {
self.highlight = false;
}
if (JSON.stringify(oldHighlight) !== JSON.stringify(self.highlight)) {
self.updateRoutes();
}
}
}));
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////