on c2fc03c help sign in
elk/runtime/Init.js
92 lines 3.1 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 baseNode = document.body;
24 var style = document.createElement('style');
25 style.type = 'text/css';
26 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
27 "body { font-family: calibri, helvetica, arial, sans-serif; }";
28 document.head.appendChild(style);
29 }
31 // create the actuall RTS. Any impure modules will attach themselves to this
32 // object. This permits many Elm programs to be embedded per document.
33 var elm = {notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs};
35 // Set up methods to communicate with Elm program from JS.
36 function send(name, value) {
37 if (typeof value === 'undefined') return function(v) { return send(name,v) };
38 var e = document.createEvent('Event');
39 e.initEvent(name + '_' + elm.id, true, true);
40 e.value = value;
41 document.dispatchEvent(e);
42 }
43 function recv(name, handler) {
44 document.addEventListener(name + '_' + elm.id, handler);
45 }
47 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
48 if (baseNode === null) return { send : send, recv : recv };
51 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
53 // evaluate the given module and extract its 'main' value.
54 signalGraph = module(elm).main;
56 // make sure the signal graph is actually a signal, extract the visual model,
57 // and filter out any unused inputs.
58 var Signal = Elm.Signal(elm);
59 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
60 visualModel = signalGraph.value;
61 inputs = ElmRuntime.filterDeadInputs(inputs);
63 var tuple2 = Elm.Native.Utils(elm).Tuple2;
64 function adjustWindow() {
65 if ('Window' in elm) {
66 var w = baseNode.clientWidth;
67 if (w !== elm.Window.dimensions.value._0) {
68 notify(elm.Window.dimensions.id,
69 tuple2(w, document.body === baseNode ?
70 window.innerHeight : baseNode.clientHeight));
71 }
72 }
73 }
75 // Add the visualModel to the DOM
76 var renderNode = Render.render(visualModel)
77 baseNode.appendChild(renderNode);
78 adjustWindow();
80 // set up updates so that the DOM is adjusted as necessary.
81 var update = Render.update;
82 function domUpdate(value) {
83 update(renderNode, visualModel, value);
84 visualModel = value;
85 adjustWindow();
86 return value;
87 }
89 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
91 return { send : send, recv : recv };
92};