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.

292 lines
8.0 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 time axis: 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. // update class
  83. var className = (this.data.className ? (' ' + this.data.className) : '') +
  84. (this.selected ? ' selected' : '');
  85. dom.box.className = this.baseClassName + className;
  86. // determine from css whether this box has overflow
  87. this.overflow = window.getComputedStyle(dom.content).overflow !== 'hidden';
  88. // recalculate size
  89. this.props.content.width = this.dom.content.offsetWidth;
  90. this.height = this.dom.box.offsetHeight;
  91. this.dirty = false;
  92. }
  93. this._repaintDeleteButton(dom.box);
  94. this._repaintDragLeft();
  95. this._repaintDragRight();
  96. };
  97. /**
  98. * Show the item in the DOM (when not already visible). The items DOM will
  99. * be created when needed.
  100. */
  101. RangeItem.prototype.show = function() {
  102. if (!this.displayed) {
  103. this.redraw();
  104. }
  105. };
  106. /**
  107. * Hide the item from the DOM (when visible)
  108. * @return {Boolean} changed
  109. */
  110. RangeItem.prototype.hide = function() {
  111. if (this.displayed) {
  112. var box = this.dom.box;
  113. if (box.parentNode) {
  114. box.parentNode.removeChild(box);
  115. }
  116. this.top = null;
  117. this.left = null;
  118. this.displayed = false;
  119. }
  120. };
  121. /**
  122. * Reposition the item horizontally
  123. * @Override
  124. */
  125. RangeItem.prototype.repositionX = function() {
  126. var parentWidth = this.parent.width;
  127. var start = this.conversion.toScreen(this.data.start);
  128. var end = this.conversion.toScreen(this.data.end);
  129. var contentLeft;
  130. var contentWidth;
  131. // limit the width of the this, as browsers cannot draw very wide divs
  132. if (start < -parentWidth) {
  133. start = -parentWidth;
  134. }
  135. if (end > 2 * parentWidth) {
  136. end = 2 * parentWidth;
  137. }
  138. var boxWidth = Math.max(end - start, 1);
  139. if (this.overflow) {
  140. this.left = start;
  141. this.width = boxWidth + this.props.content.width;
  142. contentWidth = this.props.content.width;
  143. // Note: The calculation of width is an optimistic calculation, giving
  144. // a width which will not change when moving the Timeline
  145. // So no re-stacking needed, which is nicer for the eye;
  146. }
  147. else {
  148. this.left = start;
  149. this.width = boxWidth;
  150. contentWidth = Math.min(end - start, this.props.content.width);
  151. }
  152. this.dom.box.style.left = this.left + 'px';
  153. this.dom.box.style.width = boxWidth + 'px';
  154. switch (this.options.align) {
  155. case 'left':
  156. this.dom.content.style.left = '0';
  157. break;
  158. case 'right':
  159. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding), 0) + 'px';
  160. break;
  161. case 'center':
  162. this.dom.content.style.left = Math.max((boxWidth - contentWidth - 2 * this.options.padding) / 2, 0) + 'px';
  163. break;
  164. default: // 'auto'
  165. if (this.overflow) {
  166. // when range exceeds left of the window, position the contents at the left of the visible area
  167. contentLeft = Math.max(-start, 0);
  168. }
  169. else {
  170. // when range exceeds left of the window, position the contents at the left of the visible area
  171. if (start < 0) {
  172. contentLeft = Math.min(-start,
  173. (end - start - this.props.content.width - 2 * this.options.padding));
  174. // TODO: remove the need for options.padding. it's terrible.
  175. }
  176. else {
  177. contentLeft = 0;
  178. }
  179. }
  180. this.dom.content.style.left = contentLeft + 'px';
  181. }
  182. };
  183. /**
  184. * Reposition the item vertically
  185. * @Override
  186. */
  187. RangeItem.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. RangeItem.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. RangeItem.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 = RangeItem;