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.

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