on 6810174 help sign in
elk/runtime/Init.js
177 lines 5.6 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 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 var listeners = [];
45 function addListener(relevantInputs, domNode, eventName, func) {
46 domNode.addEventListener(eventName, func);
47 var listener = {
48 relevantInputs: relevantInputs,
49 domNode: domNode,
50 eventName: eventName,
51 func: func
52 };
53 listeners.push(listener);
54 }
56 // create the actual RTS. Any impure modules will attach themselves to this
57 // object. This permits many Elm programs to be embedded per document.
58 var elm = {
59 notify:notify,
60 node:container,
61 display:display,
62 id:ElmRuntime.guid(),
63 addListener:addListener,
64 inputs:inputs
65 };
67 // Set up methods to communicate with Elm program from JS.
68 function send(name, value) {
69 if (typeof value === 'undefined') return function(v) { return send(name,v); };
70 var e = document.createEvent('Event');
71 e.initEvent(name + '_' + elm.id, true, true);
72 e.value = value;
73 document.dispatchEvent(e);
74 }
75 function recv(name, handler) {
76 document.addEventListener(name + '_' + elm.id, handler);
77 }
79 recv('log', function(e) {console.log(e.value)});
80 recv('title', function(e) {document.title = e.value});
81 recv('redirect', function(e) {
82 if (e.value.length > 0) { window.location = e.value; }
83 });
85 function swap(newModule) {
86 removeListeners(listeners);
87 var div = document.createElement('div');
88 var newElm = init(display, div, newModule, elm);
89 inputs = [];
90 // elm.send = newElm.send;
91 // elm.recv = newElm.recv;
92 // elm.swap = newElm.swap;
93 return newElm;
94 }
96 var Module = {};
97 var reportAnyErrors = function() {};
98 try {
99 Module = module.make(elm);
100 } catch(e) {
101 var directions = "<br/>&nbsp; &nbsp; Open the developer console for more details."
102 Module.main = Elm.Text.make(elm).text('<code>' + e.message + directions + '</code>');
103 reportAnyErrors = function() { throw e; }
104 }
105 inputs = ElmRuntime.filterDeadInputs(inputs);
106 filterListeners(inputs, listeners);
107 if (display !== ElmRuntime.Display.NONE) {
108 var graphicsNode = initGraphics(elm, Module);
109 }
110 if (typeof moduleToReplace !== 'undefined') {
111 ElmRuntime.swap(moduleToReplace, elm);
113 // rerender scene if graphics are enabled.
114 if (typeof graphicsNode !== 'undefined') {
115 graphicsNode.recv(0, true, 0);
116 }
117 }
119 reportAnyErrors();
120 return { send:send, recv:recv, swap:swap };
121};
123function filterListeners(inputs, listeners) {
124 loop:
125 for (var i = listeners.length; i--; ) {
126 var listener = listeners[i];
127 for (var j = inputs.length; j--; ) {
128 if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
129 continue loop;
130 }
131 }
132 listener.domNode.removeEventListener(listener.eventName, listener.func);
133 }
136function removeListeners(listeners) {
137 for (var i = listeners.length; i--; ) {
138 var listener = listeners[i];
139 listener.domNode.removeEventListener(listener.eventName, listener.func);
140 }
143function initGraphics(elm, Module) {
144 if (!('main' in Module))
145 throw new Error("'main' is missing! What do I display?!");
147 var signalGraph = Module.main;
149 // make sure the signal graph is actually a signal & extract the visual model
150 var Signal = Elm.Signal.make(elm);
151 if (!('recv' in signalGraph)) {
152 signalGraph = Signal.constant(signalGraph);
153 }
154 var currentScene = signalGraph.value;
156 // Add the currentScene to the DOM
157 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
158 elm.node.appendChild(Render.render(currentScene));
160 // set up updates so that the DOM is adjusted as necessary.
161 function domUpdate(newScene, currentScene) {
162 ElmRuntime.draw(function(_) {
163 Render.update(elm.node.firstChild, currentScene, newScene);
164 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
165 });
166 return newScene;
167 }
168 var renderer = A3(Signal.foldp, F2(domUpdate), currentScene, signalGraph);
170 // must check for resize after 'renderer' is created so
171 // that changes show up.
172 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
174 return renderer;
177}());