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.Element;
23 if (typeof baseNode === 'undefined') {
24 baseNode = Render.newElement('div');
25 document.body.appendChild(baseNode);
26 baseNode.style.width = window.innerWidth + 'px';
27 baseNode.style.height = window.innerHeight + 'px';
28 window.addEventListener('resize', function() {
29 console.log('resize the base node');
30 baseNode.style.width = window.innerWidth + 'px';
31 baseNode.style.height = window.innerHeight + 'px';
34 var style = Render.newElement('style');
35 style.type = 'text/css';
36 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
37 "body { font-family: calibri, helvetica, arial, sans-serif; }";
38 document.head.appendChild(style);
41 // create the actuall RTS. Any impure modules will attach themselves to this
42 // object. This permits many Elm programs to be embedded per document.
43 var elm = { notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs };
45 // evaluate the given module and extract its 'main' value.
46 signalGraph = module(elm).main;
48 // make sure the signal graph is actually a signal, extract the visual model,
49 // and filter out any unused inputs.
50 var Signal = Elm.Signal(elm);
51 if (!('recv' in signalGraph)) signalGraph = Signal.constant(signalGraph);
52 visualModel = signalGraph.value;
53 inputs = ElmRuntime.filterDeadInputs(inputs);
55 // Add the visualModel to the DOM
56 baseNode.appendChild(Render.render(visualModel));
58 if ('Window' in elm) {
59 var w = baseNode.clientWidth;
60 if (w !== elm.Window.dimensions.value._0) {
61 notify(elm.Window.dimensions.id,
62 Elm.Native.Utils(elm).Tuple2(w, baseNode.clientHeight));
66 // set up updates so that the DOM is adjusted as necessary.
67 var update = Render.update;
68 function domUpdate(value) {
69 update(baseNode.firstChild, visualModel, value);
74 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
76 function send(name, value) {
77 var e = document.createEvent('Event');
78 e.initEvent(name + '_' + elm.id, true, true);
80 document.dispatchEvent(e);
82 function recv(name, handler) {
83 document.addEventListener(name + '_' + elm.id, handler);
86 return { send : send, recv : recv };
89// Helper function to filter dead inputs. Not specific to initialization.
90ElmRuntime = ElmRuntime || {};
91ElmRuntime.counter = 0;
92ElmRuntime.guid = function() { ++ElmRuntime.counter; return ElmRuntime.counter; }
93ElmRuntime.filterDeadInputs = function() {
95 function isAlive(input) {
96 if (!('defaultNumberOfKids' in input)) return true;
97 var len = input.kids.length;
98 if (len == 0) return false;
99 if (len > input.defaultNumberOfKids) return true;
101 for (var i = len; i--; ) {
102 alive = alive || isAlive(input.kids[i]);
106 return function filterDeadInputs(inputs) {
108 for (var i = inputs.length; i--; ) {
109 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