mxDivResizer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Copyright (c) 2006-2015, JGraph Ltd
  3. * Copyright (c) 2006-2015, Gaudenz Alder
  4. */
  5. /**
  6. * Class: mxDivResizer
  7. *
  8. * Maintains the size of a div element in Internet Explorer. This is a
  9. * workaround for the right and bottom style being ignored in IE.
  10. *
  11. * If you need a div to cover the scrollwidth and -height of a document,
  12. * then you can use this class as follows:
  13. *
  14. * (code)
  15. * var resizer = new mxDivResizer(background);
  16. * resizer.getDocumentHeight = function()
  17. * {
  18. * return document.body.scrollHeight;
  19. * }
  20. * resizer.getDocumentWidth = function()
  21. * {
  22. * return document.body.scrollWidth;
  23. * }
  24. * resizer.resize();
  25. * (end)
  26. *
  27. * Constructor: mxDivResizer
  28. *
  29. * Constructs an object that maintains the size of a div
  30. * element when the window is being resized. This is only
  31. * required for Internet Explorer as it ignores the respective
  32. * stylesheet information for DIV elements.
  33. *
  34. * Parameters:
  35. *
  36. * div - Reference to the DOM node whose size should be maintained.
  37. * container - Optional Container that contains the div. Default is the
  38. * window.
  39. */
  40. function mxDivResizer(div, container)
  41. {
  42. if (div.nodeName.toLowerCase() == 'div')
  43. {
  44. if (container == null)
  45. {
  46. container = window;
  47. }
  48. this.div = div;
  49. var style = mxUtils.getCurrentStyle(div);
  50. if (style != null)
  51. {
  52. this.resizeWidth = style.width == 'auto';
  53. this.resizeHeight = style.height == 'auto';
  54. }
  55. mxEvent.addListener(container, 'resize',
  56. mxUtils.bind(this, function(evt)
  57. {
  58. if (!this.handlingResize)
  59. {
  60. this.handlingResize = true;
  61. this.resize();
  62. this.handlingResize = false;
  63. }
  64. })
  65. );
  66. this.resize();
  67. }
  68. };
  69. /**
  70. * Function: resizeWidth
  71. *
  72. * Boolean specifying if the width should be updated.
  73. */
  74. mxDivResizer.prototype.resizeWidth = true;
  75. /**
  76. * Function: resizeHeight
  77. *
  78. * Boolean specifying if the height should be updated.
  79. */
  80. mxDivResizer.prototype.resizeHeight = true;
  81. /**
  82. * Function: handlingResize
  83. *
  84. * Boolean specifying if the width should be updated.
  85. */
  86. mxDivResizer.prototype.handlingResize = false;
  87. /**
  88. * Function: resize
  89. *
  90. * Updates the style of the DIV after the window has been resized.
  91. */
  92. mxDivResizer.prototype.resize = function()
  93. {
  94. var w = this.getDocumentWidth();
  95. var h = this.getDocumentHeight();
  96. var l = parseInt(this.div.style.left);
  97. var r = parseInt(this.div.style.right);
  98. var t = parseInt(this.div.style.top);
  99. var b = parseInt(this.div.style.bottom);
  100. if (this.resizeWidth &&
  101. !isNaN(l) &&
  102. !isNaN(r) &&
  103. l >= 0 &&
  104. r >= 0 &&
  105. w - r - l > 0)
  106. {
  107. this.div.style.width = (w - r - l)+'px';
  108. }
  109. if (this.resizeHeight &&
  110. !isNaN(t) &&
  111. !isNaN(b) &&
  112. t >= 0 &&
  113. b >= 0 &&
  114. h - t - b > 0)
  115. {
  116. this.div.style.height = (h - t - b)+'px';
  117. }
  118. };
  119. /**
  120. * Function: getDocumentWidth
  121. *
  122. * Hook for subclassers to return the width of the document (without
  123. * scrollbars).
  124. */
  125. mxDivResizer.prototype.getDocumentWidth = function()
  126. {
  127. return document.body.clientWidth;
  128. };
  129. /**
  130. * Function: getDocumentHeight
  131. *
  132. * Hook for subclassers to return the height of the document (without
  133. * scrollbars).
  134. */
  135. mxDivResizer.prototype.getDocumentHeight = function()
  136. {
  137. return document.body.clientHeight;
  138. };
  139. __mxOutput.mxDivResizer = typeof mxDivResizer !== 'undefined' ? mxDivResizer : undefined;