abn_tree_directive.js 17 KB

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