on e3c3d21 help sign in
elk/libraries/Native/Utils.js
201 lines 6.6 kB history raw
1Elm.Native.Utils = {};
2Elm.Native.Utils.make = function(elm) {
4 elm.Native = elm.Native || {};
5 elm.Native.Utils = elm.Native.Utils || {};
6 if (elm.Native.Utils.values) return elm.Native.Utils.values;
8 function eq(x,y) {
9 if (x === y) return true;
10 if (typeof x === "object") {
11 var c = 0;
12 for (var i in x) { ++c; if (!eq(x[i],y[i])) return false; }
13 return c === Object.keys(y).length;
14 }
15 if (typeof x === 'function') {
16 throw new Error('Equality error: general function equality is ' +
17 'undecidable, and therefore, unsupported');
18 }
19 return x === y;
20 }
22 // code in Generate/JavaScript.hs depends on the particular
23 // integer values assigned to LT, EQ, and GT
24 var LT = -1, EQ = 0, GT = 1, ord = ['LT','EQ','GT'];
25 function compare(x,y) { return { ctor: ord[cmp(x,y)+1] } }
26 function cmp(x,y) {
27 var ord;
28 if (typeof x !== 'object') return x === y ? EQ : x < y ? LT : GT;
30 if (x.ctor === "::" || x.ctor === "[]") {
31 while (true) {
32 if (x.ctor === "[]" && y.ctor === "[]") return EQ;
33 if (x.ctor !== y.ctor) return x.ctor === '[]' ? LT : GT;
34 ord = cmp(x._0, y._0);
35 if (ord !== EQ) return ord;
36 x = x._1;
37 y = y._1;
38 }
39 }
41 if (x.ctor.slice(0,6) === '_Tuple') {
42 var n = x.ctor.slice(6) - 0;
43 var err = 'cannot compare tuples with more than 6 elements.';
44 if (n === 0) return EQ;
45 if (n >= 1) { ord = cmp(x._0, y._0); if (ord !== EQ) return ord;
46 if (n >= 2) { ord = cmp(x._1, y._1); if (ord !== EQ) return ord;
47 if (n >= 3) { ord = cmp(x._2, y._2); if (ord !== EQ) return ord;
48 if (n >= 4) { ord = cmp(x._3, y._3); if (ord !== EQ) return ord;
49 if (n >= 5) { ord = cmp(x._4, y._4); if (ord !== EQ) return ord;
50 if (n >= 6) { ord = cmp(x._5, y._5); if (ord !== EQ) return ord;
51 if (n >= 7) throw new Error('Comparison error: ' + err); } } } } } }
52 return EQ;
53 }
54 throw new Error('Comparison error: comparison is only defined on ints, ' +
55 'floats, times, chars, strings, lists of comparable values, ' +
56 'and tuples of comparable values.')
57 }
60 var Tuple0 = { ctor: "_Tuple0" };
61 function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
63 function chr(c) {
64 var x = new String(c);
65 x.isChar = true;
66 return x;
67 }
69 function txt(str) {
70 var t = new String(str);
71 t.isText = true;
72 return t;
73 }
75 var count = 0;
76 function guid(_) { return count++ }
78 function copy(r) {
79 var o = {};
80 for (var i in r) { o[i] = r[i]; }
81 return o;
82 }
84 function remove(x,r) {
85 var o = copy(r);
86 if (x in o._) {
87 o[x] = o._[x][0];
88 o._[x] = o._[x].slice(1);
89 if (o._[x].length === 0) { delete o._[x]; }
90 } else {
91 delete o[x];
92 }
93 return o;
94 }
96 function replace(kvs,r) {
97 var o = copy(r);
98 for (var i = kvs.length; i--; ) {
99 var kvsi = kvs[i];
100 o[kvsi[0]] = kvsi[1];
101 }
102 return o;
103 }
105 function insert(x,v,r) {
106 var o = copy(r);
107 if (x in o) o._[x] = [o[x]].concat(x in o._ ? o._[x].slice(0) : []);
108 o[x] = v;
109 return o;
110 }
112 function max(a,b) { return a > b ? a : b }
113 function min(a,b) { return a < b ? a : b }
115 function mod(a,b) {
116 if (b === 0) {
117 throw new Error("Cannot perform mod 0. Division by zero error.");
118 }
119 var r = a % b;
120 var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
122 return m === b ? 0 : m;
123 }
125 function htmlHeight(width, rawHtml) {
126 // create dummy node
127 var html = rawHtml.html;
128 var t = document.createElement('div');
129 t.innerHTML = html;
130 if (width > 0) { t.style.width = width + "px"; }
131 t.style.visibility = "hidden";
132 t.style.styleFloat = "left";
133 t.style.cssFloat = "left";
135 document.body.appendChild(t);
137 // insert interpolated values
138 var args = rawHtml.args;
139 var guid = rawHtml.guid;
140 for (var i = args.length; i--; ) {
141 var arg = args[i];
142 var span = document.getElementById('md-' + guid + '-' + i);
143 if (arg.isElement) {
144 span.style.width = arg.props.width + 'px';
145 span.style.height = arg.props.height + 'px';
146 } else {
147 span.innerHTML = arg;
148 }
149 }
151 // get dimensions
152 var style = window.getComputedStyle(t, null);
153 var w = Math.ceil(style.getPropertyValue("width").slice(0,-2) - 0);
154 var h = Math.ceil(style.getPropertyValue("height").slice(0,-2) - 0);
155 document.body.removeChild(t);
156 return Tuple2(w,h);
157 }
159 function getXY(e) {
160 var posx = 0;
161 var posy = 0;
162 if (e.pageX || e.pageY) {
163 posx = e.pageX;
164 posy = e.pageY;
165 } else if (e.clientX || e.clientY) {
166 posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
167 posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
168 }
170 if (elm.display === ElmRuntime.Display.COMPONENT) {
171 var rect = elm.node.getBoundingClientRect();
172 var relx = rect.left + document.body.scrollLeft + document.documentElement.scrollLeft;
173 var rely = rect.top + document.body.scrollTop + document.documentElement.scrollTop;
174 // TODO: figure out if there is a way to avoid rounding here
175 posx = posx - Math.round(relx) - elm.node.clientLeft;
176 posy = posy - Math.round(rely) - elm.node.clientTop;
177 }
178 return Tuple2(posx, posy);
179 }
181 return elm.Native.Utils.values = {
182 eq:eq,
183 cmp:cmp,
184 compare:F2(compare),
185 Tuple0:Tuple0,
186 Tuple2:Tuple2,
187 chr:chr,
188 txt:txt,
189 copy: copy,
190 remove: remove,
191 replace: replace,
192 insert: insert,
193 guid: guid,
194 max : F2(max),
195 min : F2(min),
196 mod : F2(mod),
197 htmlHeight: F2(htmlHeight),
198 getXY: getXY,
199 toFloat: function(x) { return +x; }
200 };
201};