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.

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