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 var Render = ElmRuntime.Render;
23 if (typeof baseNode === 'undefined') {
24 baseNode = Render.newElement('div');
25 document.appendChild(baseNode);
26 window.addEventListener('resize', function() {
27 console.log('resize the base node');
28 baseNode.style.width = document.documentElement.clientWidth + 'px';
29 baseNode.style.height = document.documentElement.clientHeight + 'px';
32 baseNode.style.overflow = 'scroll';
34 // create the actuall RTS. Any impure modules will attach themselves to this
35 // object. This permits many Elm programs to be embedded per document.
36 var elm = { notify: notify, node: baseNode, id: ElmRuntime.guid() };
38 // evaluate the given module and extract its 'main' value.
39 signalGraph = module(elm).main;
41 // make sure the signal graph is actually a signal, extract the visual model,
42 // and filter out any unused inputs.
43 var Signal = Elm.Signal(elm);
44 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
45 visualModel = signalGraph.value;
46 inputs = ElmRuntime.filterDeadInputs(inputs);
48 // Add the visualModel to the DOM
49 baseNode.appendChild(Render.render(visualModel));
51 if (p'Window' in elm) {
52 var w = baseNode.clientWidth;
53 if (w !== elm.Window.dimensions.value._0) {
54 notify(elm.Window.dimensions.id,
55 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
59 // set up updates so that the DOM is adjusted as necessary.
60 var update = Render.update;
61 function domUpdate(value) {
62 update(baseNode.firstChild, visualModel, value);
67 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
69 function send(name, value) {
70 var e = document.createEvent('Event');
71 e.initEvent(name + '_' + elm.id, true, true);
73 document.dispatchEvent(e);
75 function recv(name, handler) {
76 document.addEventListener(name + '_' + elm.id, handler);
79 return { send : send, recv : recv };
82// Helper function to filter dead inputs. Not specific to initialization.
83ElmRuntime = ElmRuntime || {};
84ElmRuntime.counter = 0;
85ElmRuntime.guid = function() { ++ElmRuntime.counter; return ElmRuntime.counter; }
86ElmRuntime.filterDeadInputs = function() {
88 function isAlive(input) {
89 if (!('defaultNumberOfKids' in input)) return true;
90 var len = input.kids.length;
91 if (len == 0) return false;
92 if (len > input.defaultNumberOfKids) return true;
94 for (var i = len; i--; ) {
95 alive = alive || isAlive(input.kids[i]);
99 return function filterDeadInputs(inputs) {
101 for (var i = inputs.length; i--; ) {
102 if (isAlive(inputs[i])) temp.push(inputs[i]);
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