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.

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