@@ -1,7 +1,28 @@
11
2−Elm.init = function(module, baseNode) {
3− 'use strict';
2+(function() {
3+'use strict';
44
5+Elm.fullscreen = function(module) {
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+ return init(ElmRuntime.Display.FULLSCREEN, document.body, module);
12+};
13+
14+Elm.node = function(width, height, module) {
15+ var container = document.createElement('div');
16+ container.style.width = width + 'px';
17+ container.style.height = height + 'px';
18+ return init(ElmRuntime.Display.COMPONENT, container, module);
19+};
20+
21+Elm.worker = function(module) {
22+ return init(ElmRuntime.Display.NONE, {}, module);
23+};
24+
25+function init(display, container, module) {
526 // defining state needed for an instance of the Elm RTS
627 var signalGraph = null;
728 var inputs = [];
@@ -18,19 +39,17 @@ Elm.init = function(module, baseNode) {
1839 return hasListener;
1940 }
2041
21− // ensure that baseNode exists and is properly formatted.
22− if (typeof baseNode === 'undefined') {
23− baseNode = document.body;
24− var style = document.createElement('style');
25− style.type = 'text/css';
26− style.innerHTML = "html,head,body { padding:0; margin:0; }" +
27− "body { font-family: calibri, helvetica, arial, sans-serif; }";
28− document.head.appendChild(style);
29− }
42+ container.offsetX = 0;
43+ container.offsetY = 0;
3044
3145 // create the actuall RTS. Any impure modules will attach themselves to this
3246 // object. This permits many Elm programs to be embedded per document.
33− var elm = {notify: notify, node: baseNode, id: ElmRuntime.guid(), inputs: inputs};
47+ var elm = { notify:notify,
48+ node:container,
49+ display:display,
50+ id:ElmRuntime.guid(),
51+ inputs:inputs
52+ };
3453
3554 // Set up methods to communicate with Elm program from JS.
3655 function send(name, value) {
@@ -44,15 +63,16 @@ Elm.init = function(module, baseNode) {
4463 document.addEventListener(name + '_' + elm.id, handler);
4564 }
4665
47− recv('elm_log', function(e) {console.log(e.value)});
48− recv('elm_title', function(e) {document.title = e.value});
49− recv('elm_redirect', function(e) {
66+ recv('log', function(e) {console.log(e.value)});
67+ recv('title', function(e) {document.title = e.value});
68+ recv('redirect', function(e) {
5069 if (e.value.length > 0) { window.location = e.value; }
5170 });
5271
5372 // If graphics are not enabled, escape early, skip over setting up DOM stuff.
54− if (baseNode === null) return { send : send, recv : recv };
55−
73+ if (display === ElmRuntime.Display.NONE) {
74+ return { send : send, recv : recv };
75+ }
5676
5777 var Render = ElmRuntime.use(ElmRuntime.Render.Element);
5878
@@ -66,22 +86,10 @@ Elm.init = function(module, baseNode) {
6686 visualModel = signalGraph.value;
6787 inputs = ElmRuntime.filterDeadInputs(inputs);
6888
69− var tuple2 = Elm.Native.Utils(elm).Tuple2;
70− function adjustWindow() {
71− if ('Window' in elm) {
72− var w = baseNode.clientWidth;
73− if (w !== elm.Window.dimensions.value._0) {
74− notify(elm.Window.dimensions.id,
75− tuple2(w, document.body === baseNode ?
76− window.innerHeight : baseNode.clientHeight));
77− }
78− }
79− }
80−
81− // Add the visualModel to the DOM
89+ // Add the visualModel to the DOM
8290 var renderNode = Render.render(visualModel)
83− baseNode.appendChild(renderNode);
84− adjustWindow();
91+ container.appendChild(renderNode);
92+ elm.Native.Window.resizeIfNeeded();
8593
8694 // set up updates so that the DOM is adjusted as necessary.
8795 var update = Render.update;
@@ -89,12 +97,14 @@ Elm.init = function(module, baseNode) {
8997 ElmRuntime.draw(function(_) {
9098 update(renderNode, visualModel, value);
9199 visualModel = value;
92− adjustWindow();
100+ elm.Native.Window.resizeIfNeeded();
93101 });
94102 return value;
95103 }
96104
97105 signalGraph = A2(Signal.lift, domUpdate, signalGraph);
98106
99− return { send : send, recv : recv };
107+ return { send : send, recv : recv, node : container };
100108};
109+
110+}());