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.

295 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 Hammer = require('../../../module/hammer');
  2. var Item = require('./Item');
  3. /**
  4. * @constructor RangeItem
  5. * @extends Item
  6. * @param {Object} data Object containing parameters start, end
  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 options
  12. */
  13. function RangeItem (data, conversion, options) {
  14. this.props = {
  15. content: {
  16. width: 0
  17. }
  18. };
  19. this.overflow = false; // if contents can overflow (css styling), this flag is set to true
  20. // validate data
  21. if (data) {
  22. if (data.start == undefined) {
  23. throw new Error('Property "start" missing in item ' + data.id);
  24. }
  25. if (data.end == undefined) {
  26. throw new Error('Property "end" missing in item ' + data.id);
  27. }
  28. }
  29. Item.call(this, data, conversion, options);
  30. }
  31. RangeItem.prototype = new Item (null, null, null);
  32. RangeItem.prototype.baseClassName = 'vis-item vis-range';
  33. /**
  34. * Check whether this item is visible inside given range
  35. * @returns {{start: Number, end: Number}} range with a timestamp for start and end
  36. * @returns {boolean} True if visible
  37. */
  38. RangeItem.prototype.isVisible = function(range) {
  39. // determine visibility
  40. return (this.data.start < range.end) && (this.data.end > range.start);
  41. };
  42. /**
  43. * Repaint the item
  44. */
  45. RangeItem.prototype.redraw = function() {
  46. var dom = this.dom;
  47. if (!dom) {
  48. // create DOM
  49. this.dom = {};
  50. dom = this.dom;
  51. // background box
  52. dom.box = document.createElement('div');
  53. // className is updated in redraw()
  54. // frame box (to prevent the item contents from overflowing
  55. dom.frame = document.createElement('div');
  56. dom.frame.className = 'vis-item-frame';
  57. dom.box.appendChild(dom.frame);
  58. // contents box
  59. dom.content = document.createElement('div');
  60. dom.content.className = 'vis-item-content';
  61. dom.frame.appendChild(dom.content);
  62. // attach this item as attribute
  63. dom.box['timeline-item'] = this;
  64. this.dirty = true;
  65. }
  66. // append DOM to parent DOM
  67. if (!this.parent) {
  68. throw new Error('Cannot redraw item: no parent attached');
  69. }
  70. if (!dom.box.parentNode) {
  71. var foreground = this.parent.dom.foreground;
  72. if (!foreground) {
  73. throw new Error('Cannot redraw item: parent has no foreground container element');
  74. }
  75. foreground.appendChild(dom.box);
  76. }
  77. this.displayed = true;
  78. // Update DOM when item is marked dirty. An item is marked dirty when:
  79. // - the item is not yet rendered
  80. // - the item's data is changed
  81. // - the item is selected/deselected
  82. if (this.dirty) {
  83. this._updateContents(this.dom.content);
  84. this._updateTitle(this.dom.box);
  85. this._updateDataAttributes(this.dom.box);
  86. this._updateStyle(this.dom.box);
  87. // update class
  88. var className = (this.data.className ? (' ' + this.data.className) : '') +
  89. (this.selected ? ' vis-selected' : '');
  90. dom.box.className = this.baseClassName + className;
  91. // determine from css whether this box has overflow
  92. this.overflow = window.getComputedStyle(dom.frame).overflow !== 'hidden';
  93. // recalculate size
  94. // turn off max-width to be able to calculate the real width
  95. // this causes an extra browser repaint/reflow, but so be it
  96. this.dom.content.style.maxWidth = 'none';
  97. this.props.content.width = this.dom.content.offsetWidth;
  98. this.height = this.dom.box.offsetHeight;
  99. this.dom.content.style.maxWidth = '';
  100. this.dirty = false;
  101. }
  102. this._repaintDeleteButton(dom.box);
  103. this._repaintDragLeft();
  104. this._repaintDragRight();
  105. };
  106. /**
  107. * Show the item in the DOM (when not already visible). The items DOM will
  108. * be created when needed.
  109. */
  110. RangeItem.prototype.show = function() {
  111. if (!this.displayed) {
  112. this.redraw();
  113. }
  114. };
  115. /**
  116. * Hide the item from the DOM (when visible)
  117. * @return {Boolean} changed
  118. */
  119. RangeItem.prototype.hide = function() {
  120. if (this.displayed) {
  121. var box = this.dom.box;
  122. if (box.parentNode) {
  123. box.parentNode.removeChild(box);
  124. }
  125. this.displayed = false;
  126. }
  127. };
  128. /**
  129. * Reposition the item horizontally
  130. * @param {boolean} [limitSize=true] If true (default), the width of the range
  131. * item will be limited, as the browser cannot
  132. * display very wide divs. This means though
  133. * that the applied left and width may
  134. * not correspond to the ranges start and end
  135. * @Override
  136. */
  137. RangeItem.prototype.repositionX = function(limitSize) {
  138. var parentWidth = this.parent.width;
  139. var start = this.conversion.toScreen(this.data.start);
  140. var end = this.conversion.toScreen(this.data.end);
  141. var contentLeft;
  142. var contentWidth;
  143. // limit the width of the range, as browsers cannot draw very wide divs
  144. if (limitSize === undefined || limitSize === true) {
  145. if (start < -parentWidth) {
  146. start = -parentWidth;
  147. }
  148. if (end > 2 * parentWidth) {
  149. end = 2 * parentWidth;
  150. }
  151. }
  152. var boxWidth = Math.max(end - start, 1);
  153. if (this.overflow) {
  154. this.left = start;
  155. this.width = boxWidth + this.props.content.width;
  156. contentWidth = this.props.content.width;
  157. // Note: The calculation of width is an optimistic calculation, giving
  158. // a width which will not change when moving the Timeline
  159. // So no re-stacking needed, which is nicer for the eye;
  160. }
  161. else {
  162. this.left = start;
  163. this.width = boxWidth;
  164. contentWidth = Math.min(end - start - 2 * this.options.padding, this.props.content.width);
  165. // TODO: remove the need for options.padding. it's terrible.
  166. }
  167. this.dom.box.style.left = this.left + 'px';
  168. this.dom.box.style.width = boxWidth + 'px';
  169. switch (this.options.align) {
  170. case 'left':
  171. this.dom.content.style.left = '0';
  172. break;
  173. case 'right':
  174. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding), 0) + 'px';
  175. break;
  176. case 'center':
  177. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding) / 2, 0) + 'px';
  178. break;
  179. default: // 'auto'
  180. // when range exceeds left of the window, position the contents at the left of the visible area
  181. if (this.overflow) {
  182. if (end > 0) {
  183. contentLeft = Math.max(-start, 0);
  184. }
  185. else {
  186. contentLeft = -contentWidth; // ensure it's not visible anymore
  187. }
  188. }
  189. else {
  190. if (start < 0) {
  191. contentLeft = -start;
  192. }
  193. else {
  194. contentLeft = 0;
  195. }
  196. }
  197. this.dom.content.style.left = contentLeft + 'px';
  198. }
  199. };
  200. /**
  201. * Reposition the item vertically
  202. * @Override
  203. */
  204. RangeItem.prototype.repositionY = function() {
  205. var orientation = this.options.orientation,
  206. box = this.dom.box;
  207. if (orientation == 'top') {
  208. box.style.top = this.top + 'px';
  209. }
  210. else {
  211. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  212. }
  213. };
  214. /**
  215. * Repaint a drag area on the left side of the range when the range is selected
  216. * @protected
  217. */
  218. RangeItem.prototype._repaintDragLeft = function () {
  219. if (this.selected && this.options.editable.updateTime && !this.dom.dragLeft) {
  220. // create and show drag area
  221. var dragLeft = document.createElement('div');
  222. dragLeft.className = 'vis-drag-left';
  223. dragLeft.dragLeftItem = this;
  224. this.dom.box.appendChild(dragLeft);
  225. this.dom.dragLeft = dragLeft;
  226. }
  227. else if (!this.selected && this.dom.dragLeft) {
  228. // delete drag area
  229. if (this.dom.dragLeft.parentNode) {
  230. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  231. }
  232. this.dom.dragLeft = null;
  233. }
  234. };
  235. /**
  236. * Repaint a drag area on the right side of the range when the range is selected
  237. * @protected
  238. */
  239. RangeItem.prototype._repaintDragRight = function () {
  240. if (this.selected && this.options.editable.updateTime && !this.dom.dragRight) {
  241. // create and show drag area
  242. var dragRight = document.createElement('div');
  243. dragRight.className = 'vis-drag-right';
  244. dragRight.dragRightItem = this;
  245. this.dom.box.appendChild(dragRight);
  246. this.dom.dragRight = dragRight;
  247. }
  248. else if (!this.selected && this.dom.dragRight) {
  249. // delete drag area
  250. if (this.dom.dragRight.parentNode) {
  251. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  252. }
  253. this.dom.dragRight = null;
  254. }
  255. };
  256. module.exports = RangeItem;