5Elm.fullscreen = function(module, ports) {
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, ports || {});
16Elm.embed = function(module, container, ports) {
17 var tag = container.tagName;
19 throw new Error('Elm.node must be given a DIV, not a ' + tag + '.');
20 } else if (container.hasChildNodes()) {
21 throw new Error('Elm.node must be given an empty DIV. No children allowed!');
23 return init(ElmRuntime.Display.COMPONENT, container, module, ports || {});
26Elm.worker = function(module, ports) {
27 return init(ElmRuntime.Display.NONE, {}, module, ports || {});
30function init(display, container, module, ports, moduleToReplace) {
31 // defining state needed for an instance of the Elm RTS
34 var updateInProgress = false;
35 function notify(id, v) {
36 if (updateInProgress) {
38 'The notify function has been called synchronously!\n' +
39 'This can lead to frames being dropped.\n' +
40 'Definitely report this to <https://github.com/evancz/Elm/issues>\n');
42 updateInProgress = true;
43 var timestep = Date.now();
44 for (var i = inputs.length; i--; ) {
45 inputs[i].recv(timestep, id, v);
47 updateInProgress = false;
51 function addListener(relevantInputs, domNode, eventName, func) {
52 domNode.addEventListener(eventName, func);
54 relevantInputs: relevantInputs,
59 listeners.push(listener);
63 for (var key in ports) {
66 // create the actual RTS. Any impure modules will attach themselves to this
67 // object. This permits many Elm programs to be embedded per document.
73 addListener:addListener,
75 ports: { incoming:ports, outgoing:{}, uses:portUses }
78 function swap(newModule) {
79 removeListeners(listeners);
80 var div = document.createElement('div');
81 var newElm = init(display, div, newModule, ports, elm);
83 // elm.swap = newElm.swap;
88 removeListeners(listeners);
93 var reportAnyErrors = function() {};
95 Module = module.make(elm);
98 var directions = "<br/> Open the developer console for more details."
99 Module.main = Elm.Text.make(elm).leftAligned('<code>' + e.message + directions + '</code>');
100 reportAnyErrors = function() { throw e; }
102 inputs = ElmRuntime.filterDeadInputs(inputs);
103 filterListeners(inputs, listeners);
104 addReceivers(elm.ports.outgoing);
105 if (display !== ElmRuntime.Display.NONE) {
106 var graphicsNode = initGraphics(elm, Module);
108 if (typeof moduleToReplace !== 'undefined') {
109 ElmRuntime.swap(moduleToReplace, elm);
111 // rerender scene if graphics are enabled.
112 if (typeof graphicsNode !== 'undefined') {
113 graphicsNode.recv(0, true, 0);
120 ports:elm.ports.outgoing,
125function checkPorts(elm) {
126 var portUses = elm.ports.uses;
127 for (var key in portUses) {
128 var uses = portUses[key]
131 "Initialization Error: provided port '" + key +
132 "' to a module that does not take it as in input.\n" +
133 "Remove '" + key + "' from the module initialization code.");
134 } else if (uses > 1) {
136 "Initialization Error: port '" + key +
137 "' has been declared multiple times in the Elm code.\n" +
138 "Remove declarations until there is exactly one.");
143function filterListeners(inputs, listeners) {
145 for (var i = listeners.length; i--; ) {
146 var listener = listeners[i];
147 for (var j = inputs.length; j--; ) {
148 if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
152 listener.domNode.removeEventListener(listener.eventName, listener.func);
156function removeListeners(listeners) {
157 for (var i = listeners.length; i--; ) {
158 var listener = listeners[i];
159 listener.domNode.removeEventListener(listener.eventName, listener.func);
163// add receivers for built-in ports if they are defined
164function addReceivers(ports) {
165 if ('log' in ports) {
166 ports.log.subscribe(function(v) { console.log(v) });
168 if ('stdout' in ports) {
169 var process = process || {};
170 var handler = process.stdout
171 ? function(v) { process.stdout.write(v); }
172 : function(v) { console.log(v); };
173 ports.stdout.subscribe(handler);
175 if ('stderr' in ports) {
176 var process = process || {};
177 var handler = process.stderr
178 ? function(v) { process.stderr.write(v); }
179 : function(v) { console.log('Error:' + v); };
180 ports.stderr.subscribe(handler);
182 if ('title' in ports) {
183 if (typeof ports.title === 'string') {
184 document.title = ports.title;
186 ports.title.subscribe(function(v) { document.title = v; });
189 if ('redirect' in ports) {
190 ports.redirect.subscribe(function(v) {
191 if (v.length > 0) window.location = v;
194 if ('favicon' in ports) {
195 if (typeof ports.favicon === 'string') {
196 changeFavicon(ports.favicon);
198 ports.favicon.subscribe(changeFavicon);
201 function changeFavicon(src) {
202 var link = document.createElement('link');
203 var oldLink = document.getElementById('elm-favicon');
204 link.id = 'elm-favicon';
205 link.rel = 'shortcut icon';
208 document.head.removeChild(oldLink);
210 document.head.appendChild(link);
214function initGraphics(elm, Module) {
215 if (!('main' in Module)) {
216 throw new Error("'main' is missing! What do I display?!");
219 var signalGraph = Module.main;
221 // make sure the signal graph is actually a signal & extract the visual model
222 var Signal = Elm.Signal.make(elm);
223 if (!('recv' in signalGraph)) {
224 signalGraph = Signal.constant(signalGraph);
226 var currentScene = signalGraph.value;
228 // Add the currentScene to the DOM
229 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
230 elm.node.appendChild(Render.render(currentScene));
232 // set up updates so that the DOM is adjusted as necessary.
233 var savedScene = currentScene;
234 function domUpdate(newScene) {
235 ElmRuntime.draw(function(_) {
236 Render.update(elm.node.firstChild, savedScene, newScene);
237 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
238 savedScene = newScene;
241 var renderer = A2(Signal.lift, domUpdate, signalGraph);
243 // must check for resize after 'renderer' is created so
244 // that changes show up.
245 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
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