-
Notifications
You must be signed in to change notification settings - Fork 41
/
json-tree.js
210 lines (203 loc) · 8.93 KB
/
json-tree.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
/*global angular */
/** Created by: Alex Wendland (me@alexwendland.com), 2014-08-06
*
* angular-json-tree
*
* Directive for creating a tree-view out of a JS Object. Only loads
* sub-nodes on demand in order to improve performance of rendering large
* objects.
*
* Attributes:
* - object (Object, 2-way): JS object to build the tree from
* - start-expanded (Boolean, 1-way, ?=true): should the tree default to expanded
*
* Usage:
* // In the controller
* scope.someObject = {
* test: 'hello',
* array: [1,1,2,3,5,8]
* };
* // In the html
* <json-tree object="someObject"></json-tree>
*
* Dependencies:
* - utils (json-tree.js)
* - ajsRecursiveDirectiveHelper (json-tree.js)
*
* Test: json-tree-test.js
*/
(function() {
'use strict';
var utils = {
/* See link for possible type values to check against.
* http://stackoverflow.com/questions/4622952/json-object-containing-array
*
* Value Class Type
* -------------------------------------
* "foo" String string
* new String("foo") String object
* 1.2 Number number
* new Number(1.2) Number object
* true Boolean boolean
* new Boolean(true) Boolean object
* new Date() Date object
* new Error() Error object
* [1,2,3] Array object
* new Array(1, 2, 3) Array object
* new Function("") Function function
* /abc/g RegExp object (function in Nitro/V8)
* new RegExp("meow") RegExp object (function in Nitro/V8)
* {} Object object
* new Object() Object object
*/
is: function is(obj, clazz) {
return Object.prototype.toString.call(obj).slice(8, -1) === clazz;
},
// See above for possible values
whatClass: function whatClass(obj) {
return Object.prototype.toString.call(obj).slice(8, -1);
},
// Iterate over an objects keyset
forKeys: function forKeys(obj, f) {
for (var key in obj) {
if (obj.hasOwnProperty(key) && typeof obj[key] !== 'function') {
if (f(key, obj[key])) {
break;
}
}
}
}
};
angular.module('angular-json-tree', ['ajs.RecursiveDirectiveHelper'])
.directive('jsonTree', [function jsonTreeDirective() {
return {
restrict: 'E',
scope: {
object: '=',
startExpanded: '&?',
rootName: '&?',
},
template: '<json-node key="rootName() || \'Object\'" value="object" start-expanded="startExpanded()"></json-node>'
};
}])
.directive('jsonNode', ['ajsRecursiveDirectiveHelper', function jsonNodeDirective(ajsRecursiveDirectiveHelper) {
return {
restrict: 'E',
scope: {
key: '=',
value: '=',
startExpanded: '&?'
},
compile: function jsonNodeDirectiveCompile(elem) {
return ajsRecursiveDirectiveHelper.compile(elem, this);
},
template: ' <span class="key" ng-click="toggleExpanded()">{{key}}</span>' +
' <span class="leaf-value" ng-if="!isExpandable">{{value}}</span>' +
' <span class="branch-preview" ng-if="isExpandable" ng-show="!isExpanded" ng-click="toggleExpanded()">{{preview}}</span>' +
' <ul class="branch-value" ng-if="isExpandable && shouldRender" ng-show="isExpanded">' +
' <li ng-repeat="(subkey,subval) in value track by subkey">' +
' <json-node key="subkey" value="subval"></json-node>' +
' </li>' +
' </ul>',
pre: function jsonNodeDirectiveLink(scope, elem, attrs) {
// Set value's type as Class for CSS styling
elem.addClass(utils.whatClass(scope.value).toLowerCase());
// If the value is an Array or Object, use expandable view type
if (utils.is(scope.value, 'Object') || utils.is(scope.value, 'Array')) {
scope.isExpandable = true;
// Add expandable class for CSS usage
elem.addClass('expandable');
// Add a class indicating an empty Object/Array (for removing expandable UI, if desired)
if (Object.keys(scope.value).length < 1) {
elem.addClass('empty');
}
// Setup preview text
var isArray = utils.is(scope.value, 'Array');
scope.preview = isArray ? '[ ' : '{ ';
utils.forKeys(scope.value, function jsonNodeDirectiveLinkForKeys(key, value) {
if (isArray) {
scope.preview += value + ', ';
} else {
scope.preview += key + ': ' + value + ', ';
}
});
scope.preview = scope.preview.substring(0, scope.preview.length - (scope.preview.length > 2 ? 2 : 0)) + (isArray ? ' ]' : ' }');
// If parent directive has startExpanded set to recursive, inherit startExpanded
if (scope.$parent && scope.$parent.startExpanded
&& scope.$parent.startExpanded() == 'recursive') {
scope.startExpanded = scope.$parent.startExpanded;
}
// If directive initially has isExpanded set, also set shouldRender to true
if (scope.startExpanded && scope.startExpanded()) {
scope.shouldRender = true;
elem.addClass('expanded');
}
// Setup isExpanded state handling
scope.isExpanded = scope.startExpanded ? scope.startExpanded() : false;
if (scope.isExpanded == 'recursive') scope.isExpanded = true;
scope.toggleExpanded = function jsonNodeDirectiveToggleExpanded() {
scope.isExpanded = !scope.isExpanded;
if (scope.isExpanded) {
elem.addClass('expanded');
} else {
elem.removeClass('expanded');
}
// For delaying subnode render until requested
scope.shouldRender = true;
};
} else {
scope.isExpandable = false;
// Add expandable class for CSS usage
elem.addClass('not-expandable');
}
}
};
}]);
/** Added by: Alex Wendland (me@alexwendland.com), 2014-08-09
* Source: http://stackoverflow.com/questions/14430655/recursion-in-angular-directives
*
* Used to allow for recursion within directives
*/
angular.module('ajs.RecursiveDirectiveHelper', [])
.factory('ajsRecursiveDirectiveHelper', ['$compile', function RecursiveDirectiveHelper($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 RecursiveDirectiveHelperCompile(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 RecursiveDirectiveHelperCompilePost(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);
}
}
};
}
};
}]);
})();