mxImageExport.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxImageExport
  7. *
  8. * Creates a new image export instance to be used with an export canvas. Here
  9. * is an example that uses this class to create an image via a backend using
  10. * <mxXmlExportCanvas>.
  11. *
  12. * (code)
  13. * var xmlDoc = mxUtils.createXmlDocument();
  14. * var root = xmlDoc.createElement('output');
  15. * xmlDoc.appendChild(root);
  16. *
  17. * var xmlCanvas = new mxXmlCanvas2D(root);
  18. * var imgExport = new mxImageExport();
  19. * imgExport.drawState(graph.getView().getState(graph.model.root), xmlCanvas);
  20. *
  21. * var bounds = graph.getGraphBounds();
  22. * var w = Math.ceil(bounds.x + bounds.width);
  23. * var h = Math.ceil(bounds.y + bounds.height);
  24. *
  25. * var xml = mxUtils.getXml(root);
  26. * new mxXmlRequest('export', 'format=png&w=' + w +
  27. * '&h=' + h + '&bg=#F9F7ED&xml=' + encodeURIComponent(xml))
  28. * .simulate(document, '_blank');
  29. * (end)
  30. *
  31. * Constructor: mxImageExport
  32. *
  33. * Constructs a new image export.
  34. */
  35. function mxImageExport() { };
  36. /**
  37. * Variable: includeOverlays
  38. *
  39. * Specifies if overlays should be included in the export. Default is false.
  40. */
  41. mxImageExport.prototype.includeOverlays = false;
  42. /**
  43. * Function: drawState
  44. *
  45. * Draws the given state and all its descendants to the given canvas.
  46. */
  47. mxImageExport.prototype.drawState = function(state, canvas)
  48. {
  49. if (state != null)
  50. {
  51. this.visitStatesRecursive(state, canvas, mxUtils.bind(this, function()
  52. {
  53. this.drawCellState.apply(this, arguments);
  54. }));
  55. // Paints the overlays
  56. if (this.includeOverlays)
  57. {
  58. this.visitStatesRecursive(state, canvas, mxUtils.bind(this, function()
  59. {
  60. this.drawOverlays.apply(this, arguments);
  61. }));
  62. }
  63. }
  64. };
  65. /**
  66. * Function: visitStatesRecursive
  67. *
  68. * Visits the given state and all its descendants to the given canvas recursively.
  69. */
  70. mxImageExport.prototype.visitStatesRecursive = function(state, canvas, visitor)
  71. {
  72. if (state != null)
  73. {
  74. visitor(state, canvas);
  75. var graph = state.view.graph;
  76. var childCount = graph.model.getChildCount(state.cell);
  77. for (var i = 0; i < childCount; i++)
  78. {
  79. var childState = graph.view.getState(graph.model.getChildAt(state.cell, i));
  80. this.visitStatesRecursive(childState, canvas, visitor);
  81. }
  82. }
  83. };
  84. /**
  85. * Function: getLinkForCellState
  86. *
  87. * Returns the link for the given cell state and canvas. This returns null.
  88. */
  89. mxImageExport.prototype.getLinkForCellState = function(state, canvas)
  90. {
  91. return null;
  92. };
  93. /**
  94. * Function: drawCellState
  95. *
  96. * Draws the given state to the given canvas.
  97. */
  98. mxImageExport.prototype.drawCellState = function(state, canvas)
  99. {
  100. // Experimental feature
  101. var link = this.getLinkForCellState(state, canvas);
  102. if (link != null)
  103. {
  104. canvas.setLink(link);
  105. }
  106. // Paints the shape and text
  107. this.drawShape(state, canvas);
  108. this.drawText(state, canvas);
  109. if (link != null)
  110. {
  111. canvas.setLink(null);
  112. }
  113. };
  114. /**
  115. * Function: drawShape
  116. *
  117. * Draws the shape of the given state.
  118. */
  119. mxImageExport.prototype.drawShape = function(state, canvas)
  120. {
  121. if (state.shape instanceof mxShape && state.shape.checkBounds())
  122. {
  123. canvas.save();
  124. state.shape.beforePaint(canvas);
  125. state.shape.paint(canvas);
  126. state.shape.afterPaint(canvas);
  127. canvas.restore();
  128. }
  129. };
  130. /**
  131. * Function: drawText
  132. *
  133. * Draws the text of the given state.
  134. */
  135. mxImageExport.prototype.drawText = function(state, canvas)
  136. {
  137. if (state.text != null && state.text.checkBounds())
  138. {
  139. canvas.save();
  140. state.text.beforePaint(canvas);
  141. state.text.paint(canvas);
  142. state.text.afterPaint(canvas);
  143. canvas.restore();
  144. }
  145. };
  146. /**
  147. * Function: drawOverlays
  148. *
  149. * Draws the overlays for the given state. This is called if <includeOverlays>
  150. * is true.
  151. */
  152. mxImageExport.prototype.drawOverlays = function(state, canvas)
  153. {
  154. if (state.overlays != null)
  155. {
  156. state.overlays.visit(function(id, shape)
  157. {
  158. if (shape instanceof mxShape)
  159. {
  160. shape.paint(canvas);
  161. }
  162. });
  163. }
  164. };