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.

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