elm/libraries/Native/Signal/Window.js
Evan Czaplicki dcbe2fc245 Change the format of module instantiation
Currently, it's only possible to create modules at the leafs of the
namespace structure. This CL lifts that restriction.

Get rid of "use strict" in many cases.
2013-09-30 00:44:31 -07:00

44 lines
1.3 KiB
JavaScript

Elm.Native.Window = {};
Elm.Native.Window.make = function(elm) {
elm.Native = elm.Native || {};
if (elm.Native.Window) return elm.Native.Window;
var Signal = Elm.Signal.make(elm);
var Tuple2 = Elm.Native.Utils.make(elm).Tuple2;
function getWidth() { return elm.node.clientWidth; }
function getHeight() {
if (elm.display === ElmRuntime.Display.FULLSCREEN) {
return window.innerHeight;
}
return elm.node.clientHeight;
}
var dimensions = Signal.constant(Tuple2(getWidth(), getHeight()));
dimensions.defaultNumberOfKids = 2;
// Do not move width and height into Elm. By setting the default number of kids,
// the resize listener can be detached.
var width = A2(Signal.lift, function(p){return p._0;}, dimensions);
width.defaultNumberOfKids = 0;
var height = A2(Signal.lift, function(p){return p._1;}, dimensions);
height.defaultNumberOfKids = 0;
function resizeIfNeeded() {
var w = getWidth();
var h = getHeight();
if (dimensions.value._0 === w && dimensions.value._1 === h) return;
elm.notify(dimensions.id, Tuple2(w,h));
}
elm.addListener([dimensions.id], window, 'resize', resizeIfNeeded);
return elm.Native.Window = {
dimensions:dimensions,
width:width,
height:height,
resizeIfNeeded:resizeIfNeeded
};
};