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.

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