jsontree.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /** Created by: Alex Wendland (me@alexwendland.com), 2014-08-06
  2. *
  3. * angular-json-tree
  4. *
  5. * Directive for creating a tree-view out of a JS Object. Only loads
  6. * sub-nodes on demand in order to improve performance of rendering large
  7. * objects.
  8. *
  9. * Attributes:
  10. * - object (Object, 2-way): JS object to build the tree from
  11. * - start-expanded (Boolean, 1-way, ?=true): should the tree default to expanded
  12. *
  13. * Usage:
  14. * // In the controller
  15. * scope.someObject = {
  16. * test: 'hello',
  17. * array: [1,1,2,3,5,8]
  18. * };
  19. * // In the html
  20. * <json-tree object="someObject"></json-tree>
  21. *
  22. * Dependencies:
  23. * - utils (json-tree.js)
  24. * - ajsRecursiveDirectiveHelper (json-tree.js)
  25. *
  26. * Test: json-tree-test.js
  27. */
  28. import angular from 'angular';
  29. import coreModule from 'app/core/core_module';
  30. var utils = {
  31. /* See link for possible type values to check against.
  32. * http://stackoverflow.com/questions/4622952/json-object-containing-array
  33. *
  34. * Value Class Type
  35. * -------------------------------------
  36. * "foo" String string
  37. * new String("foo") String object
  38. * 1.2 Number number
  39. * new Number(1.2) Number object
  40. * true Boolean boolean
  41. * new Boolean(true) Boolean object
  42. * new Date() Date object
  43. * new Error() Error object
  44. * [1,2,3] Array object
  45. * new Array(1, 2, 3) Array object
  46. * new Function("") Function function
  47. * /abc/g RegExp object (function in Nitro/V8)
  48. * new RegExp("meow") RegExp object (function in Nitro/V8)
  49. * {} Object object
  50. * new Object() Object object
  51. */
  52. is: function is(obj, clazz) {
  53. return Object.prototype.toString.call(obj).slice(8, -1) === clazz;
  54. },
  55. // See above for possible values
  56. whatClass: function whatClass(obj) {
  57. return Object.prototype.toString.call(obj).slice(8, -1);
  58. },
  59. // Iterate over an objects keyset
  60. forKeys: function forKeys(obj, f) {
  61. for (var key in obj) {
  62. if (obj.hasOwnProperty(key) && typeof obj[key] !== 'function') {
  63. if (f(key, obj[key])) {
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. };
  70. coreModule.directive('jsonTree', [function jsonTreeDirective() {
  71. return {
  72. restrict: 'E',
  73. scope: {
  74. object: '=',
  75. startExpanded: '@',
  76. rootName: '@',
  77. },
  78. template: '<json-node key="rootName" value="object" start-expanded="startExpanded"></json-node>'
  79. };
  80. }]);
  81. coreModule.directive('jsonNode', ['ajsRecursiveDirectiveHelper', function jsonNodeDirective(ajsRecursiveDirectiveHelper) {
  82. return {
  83. restrict: 'E',
  84. scope: {
  85. key: '=',
  86. value: '=',
  87. startExpanded: '@'
  88. },
  89. compile: function jsonNodeDirectiveCompile(elem) {
  90. return ajsRecursiveDirectiveHelper.compile(elem, this);
  91. },
  92. template: ' <span class="json-tree-key" ng-click="toggleExpanded()">{{key}}</span>' +
  93. ' <span class="json-tree-leaf-value" ng-if="!isExpandable">{{value}}</span>' +
  94. ' <span class="json-tree-branch-preview" ng-if="isExpandable" ng-show="!isExpanded" ng-click="toggleExpanded()">' +
  95. ' {{preview}}</span>' +
  96. ' <ul class="json-tree-branch-value" ng-if="isExpandable && shouldRender" ng-show="isExpanded">' +
  97. ' <li ng-repeat="(subkey,subval) in value">' +
  98. ' <json-node key="subkey" value="subval"></json-node>' +
  99. ' </li>' +
  100. ' </ul>',
  101. pre: function jsonNodeDirectiveLink(scope, elem, attrs) {
  102. // Set value's type as Class for CSS styling
  103. elem.addClass(utils.whatClass(scope.value).toLowerCase());
  104. // If the value is an Array or Object, use expandable view type
  105. if (utils.is(scope.value, 'Object') || utils.is(scope.value, 'Array')) {
  106. scope.isExpandable = true;
  107. // Add expandable class for CSS usage
  108. elem.addClass('expandable');
  109. // Setup preview text
  110. var isArray = utils.is(scope.value, 'Array');
  111. scope.preview = isArray ? '[ ' : '{ ';
  112. utils.forKeys(scope.value, function jsonNodeDirectiveLinkForKeys(key, value) {
  113. if (value === null) { scope.value[key] = 'null'; }
  114. if (isArray) {
  115. scope.preview += value + ', ';
  116. } else {
  117. scope.preview += key + ': ' + value + ', ';
  118. }
  119. });
  120. scope.preview = scope.preview.substring(0, scope.preview.length - (scope.preview.length > 2 ? 2 : 0)) + (isArray ? ' ]' : ' }');
  121. // If directive initially has isExpanded set, also set shouldRender to true
  122. if (scope.startExpanded) {
  123. scope.shouldRender = true;
  124. elem.addClass('expanded');
  125. }
  126. // Setup isExpanded state handling
  127. scope.isExpanded = scope.startExpanded;
  128. scope.toggleExpanded = function jsonNodeDirectiveToggleExpanded() {
  129. scope.isExpanded = !scope.isExpanded;
  130. if (scope.isExpanded) {
  131. elem.addClass('expanded');
  132. } else {
  133. elem.removeClass('expanded');
  134. }
  135. // For delaying subnode render until requested
  136. scope.shouldRender = true;
  137. };
  138. } else {
  139. scope.isExpandable = false;
  140. // Add expandable class for CSS usage
  141. elem.addClass('not-expandable');
  142. }
  143. }
  144. };
  145. }]);
  146. /** Added by: Alex Wendland (me@alexwendland.com), 2014-08-09
  147. * Source: http://stackoverflow.com/questions/14430655/recursion-in-angular-directives
  148. *
  149. * Used to allow for recursion within directives
  150. */
  151. coreModule.factory('ajsRecursiveDirectiveHelper', ['$compile', function RecursiveDirectiveHelper($compile) {
  152. return {
  153. /**
  154. * Manually compiles the element, fixing the recursion loop.
  155. * @param element
  156. * @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
  157. * @returns An object containing the linking functions.
  158. */
  159. compile: function RecursiveDirectiveHelperCompile(element, link) {
  160. // Normalize the link parameter
  161. if (angular.isFunction(link)) {
  162. link = {
  163. post: link
  164. };
  165. }
  166. // Break the recursion loop by removing the contents
  167. var contents = element.contents().remove();
  168. var compiledContents;
  169. return {
  170. pre: (link && link.pre) ? link.pre : null,
  171. /**
  172. * Compiles and re-adds the contents
  173. */
  174. post: function RecursiveDirectiveHelperCompilePost(scope, element) {
  175. // Compile the contents
  176. if (!compiledContents) {
  177. compiledContents = $compile(contents);
  178. }
  179. // Re-add the compiled contents to the element
  180. compiledContents(scope, function (clone) {
  181. element.append(clone);
  182. });
  183. // Call the post-linking function, if any
  184. if (link && link.post) {
  185. link.post.apply(null, arguments);
  186. }
  187. }
  188. };
  189. }
  190. };
  191. }]);