mxCompositeLayout.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxCompositeLayout
  7. *
  8. * Allows to compose multiple layouts into a single layout. The master layout
  9. * is the layout that handles move operations if another layout than the first
  10. * element in <layouts> should be used. The <master> layout is not executed as
  11. * the code assumes that it is part of <layouts>.
  12. *
  13. * Example:
  14. * (code)
  15. * var first = new mxFastOrganicLayout(graph);
  16. * var second = new mxParallelEdgeLayout(graph);
  17. * var layout = new mxCompositeLayout(graph, [first, second], first);
  18. * layout.execute(graph.getDefaultParent());
  19. * (end)
  20. *
  21. * Constructor: mxCompositeLayout
  22. *
  23. * Constructs a new layout using the given layouts. The graph instance is
  24. * required for creating the transaction that contains all layouts.
  25. *
  26. * Arguments:
  27. *
  28. * graph - Reference to the enclosing <mxGraph>.
  29. * layouts - Array of <mxGraphLayouts>.
  30. * master - Optional layout that handles moves. If no layout is given then
  31. * the first layout of the above array is used to handle moves.
  32. */
  33. function mxCompositeLayout(graph, layouts, master)
  34. {
  35. mxGraphLayout.call(this, graph);
  36. this.layouts = layouts;
  37. this.master = master;
  38. };
  39. /**
  40. * Extends mxGraphLayout.
  41. */
  42. mxCompositeLayout.prototype = new mxGraphLayout();
  43. mxCompositeLayout.prototype.constructor = mxCompositeLayout;
  44. /**
  45. * Variable: layouts
  46. *
  47. * Holds the array of <mxGraphLayouts> that this layout contains.
  48. */
  49. mxCompositeLayout.prototype.layouts = null;
  50. /**
  51. * Variable: master
  52. *
  53. * Reference to the <mxGraphLayouts> that handles moves. If this is null
  54. * then the first layout in <layouts> is used.
  55. */
  56. mxCompositeLayout.prototype.master = null;
  57. /**
  58. * Function: moveCell
  59. *
  60. * Implements <mxGraphLayout.moveCell> by calling move on <master> or the first
  61. * layout in <layouts>.
  62. */
  63. mxCompositeLayout.prototype.moveCell = function(cell, x, y)
  64. {
  65. if (this.master != null)
  66. {
  67. this.master.moveCell.apply(this.master, arguments);
  68. }
  69. else
  70. {
  71. this.layouts[0].moveCell.apply(this.layouts[0], arguments);
  72. }
  73. };
  74. /**
  75. * Function: execute
  76. *
  77. * Implements <mxGraphLayout.execute> by executing all <layouts> in a
  78. * single transaction.
  79. */
  80. mxCompositeLayout.prototype.execute = function(parent)
  81. {
  82. var model = this.graph.getModel();
  83. model.beginUpdate();
  84. try
  85. {
  86. for (var i = 0; i < this.layouts.length; i++)
  87. {
  88. this.layouts[i].execute.apply(this.layouts[i], arguments);
  89. }
  90. }
  91. finally
  92. {
  93. model.endUpdate();
  94. }
  95. };
  96. __mxOutput.mxCompositeLayout = typeof mxCompositeLayout !== 'undefined' ? mxCompositeLayout : undefined;