on 577fc17 help sign in
elk/libraries/Native/Utils.js
255 lines 8.2 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(l,r) {
9 var stack = [{'x': l, 'y': r}]
10 while (stack.length > 0) {
11 var front = stack.pop();
12 var x = front.x;
13 var y = front.y;
14 if (x === y) continue;
15 if (typeof x === "object") {
16 var c = 0;
17 for (var i in x) {
18 ++c;
19 if (i in y) {
20 if (i !== 'ctor') {
21 stack.push({ 'x': x[i], 'y': y[i] });
22 }
23 } else {
24 return false;
25 }
26 }
27 if ('ctor' in x) {
28 stack.push({'x': x.ctor, 'y': y.ctor});
29 }
30 if (c !== Object.keys(y).length) {
31 return false;
32 };
33 } else if (typeof x === 'function') {
34 throw new Error('Equality error: general function equality is ' +
35 'undecidable, and therefore, unsupported');
36 } else {
37 return false;
38 }
39 }
40 return true;
41 }
43 // code in Generate/JavaScript.hs depends on the particular
44 // integer values assigned to LT, EQ, and GT
45 var LT = -1, EQ = 0, GT = 1, ord = ['LT','EQ','GT'];
46 function compare(x,y) { return { ctor: ord[cmp(x,y)+1] } }
47 function cmp(x,y) {
48 var ord;
49 if (typeof x !== 'object'){
50 return x === y ? EQ : x < y ? LT : GT;
51 }
52 else if (x.isChar){
53 var a = x.toString();
54 var b = y.toString();
55 return a === b ? EQ : a < b ? LT : GT;
56 }
57 else if (x.ctor === "::" || x.ctor === "[]") {
58 while (true) {
59 if (x.ctor === "[]" && y.ctor === "[]") return EQ;
60 if (x.ctor !== y.ctor) return x.ctor === '[]' ? LT : GT;
61 ord = cmp(x._0, y._0);
62 if (ord !== EQ) return ord;
63 x = x._1;
64 y = y._1;
65 }
66 }
67 else if (x.ctor.slice(0,6) === '_Tuple') {
68 var n = x.ctor.slice(6) - 0;
69 var err = 'cannot compare tuples with more than 6 elements.';
70 if (n === 0) return EQ;
71 if (n >= 1) { ord = cmp(x._0, y._0); if (ord !== EQ) return ord;
72 if (n >= 2) { ord = cmp(x._1, y._1); if (ord !== EQ) return ord;
73 if (n >= 3) { ord = cmp(x._2, y._2); if (ord !== EQ) return ord;
74 if (n >= 4) { ord = cmp(x._3, y._3); if (ord !== EQ) return ord;
75 if (n >= 5) { ord = cmp(x._4, y._4); if (ord !== EQ) return ord;
76 if (n >= 6) { ord = cmp(x._5, y._5); if (ord !== EQ) return ord;
77 if (n >= 7) throw new Error('Comparison error: ' + err); } } } } } }
78 return EQ;
79 }
80 else {
81 throw new Error('Comparison error: comparison is only defined on ints, ' +
82 'floats, times, chars, strings, lists of comparable values, ' +
83 'and tuples of comparable values.');
84 }
85 }
88 var Tuple0 = { ctor: "_Tuple0" };
89 function Tuple2(x,y) { return { ctor:"_Tuple2", _0:x, _1:y } }
91 function chr(c) {
92 var x = new String(c);
93 x.isChar = true;
94 return x;
95 }
97 function txt(str) {
98 var t = new String(str);
99 t.text = true;
100 return t;
101 }
103 function makeText(text) {
104 var style = '';
105 var href = '';
106 while (true) {
107 if (text.style) {
108 style += text.style;
109 text = text.text;
110 continue;
111 }
112 if (text.href) {
113 href = text.href;
114 text = text.text;
115 continue;
116 }
117 if (href) text = '<a href="' + href + '">' + text + '</a>';
118 if (style) text = '<span style="' + style + '">' + text + '</span>';
119 return text;
120 }
121 }
123 var count = 0;
124 function guid(_) { return count++ }
126 function copy(r) {
127 var o = {};
128 for (var i in r) { o[i] = r[i]; }
129 return o;
130 }
132 function remove(x,r) {
133 var o = copy(r);
134 if (x in o._) {
135 o[x] = o._[x][0];
136 o._[x] = o._[x].slice(1);
137 if (o._[x].length === 0) { delete o._[x]; }
138 } else {
139 delete o[x];
140 }
141 return o;
142 }
144 function replace(kvs,r) {
145 var o = copy(r);
146 for (var i = kvs.length; i--; ) {
147 var kvsi = kvs[i];
148 o[kvsi[0]] = kvsi[1];
149 }
150 return o;
151 }
153 function insert(x,v,r) {
154 var o = copy(r);
155 if (x in o) o._[x] = [o[x]].concat(x in o._ ? o._[x].slice(0) : []);
156 o[x] = v;
157 return o;
158 }
160 function max(a,b) { return a > b ? a : b }
161 function min(a,b) { return a < b ? a : b }
163 function mod(a,b) {
164 if (b === 0) {
165 throw new Error("Cannot perform mod 0. Division by zero error.");
166 }
167 var r = a % b;
168 var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
170 return m === b ? 0 : m;
171 }
173 function htmlHeight(width, rawHtml) {
174 // create dummy node
175 var html = rawHtml.html;
176 var t = document.createElement('div');
177 t.innerHTML = html;
178 if (width > 0) { t.style.width = width + "px"; }
179 t.style.visibility = "hidden";
180 t.style.styleFloat = "left";
181 t.style.cssFloat = "left";
183 document.body.appendChild(t);
185 // insert interpolated values
186 var args = rawHtml.args;
187 var guid = rawHtml.guid;
188 for (var i = args.length; i--; ) {
189 var arg = args[i];
190 var span = document.getElementById('md-' + guid + '-' + i);
191 if (arg.isElement) {
192 span.style.width = arg.props.width + 'px';
193 span.style.height = arg.props.height + 'px';
194 } else {
195 span.innerHTML = arg;
196 }
197 }
199 // get dimensions
200 var style = window.getComputedStyle(t, null);
201 var w = Math.ceil(style.getPropertyValue("width").slice(0,-2) - 0);
202 var h = Math.ceil(style.getPropertyValue("height").slice(0,-2) - 0);
203 document.body.removeChild(t);
204 return Tuple2(w,h);
205 }
207 function getXY(e) {
208 var posx = 0;
209 var posy = 0;
210 if (e.pageX || e.pageY) {
211 posx = e.pageX;
212 posy = e.pageY;
213 } else if (e.clientX || e.clientY) {
214 posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
215 posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
216 }
218 if (elm.display === ElmRuntime.Display.COMPONENT) {
219 var rect = elm.node.getBoundingClientRect();
220 var relx = rect.left + document.body.scrollLeft + document.documentElement.scrollLeft;
221 var rely = rect.top + document.body.scrollTop + document.documentElement.scrollTop;
222 // TODO: figure out if there is a way to avoid rounding here
223 posx = posx - Math.round(relx) - elm.node.clientLeft;
224 posy = posy - Math.round(rely) - elm.node.clientTop;
225 }
226 return Tuple2(posx, posy);
227 }
229 function isJSArray(a) {
230 return a instanceof Array;
231 }
233 return elm.Native.Utils.values = {
234 eq:eq,
235 cmp:cmp,
236 compare:F2(compare),
237 Tuple0:Tuple0,
238 Tuple2:Tuple2,
239 chr:chr,
240 txt:txt,
241 makeText:makeText,
242 copy: copy,
243 remove: remove,
244 replace: replace,
245 insert: insert,
246 guid: guid,
247 max : F2(max),
248 min : F2(min),
249 mod : F2(mod),
250 htmlHeight: F2(htmlHeight),
251 getXY: getXY,
252 isJSArray: isJSArray,
253 toFloat: function(x) { return +x; }
254 };
255};