-
Notifications
You must be signed in to change notification settings - Fork 86
/
json-formatter.js
278 lines (234 loc) · 8.35 KB
/
json-formatter.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*!
* jsonformatter
*
* Version: 0.4.1 - 2015-12-01T20:27:41.483Z
* License: MIT
*/
'use strict';
angular.module('jsonFormatter', ['RecursionHelper'])
.provider('JSONFormatterConfig', function JSONFormatterConfigProvider() {
// Default values for hover preview config
var hoverPreviewEnabled = false;
var hoverPreviewArrayCount = 100;
var hoverPreviewFieldCount = 5;
return {
get hoverPreviewEnabled() {
return hoverPreviewEnabled;
},
set hoverPreviewEnabled(value) {
hoverPreviewEnabled = !!value;
},
get hoverPreviewArrayCount() {
return hoverPreviewArrayCount;
},
set hoverPreviewArrayCount(value) {
hoverPreviewArrayCount = parseInt(value, 10);
},
get hoverPreviewFieldCount() {
return hoverPreviewFieldCount;
},
set hoverPreviewFieldCount(value) {
hoverPreviewFieldCount = parseInt(value, 10);
},
$get: function () {
return {
hoverPreviewEnabled: hoverPreviewEnabled,
hoverPreviewArrayCount: hoverPreviewArrayCount,
hoverPreviewFieldCount: hoverPreviewFieldCount
};
}
};
})
.directive('jsonFormatter', ['RecursionHelper', 'JSONFormatterConfig', function jsonFormatterDirective(RecursionHelper, JSONFormatterConfig) {
function escapeString(str) {
return str.replace('"', '\"');
}
// From http://stackoverflow.com/a/332429
function getObjectName(object) {
if (object === undefined) {
return '';
}
if (object === null) {
return 'Object';
}
if (typeof object === 'object' && !object.constructor) {
return 'Object';
}
var funcNameRegex = /function (.{1,})\(/;
var results = (funcNameRegex).exec((object).constructor.toString());
if (results && results.length > 1) {
return results[1];
} else {
return '';
}
}
function getType(object) {
if (object === null) { return 'null'; }
return typeof object;
}
function getValuePreview (object, value) {
var type = getType(object);
if (type === 'null' || type === 'undefined') { return type; }
if (type === 'string') {
value = '"' + escapeString(value) + '"';
}
if (type === 'function'){
// Remove content of the function
return object.toString()
.replace(/[\r\n]/g, '')
.replace(/\{.*\}/, '') + '{…}';
}
return value;
}
function getPreview(object) {
var value = '';
if (angular.isObject(object)) {
value = getObjectName(object);
if (angular.isArray(object))
value += '[' + object.length + ']';
} else {
value = getValuePreview(object, object);
}
return value;
}
function link(scope) {
scope.isArray = function () {
return angular.isArray(scope.json);
};
scope.isObject = function() {
return angular.isObject(scope.json);
};
scope.getKeys = function (){
if (scope.isObject()) {
return Object.keys(scope.json).map(function(key) {
if (key === '') { return '""'; }
return key;
});
}
};
scope.type = getType(scope.json);
scope.hasKey = typeof scope.key !== 'undefined';
scope.getConstructorName = function(){
return getObjectName(scope.json);
};
if (scope.type === 'string'){
// Add custom type for date
if((new Date(scope.json)).toString() !== 'Invalid Date') {
scope.isDate = true;
}
// Add custom type for URLs
if (scope.json.indexOf('http') === 0) {
scope.isUrl = true;
}
}
scope.isEmptyObject = function () {
return scope.getKeys() && !scope.getKeys().length &&
scope.isOpen && !scope.isArray();
};
// If 'open' attribute is present
scope.isOpen = !!scope.open;
scope.toggleOpen = function () {
scope.isOpen = !scope.isOpen;
};
scope.childrenOpen = function () {
if (scope.open > 1){
return scope.open - 1;
}
return 0;
};
scope.openLink = function (isUrl) {
if(isUrl) {
window.location.href = scope.json;
}
};
scope.parseValue = function (value){
return getValuePreview(scope.json, value);
};
scope.showThumbnail = function () {
return !!JSONFormatterConfig.hoverPreviewEnabled && scope.isObject() && !scope.isOpen;
};
scope.getThumbnail = function () {
if (scope.isArray()) {
// if array length is greater then 100 it shows "Array[101]"
if (scope.json.length > JSONFormatterConfig.hoverPreviewArrayCount) {
return 'Array[' + scope.json.length + ']';
} else {
return '[' + scope.json.map(getPreview).join(', ') + ']';
}
} else {
var keys = scope.getKeys();
// the first five keys (like Chrome Developer Tool)
var narrowKeys = keys.slice(0, JSONFormatterConfig.hoverPreviewFieldCount);
// json value schematic information
var kvs = narrowKeys
.map(function (key) { return key + ':' + getPreview(scope.json[key]); });
// if keys count greater then 5 then show ellipsis
var ellipsis = keys.length >= 5 ? '…' : '';
return '{' + kvs.join(', ') + ellipsis + '}';
}
};
}
return {
templateUrl: 'json-formatter.html',
restrict: 'E',
replace: true,
scope: {
json: '=',
key: '=',
open: '='
},
compile: function(element) {
// Use the compile function from the RecursionHelper,
// And return the linking function(s) which it returns
return RecursionHelper.compile(element, link);
}
};
}]);
// Export to CommonJS style imports. Exporting this string makes this valid:
// angular.module('myApp', [require('jsonformatter')]);
if (typeof module === 'object') {
module.exports = 'jsonFormatter';
}
'use strict';
// from http://stackoverflow.com/a/18609594
angular.module('RecursionHelper', []).factory('RecursionHelper', ['$compile', function($compile){
return {
/**
* Manually compiles the element, fixing the recursion loop.
* @param element
* @param [link] A post-link function, or an object with function(s)
* registered via pre and post properties.
* @returns An object containing the linking functions.
*/
compile: function(element, link){
// Normalize the link parameter
if(angular.isFunction(link)){
link = { post: link };
}
// Break the recursion loop by removing the contents
var contents = element.contents().remove();
var compiledContents;
return {
pre: (link && link.pre) ? link.pre : null,
/**
* Compiles and re-adds the contents
*/
post: function(scope, element){
// Compile the contents
if(!compiledContents){
compiledContents = $compile(contents);
}
// Re-add the compiled contents to the element
compiledContents(scope, function(clone){
element.append(clone);
});
// Call the post-linking function, if any
if(link && link.post){
link.post.apply(null, arguments);
}
}
};
}
};
}]);
angular.module("jsonFormatter").run(["$templateCache", function($templateCache) {$templateCache.put("json-formatter.html","<div ng-init=\"isOpen = open && open > 0\" class=\"json-formatter-row\"><a ng-click=\"toggleOpen()\"><span class=\"toggler {{isOpen ? \'open\' : \'\'}}\" ng-if=\"isObject()\"></span> <span class=\"key\" ng-if=\"hasKey\"><span class=\"key-text\">{{key}}</span><span class=\"colon\">:</span></span> <span class=\"value\"><span ng-if=\"isObject()\"><span class=\"constructor-name\">{{getConstructorName(json)}}</span> <span ng-if=\"isArray()\"><span class=\"bracket\">[</span><span class=\"number\">{{json.length}}</span><span class=\"bracket\">]</span></span></span> <span ng-if=\"!isObject()\" ng-click=\"openLink(isUrl)\" class=\"{{type}}\" ng-class=\"{date: isDate, url: isUrl}\">{{parseValue(json)}}</span></span> <span ng-if=\"showThumbnail()\" class=\"thumbnail-text\">{{getThumbnail()}}</span></a><div class=\"children\" ng-if=\"getKeys().length && isOpen\"><json-formatter ng-repeat=\"key in getKeys() track by $index\" json=\"json[key]\" key=\"key\" open=\"childrenOpen()\"></json-formatter></div><div class=\"children empty object\" ng-if=\"isEmptyObject()\"></div><div class=\"children empty array\" ng-if=\"getKeys() && !getKeys().length && isOpen && isArray()\"></div></div>");}]);