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
7.7 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('hammerjs');
  2. var Item = require('./Item');
  3. /**
  4. * @constructor ItemRange
  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 ItemRange (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. ItemRange.prototype = new Item (null, null, null);
  32. ItemRange.prototype.baseClassName = 'item 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. ItemRange.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. ItemRange.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 = 'content';
  57. dom.box.appendChild(dom.content);
  58. // attach this item as attribute
  59. dom.box['timeline-item'] = this;
  60. }
  61. // append DOM to parent DOM
  62. if (!this.parent) {
  63. throw new Error('Cannot redraw item: no parent attached');
  64. }
  65. if (!dom.box.parentNode) {
  66. var foreground = this.parent.dom.foreground;
  67. if (!foreground) {
  68. throw new Error('Cannot redraw time axis: parent has no foreground container element');
  69. }
  70. foreground.appendChild(dom.box);
  71. }
  72. this.displayed = true;
  73. // update contents
  74. if (this.data.content != this.content) {
  75. this.content = this.data.content;
  76. if (this.content instanceof Element) {
  77. dom.content.innerHTML = '';
  78. dom.content.appendChild(this.content);
  79. }
  80. else if (this.data.content != undefined) {
  81. dom.content.innerHTML = this.content;
  82. }
  83. else {
  84. throw new Error('Property "content" missing in item ' + this.data.id);
  85. }
  86. this.dirty = true;
  87. }
  88. // update title
  89. if (this.data.title != this.title) {
  90. dom.box.title = this.data.title;
  91. this.title = this.data.title;
  92. }
  93. // update class
  94. var className = (this.data.className ? (' ' + this.data.className) : '') +
  95. (this.selected ? ' selected' : '');
  96. if (this.className != className) {
  97. this.className = className;
  98. dom.box.className = this.baseClassName + className;
  99. this.dirty = true;
  100. }
  101. // recalculate size
  102. if (this.dirty) {
  103. // determine from css whether this box has overflow
  104. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  105. this.props.content.width = this.dom.content.offsetWidth;
  106. this.height = this.dom.box.offsetHeight;
  107. this.dirty = false;
  108. }
  109. this._repaintDeleteButton(dom.box);
  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. ItemRange.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. ItemRange.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.top = null;
  133. this.left = null;
  134. this.displayed = false;
  135. }
  136. };
  137. /**
  138. * Reposition the item horizontally
  139. * @Override
  140. */
  141. // TODO: delete the old function
  142. ItemRange.prototype.repositionX = function() {
  143. var props = this.props,
  144. parentWidth = this.parent.width,
  145. start = this.conversion.toScreen(this.data.start),
  146. end = this.conversion.toScreen(this.data.end),
  147. padding = this.options.padding,
  148. contentLeft;
  149. // limit the width of the this, as browsers cannot draw very wide divs
  150. if (start < -parentWidth) {
  151. start = -parentWidth;
  152. }
  153. if (end > 2 * parentWidth) {
  154. end = 2 * parentWidth;
  155. }
  156. var boxWidth = Math.max(end - start, 1);
  157. if (this.overflow) {
  158. // when range exceeds left of the window, position the contents at the left of the visible area
  159. contentLeft = Math.max(-start, 0);
  160. this.left = start;
  161. this.width = boxWidth + 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 restacking needed, which is nicer for the eye;
  165. }
  166. else { // no overflow
  167. // when range exceeds left of the window, position the contents at the left of the visible area
  168. if (start < 0) {
  169. contentLeft = Math.min(-start,
  170. (end - start - props.content.width - 2 * padding));
  171. // TODO: remove the need for options.padding. it's terrible.
  172. }
  173. else {
  174. contentLeft = 0;
  175. }
  176. this.left = start;
  177. this.width = boxWidth;
  178. }
  179. this.dom.box.style.left = this.left + 'px';
  180. this.dom.box.style.width = boxWidth + 'px';
  181. this.dom.content.style.left = contentLeft + 'px';
  182. };
  183. /**
  184. * Reposition the item vertically
  185. * @Override
  186. */
  187. ItemRange.prototype.repositionY = function() {
  188. var orientation = this.options.orientation,
  189. box = this.dom.box;
  190. if (orientation == 'top') {
  191. box.style.top = this.top + 'px';
  192. }
  193. else {
  194. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  195. }
  196. };
  197. /**
  198. * Repaint a drag area on the left side of the range when the range is selected
  199. * @protected
  200. */
  201. ItemRange.prototype._repaintDragLeft = function () {
  202. if (this.selected && this.options.editable.updateTime && !this.dom.dragLeft) {
  203. // create and show drag area
  204. var dragLeft = document.createElement('div');
  205. dragLeft.className = 'drag-left';
  206. dragLeft.dragLeftItem = this;
  207. // TODO: this should be redundant?
  208. Hammer(dragLeft, {
  209. preventDefault: true
  210. }).on('drag', function () {
  211. //console.log('drag left')
  212. });
  213. this.dom.box.appendChild(dragLeft);
  214. this.dom.dragLeft = dragLeft;
  215. }
  216. else if (!this.selected && this.dom.dragLeft) {
  217. // delete drag area
  218. if (this.dom.dragLeft.parentNode) {
  219. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  220. }
  221. this.dom.dragLeft = null;
  222. }
  223. };
  224. /**
  225. * Repaint a drag area on the right side of the range when the range is selected
  226. * @protected
  227. */
  228. ItemRange.prototype._repaintDragRight = function () {
  229. if (this.selected && this.options.editable.updateTime && !this.dom.dragRight) {
  230. // create and show drag area
  231. var dragRight = document.createElement('div');
  232. dragRight.className = 'drag-right';
  233. dragRight.dragRightItem = this;
  234. // TODO: this should be redundant?
  235. Hammer(dragRight, {
  236. preventDefault: true
  237. }).on('drag', function () {
  238. //console.log('drag right')
  239. });
  240. this.dom.box.appendChild(dragRight);
  241. this.dom.dragRight = dragRight;
  242. }
  243. else if (!this.selected && this.dom.dragRight) {
  244. // delete drag area
  245. if (this.dom.dragRight.parentNode) {
  246. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  247. }
  248. this.dom.dragRight = null;
  249. }
  250. };
  251. module.exports = ItemRange;