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.

312 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. this._attachDataAttributes();
  102. // recalculate size
  103. if (this.dirty) {
  104. // determine from css whether this box has overflow
  105. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  106. this.props.content.width = this.dom.content.offsetWidth;
  107. this.height = this.dom.box.offsetHeight;
  108. this.dirty = false;
  109. }
  110. this._repaintDeleteButton(dom.box);
  111. this._repaintDragLeft();
  112. this._repaintDragRight();
  113. };
  114. /**
  115. * Show the item in the DOM (when not already visible). The items DOM will
  116. * be created when needed.
  117. */
  118. ItemRange.prototype.show = function() {
  119. if (!this.displayed) {
  120. this.redraw();
  121. }
  122. };
  123. /**
  124. * Hide the item from the DOM (when visible)
  125. * @return {Boolean} changed
  126. */
  127. ItemRange.prototype.hide = function() {
  128. if (this.displayed) {
  129. var box = this.dom.box;
  130. if (box.parentNode) {
  131. box.parentNode.removeChild(box);
  132. }
  133. this.top = null;
  134. this.left = null;
  135. this.displayed = false;
  136. }
  137. };
  138. /**
  139. * Reposition the item horizontally
  140. * @Override
  141. */
  142. ItemRange.prototype.repositionX = function() {
  143. var parentWidth = this.parent.width;
  144. var start = this.conversion.toScreen(this.data.start);
  145. var end = this.conversion.toScreen(this.data.end);
  146. var contentLeft;
  147. var contentWidth;
  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. this.left = start;
  158. this.width = boxWidth + this.props.content.width;
  159. contentWidth = this.props.content.width;
  160. // Note: The calculation of width is an optimistic calculation, giving
  161. // a width which will not change when moving the Timeline
  162. // So no re-stacking needed, which is nicer for the eye;
  163. }
  164. else {
  165. this.left = start;
  166. this.width = boxWidth;
  167. contentWidth = Math.min(end - start, this.props.content.width);
  168. }
  169. this.dom.box.style.left = this.left + 'px';
  170. this.dom.box.style.width = boxWidth + 'px';
  171. switch (this.options.align) {
  172. case 'left':
  173. this.dom.content.style.left = '0';
  174. break;
  175. case 'right':
  176. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding), 0) + 'px';
  177. break;
  178. case 'center':
  179. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding) / 2, 0) + 'px';
  180. break;
  181. default: // 'auto'
  182. if (this.overflow) {
  183. // when range exceeds left of the window, position the contents at the left of the visible area
  184. contentLeft = Math.max(-start, 0);
  185. }
  186. else {
  187. // when range exceeds left of the window, position the contents at the left of the visible area
  188. if (start < 0) {
  189. contentLeft = Math.min(-start,
  190. (end - start - this.props.content.width - 2 * this.options.padding));
  191. // TODO: remove the need for options.padding. it's terrible.
  192. }
  193. else {
  194. contentLeft = 0;
  195. }
  196. }
  197. this.dom.content.style.left = contentLeft + 'px';
  198. }
  199. };
  200. /**
  201. * Reposition the item vertically
  202. * @Override
  203. */
  204. ItemRange.prototype.repositionY = function() {
  205. var orientation = this.options.orientation,
  206. box = this.dom.box;
  207. if (orientation == 'top') {
  208. box.style.top = this.top + 'px';
  209. }
  210. else {
  211. box.style.top = (this.parent.height - this.top - this.height) + 'px';
  212. }
  213. };
  214. /**
  215. * Repaint a drag area on the left side of the range when the range is selected
  216. * @protected
  217. */
  218. ItemRange.prototype._repaintDragLeft = function () {
  219. if (this.selected && this.options.editable.updateTime && !this.dom.dragLeft) {
  220. // create and show drag area
  221. var dragLeft = document.createElement('div');
  222. dragLeft.className = 'drag-left';
  223. dragLeft.dragLeftItem = this;
  224. // TODO: this should be redundant?
  225. Hammer(dragLeft, {
  226. preventDefault: true
  227. }).on('drag', function () {
  228. //console.log('drag left')
  229. });
  230. this.dom.box.appendChild(dragLeft);
  231. this.dom.dragLeft = dragLeft;
  232. }
  233. else if (!this.selected && this.dom.dragLeft) {
  234. // delete drag area
  235. if (this.dom.dragLeft.parentNode) {
  236. this.dom.dragLeft.parentNode.removeChild(this.dom.dragLeft);
  237. }
  238. this.dom.dragLeft = null;
  239. }
  240. };
  241. /**
  242. * Repaint a drag area on the right side of the range when the range is selected
  243. * @protected
  244. */
  245. ItemRange.prototype._repaintDragRight = function () {
  246. if (this.selected && this.options.editable.updateTime && !this.dom.dragRight) {
  247. // create and show drag area
  248. var dragRight = document.createElement('div');
  249. dragRight.className = 'drag-right';
  250. dragRight.dragRightItem = this;
  251. // TODO: this should be redundant?
  252. Hammer(dragRight, {
  253. preventDefault: true
  254. }).on('drag', function () {
  255. //console.log('drag right')
  256. });
  257. this.dom.box.appendChild(dragRight);
  258. this.dom.dragRight = dragRight;
  259. }
  260. else if (!this.selected && this.dom.dragRight) {
  261. // delete drag area
  262. if (this.dom.dragRight.parentNode) {
  263. this.dom.dragRight.parentNode.removeChild(this.dom.dragRight);
  264. }
  265. this.dom.dragRight = null;
  266. }
  267. };
  268. module.exports = ItemRange;