filetree.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. define(function(require, exports, module) {
  2. var kity = require('../core/kity');
  3. var Layout = require('../core/layout');
  4. [-1, 1].forEach(registerLayoutForDir);
  5. function registerLayoutForDir(dir) {
  6. var name = 'filetree-' + (dir > 0 ? 'down' : 'up');
  7. Layout.register(name, kity.createClass({
  8. base: Layout,
  9. doLayout: function(parent, children, round) {
  10. var pBox = parent.getContentBox();
  11. var indent = 20;
  12. parent.setVertexOut(new kity.Point(pBox.left + indent, dir > 0 ? pBox.bottom : pBox.top));
  13. parent.setLayoutVectorOut(new kity.Vector(0, dir));
  14. if (!children.length) return;
  15. children.forEach(function(child) {
  16. var cbox = child.getContentBox();
  17. child.setLayoutTransform(new kity.Matrix());
  18. child.setVertexIn(new kity.Point(cbox.left, cbox.cy));
  19. child.setLayoutVectorIn(new kity.Vector(1, 0));
  20. });
  21. this.align(children, 'left');
  22. this.stack(children, 'y');
  23. var xAdjust = 0;
  24. xAdjust += pBox.left;
  25. xAdjust += indent;
  26. xAdjust += children[0].getStyle('margin-left');
  27. var yAdjust = 0;
  28. if (dir > 0) {
  29. yAdjust += pBox.bottom;
  30. yAdjust += parent.getStyle('margin-bottom');
  31. yAdjust += children[0].getStyle('margin-top');
  32. } else {
  33. yAdjust -= this.getTreeBox(children).bottom;
  34. yAdjust += pBox.top;
  35. yAdjust -= parent.getStyle('margin-top');
  36. yAdjust -= children[0].getStyle('margin-bottom');
  37. }
  38. this.move(children, xAdjust, yAdjust);
  39. },
  40. getOrderHint: function(node) {
  41. var hint = [];
  42. var box = node.getLayoutBox();
  43. var offset = node.getLevel() > 1 ? 3 : 5;
  44. hint.push({
  45. type: 'up',
  46. node: node,
  47. area: new kity.Box({
  48. x: box.x,
  49. y: box.top - node.getStyle('margin-top') - offset,
  50. width: box.width,
  51. height: node.getStyle('margin-top')
  52. }),
  53. path: ['M', box.x, box.top - offset, 'L', box.right, box.top - offset]
  54. });
  55. hint.push({
  56. type: 'down',
  57. node: node,
  58. area: new kity.Box({
  59. x: box.x,
  60. y: box.bottom + offset,
  61. width: box.width,
  62. height: node.getStyle('margin-bottom')
  63. }),
  64. path: ['M', box.x, box.bottom + offset, 'L', box.right, box.bottom + offset]
  65. });
  66. return hint;
  67. }
  68. }));
  69. }
  70. });