on 555cdc6 help sign in
elk/runtime/Init.js
181 lines 5.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 var container = document.createElement('div');
12 document.body.appendChild(container);
13 return init(ElmRuntime.Display.FULLSCREEN, container, module);
14};
16Elm.domNode = function(container, module) {
17 var tag = container.tagName;
18 if (tag !== 'DIV') {
19 throw new Error('Elm.node must be given a DIV, not a ' + tag + '.');
20 } else if (container.hasChildNodes()) {
21 throw new Error('Elm.node must be given an empty DIV. No children allowed!');
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, moduleToReplace) {
31 // defining state needed for an instance of the Elm RTS
32 var inputs = [];
34 function notify(id, v) {
35 var timestep = Date.now();
36 var changed = false;
37 for (var i = inputs.length; i--; ) {
38 // order is important here to avoid short-circuiting
39 changed = inputs[i].recv(timestep, id, v) || changed;
40 }
41 return changed;
42 }
44 container.offsetX = 0;
45 container.offsetY = 0;
47 var listeners = [];
48 function addListener(relevantInputs, domNode, eventName, func) {
49 domNode.addEventListener(eventName, func);
50 var listener = {
51 relevantInputs: relevantInputs,
52 domNode: domNode,
53 eventName: eventName,
54 func: func
55 };
56 listeners.push(listener);
57 }
59 // create the actual RTS. Any impure modules will attach themselves to this
60 // object. This permits many Elm programs to be embedded per document.
61 var elm = {
62 notify:notify,
63 node:container,
64 display:display,
65 id:ElmRuntime.guid(),
66 addListener:addListener,
67 inputs:inputs
68 };
70 // Set up methods to communicate with Elm program from JS.
71 function send(name, value) {
72 if (typeof value === 'undefined') return function(v) { return send(name,v); };
73 var e = document.createEvent('Event');
74 e.initEvent(name + '_' + elm.id, true, true);
75 e.value = value;
76 document.dispatchEvent(e);
77 }
78 function recv(name, handler) {
79 document.addEventListener(name + '_' + elm.id, handler);
80 }
82 recv('log', function(e) {console.log(e.value)});
83 recv('title', function(e) {document.title = e.value});
84 recv('redirect', function(e) {
85 if (e.value.length > 0) { window.location = e.value; }
86 });
88 function swap(newModule) {
89 removeListeners(listeners);
90 var div = document.createElement('div');
91 var newElm = init(display, div, newModule, elm);
92 inputs = [];
93 // elm.send = newElm.send;
94 // elm.recv = newElm.recv;
95 // elm.swap = newElm.swap;
96 return newElm;
97 }
99 var Module = {};
100 var reportAnyErrors = function() {};
101 try {
102 Module = module(elm);
103 } catch(e) {
104 var directions = "<br/>&nbsp; &nbsp; Open the developer console for more details."
105 Module.main = Elm.Text(elm).text('<code>' + e.message + directions + '</code>');
106 reportAnyErrors = function() { throw e; }
107 }
108 inputs = ElmRuntime.filterDeadInputs(inputs);
109 filterListeners(inputs, listeners);
110 if (display !== ElmRuntime.Display.NONE) {
111 var graphicsNode = initGraphics(elm, Module);
112 }
113 if (typeof moduleToReplace !== 'undefined') {
114 ElmRuntime.swap(moduleToReplace, elm);
116 // rerender scene if graphics are enabled.
117 if (typeof graphicsNode !== 'undefined') {
118 graphicsNode.value = A2( Elm.Graphics.Element(elm).spacer, 0, 0 );
119 graphicsNode.recv(0, true, 0);
120 }
121 }
123 reportAnyErrors();
124 return { send:send, recv:recv, swap:swap };
125};
127function filterListeners(inputs, listeners) {
128 loop:
129 for (var i = listeners.length; i--; ) {
130 var listener = listeners[i];
131 for (var j = inputs.length; j--; ) {
132 if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
133 continue loop;
134 }
135 }
136 listener.domNode.removeEventListener(listener.eventName, listener.func);
137 }
140function removeListeners(listeners) {
141 for (var i = listeners.length; i--; ) {
142 var listener = listeners[i];
143 listener.domNode.removeEventListener(listener.eventName, listener.func);
144 }
147function initGraphics(elm, Module) {
148 if (!('main' in Module))
149 throw new Error("'main' is missing! What do I display?!");
151 var signalGraph = Module.main;
153 // make sure the signal graph is actually a signal & extract the visual model
154 var Signal = Elm.Signal(elm);
155 if (!('recv' in signalGraph)) {
156 signalGraph = Signal.constant(signalGraph);
157 }
158 var currentScene = signalGraph.value;
160 // Add the currentScene to the DOM
161 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
162 elm.node.appendChild(Render.render(currentScene));
164 // set up updates so that the DOM is adjusted as necessary.
165 function domUpdate(newScene, currentScene) {
166 ElmRuntime.draw(function(_) {
167 Render.update(elm.node.firstChild, currentScene, newScene);
168 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
169 });
170 return newScene;
171 }
172 var renderer = A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);
174 // must check for resize after 'renderer' is created so
175 // that changes show up.
176 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
178 return renderer;
181}());