on bb2f028 help sign in
117 lines 4.6 kB history raw
2Elm.Native.Show = function(elm) {
3 'use strict';
5 elm.Native = elm.Native || {};
6 if (elm.Native.Show) return elm.Native.Show;
8 var NList = Elm.Native.List(elm);
9 var List = Elm.List(elm);
10 var Maybe = Elm.Maybe(elm);
11 var JS = Elm.JavaScript(elm);
12 var Dict = Elm.Dict(elm);
13 var Json = Elm.Json(elm);
14 var Tuple2 = Elm.Native.Utils(elm).Tuple2;
16 elm.node.addEventListener('log', function(e) { console.log(e.value); });
17 elm.node.addEventListener('title', function(e) {document.title = e.value;});
18 elm.node.addEventListener('redirect', function(e) {
19 if (e.value.length > 0) { window.location = e.value; }
20 });
21 elm.node.addEventListener('viewport', function(e) {
22 var node = document.getElementById('elm_viewport');
23 if (!node) {
24 node = document.createElement('meta');
25 node.id = 'elm_viewport';
26 node.name = 'viewport';
27 document.head.appendChild(node);
28 }
29 node.content = e.value;
30 Dispatcher.notify(elm.Window.dimensions.id,
31 Tuple2(elm.node.clientWidth, elm.node.clientHeight));
32 });
34 var toString = function(v) {
35 if (typeof v === "function") {
36 var name = v.func ? v.func.name : v.name;
37 return '<function' + (name === '' ? '' : ': ') + name + '>';
38 } else if (typeof v === "boolean") {
39 return v ? "True" : "False";
40 } else if (typeof v === "number") {
41 return v+"";
42 } else if (typeof v === "string" && v.length < 2) {
43 return "'" + showChar(v) + "'";
44 } else if (typeof v === "object" && '_' in v) {
45 var output = [];
46 for (var k in v._) {
47 for (var i = v._[k].length; i--; ) {
48 output.push(k + " = " + toString(v._[k][i]));
49 }
50 }
51 for (var k in v) {
52 if (k === '_') continue;
53 output.push(k + " = " + toString(v[k]));
54 }
55 if (output.length === 0) return "{}";
56 return "{ " + output.join(", ") + " }";
57 } else if (typeof v === "object" && 'ctor' in v) {
58 if (v.ctor.substring(0,5) === "Tuple") {
59 var output = [];
60 for (var k in v) {
61 if (k === 'ctor') continue;
62 output.push(toString(v[k]));
63 }
64 return "(" + output.join(",") + ")";
65 } else if (v.ctor === "Cons") {
66 var isStr = typeof v._0 === "string",
67 start = isStr ? '"' : "[",
68 end = isStr ? '"' : "]",
69 sep = isStr ? "" : ",",
70 f = !isStr ? toString : showChar;
71 var output = start + f(v._0);
72 v = v._1;
73 while (v.ctor === "Cons") {
74 output += sep + f(v._0);
75 v = v._1;
76 }
77 return output + end;
78 } else if (v.ctor === "Nil") {
79 return "[]";
80 } else if (v.ctor === "RBNode" || v.ctor === "RBEmpty") {
81 var cons = F3(function(k,v,acc){return NList.Cons(Tuple2(k,v),acc)});
82 var list = A3(Dict.foldr, cons, NList.Nil, v);
83 var name = "Dict";
84 if (list.ctor === "Cons" && list._0._1.ctor === "Tuple0") {
85 name = "Set";
86 list = A2(List.map, function(x){return x._0}, list);
87 }
88 return name + ".fromList " + toString(list);
89 } else {
90 var output = "";
91 for (var i in v) {
92 if (i === 'ctor') continue;
93 var str = toString(v[i]);
94 var parenless = str[0] === '{' || str.indexOf(' ') < 0;
95 output += ' ' + (parenless ? str : '(' + str + ')');
96 }
97 return v.ctor + output;
98 }
99 }
100 return v+"";
101 };
102 function show(v) { return NList.fromArray(toString(v)); }
104 function showChar (c) {
105 return c === '\n' ? '\\n' :
106 c === '\t' ? '\\t' :
107 c === '\b' ? '\\b' :
108 c === '\r' ? '\\r' :
109 c === '\v' ? '\\v' :
110 c === '\0' ? '\\0' :
111 c === '\'' ? "\\'" :
112 c === '\"' ? '\\"' :
113 c === '\\' ? '\\\\' : c;
114 }
116 return elm.Native.Show = { show:show };
117};