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 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
48 if (baseNode === null) return { send : send, recv : recv };
51 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
53 // evaluate the given module and extract its 'main' value.
54 signalGraph = module(elm).main;
56 // make sure the signal graph is actually a signal, extract the visual model,
57 // and filter out any unused inputs.
58 var Signal = Elm.Signal(elm);
59 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
60 visualModel = signalGraph.value;
61 inputs = ElmRuntime.filterDeadInputs(inputs);
63 var tuple2 = Elm.Native.Utils(elm).Tuple2;
64 function adjustWindow() {
65 if ('Window' in elm) {
66 var w = baseNode.clientWidth;
67 if (w !== elm.Window.dimensions.value._0) {
68 notify(elm.Window.dimensions.id,
69 tuple2(w, document.body === baseNode ?
70 window.innerHeight : baseNode.clientHeight));
75 // Add the visualModel to the DOM
76 var renderNode = Render.render(visualModel)
77 baseNode.appendChild(renderNode);
80 // set up updates so that the DOM is adjusted as necessary.
81 var update = Render.update;
82 function domUpdate(value) {
83 update(renderNode, visualModel, value);
89 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
91 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