vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.6 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /**
  2. * A panel can contain components
  3. * @param {Object} [options] Available parameters:
  4. * {String | Number | function} [left]
  5. * {String | Number | function} [top]
  6. * {String | Number | function} [width]
  7. * {String | Number | function} [height]
  8. * {String | function} [className]
  9. * @constructor Panel
  10. * @extends Component
  11. */
  12. function Panel(options) {
  13. this.id = util.randomUUID();
  14. this.parent = null;
  15. this.childs = [];
  16. this.options = options || {};
  17. // create frame
  18. this.frame = (typeof document !== 'undefined') ? document.createElement('div') : null;
  19. }
  20. Panel.prototype = new Component();
  21. /**
  22. * Set options. Will extend the current options.
  23. * @param {Object} [options] Available parameters:
  24. * {String | function} [className]
  25. * {String | Number | function} [left]
  26. * {String | Number | function} [top]
  27. * {String | Number | function} [width]
  28. * {String | Number | function} [height]
  29. */
  30. Panel.prototype.setOptions = Component.prototype.setOptions;
  31. /**
  32. * Get the outer frame of the panel
  33. * @returns {HTMLElement} frame
  34. */
  35. Panel.prototype.getFrame = function () {
  36. return this.frame;
  37. };
  38. /**
  39. * Append a child to the panel
  40. * @param {Component} child
  41. */
  42. Panel.prototype.appendChild = function (child) {
  43. this.childs.push(child);
  44. child.parent = this;
  45. // attach to the DOM
  46. var frame = child.getFrame();
  47. if (frame) {
  48. if (frame.parentNode) {
  49. frame.parentNode.removeChild(frame);
  50. }
  51. this.frame.appendChild(frame);
  52. }
  53. };
  54. /**
  55. * Insert a child to the panel
  56. * @param {Component} child
  57. * @param {Component} beforeChild
  58. */
  59. Panel.prototype.insertBefore = function (child, beforeChild) {
  60. var index = this.childs.indexOf(beforeChild);
  61. if (index != -1) {
  62. this.childs.splice(index, 0, child);
  63. child.parent = this;
  64. // attach to the DOM
  65. var frame = child.getFrame();
  66. if (frame) {
  67. if (frame.parentNode) {
  68. frame.parentNode.removeChild(frame);
  69. }
  70. var beforeFrame = beforeChild.getFrame();
  71. if (beforeFrame) {
  72. this.frame.insertBefore(frame, beforeFrame);
  73. }
  74. else {
  75. this.frame.appendChild(frame);
  76. }
  77. }
  78. }
  79. };
  80. /**
  81. * Remove a child from the panel
  82. * @param {Component} child
  83. */
  84. Panel.prototype.removeChild = function (child) {
  85. var index = this.childs.indexOf(child);
  86. if (index != -1) {
  87. this.childs.splice(index, 1);
  88. child.parent = null;
  89. // remove from the DOM
  90. var frame = child.getFrame();
  91. if (frame && frame.parentNode) {
  92. this.frame.removeChild(frame);
  93. }
  94. }
  95. };
  96. /**
  97. * Test whether the panel contains given child
  98. * @param {Component} child
  99. */
  100. Panel.prototype.hasChild = function (child) {
  101. var index = this.childs.indexOf(child);
  102. return (index != -1);
  103. };
  104. /**
  105. * Repaint the component
  106. * @return {boolean} Returns true if the component was resized since previous repaint
  107. */
  108. Panel.prototype.repaint = function () {
  109. var asString = util.option.asString,
  110. options = this.options,
  111. frame = this.getFrame();
  112. // update className
  113. frame.className = 'vpanel' + (options.className ? (' ' + asString(options.className)) : '');
  114. // repaint the child components
  115. var childsResized = this._repaintChilds();
  116. // update frame size
  117. this._updateSize();
  118. return this._isResized() || childsResized;
  119. };
  120. /**
  121. * Repaint all childs of the panel
  122. * @return {boolean} Returns true if the component is resized
  123. * @private
  124. */
  125. Panel.prototype._repaintChilds = function () {
  126. var resized = false;
  127. for (var i = 0, ii = this.childs.length; i < ii; i++) {
  128. resized = this.childs[i].repaint() || resized;
  129. }
  130. return resized;
  131. };
  132. /**
  133. * Apply the size from options to the panel, and recalculate it's actual size.
  134. * @private
  135. */
  136. Panel.prototype._updateSize = function () {
  137. // apply size
  138. this.frame.style.top = util.option.asSize(this.options.top);
  139. this.frame.style.bottom = util.option.asSize(this.options.bottom);
  140. this.frame.style.left = util.option.asSize(this.options.left);
  141. this.frame.style.right = util.option.asSize(this.options.right);
  142. this.frame.style.width = util.option.asSize(this.options.width, '100%');
  143. this.frame.style.height = util.option.asSize(this.options.height, '');
  144. // get actual size
  145. this.top = this.frame.offsetTop;
  146. this.left = this.frame.offsetLeft;
  147. this.width = this.frame.offsetWidth;
  148. this.height = this.frame.offsetHeight;
  149. };