on 35fe7d6 help sign in
elk/runtime/Init.js
117 lines 3.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.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) {
33 // defining state needed for an instance of the Elm RTS
34 var signalGraph = null;
35 var inputs = [];
36 var visualModel = null;
38 function notify(id, v) {
39 var timestep = Date.now();
40 var hasListener = false;
41 for (var i = inputs.length; i--; ) {
42 // must maintain the order of this stmt to avoid having the ||
43 // short-circuiting the necessary work of recv
44 hasListener = inputs[i].recv(timestep, id, v) || hasListener;
45 }
46 return hasListener;
47 }
49 container.offsetX = 0;
50 container.offsetY = 0;
52 // create the actual RTS. Any impure modules will attach themselves to this
53 // object. This permits many Elm programs to be embedded per document.
54 var elm = { notify:notify,
55 node:container,
56 display:display,
57 id:ElmRuntime.guid(),
58 inputs:inputs
59 };
61 // Set up methods to communicate with Elm program from JS.
62 function send(name, value) {
63 if (typeof value === 'undefined') return function(v) { return send(name,v); };
64 var e = document.createEvent('Event');
65 e.initEvent(name + '_' + elm.id, true, true);
66 e.value = value;
67 document.dispatchEvent(e);
68 }
69 function recv(name, handler) {
70 document.addEventListener(name + '_' + elm.id, handler);
71 }
73 recv('log', function(e) {console.log(e.value)});
74 recv('title', function(e) {document.title = e.value});
75 recv('redirect', function(e) {
76 if (e.value.length > 0) { window.location = e.value; }
77 });
79 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
80 if (display === ElmRuntime.Display.NONE) {
81 module(elm);
82 return { send : send, recv : recv };
83 }
85 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
87 // evaluate the given module and extract its 'main' value.
88 signalGraph = module(elm).main;
90 // make sure the signal graph is actually a signal, extract the visual model,
91 // and filter out any unused inputs.
92 var Signal = Elm.Signal(elm);
93 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
94 visualModel = signalGraph.value;
95 inputs = ElmRuntime.filterDeadInputs(inputs);
97 // Add the visualModel to the DOM
98 container.appendChild(Render.render(visualModel));
99 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
101 // set up updates so that the DOM is adjusted as necessary.
102 var update = Render.update;
103 function domUpdate(value) {
104 ElmRuntime.draw(function(_) {
105 update(container.firstChild, visualModel, value);
106 visualModel = value;
107 if (elm.Native.Window) elm.Native.Window.resizeIfNeeded();
108 });
109 return value;
110 }
112 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
114 return { send : send, recv : recv, node : container };
115};
117}());