on f7f137f help sign in
elk/runtime/Init.js
92 lines 3.2 kB history raw
2Elm.init = function(module, baseNode) {
3 'use strict';
5 // defining state needed for an instance of the Elm RTS
6 var signalGraph = null;
7 var inputs = [];
8 var visualModel = null;
9
10 function notify(id, v) {
11 var timestep = Date.now();
12 var hasListener = false;
13 for (var i = inputs.length; i--; ) {
14 // must maintain the order of this stmt to avoid having the ||
15 // short-circuiting the necessary work of recv
16 hasListener = inputs[i].recv(timestep, id, v) || hasListener;
17 }
18 return hasListener;
19 }
21 // ensure that baseNode exists and is properly formatted.
22 if (typeof baseNode === 'undefined') {
23 var newElement = ElmRuntime.use(ElmRuntime.Render.Utils).newElement;
24 baseNode = newElement('div');
25 document.body.appendChild(baseNode);
26 baseNode.style.width = window.innerWidth + 'px';
27 baseNode.style.height = window.innerHeight + 'px';
28 window.addEventListener('resize', function() {
29 console.log('resize the base node');
30 baseNode.style.width = window.innerWidth + 'px';
31 baseNode.style.height = window.innerHeight + 'px';
32 }, true);
34 var style = newElement('style');
35 style.type = 'text/css';
36 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
37 "body { font-family: calibri, helvetica, arial, sans-serif; }";
38 document.head.appendChild(style);
39 }
41 // create the actuall RTS. Any impure modules will attach themselves to this
42 // object. This permits many Elm programs to be embedded per document.
43 var elm = {notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs};
45 // If graphics are enabled, set up the `main` value in baseNode.
46 if (baseNode !== null) {
47 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
49 // evaluate the given module and extract its 'main' value.
50 signalGraph = module(elm).main;
52 // make sure the signal graph is actually a signal, extract the visual model,
53 // and filter out any unused inputs.
54 var Signal = Elm.Signal(elm);
55 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
56 visualModel = signalGraph.value;
57 inputs = ElmRuntime.filterDeadInputs(inputs);
59 // Add the visualModel to the DOM
60 baseNode.appendChild(Render.render(visualModel));
62 if ('Window' in elm) {
63 var w = baseNode.clientWidth;
64 if (w !== elm.Window.dimensions.value._0) {
65 notify(elm.Window.dimensions.id,
66 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
67 }
68 }
70 // set up updates so that the DOM is adjusted as necessary.
71 var update = Render.update;
72 function domUpdate(value) {
73 update(baseNode.firstChild, visualModel, value);
74 visualModel = value;
75 return value;
76 }
78 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
79 }
81 function send(name, value) {
82 var e = document.createEvent('Event');
83 e.initEvent(name + '_' + elm.id, true, true);
84 e.value = value;
85 document.dispatchEvent(e);
86 }
87 function recv(name, handler) {
88 document.addEventListener(name + '_' + elm.id, handler);
89 }
91 return { send : send, recv : recv };
92};