on 6c2fb97 help sign in
elk/runtime/Init.js
134 lines 4.3 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 var container = document.createElement('div');
12 document.body.appendChild(container);
13 return init(ElmRuntime.Display.FULLSCREEN, container, module);
14};
16Elm.byId = function(id, module) {
17 var container = document.getElementById(id);
18 var tag = container.tagName;
19 if (tag !== 'DIV') {
20 throw new Error('Elm.byId must be given a div, not a ' + tag + '.');
21 }
22 while (container.hasChildNodes()) {
23 container.removeChild(container.lastChild);
24 }
25 return init(ElmRuntime.Display.COMPONENT, container, module);
26};
28Elm.worker = function(module) {
29 return init(ElmRuntime.Display.NONE, {}, module);
30};
32function init(display, container, module, moduleToReplace) {
33 // defining state needed for an instance of the Elm RTS
34 var inputs = [];
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 function swap(newModule) {
78 var div = document.createElement('div');
79 if (container.id) { div.id = container.id; }
80 var newElm = init(display, div, newModule, elm);
81 // elm.send = newElm.send;
82 // elm.recv = newElm.recv;
83 // elm.swap = newElm.swap;
84 return newElm;
85 }
87 var Module = module(elm);
88 inputs = ElmRuntime.filterDeadInputs(inputs);
89 if (display !== ElmRuntime.Display.NONE) {
90 var graphicsNode = initGraphics(elm, Module);
91 }
92 if (typeof moduleToReplace !== 'undefined') {
93 ElmRuntime.swap(moduleToReplace, elm);
95 // rerender scene if graphics are enabled.
96 if (typeof graphicsNode !== 'undefined') {
97 graphicsNode.value = A2( Elm.Graphics.Element(elm).spacer, 0, 0 );
98 graphicsNode.recv(0, true, 0);
99 }
100 }
102 return { send:send, recv:recv, swap:swap };
103};
105function initGraphics(elm, Module) {
106 if (!('main' in Module))
107 throw new Error("'main' is missing! What do I display?!");
109 var signalGraph = Module.main;
111 // make sure the signal graph is actually a signal & extract the visual model
112 var Signal = Elm.Signal(elm);
113 if (!('recv' in signalGraph)) {
114 signalGraph = Signal.constant(signalGraph);
115 }
116 var currentScene = signalGraph.value;
118 // Add the currentScene to the DOM
119 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
120 elm.node.appendChild(Render.render(currentScene));
121 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
123 // set up updates so that the DOM is adjusted as necessary.
124 function domUpdate(newScene, currentScene) {
125 ElmRuntime.draw(function(_) {
126 Render.update(elm.node.firstChild, currentScene, newScene);
127 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
128 });
129 return newScene;
130 }
131 return A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);
134}());