jsontree.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 (isArray) {
  114. scope.preview += value + ', ';
  115. } else {
  116. scope.preview += key + ': ' + value + ', ';
  117. }
  118. });
  119. scope.preview = scope.preview.substring(0, scope.preview.length - (scope.preview.length > 2 ? 2 : 0)) + (isArray ? ' ]' : ' }');
  120. // If directive initially has isExpanded set, also set shouldRender to true
  121. if (scope.startExpanded) {
  122. scope.shouldRender = true;
  123. elem.addClass('expanded');
  124. }
  125. // Setup isExpanded state handling
  126. scope.isExpanded = scope.startExpanded;
  127. scope.toggleExpanded = function jsonNodeDirectiveToggleExpanded() {
  128. scope.isExpanded = !scope.isExpanded;
  129. if (scope.isExpanded) {
  130. elem.addClass('expanded');
  131. } else {
  132. elem.removeClass('expanded');
  133. }
  134. // For delaying subnode render until requested
  135. scope.shouldRender = true;
  136. };
  137. } else {
  138. scope.isExpandable = false;
  139. // Add expandable class for CSS usage
  140. elem.addClass('not-expandable');
  141. }
  142. }
  143. };
  144. }]);
  145. /** Added by: Alex Wendland (me@alexwendland.com), 2014-08-09
  146. * Source: http://stackoverflow.com/questions/14430655/recursion-in-angular-directives
  147. *
  148. * Used to allow for recursion within directives
  149. */
  150. coreModule.factory('ajsRecursiveDirectiveHelper', ['$compile', function RecursiveDirectiveHelper($compile) {
  151. return {
  152. /**
  153. * Manually compiles the element, fixing the recursion loop.
  154. * @param element
  155. * @param [link] A post-link function, or an object with function(s) registered via pre and post properties.
  156. * @returns An object containing the linking functions.
  157. */
  158. compile: function RecursiveDirectiveHelperCompile(element, link) {
  159. // Normalize the link parameter
  160. if (angular.isFunction(link)) {
  161. link = {
  162. post: link
  163. };
  164. }
  165. // Break the recursion loop by removing the contents
  166. var contents = element.contents().remove();
  167. var compiledContents;
  168. return {
  169. pre: (link && link.pre) ? link.pre : null,
  170. /**
  171. * Compiles and re-adds the contents
  172. */
  173. post: function RecursiveDirectiveHelperCompilePost(scope, element) {
  174. // Compile the contents
  175. if (!compiledContents) {
  176. compiledContents = $compile(contents);
  177. }
  178. // Re-add the compiled contents to the element
  179. compiledContents(scope, function (clone) {
  180. element.append(clone);
  181. });
  182. // Call the post-linking function, if any
  183. if (link && link.post) {
  184. link.post.apply(null, arguments);
  185. }
  186. }
  187. };
  188. }
  189. };
  190. }]);