touchpage 0.2.1

control panel server with shared-state web controls
Documentation
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="UTF-8">
  <title>Main</title>
  <style> body { margin: 0; }</style>
</head>
<body>
<div id="elm"></div>
<script>{{elm-main}}</script>
<script>
  
  function getTextWidth (t, f) {
    var blah = getTextMetrics(t, f);
    return blah.width; 
  };

  function getTextMetrics (text, font) {
     // re-use canvas object for better performance
     var canvas = getTextMetrics.canvas || (getTextMetrics.canvas = document.createElement("canvas"));
     var context = canvas.getContext("2d");
     context.font = font;
     var metrics = context.measureText(text);
     return metrics;
    };

  function requestTextSize (rts) {
    // console.log (" rts: " + JSON.stringify(rts, null, 4)); 
    metrics = getTextMetrics(rts.string, rts.font);
    // console.log( "getTextMetricssss: " + metrics.width);
    var reply = { width : metrics.width, controlId : rts.controlId };
    // console.log (" reply: " + JSON.stringify(reply, null, 4));
    app.ports.receiveTextMetrics.send(reply);
  }
 
</script>
<script>
  var mySockets = {};

  function sendSocketCommand(wat) {
    // console.log( "ssc: " +  JSON.stringify(wat, null, 4));
    if (wat.cmd == "connect") 
    {
      // console.log("connecting!");
      socket = new WebSocket(wat.address, wat.protocol);
      socket.onmessage = function (event) {
        // console.log( "onmessage: " +  JSON.stringify(event.data, null, 4));
        app.ports.receiveSocketMsg.send({ name : wat.name
                                        , msg : "data"
                                        , data : event.data} );
    	}
    	mySockets[wat.name] = socket;
    }
    else if (wat.cmd == "send")
    {
      // console.log("sending to socket: " + wat.name );
      mySockets[wat.name].send(wat.content); 
    }
    else if (wat.cmd == "close")
    {
      // console.log("closing socket: " + wat.name);
      mySockets[wat.name].close();
      delete mySockets[wat.name];
    }
  }

</script>
<script>
    var app = Elm.Main.init( 
        { flags: { location : document.location.origin || "", 
                   wsport : {{websockets-port}},
                   width : window.innerWidth, 
                   height : window.innerHeight
                 }, 
          node: document.getElementById("elm") 
        });
    if (document.getElementById("elm"))
    {
      document.getElementById("elm").innerText = 'This is a headless program, meaning there is nothing to show here.\n\nI started the program anyway though, and you can access it as `app` in the developer console.';
    }
    app.ports.sendSocketCommand.subscribe(sendSocketCommand);
    app.ports.requestTextSize.subscribe(requestTextSize);
</script>
</body>
</html>