tree.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. app.controller('AbnTestController', function($scope, $timeout) {
  2. var apple_selected, tree, treedata_avm, treedata_geography;
  3. $scope.my_tree_handler = function(branch) {
  4. var _ref;
  5. $scope.output = "You selected: " + branch.label;
  6. if ((_ref = branch.data) != null ? _ref.description : void 0) {
  7. return $scope.output += '(' + branch.data.description + ')';
  8. }
  9. };
  10. apple_selected = function(branch) {
  11. return $scope.output = "APPLE! : " + branch.label;
  12. };
  13. treedata_avm = [
  14. {
  15. label: 'Animal',
  16. children: [
  17. {
  18. label: 'Dog',
  19. data: {
  20. description: "man's best friend"
  21. }
  22. }, {
  23. label: 'Cat',
  24. data: {
  25. description: "Felis catus"
  26. }
  27. }, {
  28. label: 'Hippopotamus',
  29. data: {
  30. description: "hungry, hungry"
  31. }
  32. }, {
  33. label: 'Chicken',
  34. children: ['White Leghorn', 'Rhode Island Red', 'Jersey Giant']
  35. }
  36. ]
  37. }, {
  38. label: 'Vegetable',
  39. data: {
  40. definition: "A plant or part of a plant used as food, typically as accompaniment to meat or fish, such as a cabbage, potato, carrot, or bean.",
  41. data_can_contain_anything: true
  42. },
  43. onSelect: function(branch) {
  44. return $scope.output = "Vegetable: " + branch.data.definition;
  45. },
  46. children: [
  47. {
  48. label: 'Oranges'
  49. }, {
  50. label: 'Apples',
  51. children: [
  52. {
  53. label: 'Granny Smith',
  54. onSelect: apple_selected
  55. }, {
  56. label: 'Red Delicous',
  57. onSelect: apple_selected
  58. }, {
  59. label: 'Fuji',
  60. onSelect: apple_selected
  61. }
  62. ]
  63. }
  64. ]
  65. }, {
  66. label: 'Mineral',
  67. children: [
  68. {
  69. label: 'Rock',
  70. children: ['Igneous', 'Sedimentary', 'Metamorphic']
  71. }, {
  72. label: 'Metal',
  73. children: ['Aluminum', 'Steel', 'Copper']
  74. }, {
  75. label: 'Plastic',
  76. children: [
  77. {
  78. label: 'Thermoplastic',
  79. children: ['polyethylene', 'polypropylene', 'polystyrene', ' polyvinyl chloride']
  80. }, {
  81. label: 'Thermosetting Polymer',
  82. children: ['polyester', 'polyurethane', 'vulcanized rubber', 'bakelite', 'urea-formaldehyde']
  83. }
  84. ]
  85. }
  86. ]
  87. }
  88. ];
  89. treedata_geography = [
  90. {
  91. label: 'North America',
  92. children: [
  93. {
  94. label: 'Canada',
  95. children: ['Toronto', 'Vancouver']
  96. }, {
  97. label: 'USA',
  98. children: ['New York', 'Los Angeles']
  99. }, {
  100. label: 'Mexico',
  101. children: ['Mexico City', 'Guadalajara']
  102. }
  103. ]
  104. }, {
  105. label: 'South America',
  106. children: [
  107. {
  108. label: 'Venezuela',
  109. children: ['Caracas', 'Maracaibo']
  110. }, {
  111. label: 'Brazil',
  112. children: ['Sao Paulo', 'Rio de Janeiro']
  113. }, {
  114. label: 'Argentina',
  115. children: ['Buenos Aires', 'Cordoba']
  116. }
  117. ]
  118. }
  119. ];
  120. $scope.my_data = treedata_avm;
  121. $scope.try_changing_the_tree_data = function() {
  122. if ($scope.my_data === treedata_avm) {
  123. return $scope.my_data = treedata_geography;
  124. } else {
  125. return $scope.my_data = treedata_avm;
  126. }
  127. };
  128. $scope.my_tree = tree = {};
  129. $scope.try_async_load = function() {
  130. $scope.my_data = [];
  131. $scope.doing_async = true;
  132. return $timeout(function() {
  133. if (Math.random() < 0.5) {
  134. $scope.my_data = treedata_avm;
  135. } else {
  136. $scope.my_data = treedata_geography;
  137. }
  138. $scope.doing_async = false;
  139. return tree.expand_all();
  140. }, 1000);
  141. };
  142. return $scope.try_adding_a_branch = function() {
  143. var b;
  144. b = tree.get_selected_branch();
  145. return tree.add_branch(b, {
  146. label: 'New Branch',
  147. data: {
  148. something: 42,
  149. "else": 43
  150. }
  151. });
  152. };
  153. });