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.

290 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('../../../module/hammer');
  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. ItemRange.prototype.repositionX = function() {
  142. var props = this.props,
  143. parentWidth = this.parent.width,
  144. start = this.conversion.toScreen(this.data.start),
  145. end = this.conversion.toScreen(this.data.end),
  146. padding = this.options.padding,
  147. contentLeft;
  148. // limit the width of the this, as browsers cannot draw very wide divs
  149. if (start < -parentWidth) {
  150. start = -parentWidth;
  151. }
  152. if (end > 2 * parentWidth) {
  153. end = 2 * parentWidth;
  154. }
  155. var boxWidth = Math.max(end - start, 1);
  156. if (this.overflow) {
  157. // when range exceeds left of the window, position the contents at the left of the visible area
  158. contentLeft = Math.max(-start, 0);
  159. this.left = start;
  160. this.width = boxWidth + this.props.content.width;
  161. // Note: The calculation of width is an optimistic calculation, giving
  162. // a width which will not change when moving the Timeline
  163. // So no restacking needed, which is nicer for the eye;
  164. }
  165. else { // no overflow
  166. // when range exceeds left of the window, position the contents at the left of the visible area
  167. if (start < 0) {
  168. contentLeft = Math.min(-start,
  169. (end - start - props.content.width - 2 * padding));
  170. // TODO: remove the need for options.padding. it's terrible.
  171. }
  172. else {
  173. contentLeft = 0;
  174. }
  175. this.left = start;
  176. this.width = boxWidth;
  177. }
  178. this.dom.box.style.left = this.left + 'px';
  179. this.dom.box.style.width = boxWidth + 'px';
  180. this.dom.content.style.left = contentLeft + 'px';
  181. };
  182. /**
  183. * Reposition the item vertically
  184. * @Override
  185. */
  186. ItemRange.prototype.repositionY = function() {
  187. var orientation = this.options.orientation,
  188. box = this.dom.box;
  189. if (orientation == 'top') {
  190. box.style.top = this.top + 'px';
  191. }
  192. else {
  193. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  194. }
  195. };
  196. /**
  197. * Repaint a drag area on the left side of the range when the range is selected
  198. * @protected
  199. */
  200. ItemRange.prototype._repaintDragLeft = function () {
  201. if (this.selected && this.options.editable.updateTime && !this.dom.dragLeft) {
  202. // create and show drag area
  203. var dragLeft = document.createElement('div');
  204. dragLeft.className = 'drag-left';
  205. dragLeft.dragLeftItem = this;
  206. // TODO: this should be redundant?
  207. Hammer(dragLeft, {
  208. preventDefault: true
  209. }).on('drag', function () {
  210. //console.log('drag left')
  211. });
  212. this.dom.box.appendChild(dragLeft);
  213. this.dom.dragLeft = dragLeft;
  214. }
  215. else if (!this.selected && this.dom.dragLeft) {
  216. // delete drag area
  217. if (this.dom.dragLeft.parentNode) {
  218. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  219. }
  220. this.dom.dragLeft = null;
  221. }
  222. };
  223. /**
  224. * Repaint a drag area on the right side of the range when the range is selected
  225. * @protected
  226. */
  227. ItemRange.prototype._repaintDragRight = function () {
  228. if (this.selected && this.options.editable.updateTime && !this.dom.dragRight) {
  229. // create and show drag area
  230. var dragRight = document.createElement('div');
  231. dragRight.className = 'drag-right';
  232. dragRight.dragRightItem = this;
  233. // TODO: this should be redundant?
  234. Hammer(dragRight, {
  235. preventDefault: true
  236. }).on('drag', function () {
  237. //console.log('drag right')
  238. });
  239. this.dom.box.appendChild(dragRight);
  240. this.dom.dragRight = dragRight;
  241. }
  242. else if (!this.selected && this.dom.dragRight) {
  243. // delete drag area
  244. if (this.dom.dragRight.parentNode) {
  245. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  246. }
  247. this.dom.dragRight = null;
  248. }
  249. };
  250. module.exports = ItemRange;