mxTemporaryCellStates.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2006-2017, JGraph Ltd
  3. * Copyright (c) 2006-2017, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxTemporaryCellStates
  7. *
  8. * Creates a temporary set of cell states.
  9. */
  10. function mxTemporaryCellStates(view, scale, cells, isCellVisibleFn, getLinkForCellState, getLinkTargetForCellState)
  11. {
  12. scale = (scale != null) ? scale : 1;
  13. this.view = view;
  14. // Stores the previous state
  15. this.oldValidateCellState = view.validateCellState;
  16. this.oldBounds = view.getGraphBounds();
  17. this.oldStates = view.getStates();
  18. this.oldScale = view.getScale();
  19. this.oldDoRedrawShape = view.graph.cellRenderer.doRedrawShape;
  20. var self = this;
  21. // Overrides doRedrawShape and paint shape to add links on shapes
  22. if (getLinkForCellState != null)
  23. {
  24. view.graph.cellRenderer.doRedrawShape = function(state)
  25. {
  26. var oldPaint = state.shape.paint;
  27. state.shape.paint = function(c)
  28. {
  29. var link = getLinkForCellState(state);
  30. if (link != null)
  31. {
  32. c.setLink(link, (getLinkTargetForCellState != null) ?
  33. getLinkTargetForCellState(state) : null);
  34. }
  35. oldPaint.apply(this, arguments);
  36. if (link != null)
  37. {
  38. c.setLink(null);
  39. }
  40. };
  41. self.oldDoRedrawShape.apply(view.graph.cellRenderer, arguments);
  42. state.shape.paint = oldPaint;
  43. };
  44. }
  45. // Overrides validateCellState to ignore invisible cells
  46. view.validateCellState = function(cell, resurse)
  47. {
  48. if (cell == null || isCellVisibleFn == null || isCellVisibleFn(cell))
  49. {
  50. return self.oldValidateCellState.apply(view, arguments);
  51. }
  52. return null;
  53. };
  54. // Creates space for new states
  55. view.setStates(new mxDictionary());
  56. view.setScale(scale);
  57. if (cells != null)
  58. {
  59. view.resetValidationState();
  60. var bbox = null;
  61. // Validates the vertices and edges without adding them to
  62. // the model so that the original cells are not modified
  63. for (var i = 0; i < cells.length; i++)
  64. {
  65. var bounds = view.getBoundingBox(view.validateCellState(view.validateCell(cells[i])));
  66. if (bbox == null)
  67. {
  68. bbox = bounds;
  69. }
  70. else
  71. {
  72. bbox.add(bounds);
  73. }
  74. }
  75. view.setGraphBounds(bbox || new mxRectangle());
  76. }
  77. };
  78. /**
  79. * Variable: view
  80. *
  81. * Holds the width of the rectangle. Default is 0.
  82. */
  83. mxTemporaryCellStates.prototype.view = null;
  84. /**
  85. * Variable: oldStates
  86. *
  87. * Holds the height of the rectangle. Default is 0.
  88. */
  89. mxTemporaryCellStates.prototype.oldStates = null;
  90. /**
  91. * Variable: oldBounds
  92. *
  93. * Holds the height of the rectangle. Default is 0.
  94. */
  95. mxTemporaryCellStates.prototype.oldBounds = null;
  96. /**
  97. * Variable: oldScale
  98. *
  99. * Holds the height of the rectangle. Default is 0.
  100. */
  101. mxTemporaryCellStates.prototype.oldScale = null;
  102. /**
  103. * Function: destroy
  104. *
  105. * Returns the top, left corner as a new <mxPoint>.
  106. */
  107. mxTemporaryCellStates.prototype.destroy = function()
  108. {
  109. this.view.setScale(this.oldScale);
  110. this.view.setStates(this.oldStates);
  111. this.view.setGraphBounds(this.oldBounds);
  112. this.view.validateCellState = this.oldValidateCellState;
  113. this.view.graph.cellRenderer.doRedrawShape = this.oldDoRedrawShape;
  114. };