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.

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