selectAll.directive.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. angular.module('kityminderEditor')
  2. .directive('selectAll', function() {
  3. return {
  4. restrict: 'E',
  5. templateUrl: 'ui/directive/selectAll/selectAll.html',
  6. scope: {
  7. minder: '='
  8. },
  9. replace: true,
  10. link: function($scope) {
  11. var minder = $scope.minder;
  12. $scope.items = ['revert', 'siblings', 'level', 'path', 'tree'];
  13. $scope.select = {
  14. all: function() {
  15. var selection = [];
  16. minder.getRoot().traverse(function(node) {
  17. selection.push(node);
  18. });
  19. minder.select(selection, true);
  20. minder.fire('receiverfocus');
  21. },
  22. revert: function() {
  23. var selected = minder.getSelectedNodes();
  24. var selection = [];
  25. minder.getRoot().traverse(function(node) {
  26. if (selected.indexOf(node) == -1) {
  27. selection.push(node);
  28. }
  29. });
  30. minder.select(selection, true);
  31. minder.fire('receiverfocus');
  32. },
  33. siblings: function() {
  34. var selected = minder.getSelectedNodes();
  35. var selection = [];
  36. selected.forEach(function(node) {
  37. if (!node.parent) return;
  38. node.parent.children.forEach(function(sibling) {
  39. if (selection.indexOf(sibling) == -1) selection.push(sibling);
  40. });
  41. });
  42. minder.select(selection, true);
  43. minder.fire('receiverfocus');
  44. },
  45. level: function() {
  46. var selectedLevel = minder.getSelectedNodes().map(function(node) {
  47. return node.getLevel();
  48. });
  49. var selection = [];
  50. minder.getRoot().traverse(function(node) {
  51. if (selectedLevel.indexOf(node.getLevel()) != -1) {
  52. selection.push(node);
  53. }
  54. });
  55. minder.select(selection, true);
  56. minder.fire('receiverfocus');
  57. },
  58. path: function() {
  59. var selected = minder.getSelectedNodes();
  60. var selection = [];
  61. selected.forEach(function(node) {
  62. while(node && selection.indexOf(node) == -1) {
  63. selection.push(node);
  64. node = node.parent;
  65. }
  66. });
  67. minder.select(selection, true);
  68. minder.fire('receiverfocus');
  69. },
  70. tree: function() {
  71. var selected = minder.getSelectedNodes();
  72. var selection = [];
  73. selected.forEach(function(parent) {
  74. parent.traverse(function(node) {
  75. if (selection.indexOf(node) == -1) selection.push(node);
  76. });
  77. });
  78. minder.select(selection, true);
  79. minder.fire('receiverfocus');
  80. }
  81. };
  82. }
  83. }
  84. });