5var Display = { FULLSCREEN: 0, COMPONENT: 1, NONE: 2 };
8Elm.fullscreen = function(module, ports) {
9 var style = document.createElement('style');
10 style.type = 'text/css';
11 style.innerHTML = "html,head,body { padding:0; margin:0; }" +
12 "body { font-family: calibri, helvetica, arial, sans-serif; }";
13 document.head.appendChild(style);
14 var container = document.createElement('div');
15 document.body.appendChild(container);
16 return init(Display.FULLSCREEN, container, module, ports || {});
19Elm.embed = function(module, container, ports) {
20 var tag = container.tagName;
22 throw new Error('Elm.node must be given a DIV, not a ' + tag + '.');
23 } else if (container.hasChildNodes()) {
24 throw new Error('Elm.node must be given an empty DIV. No children allowed!');
26 return init(Display.COMPONENT, container, module, ports || {});
29Elm.worker = function(module, ports) {
30 return init(Display.NONE, {}, module, ports || {});
33function init(display, container, module, ports, moduleToReplace) {
34 // defining state needed for an instance of the Elm RTS
38 * Elm's time traveling debugger lets you interrupt the smooth flow of time
39 * by pausing and continuing program execution. To ensure the user sees a
40 * program that moves smoothly through the pause/continue time gap,
41 * we need to adjsut the value of Date.now().
43 var timer = function() {
46 var now = function() {
47 return Date.now() - inducedDelay;
50 var addDelay = function(d) {
60 var updateInProgress = false;
61 function notify(id, v) {
62 if (updateInProgress) {
64 'The notify function has been called synchronously!\n' +
65 'This can lead to frames being dropped.\n' +
66 'Definitely report this to <https://github.com/elm-lang/Elm/issues>\n');
68 updateInProgress = true;
69 var timestep = timer.now();
70 for (var i = inputs.length; i--; ) {
71 inputs[i].recv(timestep, id, v);
73 updateInProgress = false;
75 function setTimeout(func, delay) {
76 window.setTimeout(func, delay);
80 function addListener(relevantInputs, domNode, eventName, func) {
81 domNode.addEventListener(eventName, func);
83 relevantInputs: relevantInputs,
88 listeners.push(listener);
92 for (var key in ports) {
95 // create the actual RTS. Any impure modules will attach themselves to this
96 // object. This permits many Elm programs to be embedded per document.
99 setTimeout:setTimeout,
102 addListener:addListener,
105 ports: { incoming:ports, outgoing:{}, uses:portUses }
108 function swap(newModule) {
109 removeListeners(listeners);
110 var div = document.createElement('div');
111 var newElm = init(display, div, newModule, ports, elm);
113 // elm.swap = newElm.swap;
118 removeListeners(listeners);
123 var reportAnyErrors = function() {};
125 Module = module.make(elm);
128 var directions = "<br/> Open the developer console for more details."
129 Module.main = Elm.Text.make(elm).leftAligned('<code>' + e.message + directions + '</code>');
130 reportAnyErrors = function() { throw e; }
132 inputs = filterDeadInputs(inputs);
133 filterListeners(inputs, listeners);
134 addReceivers(elm.ports.outgoing);
135 if (display !== Display.NONE) {
136 var graphicsNode = initGraphics(elm, Module);
138 if (typeof moduleToReplace !== 'undefined') {
139 ElmRuntime.swap(moduleToReplace, elm);
141 // rerender scene if graphics are enabled.
142 if (typeof graphicsNode !== 'undefined') {
143 graphicsNode.recv(0, true, 0);
150 ports:elm.ports.outgoing,
155function checkPorts(elm) {
156 var portUses = elm.ports.uses;
157 for (var key in portUses) {
158 var uses = portUses[key]
161 "Initialization Error: provided port '" + key +
162 "' to a module that does not take it as in input.\n" +
163 "Remove '" + key + "' from the module initialization code.");
164 } else if (uses > 1) {
166 "Initialization Error: port '" + key +
167 "' has been declared multiple times in the Elm code.\n" +
168 "Remove declarations until there is exactly one.");
173function filterListeners(inputs, listeners) {
175 for (var i = listeners.length; i--; ) {
176 var listener = listeners[i];
177 for (var j = inputs.length; j--; ) {
178 if (listener.relevantInputs.indexOf(inputs[j].id) >= 0) {
182 listener.domNode.removeEventListener(listener.eventName, listener.func);
186function removeListeners(listeners) {
187 for (var i = listeners.length; i--; ) {
188 var listener = listeners[i];
189 listener.domNode.removeEventListener(listener.eventName, listener.func);
193// add receivers for built-in ports if they are defined
194function addReceivers(ports) {
195 if ('log' in ports) {
196 ports.log.subscribe(function(v) { console.log(v) });
198 if ('stdout' in ports) {
199 var process = process || {};
200 var handler = process.stdout
201 ? function(v) { process.stdout.write(v); }
202 : function(v) { console.log(v); };
203 ports.stdout.subscribe(handler);
205 if ('stderr' in ports) {
206 var process = process || {};
207 var handler = process.stderr
208 ? function(v) { process.stderr.write(v); }
209 : function(v) { console.log('Error:' + v); };
210 ports.stderr.subscribe(handler);
212 if ('title' in ports) {
213 if (typeof ports.title === 'string') {
214 document.title = ports.title;
216 ports.title.subscribe(function(v) { document.title = v; });
219 if ('redirect' in ports) {
220 ports.redirect.subscribe(function(v) {
221 if (v.length > 0) window.location = v;
224 if ('favicon' in ports) {
225 if (typeof ports.favicon === 'string') {
226 changeFavicon(ports.favicon);
228 ports.favicon.subscribe(changeFavicon);
231 function changeFavicon(src) {
232 var link = document.createElement('link');
233 var oldLink = document.getElementById('elm-favicon');
234 link.id = 'elm-favicon';
235 link.rel = 'shortcut icon';
238 document.head.removeChild(oldLink);
240 document.head.appendChild(link);
245function filterDeadInputs(inputs) {
247 for (var i = inputs.length; i--; ) {
248 if (isAlive(inputs[i])) temp.push(inputs[i]);
254function isAlive(input) {
255 if (!('defaultNumberOfKids' in input)) return true;
256 var len = input.kids.length;
257 if (len === 0) return false;
258 if (len > input.defaultNumberOfKids) return true;
260 for (var i = len; i--; ) {
261 alive = alive || isAlive(input.kids[i]);
269function initGraphics(elm, Module) {
270 if (!('main' in Module)) {
271 throw new Error("'main' is missing! What do I display?!");
274 var signalGraph = Module.main;
276 // make sure the signal graph is actually a signal & extract the visual model
277 var Signal = Elm.Signal.make(elm);
278 if (!('recv' in signalGraph)) {
279 signalGraph = Signal.constant(signalGraph);
281 var currentScene = signalGraph.value;
283 // Add the currentScene to the DOM
284 var Element = Elm.Native.Graphics.Element.make(elm);
285 elm.node.appendChild(Element.render(currentScene));
287 // set up updates so that the DOM is adjusted as necessary.
288 var savedScene = currentScene;
289 var previousDrawId = 0;
290 function domUpdate(newScene) {
291 previousDrawId = draw(previousDrawId, function(_) {
292 Element.update(elm.node.firstChild, savedScene, newScene);
293 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
294 savedScene = newScene;
297 var renderer = A2(Signal.lift, domUpdate, signalGraph);
299 // must check for resize after 'renderer' is created so
300 // that changes show up.
301 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
307// define function for drawing efficiently
309// draw : RequestID -> (() -> ()) -> RequestID
311// Takes a "RequestID" allowing you to cancel old requests if possible.
312// Returns a "RequestID" so you can refer to past requests.
314function draw(previousRequestID, callback) {
316 return previousRequestID;
319var vendors = ['ms', 'moz', 'webkit', 'o'];
320var win = typeof window !== 'undefined' ? window : {};
321for (var i = 0; i < vendors.length && !win.requestAnimationFrame; ++i) {
322 win.requestAnimationFrame = win[vendors[i]+'RequestAnimationFrame'];
323 win.cancelAnimationFrame = win[vendors[i]+'CancelAnimationFrame'] ||
324 win[vendors[i]+'CancelRequestAnimationFrame'];
326if (win.requestAnimationFrame && win.cancelAnimationFrame) {
327 draw = function(previousRequestID, callback) {
328 win.cancelAnimationFrame(previousRequestID);
329 return win.requestAnimationFrame(callback);
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