Skip to content

Commit

Permalink
fixed event handling to use monad env
Browse files Browse the repository at this point in the history
  • Loading branch information
zot committed Apr 24, 2012
1 parent 0a3bc74 commit f32582e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
10 changes: 8 additions & 2 deletions notebook.cs
Expand Up @@ -153,14 +153,19 @@ exBox.removeChild exBox.lastChild

prepExpr = (txt)-> if txt[0] in '=!' then txt else "=#{txt}"

envFor = (exBox)->
write: (msg)-> exBox.innerHTML += msg + "<br>"
envFor = (box)->
exBox = getBox box
write: (msg)->
div = document.createElement('div')
div.innerHTML = "#{msg}\n"
exBox.appendChild(div)
prompt:(msg, cont)-> cont(window.prompt(msg))

makeOutputBox = (source)->
node = document.createElement 'div'
node.setAttribute 'LeisureOutput', ''
node.setAttribute 'Leisure', ''
node.setAttribute 'LeisureBox', ''
node.setAttribute 'class', 'output'
node.setAttribute 'contentEditable', 'false'
node.source = source
Expand Down Expand Up @@ -283,5 +288,6 @@ nodeEnd child
root.bindNotebook = bindNotebook
root.evalOutput = evalOutput
root.cleanOutput = cleanOutput
root.envFor = envFor
#root.selection = -> window.getSelection().getRangeAt(0)
#root.test = -> flatten(root.selection().cloneContents().childNodes[0])
12 changes: 10 additions & 2 deletions notebook.js
Expand Up @@ -240,10 +240,15 @@
}
};

envFor = function envFor(exBox) {
envFor = function envFor(box) {
var exBox;
exBox = getBox(box);
return {
write: function write(msg) {
return exBox.innerHTML += msg + "<br>";
var div;
div = document.createElement('div');
div.innerHTML = "" + msg + "\n";
return exBox.appendChild(div);
},
prompt: function prompt(msg, cont) {
return cont(window.prompt(msg));
Expand All @@ -256,6 +261,7 @@
node = document.createElement('div');
node.setAttribute('LeisureOutput', '');
node.setAttribute('Leisure', '');
node.setAttribute('LeisureBox', '');
node.setAttribute('class', 'output');
node.setAttribute('contentEditable', 'false');
node.source = source;
Expand Down Expand Up @@ -447,4 +453,6 @@

root.cleanOutput = cleanOutput;

root.envFor = envFor;

}).call(this);
6 changes: 4 additions & 2 deletions prim.cs
Expand Up @@ -68,10 +68,10 @@ cont Math.floor(from() + Math.random() * (to() - from() + 1))
eventCmds = []
running = false

leisureEvent = (leisureFuncName, evt)->
leisureEvent = (leisureFuncName, evt, env)->
currentEvent = evt
monad = Leisure.eval("#{Leisure.nameSub(leisureFuncName)}()")(laz(evt))
runMonad monad, defaultEnv, ->
runMonad monad, (env ? defaultEnv), ->

runMonad = (monad, env, cont)->
eventCmds.push ->
Expand All @@ -86,6 +86,8 @@ cont Math.floor(from() + Math.random() * (to() - from() + 1))
runMonads = (monad, env, cont)->
if monad?.cmd?
monad.cmd env, (value) ->
#change to this use setTimeout, kind of like this (which doesn't quite work)
#if monad.binding? then global.setTimeout((->runMonads monad.binding(-> value), env, cont), 1)
if monad.binding? then runMonads monad.binding(-> value), env, cont
else cont(value)
else throw new Error("Attempted to run something that's not a monad")
Expand Down
4 changes: 2 additions & 2 deletions prim.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions ttt_v2.html
Expand Up @@ -18,11 +18,15 @@
<script src="ttt.js"></script>

<script>//<[!CDATA[

function initSVG(el) {
el.getSVGDocument().defaultView.leisureEnv = el.leisureEnv = Notebook.envFor(el);
//delay this to the next cycle
window.setTimeout(tttLoaded, 1);
}
function tttLoaded()
{
tttWin = document.getElementById('scene').getSVGDocument().defaultView
tttWin.leisureEvent = function(funcName) {Prim.leisureEvent(funcName, tttWin.event);}
tttWin.leisureEvent = function(funcName) {Prim.leisureEvent(funcName, tttWin.event, tttWin.leisureEnv);}
tttWin.leisureEvent('newGame')
}

Expand Down Expand Up @@ -200,7 +204,7 @@
<pre style="position: absolute; width: 100%; min-height: 100%; margin: 0; padding: 0" id="editor" class="cell" contentEditable="true">


svg-map = hash-from-list [ 'id', 'scene', 'data', 'ttt.svg', 'onload', 'tttLoaded()', 'width', 500, 'height', 600 ]
svg-map = hash-from-list [ 'id', 'scene', 'data', 'ttt.svg', 'onload', 'initSVG(this)', 'width', 500, 'height', 600 ]

svg-file svg-map

Expand Down

0 comments on commit f32582e

Please sign in to comment.