on 60aaaf4 help sign in
elk/runtime/Init.js
183 lines 5.8 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.embed = function(module, container) {
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 var updateInProgress = false;
35 function notify(id, v) {
36 if (updateInProgress) {
37 throw new Error(
38 'The notify function has been called synchronously!\n' +
39 'This can lead to frames being dropped.\n' +
40 'Definitely report this to <https://github.com/evancz/Elm/issues>\n');
41 }
42 updateInProgress = true;
43 var timestep = Date.now();
44 for (var i = inputs.length; i--; ) {
45 inputs[i].recv(timestep, id, v);
46 }
47 updateInProgress = false;
48 }
50 var listeners = [];
51 function addListener(relevantInputs, domNode, eventName, func) {
52 domNode.addEventListener(eventName, func);
53 var listener = {
54 relevantInputs: relevantInputs,
55 domNode: domNode,
56 eventName: eventName,
57 func: func
58 };
59 listeners.push(listener);
60 }
62 // create the actual RTS. Any impure modules will attach themselves to this
63 // object. This permits many Elm programs to be embedded per document.
64 var elm = {
65 notify:notify,
66 node:container,
67 display:display,
68 id:ElmRuntime.guid(),
69 addListener:addListener,
70 inputs:inputs
71 };
73 // Set up methods to communicate with Elm program from JS.
74 function send(name, value) {
75 if (typeof value === 'undefined') return function(v) { return send(name,v); };
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 recv('log', function(e) {console.log(e.value)});
86 recv('title', function(e) {document.title = e.value});
87 recv('redirect', function(e) {
88 if (e.value.length > 0) { window.location = e.value; }
89 });
91 function swap(newModule) {
92 removeListeners(listeners);
93 var div = document.createElement('div');
94 var newElm = init(display, div, newModule, elm);
95 inputs = [];
96 // elm.send = newElm.send;
97 // elm.recv = newElm.recv;
98 // elm.swap = newElm.swap;
99 return newElm;
100 }
102 var Module = {};
103 var reportAnyErrors = function() {};
104 try {
105 Module = module.make(elm);
106 } catch(e) {
107 var directions = "<br/>&nbsp; &nbsp; Open the developer console for more details."
108 Module.main = Elm.Text.make(elm).text('<code>' + e.message + directions + '</code>');
109 reportAnyErrors = function() { throw e; }
110 }
111 inputs = ElmRuntime.filterDeadInputs(inputs);
112 filterListeners(inputs, listeners);
113 if (display !== ElmRuntime.Display.NONE) {
114 var graphicsNode = initGraphics(elm, Module);
115 }
116 if (typeof moduleToReplace !== 'undefined') {
117 ElmRuntime.swap(moduleToReplace, elm);
119 // rerender scene if graphics are enabled.
120 if (typeof graphicsNode !== 'undefined') {
121 graphicsNode.recv(0, true, 0);
122 }
123 }
125 reportAnyErrors();
126 return { send:send, recv:recv, swap:swap };
127};
129function filterListeners(inputs, listeners) {
130 loop:
131 for (var i = listeners.length; i--; ) {
132 var listener = listeners[i];
133 for (var j = inputs.length; j--; ) {
134 if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
135 continue loop;
136 }
137 }
138 listener.domNode.removeEventListener(listener.eventName, listener.func);
139 }
142function removeListeners(listeners) {
143 for (var i = listeners.length; i--; ) {
144 var listener = listeners[i];
145 listener.domNode.removeEventListener(listener.eventName, listener.func);
146 }
149function initGraphics(elm, Module) {
150 if (!('main' in Module))
151 throw new Error("'main' is missing! What do I display?!");
153 var signalGraph = Module.main;
155 // make sure the signal graph is actually a signal & extract the visual model
156 var Signal = Elm.Signal.make(elm);
157 if (!('recv' in signalGraph)) {
158 signalGraph = Signal.constant(signalGraph);
159 }
160 var currentScene = signalGraph.value;
162 // Add the currentScene to the DOM
163 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
164 elm.node.appendChild(Render.render(currentScene));
166 // set up updates so that the DOM is adjusted as necessary.
167 function domUpdate(newScene, currentScene) {
168 ElmRuntime.draw(function(_) {
169 Render.update(elm.node.firstChild, currentScene, newScene);
170 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
171 });
172 return newScene;
173 }
174 var renderer = A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);
176 // must check for resize after 'renderer' is created so
177 // that changes show up.
178 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
180 return renderer;
183}());