mxTemporaryCellStates.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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)
  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);
  33. }
  34. oldPaint.apply(this, arguments);
  35. if (link != null)
  36. {
  37. c.setLink(null);
  38. }
  39. };
  40. self.oldDoRedrawShape.apply(view.graph.cellRenderer, arguments);
  41. state.shape.paint = oldPaint;
  42. };
  43. }
  44. // Overrides validateCellState to ignore invisible cells
  45. view.validateCellState = function(cell, resurse)
  46. {
  47. if (cell == null || isCellVisibleFn == null || isCellVisibleFn(cell))
  48. {
  49. return self.oldValidateCellState.apply(view, arguments);
  50. }
  51. return null;
  52. };
  53. // Creates space for new states
  54. view.setStates(new mxDictionary());
  55. view.setScale(scale);
  56. if (cells != null)
  57. {
  58. view.resetValidationState();
  59. var bbox = null;
  60. // Validates the vertices and edges without adding them to
  61. // the model so that the original cells are not modified
  62. for (var i = 0; i < cells.length; i++)
  63. {
  64. var bounds = view.getBoundingBox(view.validateCellState(view.validateCell(cells[i])));
  65. if (bbox == null)
  66. {
  67. bbox = bounds;
  68. }
  69. else
  70. {
  71. bbox.add(bounds);
  72. }
  73. }
  74. view.setGraphBounds(bbox || new mxRectangle());
  75. }
  76. };
  77. /**
  78. * Variable: view
  79. *
  80. * Holds the width of the rectangle. Default is 0.
  81. */
  82. mxTemporaryCellStates.prototype.view = null;
  83. /**
  84. * Variable: oldStates
  85. *
  86. * Holds the height of the rectangle. Default is 0.
  87. */
  88. mxTemporaryCellStates.prototype.oldStates = null;
  89. /**
  90. * Variable: oldBounds
  91. *
  92. * Holds the height of the rectangle. Default is 0.
  93. */
  94. mxTemporaryCellStates.prototype.oldBounds = null;
  95. /**
  96. * Variable: oldScale
  97. *
  98. * Holds the height of the rectangle. Default is 0.
  99. */
  100. mxTemporaryCellStates.prototype.oldScale = null;
  101. /**
  102. * Function: destroy
  103. *
  104. * Returns the top, left corner as a new <mxPoint>.
  105. */
  106. mxTemporaryCellStates.prototype.destroy = function()
  107. {
  108. this.view.setScale(this.oldScale);
  109. this.view.setStates(this.oldStates);
  110. this.view.setGraphBounds(this.oldBounds);
  111. this.view.validateCellState = this.oldValidateCellState;
  112. this.view.graph.cellRenderer.doRedrawShape = this.oldDoRedrawShape;
  113. };