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.

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