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.1 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. var isVisible;
  42. var align = this.options.align;
  43. var msPerPixel = (range.end - range.start) / range.body.dom.center.clientWidth;
  44. var widthInMs = this.width * msPerPixel;
  45. if (align == 'right') {
  46. isVisible = (this.data.start.getTime() > range.start ) && (this.data.start.getTime() - widthInMs < range.end);
  47. }
  48. else if (align == 'left') {
  49. isVisible = (this.data.start.getTime() + widthInMs > range.start ) && (this.data.start.getTime() < range.end);
  50. }
  51. else {
  52. // default or 'center'
  53. isVisible = (this.data.start.getTime() + widthInMs/2 > range.start ) && (this.data.start.getTime() - widthInMs/2 < range.end);
  54. }
  55. return isVisible;
  56. };
  57. /**
  58. * Repaint the item
  59. */
  60. BoxItem.prototype.redraw = function() {
  61. var dom = this.dom;
  62. if (!dom) {
  63. // create DOM
  64. this.dom = {};
  65. dom = this.dom;
  66. // create main box
  67. dom.box = document.createElement('DIV');
  68. // contents box (inside the background box). used for making margins
  69. dom.content = document.createElement('DIV');
  70. dom.content.className = 'vis-item-content';
  71. dom.box.appendChild(dom.content);
  72. // line to axis
  73. dom.line = document.createElement('DIV');
  74. dom.line.className = 'vis-line';
  75. // dot on axis
  76. dom.dot = document.createElement('DIV');
  77. dom.dot.className = 'vis-dot';
  78. // attach this item as attribute
  79. dom.box['timeline-item'] = this;
  80. this.dirty = true;
  81. }
  82. // append DOM to parent DOM
  83. if (!this.parent) {
  84. throw new Error('Cannot redraw item: no parent attached');
  85. }
  86. if (!dom.box.parentNode) {
  87. var foreground = this.parent.dom.foreground;
  88. if (!foreground) throw new Error('Cannot redraw item: parent has no foreground container element');
  89. foreground.appendChild(dom.box);
  90. }
  91. if (!dom.line.parentNode) {
  92. var background = this.parent.dom.background;
  93. if (!background) throw new Error('Cannot redraw item: parent has no background container element');
  94. background.appendChild(dom.line);
  95. }
  96. if (!dom.dot.parentNode) {
  97. var axis = this.parent.dom.axis;
  98. if (!background) throw new Error('Cannot redraw item: parent has no axis container element');
  99. axis.appendChild(dom.dot);
  100. }
  101. this.displayed = true;
  102. // Update DOM when item is marked dirty. An item is marked dirty when:
  103. // - the item is not yet rendered
  104. // - the item's data is changed
  105. // - the item is selected/deselected
  106. if (this.dirty) {
  107. this._updateContents(this.dom.content);
  108. this._updateTitle(this.dom.box);
  109. this._updateDataAttributes(this.dom.box);
  110. this._updateStyle(this.dom.box);
  111. var editable = (this.options.editable.updateTime ||
  112. this.options.editable.updateGroup ||
  113. this.editable === true) &&
  114. this.editable !== false;
  115. // update class
  116. var className = (this.data.className? ' ' + this.data.className : '') +
  117. (this.selected ? ' vis-selected' : '') +
  118. (editable ? ' vis-editable' : ' vis-readonly');
  119. dom.box.className = 'vis-item vis-box' + className;
  120. dom.line.className = 'vis-item vis-line' + className;
  121. dom.dot.className = 'vis-item vis-dot' + className;
  122. // set initial position in the visible range of the grid so that the
  123. // rendered box size can be determinated correctly, even the content
  124. // has a dynamic width (fixes #2032).
  125. var previousRight = dom.box.style.right;
  126. var previousLeft = dom.box.style.left;
  127. if (this.options.rtl) {
  128. dom.box.style.right = "0px";
  129. } else {
  130. dom.box.style.left = "0px";
  131. }
  132. // recalculate size
  133. this.props.dot.height = dom.dot.offsetHeight;
  134. this.props.dot.width = dom.dot.offsetWidth;
  135. this.props.line.width = dom.line.offsetWidth;
  136. this.width = dom.box.offsetWidth;
  137. this.height = dom.box.offsetHeight;
  138. // restore previous position
  139. if (this.options.rtl) {
  140. dom.box.style.right = previousRight;
  141. } else {
  142. dom.box.style.left = previousLeft;
  143. }
  144. this.dirty = false;
  145. }
  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;