abn_tree_directivechange.js 30 KB

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