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.

240 lines
6.6 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. var Item = require('./Item');
  2. var util = require('../../../util');
  3. /**
  4. * @constructor BoxItem
  5. * @extends Item
  6. * @param {Object} data Object containing parameters start
  7. * content, className.
  8. * @param {{toScreen: function, toTime: function}} conversion
  9. * Conversion functions from time to screen and vice versa
  10. * @param {Object} [options] Configuration options
  11. * // TODO: describe available options
  12. */
  13. function BoxItem (data, conversion, options) {
  14. this.props = {
  15. dot: {
  16. width: 0,
  17. height: 0
  18. },
  19. line: {
  20. width: 0,
  21. height: 0
  22. }
  23. };
  24. // validate data
  25. if (data) {
  26. if (data.start == undefined) {
  27. throw new Error('Property "start" missing in item ' + data);
  28. }
  29. }
  30. Item.call(this, data, conversion, options);
  31. }
  32. BoxItem.prototype = new Item (null, null, null);
  33. /**
  34. * Check whether this item is visible inside given range
  35. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  36. * @returns {boolean} True if visible
  37. */
  38. BoxItem.prototype.isVisible = function(range) {
  39. // determine visibility
  40. // TODO: account for the real width of the item. Right now we just add 1/4 to the window
  41. var interval = (range.end - range.start) / 4;
  42. return (this.data.start > range.start - interval) && (this.data.start < range.end + interval);
  43. };
  44. /**
  45. * Repaint the item
  46. */
  47. BoxItem.prototype.redraw = function() {
  48. var dom = this.dom;
  49. if (!dom) {
  50. // create DOM
  51. this.dom = {};
  52. dom = this.dom;
  53. // create main box
  54. dom.box = document.createElement('DIV');
  55. // contents box (inside the background box). used for making margins
  56. dom.content = document.createElement('DIV');
  57. dom.content.className = 'vis-item-content';
  58. dom.box.appendChild(dom.content);
  59. // line to axis
  60. dom.line = document.createElement('DIV');
  61. dom.line.className = 'vis-line';
  62. // dot on axis
  63. dom.dot = document.createElement('DIV');
  64. dom.dot.className = 'vis-dot';
  65. // attach this item as attribute
  66. dom.box['timeline-item'] = this;
  67. this.dirty = true;
  68. }
  69. // append DOM to parent DOM
  70. if (!this.parent) {
  71. throw new Error('Cannot redraw item: no parent attached');
  72. }
  73. if (!dom.box.parentNode) {
  74. var foreground = this.parent.dom.foreground;
  75. if (!foreground) throw new Error('Cannot redraw item: parent has no foreground container element');
  76. foreground.appendChild(dom.box);
  77. }
  78. if (!dom.line.parentNode) {
  79. var background = this.parent.dom.background;
  80. if (!background) throw new Error('Cannot redraw item: parent has no background container element');
  81. background.appendChild(dom.line);
  82. }
  83. if (!dom.dot.parentNode) {
  84. var axis = this.parent.dom.axis;
  85. if (!background) throw new Error('Cannot redraw item: parent has no axis container element');
  86. axis.appendChild(dom.dot);
  87. }
  88. this.displayed = true;
  89. // Update DOM when item is marked dirty. An item is marked dirty when:
  90. // - the item is not yet rendered
  91. // - the item's data is changed
  92. // - the item is selected/deselected
  93. if (this.dirty) {
  94. this._updateContents(this.dom.content);
  95. this._updateTitle(this.dom.box);
  96. this._updateDataAttributes(this.dom.box);
  97. this._updateStyle(this.dom.box);
  98. var editable = (this.options.editable.updateTime ||
  99. this.options.editable.updateGroup ||
  100. this.editable === true) &&
  101. this.editable !== false;
  102. // update class
  103. var className = (this.data.className? ' ' + this.data.className : '') +
  104. (this.selected ? ' vis-selected' : '') +
  105. (editable ? ' vis-editable' : ' vis-readonly');
  106. dom.box.className = 'vis-item vis-box' + className;
  107. dom.line.className = 'vis-item vis-line' + className;
  108. dom.dot.className = 'vis-item vis-dot' + className;
  109. // recalculate size
  110. this.props.dot.height = dom.dot.offsetHeight;
  111. this.props.dot.width = dom.dot.offsetWidth;
  112. this.props.line.width = dom.line.offsetWidth;
  113. this.width = dom.box.offsetWidth;
  114. this.height = dom.box.offsetHeight;
  115. this.dirty = false;
  116. }
  117. this._repaintDeleteButton(dom.box);
  118. };
  119. /**
  120. * Show the item in the DOM (when not already displayed). The items DOM will
  121. * be created when needed.
  122. */
  123. BoxItem.prototype.show = function() {
  124. if (!this.displayed) {
  125. this.redraw();
  126. }
  127. };
  128. /**
  129. * Hide the item from the DOM (when visible)
  130. */
  131. BoxItem.prototype.hide = function() {
  132. if (this.displayed) {
  133. var dom = this.dom;
  134. if (dom.box.parentNode) dom.box.parentNode.removeChild(dom.box);
  135. if (dom.line.parentNode) dom.line.parentNode.removeChild(dom.line);
  136. if (dom.dot.parentNode) dom.dot.parentNode.removeChild(dom.dot);
  137. this.displayed = false;
  138. }
  139. };
  140. /**
  141. * Reposition the item horizontally
  142. * @Override
  143. */
  144. BoxItem.prototype.repositionX = function() {
  145. var start = this.conversion.toScreen(this.data.start);
  146. var align = this.options.align;
  147. var left;
  148. // calculate left position of the box
  149. if (align == 'right') {
  150. this.left = start - this.width;
  151. }
  152. else if (align == 'left') {
  153. this.left = start;
  154. }
  155. else {
  156. // default or 'center'
  157. this.left = start - this.width / 2;
  158. }
  159. // reposition box
  160. this.dom.box.style.left = this.left + 'px';
  161. // reposition line
  162. this.dom.line.style.left = (start - this.props.line.width / 2) + 'px';
  163. // reposition dot
  164. this.dom.dot.style.left = (start - this.props.dot.width / 2) + 'px';
  165. };
  166. /**
  167. * Reposition the item vertically
  168. * @Override
  169. */
  170. BoxItem.prototype.repositionY = function() {
  171. var orientation = this.options.orientation.item;
  172. var box = this.dom.box;
  173. var line = this.dom.line;
  174. var dot = this.dom.dot;
  175. if (orientation == 'top') {
  176. box.style.top = (this.top || 0) + 'px';
  177. line.style.top = '0';
  178. line.style.height = (this.parent.top + this.top + 1) + 'px';
  179. line.style.bottom = '';
  180. }
  181. else { // orientation 'bottom'
  182. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  183. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  184. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  185. line.style.top = (itemSetHeight - lineHeight) + 'px';
  186. line.style.bottom = '0';
  187. }
  188. dot.style.top = (-this.props.dot.height / 2) + 'px';
  189. };
  190. /**
  191. * Return the width of the item left from its start date
  192. * @return {number}
  193. */
  194. BoxItem.prototype.getWidthLeft = function () {
  195. return this.width / 2;
  196. };
  197. /**
  198. * Return the width of the item right from its start date
  199. * @return {number}
  200. */
  201. BoxItem.prototype.getWidthRight = function () {
  202. return this.width / 2;
  203. };
  204. module.exports = BoxItem;