abn_tree_directive.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. (function() {
  2. var module;
  3. module = angular.module('angularBootstrapNavTree', []);
  4. module.directive('abnTree', [
  5. '$timeout', function($timeout) {
  6. return {
  7. restrict: 'E',
  8. template: "<ul class=\"nav nav-list nav-pills nav-stacked abn-tree\">\n <li ng-repeat=\"row in tree_rows | filter:{visible:true} track by row.branch.uid\" ng-animate=\"'abn-tree-animate'\" ng-class=\"'level-' + {{ row.level }} + (row.branch.selected ? ' active':'')\" class=\"abn-tree-row\">\n <a ng-click=\"user_clicks_branch(row.branch)\">\n <i ng-class=\"row.tree_icon\" ng-click=\"row.branch.expanded = !row.branch.expanded\" class=\"indented tree-icon\"> </i>\n <span class=\"indented tree-label\">{{ row.label }} </span>\n </a>\n </li>\n</ul>",
  9. replace: true,
  10. scope: {
  11. treeData: '=',
  12. onSelect: '&',
  13. initialSelection: '@',
  14. treeControl: '='
  15. },
  16. link: function(scope, element, attrs) {
  17. var error, expand_all_parents, expand_level, for_all_ancestors, for_each_branch, get_parent, n, on_treeData_change, select_branch, selected_branch, tree;
  18. error = function(s) {
  19. console.log('ERROR:' + s);
  20. debugger;
  21. return void 0;
  22. };
  23. if (attrs.iconExpand == null) {
  24. attrs.iconExpand = 'icon-plus glyphicon glyphicon-plus fa fa-plus';
  25. }
  26. if (attrs.iconCollapse == null) {
  27. attrs.iconCollapse = 'icon-minus glyphicon glyphicon-minus fa fa-minus';
  28. }
  29. if (attrs.iconLeaf == null) {
  30. attrs.iconLeaf = 'icon-file glyphicon glyphicon-file fa fa-file';
  31. }
  32. if (attrs.expandLevel == null) {
  33. attrs.expandLevel = '3';
  34. }
  35. expand_level = parseInt(attrs.expandLevel, 10);
  36. if (!scope.treeData) {
  37. alert('no treeData defined for the tree!');
  38. return;
  39. }
  40. if (scope.treeData.length == null) {
  41. if (treeData.label != null) {
  42. scope.treeData = [treeData];
  43. } else {
  44. alert('treeData should be an array of root branches');
  45. return;
  46. }
  47. }
  48. for_each_branch = function(f) {
  49. var do_f, root_branch, _i, _len, _ref, _results;
  50. do_f = function(branch, level) {
  51. var child, _i, _len, _ref, _results;
  52. f(branch, level);
  53. if (branch.children != null) {
  54. _ref = branch.children;
  55. _results = [];
  56. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  57. child = _ref[_i];
  58. _results.push(do_f(child, level + 1));
  59. }
  60. return _results;
  61. }
  62. };
  63. _ref = scope.treeData;
  64. _results = [];
  65. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  66. root_branch = _ref[_i];
  67. _results.push(do_f(root_branch, 1));
  68. }
  69. return _results;
  70. };
  71. selected_branch = null;
  72. select_branch = function(branch) {
  73. if (!branch) {
  74. if (selected_branch != null) {
  75. selected_branch.selected = false;
  76. }
  77. selected_branch = null;
  78. return;
  79. }
  80. if (branch !== selected_branch) {
  81. if (selected_branch != null) {
  82. selected_branch.selected = false;
  83. }
  84. branch.selected = true;
  85. selected_branch = branch;
  86. expand_all_parents(branch);
  87. if (branch.onSelect != null) {
  88. return $timeout(function() {
  89. return branch.onSelect(branch);
  90. });
  91. } else {
  92. if (scope.onSelect != null) {
  93. return $timeout(function() {
  94. return scope.onSelect({
  95. branch: branch
  96. });
  97. });
  98. }
  99. }
  100. }
  101. };
  102. scope.user_clicks_branch = function(branch) {
  103. if (branch !== selected_branch) {
  104. return select_branch(branch);
  105. }
  106. };
  107. get_parent = function(child) {
  108. var parent;
  109. parent = void 0;
  110. if (child.parent_uid) {
  111. for_each_branch(function(b) {
  112. if (b.uid === child.parent_uid) {
  113. return parent = b;
  114. }
  115. });
  116. }
  117. return parent;
  118. };
  119. for_all_ancestors = function(child, fn) {
  120. var parent;
  121. parent = get_parent(child);
  122. if (parent != null) {
  123. fn(parent);
  124. return for_all_ancestors(parent, fn);
  125. }
  126. };
  127. expand_all_parents = function(child) {
  128. return for_all_ancestors(child, function(b) {
  129. return b.expanded = true;
  130. });
  131. };
  132. scope.tree_rows = [];
  133. on_treeData_change = function() {
  134. var add_branch_to_list, root_branch, _i, _len, _ref, _results;
  135. for_each_branch(function(b, level) {
  136. if (!b.uid) {
  137. return b.uid = "" + Math.random();
  138. }
  139. });
  140. console.log('UIDs are set.');
  141. for_each_branch(function(b) {
  142. var child, _i, _len, _ref, _results;
  143. if (angular.isArray(b.children)) {
  144. _ref = b.children;
  145. _results = [];
  146. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  147. child = _ref[_i];
  148. _results.push(child.parent_uid = b.uid);
  149. }
  150. return _results;
  151. }
  152. });
  153. scope.tree_rows = [];
  154. for_each_branch(function(branch) {
  155. var child, f;
  156. if (branch.children) {
  157. if (branch.children.length > 0) {
  158. f = function(e) {
  159. if (typeof e === 'string') {
  160. return {
  161. label: e,
  162. children: []
  163. };
  164. } else {
  165. return e;
  166. }
  167. };
  168. return branch.children = (function() {
  169. var _i, _len, _ref, _results;
  170. _ref = branch.children;
  171. _results = [];
  172. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  173. child = _ref[_i];
  174. _results.push(f(child));
  175. }
  176. return _results;
  177. })();
  178. }
  179. } else {
  180. return branch.children = [];
  181. }
  182. });
  183. add_branch_to_list = function(level, branch, visible) {
  184. var child, child_visible, tree_icon, _i, _len, _ref, _results;
  185. if (branch.expanded == null) {
  186. branch.expanded = false;
  187. }
  188. if (!branch.children || branch.children.length === 0) {
  189. tree_icon = attrs.iconLeaf;
  190. } else {
  191. if (branch.expanded) {
  192. tree_icon = attrs.iconCollapse;
  193. } else {
  194. tree_icon = attrs.iconExpand;
  195. }
  196. }
  197. scope.tree_rows.push({
  198. level: level,
  199. branch: branch,
  200. label: branch.label,
  201. tree_icon: tree_icon,
  202. visible: visible
  203. });
  204. if (branch.children != null) {
  205. _ref = branch.children;
  206. _results = [];
  207. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  208. child = _ref[_i];
  209. child_visible = visible && branch.expanded;
  210. _results.push(add_branch_to_list(level + 1, child, child_visible));
  211. }
  212. return _results;
  213. }
  214. };
  215. _ref = scope.treeData;
  216. _results = [];
  217. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  218. root_branch = _ref[_i];
  219. _results.push(add_branch_to_list(1, root_branch, true));
  220. }
  221. return _results;
  222. };
  223. scope.$watch('treeData', on_treeData_change, true);
  224. if (attrs.initialSelection != null) {
  225. for_each_branch(function(b) {
  226. if (b.label === attrs.initialSelection) {
  227. return $timeout(function() {
  228. return select_branch(b);
  229. });
  230. }
  231. });
  232. }
  233. n = scope.treeData.length;
  234. console.log('num root branches = ' + n);
  235. for_each_branch(function(b, level) {
  236. b.level = level;
  237. return b.expanded = b.level < expand_level;
  238. });
  239. if (scope.treeControl != null) {
  240. if (angular.isObject(scope.treeControl)) {
  241. tree = scope.treeControl;
  242. tree.expand_all = function() {
  243. return for_each_branch(function(b, level) {
  244. return b.expanded = true;
  245. });
  246. };
  247. tree.collapse_all = function() {
  248. return for_each_branch(function(b, level) {
  249. return b.expanded = false;
  250. });
  251. };
  252. tree.get_first_branch = function() {
  253. n = scope.treeData.length;
  254. if (n > 0) {
  255. return scope.treeData[0];
  256. }
  257. };
  258. tree.select_first_branch = function() {
  259. var b;
  260. b = tree.get_first_branch();
  261. return tree.select_branch(b);
  262. };
  263. tree.get_selected_branch = function() {
  264. return selected_branch;
  265. };
  266. tree.get_parent_branch = function(b) {
  267. return get_parent(b);
  268. };
  269. tree.select_branch = function(b) {
  270. select_branch(b);
  271. return b;
  272. };
  273. tree.get_children = function(b) {
  274. return b.children;
  275. };
  276. tree.select_parent_branch = function(b) {
  277. var p;
  278. if (b == null) {
  279. b = tree.get_selected_branch();
  280. }
  281. if (b != null) {
  282. p = tree.get_parent_branch(b);
  283. if (p != null) {
  284. tree.select_branch(p);
  285. return p;
  286. }
  287. }
  288. };
  289. tree.add_branch = function(parent, new_branch) {
  290. if (parent != null) {
  291. parent.children.push(new_branch);
  292. parent.expanded = true;
  293. } else {
  294. scope.treeData.push(new_branch);
  295. }
  296. return new_branch;
  297. };
  298. tree.add_root_branch = function(new_branch) {
  299. tree.add_branch(null, new_branch);
  300. return new_branch;
  301. };
  302. tree.expand_branch = function(b) {
  303. if (b == null) {
  304. b = tree.get_selected_branch();
  305. }
  306. if (b != null) {
  307. b.expanded = true;
  308. return b;
  309. }
  310. };
  311. tree.collapse_branch = function(b) {
  312. if (b == null) {
  313. b = selected_branch;
  314. }
  315. if (b != null) {
  316. b.expanded = false;
  317. return b;
  318. }
  319. };
  320. tree.get_siblings = function(b) {
  321. var p, siblings;
  322. if (b == null) {
  323. b = selected_branch;
  324. }
  325. if (b != null) {
  326. p = tree.get_parent_branch(b);
  327. if (p) {
  328. siblings = p.children;
  329. } else {
  330. siblings = scope.treeData;
  331. }
  332. return siblings;
  333. }
  334. };
  335. tree.get_next_sibling = function(b) {
  336. var i, siblings;
  337. if (b == null) {
  338. b = selected_branch;
  339. }
  340. if (b != null) {
  341. siblings = tree.get_siblings(b);
  342. n = siblings.length;
  343. i = siblings.indexOf(b);
  344. if (i < n) {
  345. return siblings[i + 1];
  346. }
  347. }
  348. };
  349. tree.get_prev_sibling = function(b) {
  350. var i, siblings;
  351. if (b == null) {
  352. b = selected_branch;
  353. }
  354. siblings = tree.get_siblings(b);
  355. n = siblings.length;
  356. i = siblings.indexOf(b);
  357. if (i > 0) {
  358. return siblings[i - 1];
  359. }
  360. };
  361. tree.select_next_sibling = function(b) {
  362. var next;
  363. if (b == null) {
  364. b = selected_branch;
  365. }
  366. if (b != null) {
  367. next = tree.get_next_sibling(b);
  368. if (next != null) {
  369. return tree.select_branch(next);
  370. }
  371. }
  372. };
  373. tree.select_prev_sibling = function(b) {
  374. var prev;
  375. if (b == null) {
  376. b = selected_branch;
  377. }
  378. if (b != null) {
  379. prev = tree.get_prev_sibling(b);
  380. if (prev != null) {
  381. return tree.select_branch(prev);
  382. }
  383. }
  384. };
  385. tree.get_first_child = function(b) {
  386. var _ref;
  387. if (b == null) {
  388. b = selected_branch;
  389. }
  390. if (b != null) {
  391. if (((_ref = b.children) != null ? _ref.length : void 0) > 0) {
  392. return b.children[0];
  393. }
  394. }
  395. };
  396. tree.get_closest_ancestor_next_sibling = function(b) {
  397. var next, parent;
  398. next = tree.get_next_sibling(b);
  399. if (next != null) {
  400. return next;
  401. } else {
  402. parent = tree.get_parent_branch(b);
  403. return tree.get_closest_ancestor_next_sibling(parent);
  404. }
  405. };
  406. tree.get_next_branch = function(b) {
  407. var next;
  408. if (b == null) {
  409. b = selected_branch;
  410. }
  411. if (b != null) {
  412. next = tree.get_first_child(b);
  413. if (next != null) {
  414. return next;
  415. } else {
  416. next = tree.get_closest_ancestor_next_sibling(b);
  417. return next;
  418. }
  419. }
  420. };
  421. tree.select_next_branch = function(b) {
  422. var next;
  423. if (b == null) {
  424. b = selected_branch;
  425. }
  426. if (b != null) {
  427. next = tree.get_next_branch(b);
  428. if (next != null) {
  429. tree.select_branch(next);
  430. return next;
  431. }
  432. }
  433. };
  434. tree.last_descendant = function(b) {
  435. var last_child;
  436. if (b == null) {
  437. debugger;
  438. }
  439. n = b.children.length;
  440. if (n === 0) {
  441. return b;
  442. } else {
  443. last_child = b.children[n - 1];
  444. return tree.last_descendant(last_child);
  445. }
  446. };
  447. tree.get_prev_branch = function(b) {
  448. var parent, prev_sibling;
  449. if (b == null) {
  450. b = selected_branch;
  451. }
  452. if (b != null) {
  453. prev_sibling = tree.get_prev_sibling(b);
  454. if (prev_sibling != null) {
  455. return tree.last_descendant(prev_sibling);
  456. } else {
  457. parent = tree.get_parent_branch(b);
  458. return parent;
  459. }
  460. }
  461. };
  462. return tree.select_prev_branch = function(b) {
  463. var prev;
  464. if (b == null) {
  465. b = selected_branch;
  466. }
  467. if (b != null) {
  468. prev = tree.get_prev_branch(b);
  469. if (prev != null) {
  470. tree.select_branch(prev);
  471. return prev;
  472. }
  473. }
  474. };
  475. }
  476. }
  477. }
  478. };
  479. }
  480. ]);
  481. }).call(this);