on master help sign in
Fix `croppedImage`, `show` for tuples, and init for resizing initial dom node.
commit152ef51a989f588d3dff146045b3510485fd4136 authorevancz <info@elm-lang.org> dateWed 13 Mar 2013, 00:52 (14 years ago) parent6b071db
3 files changed+18−14
libraries/Native/Show.js+2−2 runtime/Init.js+4−2 runtime/Render/Element.js+12−10
libraries/Native/Show.js+2−2
@@ -55,8 +55,8 @@ Elm.Native.Show = function(elm) {
5555 return "{ " + output.join(", ") + " }";
5656 } else if ('ctor' in v) {
5757 if (v.ctor.substring(0,5) === "Tuple") {
58− var output = new Array(v.length-1);
59− for (var i = v.length; --i; ) { output[i-1] = toString(v[i]); }
58+ var output = [];
59+ for (var k in v) { if (k === 'ctor') continue; output.push(toString(v[k])); }
6060 return "(" + output.join(",") + ")";
6161 } else if (v.ctor === "Cons") {
6262 var isStr = typeof v._0 === "string";
runtime/Init.js+4−2
@@ -23,10 +23,12 @@ Elm.init = function(module, baseNode) {
2323 if (typeof baseNode === 'undefined') {
2424 baseNode = Render.newElement('div');
2525 document.body.appendChild(baseNode);
26+ baseNode.style.width = window.innerWidth + 'px';
27+ baseNode.style.height = window.innerHeight + 'px';
2628 window.addEventListener('resize', function() {
2729 console.log('resize the base node');
28− baseNode.style.width = document.documentElement.clientWidth + 'px';
29− baseNode.style.height = document.documentElement.clientHeight + 'px';
30+ baseNode.style.width = window.innerWidth + 'px';
31+ baseNode.style.height = window.innerHeight + 'px';
3032 }, true);
3133
3234 var style = Render.newElement('style');
runtime/Render/Element.js+12−10
@@ -2,7 +2,8 @@
22ElmRuntime.Render.Element = function() {
33'use strict';
44
5−var fromList = Elm.JavaScript({}).fromList
5+var fromList = Elm.JavaScript({}).fromList;
6+var eq = Elm.Native.Utils({}).eq;
67
78function newElement(elementType) {
89 var e = document.createElement(elementType);
@@ -41,7 +42,7 @@ function image(props, img) {
4142 switch (img._0.ctor) {
4243 case 'Plain': return plainImage(img._3);
4344 case 'Fitted': return fittedImage(props.width, props.height, img._3);
44− case 'Cropped': return croppedImage(img._0._0,props.width,props.height,img._3);
45+ case 'Cropped': return croppedImage(img,props.width,props.height,img._3);
4546 }
4647}
4748
@@ -80,7 +81,8 @@ function fittedImage(w, h, src) {
8081 return e;
8182}
8283
83−function croppedImage(pos, w, h, src) {
84+function croppedImage(elem, w, h, src) {
85+ var pos = elem._0._0;
8486 var e = newElement('div');
8587 e.style.position = "relative";
8688 e.style.overflow = "hidden";
@@ -89,11 +91,11 @@ function croppedImage(pos, w, h, src) {
8991 img.onload = function() {
9092 img.style.position = 'absolute';
9193 img.style.margin = 'auto';
92− var sw = dimensions.width / w, sh = dimensions.height / h;
93− img.style.width = (this.width * sw) + 'px';
94− img.style.height = (this.height * sh) + 'px';
95− img.style.left = (- pos._0 * sw) + 'px';
96− img.style.top = (- pos._1 * sh) + 'px';
94+ var sw = w / elem._1, sh = h / elem._2;
95+ img.style.width = ((this.width * sw)|0) + 'px';
96+ img.style.height = ((this.height * sh)|0) + 'px';
97+ img.style.left = ((- pos._0 * sw)|0) + 'px';
98+ img.style.top = ((- pos._1 * sh)|0) + 'px';
9799 };
98100 img.src = src;
99101 img.name = src;
@@ -207,7 +209,7 @@ function update(node, curr, next) {
207209 case "Image":
208210 if (nextE._0.ctor === 'Plain') {
209211 if (nextE._3 !== currE._3) node.src = nextE._3;
210− } else if (!Value.eq(nextE,currE) ||
212+ } else if (!eq(nextE,currE) ||
211213 next.props.width !== curr.props.width ||
212214 next.props.height !== curr.props.height) {
213215 node.parentNode.replaceChild(render(next),node);
@@ -255,7 +257,7 @@ function update(node, curr, next) {
255257 if (next._0 !== curr._0) node.parentNode.replaceChild(render(next),node);
256258 break;
257259 }
258− var props = next.props, currP = curr.props;
260+ var props = next.props, currP = curr.props, e = node;
259261 if (props.width !== currP.width) e.style.width = (props.width |0) + 'px';
260262 if (props.height !== currP.height) e.style.height = (props.height|0) + 'px';
261263 if (props.opacity !== 1 && props.opacity !== currP.opacity) {