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.

300 lines
8.9 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. * @param {{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. var isVisible;
  42. var align = this.options.align;
  43. var widthInMs = this.width * range.getMillisecondsPerPixel();
  44. if (align == 'right') {
  45. isVisible = (this.data.start.getTime() > range.start ) && (this.data.start.getTime() - widthInMs < range.end);
  46. }
  47. else if (align == 'left') {
  48. isVisible = (this.data.start.getTime() + widthInMs > range.start ) && (this.data.start.getTime() < range.end);
  49. }
  50. else {
  51. // default or 'center'
  52. isVisible = (this.data.start.getTime() + widthInMs/2 > range.start ) && (this.data.start.getTime() - widthInMs/2 < range.end);
  53. }
  54. return isVisible;
  55. };
  56. /**
  57. * Repaint the item
  58. */
  59. BoxItem.prototype.redraw = function() {
  60. var dom = this.dom;
  61. if (!dom) {
  62. // create DOM
  63. this.dom = {};
  64. dom = this.dom;
  65. // create main box
  66. dom.box = document.createElement('DIV');
  67. // contents box (inside the background box). used for making margins
  68. dom.content = document.createElement('DIV');
  69. dom.content.className = 'vis-item-content';
  70. dom.box.appendChild(dom.content);
  71. // line to axis
  72. dom.line = document.createElement('DIV');
  73. dom.line.className = 'vis-line';
  74. // dot on axis
  75. dom.dot = document.createElement('DIV');
  76. dom.dot.className = 'vis-dot';
  77. // attach this item as attribute
  78. dom.box['timeline-item'] = this;
  79. this.dirty = true;
  80. }
  81. // append DOM to parent DOM
  82. if (!this.parent) {
  83. throw new Error('Cannot redraw item: no parent attached');
  84. }
  85. if (!dom.box.parentNode) {
  86. var foreground = this.parent.dom.foreground;
  87. if (!foreground) throw new Error('Cannot redraw item: parent has no foreground container element');
  88. foreground.appendChild(dom.box);
  89. }
  90. if (!dom.line.parentNode) {
  91. var background = this.parent.dom.background;
  92. if (!background) throw new Error('Cannot redraw item: parent has no background container element');
  93. background.appendChild(dom.line);
  94. }
  95. if (!dom.dot.parentNode) {
  96. var axis = this.parent.dom.axis;
  97. if (!background) throw new Error('Cannot redraw item: parent has no axis container element');
  98. axis.appendChild(dom.dot);
  99. }
  100. this.displayed = true;
  101. // Update DOM when item is marked dirty. An item is marked dirty when:
  102. // - the item is not yet rendered
  103. // - the item's data is changed
  104. // - the item is selected/deselected
  105. if (this.dirty) {
  106. this._updateContents(this.dom.content);
  107. this._updateDataAttributes(this.dom.box);
  108. this._updateStyle(this.dom.box);
  109. var editable = (this.editable.updateTime || this.editable.updateGroup);
  110. // update class
  111. var className = (this.data.className? ' ' + this.data.className : '') +
  112. (this.selected ? ' vis-selected' : '') +
  113. (editable ? ' vis-editable' : ' vis-readonly');
  114. dom.box.className = 'vis-item vis-box' + className;
  115. dom.line.className = 'vis-item vis-line' + className;
  116. dom.dot.className = 'vis-item vis-dot' + className;
  117. // set initial position in the visible range of the grid so that the
  118. // rendered box size can be determinated correctly, even the content
  119. // has a dynamic width (fixes #2032).
  120. var previousRight = dom.box.style.right;
  121. var previousLeft = dom.box.style.left;
  122. if (this.options.rtl) {
  123. dom.box.style.right = "0px";
  124. } else {
  125. dom.box.style.left = "0px";
  126. }
  127. // recalculate size
  128. this.props.dot.height = dom.dot.offsetHeight;
  129. this.props.dot.width = dom.dot.offsetWidth;
  130. this.props.line.width = dom.line.offsetWidth;
  131. this.width = dom.box.offsetWidth;
  132. this.height = dom.box.offsetHeight;
  133. // restore previous position
  134. if (this.options.rtl) {
  135. dom.box.style.right = previousRight;
  136. } else {
  137. dom.box.style.left = previousLeft;
  138. }
  139. this.dirty = false;
  140. }
  141. this._repaintOnItemUpdateTimeTooltip(dom.box);
  142. this._repaintDragCenter();
  143. this._repaintDeleteButton(dom.box);
  144. };
  145. /**
  146. * Show the item in the DOM (when not already displayed). The items DOM will
  147. * be created when needed.
  148. */
  149. BoxItem.prototype.show = function() {
  150. if (!this.displayed) {
  151. this.redraw();
  152. }
  153. };
  154. /**
  155. * Hide the item from the DOM (when visible)
  156. */
  157. BoxItem.prototype.hide = function() {
  158. if (this.displayed) {
  159. var dom = this.dom;
  160. if (dom.box.parentNode) dom.box.parentNode.removeChild(dom.box);
  161. if (dom.line.parentNode) dom.line.parentNode.removeChild(dom.line);
  162. if (dom.dot.parentNode) dom.dot.parentNode.removeChild(dom.dot);
  163. this.displayed = false;
  164. }
  165. };
  166. /**
  167. * Reposition the item horizontally
  168. * @Override
  169. */
  170. BoxItem.prototype.repositionX = function() {
  171. var start = this.conversion.toScreen(this.data.start);
  172. var align = this.options.align;
  173. // calculate left position of the box
  174. if (align == 'right') {
  175. if (this.options.rtl) {
  176. this.right = start - this.width;
  177. // reposition box, line, and dot
  178. this.dom.box.style.right = this.right + 'px';
  179. this.dom.line.style.right = (start - this.props.line.width) + 'px';
  180. this.dom.dot.style.right = (start - this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  181. } else {
  182. this.left = start - this.width;
  183. // reposition box, line, and dot
  184. this.dom.box.style.left = this.left + 'px';
  185. this.dom.line.style.left = (start - this.props.line.width) + 'px';
  186. this.dom.dot.style.left = (start - this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  187. }
  188. }
  189. else if (align == 'left') {
  190. if (this.options.rtl) {
  191. this.right = start;
  192. // reposition box, line, and dot
  193. this.dom.box.style.right = this.right + 'px';
  194. this.dom.line.style.right = start + 'px';
  195. this.dom.dot.style.right = (start + this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  196. } else {
  197. this.left = start;
  198. // reposition box, line, and dot
  199. this.dom.box.style.left = this.left + 'px';
  200. this.dom.line.style.left = start + 'px';
  201. this.dom.dot.style.left = (start + this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  202. }
  203. }
  204. else {
  205. // default or 'center'
  206. if (this.options.rtl) {
  207. this.right = start - this.width / 2;
  208. // reposition box, line, and dot
  209. this.dom.box.style.right = this.right + 'px';
  210. this.dom.line.style.right = (start - this.props.line.width) + 'px';
  211. this.dom.dot.style.right = (start - this.props.dot.width / 2) + 'px';
  212. } else {
  213. this.left = start - this.width / 2;
  214. // reposition box, line, and dot
  215. this.dom.box.style.left = this.left + 'px';
  216. this.dom.line.style.left = (start - this.props.line.width / 2) + 'px';
  217. this.dom.dot.style.left = (start - this.props.dot.width / 2) + 'px';
  218. }
  219. }
  220. };
  221. /**
  222. * Reposition the item vertically
  223. * @Override
  224. */
  225. BoxItem.prototype.repositionY = function() {
  226. var orientation = this.options.orientation.item;
  227. var box = this.dom.box;
  228. var line = this.dom.line;
  229. var dot = this.dom.dot;
  230. if (orientation == 'top') {
  231. box.style.top = (this.top || 0) + 'px';
  232. line.style.top = '0';
  233. line.style.height = (this.parent.top + this.top + 1) + 'px';
  234. line.style.bottom = '';
  235. }
  236. else { // orientation 'bottom'
  237. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  238. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  239. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  240. line.style.top = (itemSetHeight - lineHeight) + 'px';
  241. line.style.bottom = '0';
  242. }
  243. dot.style.top = (-this.props.dot.height / 2) + 'px';
  244. };
  245. /**
  246. * Return the width of the item left from its start date
  247. * @return {number}
  248. */
  249. BoxItem.prototype.getWidthLeft = function () {
  250. return this.width / 2;
  251. };
  252. /**
  253. * Return the width of the item right from its start date
  254. * @return {number}
  255. */
  256. BoxItem.prototype.getWidthRight = function () {
  257. return this.width / 2;
  258. };
  259. module.exports = BoxItem;