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.

294 lines
8.3 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-overflow';
  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, this.props.content.width);
  165. }
  166. this.dom.box.style.left = this.left + 'px';
  167. this.dom.box.style.width = boxWidth + 'px';
  168. switch (this.options.align) {
  169. case 'left':
  170. this.dom.content.style.left = '0';
  171. break;
  172. case 'right':
  173. this.dom.content.style.left = Math.max((boxWidth - contentWidth), 0) + 'px';
  174. break;
  175. case 'center':
  176. this.dom.content.style.left = Math.max((boxWidth - contentWidth) / 2, 0) + 'px';
  177. break;
  178. default: // 'auto'
  179. // when range exceeds left of the window, position the contents at the left of the visible area
  180. if (this.overflow) {
  181. if (end > 0) {
  182. contentLeft = Math.max(-start, 0);
  183. }
  184. else {
  185. contentLeft = -contentWidth; // ensure it's not visible anymore
  186. }
  187. }
  188. else {
  189. if (start < 0) {
  190. contentLeft = -start;
  191. }
  192. else {
  193. contentLeft = 0;
  194. }
  195. }
  196. this.dom.content.style.left = contentLeft + 'px';
  197. }
  198. };
  199. /**
  200. * Reposition the item vertically
  201. * @Override
  202. */
  203. RangeItem.prototype.repositionY = function() {
  204. var orientation = this.options.orientation.item;
  205. var box = this.dom.box;
  206. if (orientation == 'top') {
  207. box.style.top = this.top + 'px';
  208. }
  209. else {
  210. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  211. }
  212. };
  213. /**
  214. * Repaint a drag area on the left side of the range when the range is selected
  215. * @protected
  216. */
  217. RangeItem.prototype._repaintDragLeft = function () {
  218. if (this.selected && this.options.editable.updateTime && !this.dom.dragLeft) {
  219. // create and show drag area
  220. var dragLeft = document.createElement('div');
  221. dragLeft.className = 'vis-drag-left';
  222. dragLeft.dragLeftItem = this;
  223. this.dom.box.appendChild(dragLeft);
  224. this.dom.dragLeft = dragLeft;
  225. }
  226. else if (!this.selected && this.dom.dragLeft) {
  227. // delete drag area
  228. if (this.dom.dragLeft.parentNode) {
  229. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  230. }
  231. this.dom.dragLeft = null;
  232. }
  233. };
  234. /**
  235. * Repaint a drag area on the right side of the range when the range is selected
  236. * @protected
  237. */
  238. RangeItem.prototype._repaintDragRight = function () {
  239. if (this.selected && this.options.editable.updateTime && !this.dom.dragRight) {
  240. // create and show drag area
  241. var dragRight = document.createElement('div');
  242. dragRight.className = 'vis-drag-right';
  243. dragRight.dragRightItem = this;
  244. this.dom.box.appendChild(dragRight);
  245. this.dom.dragRight = dragRight;
  246. }
  247. else if (!this.selected && this.dom.dragRight) {
  248. // delete drag area
  249. if (this.dom.dragRight.parentNode) {
  250. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  251. }
  252. this.dom.dragRight = null;
  253. }
  254. };
  255. module.exports = RangeItem;