-
Notifications
You must be signed in to change notification settings - Fork 86
/
json-formatter.js
133 lines (115 loc) · 3.16 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
'use strict';
angular.module('jsonFormatter', ['RecursionHelper'])
.directive('jsonFormatter', ['RecursionHelper', function (RecursionHelper) {
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 link(scope, element, attributes) {
scope.isArray = function () {
return Array.isArray(scope.json);
};
scope.isObject = function() {
return scope.json && typeof scope.json === 'object';
};
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){
scope.type = getType(scope.json);
if (scope.type === 'null') {
return 'null';
}
if (scope.type === 'undefined') {
return 'undefined';
}
if (scope.type === 'string') {
value = '"' + escapeString(value) + '"';
}
if (scope.type === 'function'){
// Remove content of the function
return scope.json.toString()
.replace(/[\r\n]/g, '')
.replace(/\{.*\}/, '') + '{ ... }';
}
return value;
};
}
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);
}
};
}]);