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