on aa7a67f help sign in
102 lines 3.2 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);
15 elm.node.addEventListener('log', function(e) { console.log(e.value); });
16 elm.node.addEventListener('title', function(e) {document.title = e.value;});
17 elm.node.addEventListener('redirect', function(e) {
18 if (e.value.length > 0) { window.location = e.value; }
19 });
20 elm.node.addEventListener('viewport', function(e) {
21 var node = document.getElementById('elm_viewport');
22 if (!node) {
23 node = document.createElement('meta');
24 node.id = 'elm_viewport';
25 node.name = 'viewport';
26 document.head.appendChild(node);
27 }
28 node.content = e.value;
29 Dispatcher.notify(elm.Window.dimensions.id,
30 Tuple2(elm.node.clientWidth, elm.node.clientHeight));
31 });
33 var toString = function(v) {
34 if (typeof v === "function") {
35 return "<function>";
36 } else if (typeof v === "boolean") {
37 return v ? "True" : "False";
38 } else if (typeof v === "number") {
39 return v+"";
40 } else if (typeof v === "string" && v.length < 2) {
41 return "'"+v+"'";
42 } else if (typeof v === "object" && ('_' in v)) {
43 var output = [];
44 for (var k in v._) {
45 console.log(k,v._[k]);
46 for (var i = v._[k].length; i--; ) {
47 output.push(k + " = " + toString(v._[k][i]));
48 }
49 }
50 for (var k in v) {
51 if (k === '_') continue;
52 output.push(k + " = " + toString(v[k]));
53 }
54 if (output.length === 0) return "{}";
55 return "{ " + output.join(", ") + " }";
56 } else if ('ctor' in v) {
57 if (v.ctor.substring(0,5) === "Tuple") {
58 var output = [];
59 for (var k in v) {
60 if (k === 'ctor') continue;
61 output.push(toString(v[k]));
62 }
63 return "(" + output.join(",") + ")";
64 } else if (v.ctor === "Cons") {
65 var isStr = typeof v._0 === "string",
66 start = isStr ? '"' : "[",
67 end = isStr ? '"' : "]",
68 sep = isStr ? "" : ",",
69 f = !isStr ? toString : function(x){
70 return x === '\n' ? '\\n' : x;
71 };
72 var output = start + f(v._0);
73 v = v._1;
74 while (v.ctor === "Cons") {
75 output += sep + f(v._0);
76 v = v._1;
77 }
78 return output + end;
79 } else if (v.ctor === "Nil") {
80 return "[]";
81 } else if (v.ctor === "RBNode" || v.ctor === "RBEmpty") {
82 var cons = F3(function(k,v,acc){return NList.Cons(Tuple2(k,v),acc)});
83 var list = A3(Dict.foldr, cons, NList.Nil, v);
84 var name = "Dict";
85 if (list.ctor === "Cons" && list._0._1.ctor === "Tuple0") {
86 name = "Set";
87 list = A2(List.map, function(x){return x._0}, list);
88 }
89 return "(" + name + ".fromList " + toString(list) + ")";
90 } else {
91 var output = "";
92 for (var i = v.length; --i; ) { output = ' ' + toString(v[i]) + output; }
93 output = v.ctor + output;
94 return (v.length > 1) ? "(" + output + ")" : output;
95 }
96 }
97 return v+"";
98 };
99 function show(v) { return NList.fromArray(toString(v)); }
101 return elm.Native.Show = { show:show };
102};