mxRectangleShape.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxRectangleShape
  7. *
  8. * Extends <mxShape> to implement a rectangle shape.
  9. * This shape is registered under <mxConstants.SHAPE_RECTANGLE>
  10. * in <mxCellRenderer>.
  11. *
  12. * Constructor: mxRectangleShape
  13. *
  14. * Constructs a new rectangle shape.
  15. *
  16. * Parameters:
  17. *
  18. * bounds - <mxRectangle> that defines the bounds. This is stored in
  19. * <mxShape.bounds>.
  20. * fill - String that defines the fill color. This is stored in <fill>.
  21. * stroke - String that defines the stroke color. This is stored in <stroke>.
  22. * strokewidth - Optional integer that defines the stroke width. Default is
  23. * 1. This is stored in <strokewidth>.
  24. */
  25. function mxRectangleShape(bounds, fill, stroke, strokewidth)
  26. {
  27. mxShape.call(this);
  28. this.bounds = bounds;
  29. this.fill = fill;
  30. this.stroke = stroke;
  31. this.strokewidth = (strokewidth != null) ? strokewidth : 1;
  32. };
  33. /**
  34. * Extends mxShape.
  35. */
  36. mxUtils.extend(mxRectangleShape, mxShape);
  37. /**
  38. * Function: isHtmlAllowed
  39. *
  40. * Returns true for non-rounded, non-rotated shapes with no glass gradient.
  41. */
  42. mxRectangleShape.prototype.isHtmlAllowed = function()
  43. {
  44. var events = true;
  45. if (this.style != null)
  46. {
  47. events = mxUtils.getValue(this.style, mxConstants.STYLE_POINTER_EVENTS, '1') == '1';
  48. }
  49. return !this.isRounded && !this.glass && this.rotation == 0 && (events ||
  50. (this.fill != null && this.fill != mxConstants.NONE));
  51. };
  52. /**
  53. * Function: paintBackground
  54. *
  55. * Generic background painting implementation.
  56. */
  57. mxRectangleShape.prototype.paintBackground = function(c, x, y, w, h)
  58. {
  59. var events = true;
  60. if (this.style != null)
  61. {
  62. events = mxUtils.getValue(this.style, mxConstants.STYLE_POINTER_EVENTS, '1') == '1';
  63. }
  64. if (events || (this.fill != null && this.fill != mxConstants.NONE) ||
  65. (this.stroke != null && this.stroke != mxConstants.NONE))
  66. {
  67. if (!events && (this.fill == null || this.fill == mxConstants.NONE))
  68. {
  69. c.pointerEvents = false;
  70. }
  71. if (this.isRounded)
  72. {
  73. var r = 0;
  74. if (mxUtils.getValue(this.style, mxConstants.STYLE_ABSOLUTE_ARCSIZE, 0) == '1')
  75. {
  76. r = Math.min(w / 2, Math.min(h / 2, mxUtils.getValue(this.style,
  77. mxConstants.STYLE_ARCSIZE, mxConstants.LINE_ARCSIZE) / 2));
  78. }
  79. else
  80. {
  81. var f = mxUtils.getValue(this.style, mxConstants.STYLE_ARCSIZE,
  82. mxConstants.RECTANGLE_ROUNDING_FACTOR * 100) / 100;
  83. r = Math.min(w * f, h * f);
  84. }
  85. c.roundrect(x, y, w, h, r, r);
  86. }
  87. else
  88. {
  89. c.rect(x, y, w, h);
  90. }
  91. c.fillAndStroke();
  92. }
  93. };
  94. /**
  95. * Function: isRoundable
  96. *
  97. * Adds roundable support.
  98. */
  99. mxRectangleShape.prototype.isRoundable = function(c, x, y, w, h)
  100. {
  101. return true;
  102. };
  103. /**
  104. * Function: paintForeground
  105. *
  106. * Generic background painting implementation.
  107. */
  108. mxRectangleShape.prototype.paintForeground = function(c, x, y, w, h)
  109. {
  110. if (this.glass && !this.outline && this.fill != null && this.fill != mxConstants.NONE)
  111. {
  112. this.paintGlassEffect(c, x, y, w, h, this.getArcSize(w + this.strokewidth, h + this.strokewidth));
  113. }
  114. };
  115. __mxOutput.mxRectangleShape = typeof mxRectangleShape !== 'undefined' ? mxRectangleShape : undefined;