2013-03-10 03:02:07 +00:00
|
|
|
|
|
|
|
Elm.Native.Utils = function(elm) {
|
|
|
|
'use strict';
|
|
|
|
|
2013-03-12 07:48:11 +00:00
|
|
|
elm.Native = elm.Native || {};
|
|
|
|
if (elm.Native.Utils) return elm.Native.Utils;
|
|
|
|
|
2013-03-10 03:02:07 +00:00
|
|
|
function eq(x,y) {
|
|
|
|
if (x === y) return true;
|
|
|
|
if (typeof x === "object") {
|
|
|
|
var c = 0;
|
|
|
|
for (var i in x) { ++c; if (!eq(x[i],y[i])) return false; }
|
|
|
|
return c === Object.keys(y).length;
|
|
|
|
}
|
|
|
|
if (typeof x === 'function') {
|
2013-05-11 20:42:45 +00:00
|
|
|
throw new Error('Equality error: general function equality is ' +
|
|
|
|
'undecidable, and therefore, unsupported');
|
2013-03-10 03:02:07 +00:00
|
|
|
}
|
|
|
|
return x === y;
|
|
|
|
}
|
|
|
|
|
|
|
|
var EQ = 0, LT = 1, GT = 2, ord = ['EQ','LT','GT'];
|
|
|
|
function compare(x,y) { return { ctor: ord[cmp(x,y)] } }
|
|
|
|
function cmp(x,y) {
|
2013-05-11 20:42:45 +00:00
|
|
|
var ord;
|
2013-03-10 03:02:07 +00:00
|
|
|
if (typeof x !== 'object') return x === y ? EQ : x < y ? LT : GT;
|
|
|
|
|
|
|
|
if (x.ctor === "Cons" || x.ctor === "Nil") {
|
2013-05-11 20:42:45 +00:00
|
|
|
while (true) {
|
|
|
|
if (x.ctor === "Nil" && y.ctor === "Nil") return EQ;
|
|
|
|
if (x.ctor !== y.ctor) return x.ctor === 'Nil' ? LT : GT;
|
|
|
|
ord = cmp(x._0, y._0);
|
|
|
|
if (ord !== EQ) return ord;
|
|
|
|
x = x._1;
|
|
|
|
y = y._1;
|
|
|
|
}
|
2013-03-10 03:02:07 +00:00
|
|
|
}
|
2013-05-11 20:42:45 +00:00
|
|
|
|
2013-03-10 03:02:07 +00:00
|
|
|
if (x.ctor.slice(0,5) === 'Tuple') {
|
|
|
|
var n = x.ctor.slice(5) - 0;
|
|
|
|
var err = 'cannot compare tuples with more than 6 elements.';
|
|
|
|
if (n === 0) return EQ;
|
|
|
|
if (n >= 1) { ord = cmp(x._0, y._0); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 2) { ord = cmp(x._1, y._1); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 3) { ord = cmp(x._2, y._2); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 4) { ord = cmp(x._3, y._3); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 5) { ord = cmp(x._4, y._4); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 6) { ord = cmp(x._5, y._5); if (ord !== EQ) return ord;
|
|
|
|
if (n >= 7) throw new Error('Comparison error: ' + err); } } } } } }
|
|
|
|
return EQ;
|
|
|
|
}
|
|
|
|
throw new Error('Comparison error: comparison is only defined on ints, ' +
|
2013-05-11 20:42:45 +00:00
|
|
|
'floats, times, chars, strings, lists of comparable values, ' +
|
|
|
|
'and tuples of comparable values.')
|
2013-03-10 03:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-12 07:48:11 +00:00
|
|
|
var Tuple0 = { ctor: "Tuple0" };
|
|
|
|
function Tuple2(x,y) { return { ctor:"Tuple2", _0:x, _1:y } }
|
2013-03-10 03:02:07 +00:00
|
|
|
|
2013-03-10 09:40:31 +00:00
|
|
|
var count = 0;
|
|
|
|
function guid(_) { return count++ }
|
2013-03-10 03:02:07 +00:00
|
|
|
|
|
|
|
function copy(r) {
|
|
|
|
var o = {};
|
|
|
|
for (var i in r) { o[i] = r[i]; }
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(x,r) {
|
|
|
|
var o = copy(r);
|
|
|
|
if (x in o._) {
|
2013-05-11 20:42:45 +00:00
|
|
|
o[x] = o._[x][0];
|
|
|
|
o._[x] = o._[x].slice(1);
|
|
|
|
if (o._[x].length === 0) { delete o._[x]; }
|
2013-03-10 03:02:07 +00:00
|
|
|
} else {
|
2013-05-11 20:42:45 +00:00
|
|
|
delete o[x];
|
2013-03-10 03:02:07 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
function replace(kvs,r) {
|
|
|
|
var o = copy(r);
|
|
|
|
for (var i = kvs.length; i--; ) {
|
2013-05-11 20:42:45 +00:00
|
|
|
var kvsi = kvs[i];
|
|
|
|
o[kvsi[0]] = kvsi[1];
|
2013-03-10 03:02:07 +00:00
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
function insert(x,v,r) {
|
|
|
|
var o = copy(r);
|
|
|
|
if (x in o) o._[x] = [o[x]].concat(x in o._ ? o._[x].slice(0) : []);
|
|
|
|
o[x] = v;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2013-03-12 07:48:11 +00:00
|
|
|
function max(a,b) { return a > b ? a : b }
|
|
|
|
function min(a,b) { return a < b ? a : b }
|
|
|
|
|
2013-03-13 05:59:15 +00:00
|
|
|
function htmlHeight(width, html) {
|
|
|
|
var t = document.createElement('div');
|
|
|
|
t.innerHTML = html;
|
2013-04-15 01:07:26 +00:00
|
|
|
if (width > 0) { t.style.width = width + "px"; }
|
2013-03-13 05:59:15 +00:00
|
|
|
t.style.visibility = "hidden";
|
|
|
|
t.style.styleFloat = "left";
|
|
|
|
t.style.cssFloat = "left";
|
2013-05-11 20:42:45 +00:00
|
|
|
|
2013-04-15 01:07:26 +00:00
|
|
|
elm.node.appendChild(t);
|
|
|
|
var w = t.clientWidth;
|
|
|
|
var h = t.clientHeight;
|
|
|
|
elm.node.removeChild(t);
|
|
|
|
return Tuple2(w,h);
|
2013-03-13 05:59:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 14:29:06 +00:00
|
|
|
function adjustOffset() {
|
|
|
|
var node = elm.node;
|
2013-05-15 05:48:45 +00:00
|
|
|
var offsetX = 0, offsetY = 0;
|
2013-05-14 14:29:06 +00:00
|
|
|
if (node.offsetParent) {
|
|
|
|
do {
|
|
|
|
offsetX += node.offsetLeft;
|
|
|
|
offsetY += node.offsetTop;
|
|
|
|
} while (node = node.offsetParent);
|
|
|
|
}
|
2013-05-15 05:53:01 +00:00
|
|
|
elm.node.offsetX = offsetX;
|
|
|
|
elm.node.offsetY = offsetY;
|
2013-05-14 14:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (elm.display === ElmRuntime.Display.COMPONENT) {
|
|
|
|
elm.node.addEventListener('mouseover', adjustOffset);
|
|
|
|
}
|
|
|
|
|
2013-03-10 03:02:07 +00:00
|
|
|
return elm.Native.Utils = {
|
|
|
|
eq:eq,
|
2013-04-10 05:15:06 +00:00
|
|
|
cmp:compare,
|
|
|
|
compare:F2(compare),
|
2013-03-10 03:02:07 +00:00
|
|
|
Tuple0:Tuple0,
|
|
|
|
Tuple2:Tuple2,
|
|
|
|
copy: copy,
|
|
|
|
remove: remove,
|
|
|
|
replace: replace,
|
2013-03-10 09:40:31 +00:00
|
|
|
insert: insert,
|
2013-03-12 07:48:11 +00:00
|
|
|
guid: guid,
|
|
|
|
max : F2(max),
|
2013-03-13 05:59:15 +00:00
|
|
|
min : F2(min),
|
2013-03-23 01:27:03 +00:00
|
|
|
htmlHeight: F2(htmlHeight),
|
|
|
|
toFloat: function(x){return x}
|
2013-03-10 03:02:07 +00:00
|
|
|
};
|
2013-05-11 20:42:45 +00:00
|
|
|
};
|