on master help sign in
Move necessary parts of runtime/Utils.js to Init.js
commit9d0075cc7143a315fb46859f7f4851d0a4a05fed authorEvan Czaplicki <info@elm-lang.org> dateTue 4 Nov 2014, 14:41 (12 years ago) parentfcdbe58
2 files changed+64−72
runtime/Init.js+64−10 runtime/Utils.js−62
runtime/Init.js+64−10
@@ -2,6 +2,9 @@
22(function() {
33'use strict';
44
5+var Display = { FULLSCREEN: 0, COMPONENT: 1, NONE: 2 };
6+
7+
58Elm.fullscreen = function(module, ports) {
69 var style = document.createElement('style');
710 style.type = 'text/css';
@@ -10,7 +13,7 @@ Elm.fullscreen = function(module, ports) {
1013 document.head.appendChild(style);
1114 var container = document.createElement('div');
1215 document.body.appendChild(container);
13− return init(ElmRuntime.Display.FULLSCREEN, container, module, ports || {});
16+ return init(Display.FULLSCREEN, container, module, ports || {});
1417};
1518
1619Elm.embed = function(module, container, ports) {
@@ -20,11 +23,11 @@ Elm.embed = function(module, container, ports) {
2023 } else if (container.hasChildNodes()) {
2124 throw new Error('Elm.node must be given an empty DIV. No children allowed!');
2225 }
23− return init(ElmRuntime.Display.COMPONENT, container, module, ports || {});
26+ return init(Display.COMPONENT, container, module, ports || {});
2427};
2528
2629Elm.worker = function(module, ports) {
27− return init(ElmRuntime.Display.NONE, {}, module, ports || {});
30+ return init(Display.NONE, {}, module, ports || {});
2831};
2932
3033function init(display, container, module, ports, moduleToReplace) {
@@ -96,7 +99,6 @@ function init(display, container, module, ports, moduleToReplace) {
9699 setTimeout:setTimeout,
97100 node:container,
98101 display:display,
99− id:ElmRuntime.guid(),
100102 addListener:addListener,
101103 inputs:inputs,
102104 timer:timer,
@@ -127,10 +129,10 @@ function init(display, container, module, ports, moduleToReplace) {
127129 Module.main = Elm.Text.make(elm).leftAligned('<code>' + e.message + directions + '</code>');
128130 reportAnyErrors = function() { throw e; }
129131 }
130− inputs = ElmRuntime.filterDeadInputs(inputs);
132+ inputs = filterDeadInputs(inputs);
131133 filterListeners(inputs, listeners);
132134 addReceivers(elm.ports.outgoing);
133− if (display !== ElmRuntime.Display.NONE) {
135+ if (display !== Display.NONE) {
134136 var graphicsNode = initGraphics(elm, Module);
135137 }
136138 if (typeof moduleToReplace !== 'undefined') {
@@ -239,6 +241,31 @@ function addReceivers(ports) {
239241 }
240242}
241243
244+
245+function filterDeadInputs(inputs) {
246+ var temp = [];
247+ for (var i = inputs.length; i--; ) {
248+ if (isAlive(inputs[i])) temp.push(inputs[i]);
249+ }
250+ return temp;
251+}
252+
253+
254+function 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;
259+ var alive = false;
260+ for (var i = len; i--; ) {
261+ alive = alive || isAlive(input.kids[i]);
262+ }
263+ return alive;
264+}
265+
266+
267+//// RENDERING ////
268+
242269function initGraphics(elm, Module) {
243270 if (!('main' in Module)) {
244271 throw new Error("'main' is missing! What do I display?!");
@@ -254,15 +281,15 @@ function initGraphics(elm, Module) {
254281 var currentScene = signalGraph.value;
255282
256283 // Add the currentScene to the DOM
257− var Render = ElmRuntime.use(ElmRuntime.Render.Element);
258− elm.node.appendChild(Render.render(currentScene));
284+ var Element = Elm.Native.Graphics.Element.make(elm);
285+ elm.node.appendChild(Element.render(currentScene));
259286
260287 // set up updates so that the DOM is adjusted as necessary.
261288 var savedScene = currentScene;
262289 var previousDrawId = 0;
263290 function domUpdate(newScene) {
264− previousDrawId = ElmRuntime.draw(previousDrawId, function(_) {
265− Render.update(elm.node.firstChild, savedScene, newScene);
291+ previousDrawId = draw(previousDrawId, function(_) {
292+ Element.update(elm.node.firstChild, savedScene, newScene);
266293 if (elm.Native.Window) elm.Native.Window.values.resizeIfNeeded();
267294 savedScene = newScene;
268295 });
@@ -276,4 +303,31 @@ function initGraphics(elm, Module) {
276303 return renderer;
277304}
278305
306+
307+// define function for drawing efficiently
308+//
309+// draw : RequestID -> (() -> ()) -> RequestID
310+//
311+// Takes a "RequestID" allowing you to cancel old requests if possible.
312+// Returns a "RequestID" so you can refer to past requests.
313+//
314+function draw(previousRequestID, callback) {
315+ callback();
316+ return previousRequestID;
317+}
318+
319+var vendors = ['ms', 'moz', 'webkit', 'o'];
320+var win = typeof window !== 'undefined' ? window : {};
321+for (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'];
325+}
326+if (win.requestAnimationFrame && win.cancelAnimationFrame) {
327+ draw = function(previousRequestID, callback) {
328+ win.cancelAnimationFrame(previousRequestID);
329+ return win.requestAnimationFrame(callback);
330+ };
331+}
332+
279333}());
runtime/Utils.jsdeleted−62
@@ -1,62 +0,0 @@
1−
2−(function() {
3−'use strict';
4−
5−ElmRuntime.Display = { FULLSCREEN: 0, COMPONENT: 1, NONE: 2 };
6−
7−ElmRuntime.counter = 0;
8−ElmRuntime.guid = function() { return ElmRuntime.counter++; }
9−
10−ElmRuntime.use = function(M) {
11− if (typeof M === 'function') M = M();
12− return M;
13−};
14−
15−function isAlive(input) {
16− if (!('defaultNumberOfKids' in input)) return true;
17− var len = input.kids.length;
18− if (len === 0) return false;
19− if (len > input.defaultNumberOfKids) return true;
20− var alive = false;
21− for (var i = len; i--; ) {
22− alive = alive || isAlive(input.kids[i]);
23− }
24− return alive;
25−}
26−
27−ElmRuntime.filterDeadInputs = function(inputs) {
28− var temp = [];
29− for (var i = inputs.length; i--; ) {
30− if (isAlive(inputs[i])) temp.push(inputs[i]);
31− }
32− return temp;
33−};
34−
35−// define function for drawing efficiently
36−//
37−// draw : RequestID -> (() -> ()) -> RequestID
38−//
39−// Takes a "RequestID" allowing you to cancel old requests if possible.
40−// Returns a "RequestID" so you can refer to past requests.
41−//
42−var vendors = ['ms', 'moz', 'webkit', 'o'];
43−var win = typeof window !== 'undefined' ? window : {};
44−for (var i = 0; i < vendors.length && !win.requestAnimationFrame; ++i) {
45− win.requestAnimationFrame = win[vendors[i]+'RequestAnimationFrame'];
46− win.cancelAnimationFrame = win[vendors[i]+'CancelAnimationFrame'] ||
47− win[vendors[i]+'CancelRequestAnimationFrame'];
48−}
49−
50−if (win.requestAnimationFrame && win.cancelAnimationFrame) {
51− ElmRuntime.draw = function(previousRequestID, callback) {
52− win.cancelAnimationFrame(previousRequestID);
53− return win.requestAnimationFrame(callback);
54− };
55−} else {
56− ElmRuntime.draw = function(previousRequestID, callback) {
57− callback();
58− return previousRequestID;
59− };
60−}
61−
62−}());