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.

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