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.

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