2Elm.init = function(module, baseNode) {
5 // defining state needed for an instance of the Elm RTS
6 var signalGraph = null;
8 var visualModel = null;
10 function notify(id, v) {
11 var timestep = Date.now();
12 var hasListener = false;
13 for (var i = inputs.length; i--; ) {
14 // must maintain the order of this stmt to avoid having the ||
15 // short-circuiting the necessary work of recv
16 hasListener = inputs[i].recv(timestep, id, v) || hasListener;
21 // ensure that baseNode exists and is properly formatted.
22 if (typeof baseNode === 'undefined') {
23 baseNode = document.body;
24 var style = document.createElement('style');
25 style.type = 'text/css';
26 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
27 "body { font-family: calibri, helvetica, arial, sans-serif; }";
28 document.head.appendChild(style);
31 // create the actuall RTS. Any impure modules will attach themselves to this
32 // object. This permits many Elm programs to be embedded per document.
33 var elm = {notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs};
35 // Set up methods to communicate with Elm program from JS.
36 function send(name, value) {
37 if (typeof value === 'undefined') return function(v) { return send(name,v) };
38 var e = document.createEvent('Event');
39 e.initEvent(name + '_' + elm.id, true, true);
41 document.dispatchEvent(e);
43 function recv(name, handler) {
44 document.addEventListener(name + '_' + elm.id, handler);
47 recv('elm_log', function(e) {console.log(e.value)});
48 recv('elm_title', function(e) {document.title = e.value});
49 recv('elm_redirect', function(e) {
50 if (e.value.length > 0) { window.location = e.value; }
53 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
54 if (baseNode === null) return { send : send, recv : recv };
57 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
59 // evaluate the given module and extract its 'main' value.
60 signalGraph = module(elm).main;
62 // make sure the signal graph is actually a signal, extract the visual model,
63 // and filter out any unused inputs.
64 var Signal = Elm.Signal(elm);
65 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
66 visualModel = signalGraph.value;
67 inputs = ElmRuntime.filterDeadInputs(inputs);
69 var tuple2 = Elm.Native.Utils(elm).Tuple2;
70 function adjustWindow() {
71 if ('Window' in elm) {
72 var w = baseNode.clientWidth;
73 if (w !== elm.Window.dimensions.value._0) {
74 notify(elm.Window.dimensions.id,
75 tuple2(w, document.body === baseNode ?
76 window.innerHeight : baseNode.clientHeight));
81 // Add the visualModel to the DOM
82 var renderNode = Render.render(visualModel)
83 baseNode.appendChild(renderNode);
86 // set up updates so that the DOM is adjusted as necessary.
87 var update = Render.update;
88 function domUpdate(value) {
89 ElmRuntime.draw(function(_) {
90 update(renderNode, visualModel, value);
97 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
99 return { send : send, recv : recv };
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list