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.

290 lines
8.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. 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. // set initial position in the visible range of the grid so that the
  111. // rendered box size can be determinated correctly, even the content
  112. // has a dynamic width (fixes #2032).
  113. var previousRight = dom.box.style.right;
  114. var previousLeft = dom.box.style.left;
  115. if (this.options.rtl) {
  116. dom.box.style.right = "0px";
  117. } else {
  118. dom.box.style.left = "0px";
  119. }
  120. // recalculate size
  121. this.props.dot.height = dom.dot.offsetHeight;
  122. this.props.dot.width = dom.dot.offsetWidth;
  123. this.props.line.width = dom.line.offsetWidth;
  124. this.width = dom.box.offsetWidth;
  125. this.height = dom.box.offsetHeight;
  126. // restore previous position
  127. if (this.options.rtl) {
  128. dom.box.style.right = previousRight;
  129. } else {
  130. dom.box.style.left = previousLeft;
  131. }
  132. this.dirty = false;
  133. }
  134. this._repaintDeleteButton(dom.box);
  135. };
  136. /**
  137. * Show the item in the DOM (when not already displayed). The items DOM will
  138. * be created when needed.
  139. */
  140. BoxItem.prototype.show = function() {
  141. if (!this.displayed) {
  142. this.redraw();
  143. }
  144. };
  145. /**
  146. * Hide the item from the DOM (when visible)
  147. */
  148. BoxItem.prototype.hide = function() {
  149. if (this.displayed) {
  150. var dom = this.dom;
  151. if (dom.box.parentNode) dom.box.parentNode.removeChild(dom.box);
  152. if (dom.line.parentNode) dom.line.parentNode.removeChild(dom.line);
  153. if (dom.dot.parentNode) dom.dot.parentNode.removeChild(dom.dot);
  154. this.displayed = false;
  155. }
  156. };
  157. /**
  158. * Reposition the item horizontally
  159. * @Override
  160. */
  161. BoxItem.prototype.repositionX = function() {
  162. var start = this.conversion.toScreen(this.data.start);
  163. var align = this.options.align;
  164. // calculate left position of the box
  165. if (align == 'right') {
  166. if (this.options.rtl) {
  167. this.right = start - this.width;
  168. // reposition box, line, and dot
  169. this.dom.box.style.right = this.right + 'px';
  170. this.dom.line.style.right = (start - this.props.line.width) + 'px';
  171. this.dom.dot.style.right = (start - this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  172. } else {
  173. this.left = start - this.width;
  174. // reposition box, line, and dot
  175. this.dom.box.style.left = this.left + 'px';
  176. this.dom.line.style.left = (start - this.props.line.width) + 'px';
  177. this.dom.dot.style.left = (start - this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  178. }
  179. }
  180. else if (align == 'left') {
  181. if (this.options.rtl) {
  182. this.right = start;
  183. // reposition box, line, and dot
  184. this.dom.box.style.right = this.right + 'px';
  185. this.dom.line.style.right = start + 'px';
  186. this.dom.dot.style.right = (start + this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  187. } else {
  188. this.left = start;
  189. // reposition box, line, and dot
  190. this.dom.box.style.left = this.left + 'px';
  191. this.dom.line.style.left = start + 'px';
  192. this.dom.dot.style.left = (start + this.props.line.width / 2 - this.props.dot.width / 2) + 'px';
  193. }
  194. }
  195. else {
  196. // default or 'center'
  197. if (this.options.rtl) {
  198. this.right = start - this.width / 2;
  199. // reposition box, line, and dot
  200. this.dom.box.style.right = this.right + 'px';
  201. this.dom.line.style.right = (start - this.props.line.width) + 'px';
  202. this.dom.dot.style.right = (start - this.props.dot.width / 2) + 'px';
  203. } else {
  204. this.left = start - this.width / 2;
  205. // reposition box, line, and dot
  206. this.dom.box.style.left = this.left + 'px';
  207. this.dom.line.style.left = (start - this.props.line.width / 2) + 'px';
  208. this.dom.dot.style.left = (start - this.props.dot.width / 2) + 'px';
  209. }
  210. }
  211. };
  212. /**
  213. * Reposition the item vertically
  214. * @Override
  215. */
  216. BoxItem.prototype.repositionY = function() {
  217. var orientation = this.options.orientation.item;
  218. var box = this.dom.box;
  219. var line = this.dom.line;
  220. var dot = this.dom.dot;
  221. if (orientation == 'top') {
  222. box.style.top = (this.top || 0) + 'px';
  223. line.style.top = '0';
  224. line.style.height = (this.parent.top + this.top + 1) + 'px';
  225. line.style.bottom = '';
  226. }
  227. else { // orientation 'bottom'
  228. var itemSetHeight = this.parent.itemSet.props.height; // TODO: this is nasty
  229. var lineHeight = itemSetHeight - this.parent.top - this.parent.height + this.top;
  230. box.style.top = (this.parent.height - this.top - this.height || 0) + 'px';
  231. line.style.top = (itemSetHeight - lineHeight) + 'px';
  232. line.style.bottom = '0';
  233. }
  234. dot.style.top = (-this.props.dot.height / 2) + 'px';
  235. };
  236. /**
  237. * Return the width of the item left from its start date
  238. * @return {number}
  239. */
  240. BoxItem.prototype.getWidthLeft = function () {
  241. return this.width / 2;
  242. };
  243. /**
  244. * Return the width of the item right from its start date
  245. * @return {number}
  246. */
  247. BoxItem.prototype.getWidthRight = function () {
  248. return this.width / 2;
  249. };
  250. module.exports = BoxItem;