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.

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