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.

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