on 17a44f3 help sign in
elk/runtime/Init.js
115 lines 3.7 kB history raw
2(function() {
3'use strict';
5Elm.fullscreen = function(module) {
6 var style = document.createElement('style');
7 style.type = 'text/css';
8 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
9 "body { font-family: calibri, helvetica, arial, sans-serif; }";
10 document.head.appendChild(style);
11 return init(ElmRuntime.Display.FULLSCREEN, document.body, module);
12};
14Elm.byId = function(id, module) {
15 var container = document.getElementById(id);
16 var tag = container.tagName;
17 if (tag !== 'DIV') {
18 throw new Error('Elm.byId must be given a div, not a ' + tag + '.');
19 }
20 while (container.hasChildNodes()) {
21 container.removeChild(container.lastChild);
22 }
23 return init(ElmRuntime.Display.COMPONENT, container, module);
24};
26Elm.worker = function(module) {
27 return init(ElmRuntime.Display.NONE, {}, module);
28};
30function init(display, container, module) {
31 // defining state needed for an instance of the Elm RTS
32 var signalGraph = null;
33 var inputs = [];
34 var visualModel = null;
36 function notify(id, v) {
37 var timestep = Date.now();
38 var hasListener = false;
39 for (var i = inputs.length; i--; ) {
40 // must maintain the order of this stmt to avoid having the ||
41 // short-circuiting the necessary work of recv
42 hasListener = inputs[i].recv(timestep, id, v) || hasListener;
43 }
44 return hasListener;
45 }
47 container.offsetX = 0;
48 container.offsetY = 0;
50 // create the actual RTS. Any impure modules will attach themselves to this
51 // object. This permits many Elm programs to be embedded per document.
52 var elm = { notify:notify,
53 node:container,
54 display:display,
55 id:ElmRuntime.guid(),
56 inputs:inputs
57 };
59 // Set up methods to communicate with Elm program from JS.
60 function send(name, value) {
61 if (typeof value === 'undefined') return function(v) { return send(name,v); };
62 var e = document.createEvent('Event');
63 e.initEvent(name + '_' + elm.id, true, true);
64 e.value = value;
65 document.dispatchEvent(e);
66 }
67 function recv(name, handler) {
68 document.addEventListener(name + '_' + elm.id, handler);
69 }
71 recv('log', function(e) {console.log(e.value)});
72 recv('title', function(e) {document.title = e.value});
73 recv('redirect', function(e) {
74 if (e.value.length > 0) { window.location = e.value; }
75 });
77 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
78 if (display === ElmRuntime.Display.NONE) {
79 return { send : send, recv : recv };
80 }
82 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
84 // evaluate the given module and extract its 'main' value.
85 signalGraph = module(elm).main;
87 // make sure the signal graph is actually a signal, extract the visual model,
88 // and filter out any unused inputs.
89 var Signal = Elm.Signal(elm);
90 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
91 visualModel = signalGraph.value;
92 inputs = ElmRuntime.filterDeadInputs(inputs);
94 // Add the visualModel to the DOM
95 var renderNode = Render.render(visualModel)
96 container.appendChild(renderNode);
97 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
99 // set up updates so that the DOM is adjusted as necessary.
100 var update = Render.update;
101 function domUpdate(value) {
102 ElmRuntime.draw(function(_) {
103 update(renderNode, visualModel, value);
104 visualModel = value;
105 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
106 });
107 return value;
108 }
110 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
112 return { send : send, recv : recv, node : container };
113};
115}());