on 2edcced help sign in
elk/libraries/Native/Utils.js
160 lines 5.1 kB history raw
2Elm.Native.Utils = function(elm) {
3 'use strict';
5 elm.Native = elm.Native || {};
6 if (elm.Native.Utils) return elm.Native.Utils;
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 var count = 0;
64 function guid(_) { return count++ }
66 function copy(r) {
67 var o = {};
68 for (var i in r) { o[i] = r[i]; }
69 return o;
70 }
72 function remove(x,r) {
73 var o = copy(r);
74 if (x in o._) {
75 o[x] = o._[x][0];
76 o._[x] = o._[x].slice(1);
77 if (o._[x].length === 0) { delete o._[x]; }
78 } else {
79 delete o[x];
80 }
81 return o;
82 }
84 function replace(kvs,r) {
85 var o = copy(r);
86 for (var i = kvs.length; i--; ) {
87 var kvsi = kvs[i];
88 o[kvsi[0]] = kvsi[1];
89 }
90 return o;
91 }
93 function insert(x,v,r) {
94 var o = copy(r);
95 if (x in o) o._[x] = [o[x]].concat(x in o._ ? o._[x].slice(0) : []);
96 o[x] = v;
97 return o;
98 }
100 function max(a,b) { return a > b ? a : b }
101 function min(a,b) { return a < b ? a : b }
103 function mod(a,b) {
104 var r = a % b;
105 var m = a === 0 ? 0 : (b > 0 ? (a >= 0 ? r : r+b) : -mod(-a,-b));
107 return m === b ? 0 : m;
108 }
110 function htmlHeight(width, html) {
111 var t = document.createElement('div');
112 t.innerHTML = html;
113 if (width > 0) { t.style.width = width + "px"; }
114 t.style.visibility = "hidden";
115 t.style.styleFloat = "left";
116 t.style.cssFloat = "left";
118 document.body.appendChild(t);
119 var style = window.getComputedStyle(t, null);
120 var w = Math.ceil(style.getPropertyValue("width").slice(0,-2) - 0);
121 var h = Math.ceil(style.getPropertyValue("height").slice(0,-2) - 0);
122 document.body.removeChild(t);
123 return Tuple2(w,h);
124 }
126 function adjustOffset() {
127 var node = elm.node;
128 var offsetX = 0, offsetY = 0;
129 if (node.offsetParent) {
130 do {
131 offsetX += node.offsetLeft;
132 offsetY += node.offsetTop;
133 } while (node = node.offsetParent);
134 }
135 elm.node.offsetX = offsetX;
136 elm.node.offsetY = offsetY;
137 }
139 if (elm.display === ElmRuntime.Display.COMPONENT) {
140 elm.addListener(elm.inputs, elm.node, 'mouseover', adjustOffset);
141 }
143 return elm.Native.Utils = {
144 eq:eq,
145 cmp:cmp,
146 compare:F2(compare),
147 Tuple0:Tuple0,
148 Tuple2:Tuple2,
149 copy: copy,
150 remove: remove,
151 replace: replace,
152 insert: insert,
153 guid: guid,
154 max : F2(max),
155 min : F2(min),
156 mod : F2(mod),
157 htmlHeight: F2(htmlHeight),
158 toFloat: function(x){return x}
159 };
160};