on 7dbb052 help sign in
elk/runtime/Init.js
113 lines 3.8 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 var Render = ElmRuntime.Render.Element;
23 if (typeof baseNode === 'undefined') {
24 baseNode = Render.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 = Render.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 // evaluate the given module and extract its 'main' value.
46 signalGraph = module(elm).main;
48 // make sure the signal graph is actually a signal, extract the visual model,
49 // and filter out any unused inputs.
50 var Signal = Elm.Signal(elm);
51 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
52 visualModel = signalGraph.value;
53 inputs = ElmRuntime.filterDeadInputs(inputs);
55 // Add the visualModel to the DOM
56 baseNode.appendChild(Render.render(visualModel));
58 if ('Window' in elm) {
59 var w = baseNode.clientWidth;
60 if (w !== elm.Window.dimensions.value._0) {
61 notify(elm.Window.dimensions.id,
62 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
63 }
64 }
66 // set up updates so that the DOM is adjusted as necessary.
67 var update = Render.update;
68 function domUpdate(value) {
69 update(baseNode.firstChild, visualModel, value);
70 visualModel = value;
71 return value;
72 }
74 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
76 function send(name, value) {
77 var e = document.createEvent('Event');
78 e.initEvent(name + '_' + elm.id, true, true);
79 e.value = value;
80 document.dispatchEvent(e);
81 }
82 function recv(name, handler) {
83 document.addEventListener(name + '_' + elm.id, handler);
84 }
86 return { send : send, recv : recv };
87};
89// Helper function to filter dead inputs. Not specific to initialization.
90ElmRuntime = ElmRuntime || {};
91ElmRuntime.counter = 0;
92ElmRuntime.guid = function() { ++ElmRuntime.counter; return ElmRuntime.counter; }
93ElmRuntime.filterDeadInputs = function() {
94 'use strict';
95 function isAlive(input) {
96 if (!('defaultNumberOfKids' in input)) return true;
97 var len = input.kids.length;
98 if (len == 0) return false;
99 if (len > input.defaultNumberOfKids) return true;
100 var alive = false;
101 for (var i = len; i--; ) {
102 alive = alive || isAlive(input.kids[i]);
103 }
104 return alive;
105 }
106 return function filterDeadInputs(inputs) {
107 var temp = [];
108 for (var i = inputs.length; i--; ) {
109 if (isAlive(inputs[i])) temp.push(inputs[i]);
110 }
111 return temp;
112 };
113}();