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.

245 lines
7.1 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. // calculate left position of the box
  148. if (align == 'right') {
  149. this.left = start - this.width;
  150. // reposition box, line, and dot
  151. this.dom.box.style.left = this.left + 'px';
  152. this.dom.line.style.left = (start - this.props.line.width) + 'px';
  153. this.dom.dot.style.left = (start - this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  154. }
  155. else if (align == 'left') {
  156. this.left = start;
  157. // reposition box, line, and dot
  158. this.dom.box.style.left = this.left + 'px';
  159. this.dom.line.style.left = start + 'px';
  160. this.dom.dot.style.left = (start + this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  161. }
  162. else {
  163. // default or 'center'
  164. this.left = start - this.width / 2;
  165. // reposition box, line, and dot
  166. this.dom.box.style.left = this.left + 'px';
  167. this.dom.line.style.left = (start - this.props.line.width / 2) + 'px';
  168. this.dom.dot.style.left = (start - this.props.dot.width / 2) + 'px';
  169. }
  170. };
  171. /**
  172. * Reposition the item vertically
  173. * @Override
  174. */
  175. BoxItem.prototype.repositionY = function() {
  176. var orientation = this.options.orientation.item;
  177. var box = this.dom.box;
  178. var line = this.dom.line;
  179. var dot = this.dom.dot;
  180. if (orientation == 'top') {
  181. box.style.top = (this.top || 0) + 'px';
  182. line.style.top = '0';
  183. line.style.height = (this.parent.top + this.top + 1) + 'px';
  184. line.style.bottom = '';
  185. }
  186. else { // orientation 'bottom'
  187. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  188. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  189. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  190. line.style.top = (itemSetHeight - lineHeight) + 'px';
  191. line.style.bottom = '0';
  192. }
  193. dot.style.top = (-this.props.dot.height / 2) + 'px';
  194. };
  195. /**
  196. * Return the width of the item left from its start date
  197. * @return {number}
  198. */
  199. BoxItem.prototype.getWidthLeft = function () {
  200. return this.width / 2;
  201. };
  202. /**
  203. * Return the width of the item right from its start date
  204. * @return {number}
  205. */
  206. BoxItem.prototype.getWidthRight = function () {
  207. return this.width / 2;
  208. };
  209. module.exports = BoxItem;