on 15c35c8 help sign in
elk/runtime/Init.js
112 lines 3.7 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;
23 if (typeof baseNode === 'undefined') {
24 baseNode = Render.newElement('div');
25 document.appendChild(baseNode);
26 window.addEventListener('resize', function() {
27 console.log('resize the base node');
28 baseNode.style.width = document.documentElement.clientWidth + 'px';
29 baseNode.style.height = document.documentElement.clientHeight + 'px';
30 }, true);
32 var style = Render.newElement('style');
33 style.type = 'text/css';
34 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
35 "body { font-family: calibri, helvetica, arial, sans-serif; }";
36 document.head.appendChild(style);
37 }
38 baseNode.style.overflow = 'scroll';
40 // create the actuall RTS. Any impure modules will attach themselves to this
41 // object. This permits many Elm programs to be embedded per document.
42 var elm = { notify: notify, node: baseNode, id: ElmRuntime.guid() };
44 // evaluate the given module and extract its 'main' value.
45 signalGraph = module(elm).main;
47 // make sure the signal graph is actually a signal, extract the visual model,
48 // and filter out any unused inputs.
49 var Signal = Elm.Signal(elm);
50 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
51 visualModel = signalGraph.value;
52 inputs = ElmRuntime.filterDeadInputs(inputs);
54 // Add the visualModel to the DOM
55 baseNode.appendChild(Render.render(visualModel));
57 if ('Window' in elm) {
58 var w = baseNode.clientWidth;
59 if (w !== elm.Window.dimensions.value._0) {
60 notify(elm.Window.dimensions.id,
61 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
62 }
63 }
65 // set up updates so that the DOM is adjusted as necessary.
66 var update = Render.update;
67 function domUpdate(value) {
68 update(baseNode.firstChild, visualModel, value);
69 visualModel = value;
70 return value;
71 }
73 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
75 function send(name, value) {
76 var e = document.createEvent('Event');
77 e.initEvent(name + '_' + elm.id, true, true);
78 e.value = value;
79 document.dispatchEvent(e);
80 }
81 function recv(name, handler) {
82 document.addEventListener(name + '_' + elm.id, handler);
83 }
85 return { send : send, recv : recv };
86};
88// Helper function to filter dead inputs. Not specific to initialization.
89ElmRuntime = ElmRuntime || {};
90ElmRuntime.counter = 0;
91ElmRuntime.guid = function() { ++ElmRuntime.counter; return ElmRuntime.counter; }
92ElmRuntime.filterDeadInputs = function() {
93 'use strict';
94 function isAlive(input) {
95 if (!('defaultNumberOfKids' in input)) return true;
96 var len = input.kids.length;
97 if (len == 0) return false;
98 if (len > input.defaultNumberOfKids) return true;
99 var alive = false;
100 for (var i = len; i--; ) {
101 alive = alive || isAlive(input.kids[i]);
102 }
103 return alive;
104 }
105 return function filterDeadInputs(inputs) {
106 var temp = [];
107 for (var i = inputs.length; i--; ) {
108 if (isAlive(inputs[i])) temp.push(inputs[i]);
109 }
110 return temp;
111 };
112}();