-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtreedata.js
156 lines (140 loc) · 4.55 KB
/
treedata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//converting object data in http://mbraak.github.io/jqTree/ format
//----------------------------------------------------------------
//return formatted html string with result type name and color style
//server-eval smartpackage adds custom property:
// ____TYPE____ for special objects (e.g. Error, Function, ...)
var _typeHtml = function(value) {
var type = "unknown";
var type_style = 'style="white-space: pre; color: ';
if (value.____TYPE____) {
type = value.____TYPE____;
if (type === "[Circular]") {
type += "[" + value.path + "]";
type_style += 'red;"';
} else if (type.indexOf("[Object]") >= 0) {
type_style += 'green;"';
} else if (type === "[Error]") {
type += "[" + value.err + "]";
type_style += 'red;"';
} else if (type === "[Function]") {
type_style += 'blue;"';
} else if (type === '[Tinytest]') {
type += '[<a href="http://' +
ServerEval.currentHost() + ':' + value.port + '" ' +
'target="_blank">open..</a>]';
type_style += 'brown;"';
} else {
type_style += 'gray;"';
}
} else if (_.isArray(value)) {
type = '[Array[' + value.length + ']]';
type_style += 'orange;"';
} else if (_.isObject(value)) {
type = '[Object]';
type_style += 'green;"';
} else {
type = 'null';
type_style += 'red;"';
}
return '<span ' + type_style + '>' + type + "</span>";
};
//converts error objects in jqtree format
var _errorToTreeData = function(obj) {
var tree_data = [];
var belowEval = false;
var stacktrace = _.map(obj.stack || [], function(value, key, list) {
value = value.replace(/\s*at\s*/, '');
var call = value.replace(/\s+/, "|").split("|");
var location = (call.length === 2 ? call[1] : call[0]);
var method = (call.length === 2 ? call[0] : 'anonymous');
if (method === "eval") {
belowEval = true;
return undefined; //remove the eval call
}
var special = "";
if (!belowEval) {
//above the eval call itself is most likely the important stuff
special = "important";
} else if (location.indexOf("server-eval") >= 0 || method.indexOf("__serverEval") >= 0) {
//internal server-eval overhead
special = "internal";
}
var error_method = '<span class="error_method ' + special + '">' +
method + '</span>';
var error_location = '<span class="error_location ' + special + '">' +
location + '</span>';
return {
'label': error_method + error_location
};
});
stacktrace = _.compact(stacktrace);
tree_data.push({
'label': _typeHtml(obj),
'children': stacktrace
});
return tree_data;
};
var _createSubtree = function(label, children) {
return {
'label': label,
'children': children
};
};
var formatByteSize = function(bytes) {
bytes = parseInt(bytes, 10);
var bytes_formatted;
if (isNaN(bytes) || bytes === 0) {
return '';
} else if (bytes < 1024) {
bytes_formatted = bytes + "byte";
} else if (bytes < 1048576) {
bytes_formatted = (bytes / 1024).toFixed(2) + "kb";
} else {
floatNum = bytes / 1048576;
bytes_formatted = floatNum.toFixed(2) + "mb";
}
return '<span style="font-size: 0.6em;">' + bytes_formatted + ' est.</span>';
};
//converts all kind of result objects in jqtree format
//recursive function adding subtrees, subtree subtrees, ...
var objectToTreeData = function(obj_raw, top_level) {
var obj = obj_raw._id && obj_raw.result ? obj_raw.result /* special object from server-eval */ : obj_raw;
if (!_.isObject(obj)) return [];
var tree_data = [];
var isError = obj.____TYPE____ === "[Error]";
var isCircular = obj.____TYPE____ === "[Circular]";
if (isError) {
if (obj.size_error) {
obj.err = obj.err.replace(/IGNORE/, '<a class="ignore_size" id="' + obj_raw._id + '">ignore..</a>');
}
return _errorToTreeData(obj);
}
for (var key in obj) {
var value = obj[key];
//dont show special result type and circular path property in tree
if (key === "____TYPE____" || isCircular && key === "path") {
continue;
}
var sub_tree;
if (_.isObject(value)) {
//sub_tree with children (objects)
sub_tree = _createSubtree(key + ": " + _typeHtml(value), objectToTreeData(value, false));
} else {
//sub_tree without children (string, number, boolean)
sub_tree = _createSubtree(key + ": " + wrapPrimitives(value));
}
//top level label is just the _typeHtml and properties are direct children
if (top_level) {
if (tree_data.length === 0) {
tree_data.push(_createSubtree(_typeHtml(obj) + formatByteSize(obj_raw.size), []));
}
tree_data[0].children.push(sub_tree);
} else {
tree_data.push(sub_tree);
}
}
if (top_level && tree_data.length === 0) {
tree_data.push(_createSubtree(_typeHtml(obj)));
}
return tree_data;
};