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.

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