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.

840 lines
26 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
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
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
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
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 util = require('../util');
  2. var hammerUtil = require('../hammerUtil');
  3. var moment = require('../module/moment');
  4. var Component = require('./component/Component');
  5. var DateUtil = require('./DateUtil');
  6. /**
  7. * @constructor Range
  8. * A Range controls a numeric range with a start and end value.
  9. * The Range adjusts the range based on mouse events or programmatic changes,
  10. * and triggers events when the range is changing or has been changed.
  11. * @param {{dom: Object, domProps: Object, emitter: Emitter}} body
  12. * @param {Object} [options] See description at Range.setOptions
  13. */
  14. function Range(body, options) {
  15. var now = moment().hours(0).minutes(0).seconds(0).milliseconds(0);
  16. var start = now.clone().add(-3, 'days').valueOf();
  17. var end = now.clone().add(-3, 'days').valueOf();
  18. if(options === undefined) {
  19. this.start = start;
  20. this.end = end;
  21. } else {
  22. this.start = options.start || start
  23. this.end = options.end || end
  24. }
  25. this.rolling = false;
  26. this.body = body;
  27. this.deltaDifference = 0;
  28. this.scaleOffset = 0;
  29. this.startToFront = false;
  30. this.endToFront = true;
  31. // default options
  32. this.defaultOptions = {
  33. rtl: false,
  34. start: null,
  35. end: null,
  36. moment: moment,
  37. direction: 'horizontal', // 'horizontal' or 'vertical'
  38. moveable: true,
  39. zoomable: true,
  40. min: null,
  41. max: null,
  42. zoomMin: 10, // milliseconds
  43. zoomMax: 1000 * 60 * 60 * 24 * 365 * 10000 // milliseconds
  44. };
  45. this.options = util.extend({}, this.defaultOptions);
  46. this.props = {
  47. touch: {}
  48. };
  49. this.animationTimer = null;
  50. // drag listeners for dragging
  51. this.body.emitter.on('panstart', this._onDragStart.bind(this));
  52. this.body.emitter.on('panmove', this._onDrag.bind(this));
  53. this.body.emitter.on('panend', this._onDragEnd.bind(this));
  54. // mouse wheel for zooming
  55. this.body.emitter.on('mousewheel', this._onMouseWheel.bind(this));
  56. // pinch to zoom
  57. this.body.emitter.on('touch', this._onTouch.bind(this));
  58. this.body.emitter.on('pinch', this._onPinch.bind(this));
  59. // on click of rolling mode button
  60. this.body.dom.rollingModeBtn.addEventListener('click', this.startRolling.bind(this));
  61. this.setOptions(options);
  62. }
  63. Range.prototype = new Component();
  64. /**
  65. * Set options for the range controller
  66. * @param {Object} options Available options:
  67. * {Number | Date | String} start Start date for the range
  68. * {Number | Date | String} end End date for the range
  69. * {Number} min Minimum value for start
  70. * {Number} max Maximum value for end
  71. * {Number} zoomMin Set a minimum value for
  72. * (end - start).
  73. * {Number} zoomMax Set a maximum value for
  74. * (end - start).
  75. * {Boolean} moveable Enable moving of the range
  76. * by dragging. True by default
  77. * {Boolean} zoomable Enable zooming of the range
  78. * by pinching/scrolling. True by default
  79. */
  80. Range.prototype.setOptions = function (options) {
  81. if (options) {
  82. // copy the options that we know
  83. var fields = [
  84. 'animation', 'direction', 'min', 'max', 'zoomMin', 'zoomMax', 'moveable', 'zoomable',
  85. 'moment', 'activate', 'hiddenDates', 'zoomKey', 'rtl', 'showCurrentTime', 'rollMode', 'horizontalScroll'
  86. ];
  87. util.selectiveExtend(fields, this.options, options);
  88. if (options.rollingMode) {
  89. this.startRolling();
  90. }
  91. if ('start' in options || 'end' in options) {
  92. // apply a new range. both start and end are optional
  93. this.setRange(options.start, options.end);
  94. }
  95. }
  96. };
  97. /**
  98. * Test whether direction has a valid value
  99. * @param {String} direction 'horizontal' or 'vertical'
  100. */
  101. function validateDirection (direction) {
  102. if (direction != 'horizontal' && direction != 'vertical') {
  103. throw new TypeError('Unknown direction "' + direction + '". ' +
  104. 'Choose "horizontal" or "vertical".');
  105. }
  106. }
  107. /**
  108. * Start auto refreshing the current time bar
  109. */
  110. Range.prototype.startRolling = function() {
  111. var me = this;
  112. function update () {
  113. me.stopRolling();
  114. me.rolling = true;
  115. var interval = me.end - me.start;
  116. var t = util.convert(new Date(), 'Date').valueOf();
  117. var start = t - interval / 2;
  118. var end = t + interval / 2;
  119. var animation = (me.options && me.options.animation !== undefined) ? me.options.animation : true;
  120. me.setRange(start, end, false);
  121. // determine interval to refresh
  122. var scale = me.conversion(me.body.domProps.center.width).scale;
  123. var interval = 1 / scale / 10;
  124. if (interval < 30) interval = 30;
  125. if (interval > 1000) interval = 1000;
  126. me.body.dom.rollingModeBtn.style.visibility = "hidden";
  127. // start a renderTimer to adjust for the new time
  128. me.currentTimeTimer = setTimeout(update, interval);
  129. }
  130. update();
  131. };
  132. /**
  133. * Stop auto refreshing the current time bar
  134. */
  135. Range.prototype.stopRolling = function() {
  136. if (this.currentTimeTimer !== undefined) {
  137. clearTimeout(this.currentTimeTimer);
  138. this.rolling = false;
  139. this.body.dom.rollingModeBtn.style.visibility = "visible";
  140. }
  141. };
  142. /**
  143. * Set a new start and end range
  144. * @param {Date | Number | String} [start]
  145. * @param {Date | Number | String} [end]
  146. * @param {boolean | {duration: number, easingFunction: string}} [animation=false]
  147. * If true (default), the range is animated
  148. * smoothly to the new window. An object can be
  149. * provided to specify duration and easing function.
  150. * Default duration is 500 ms, and default easing
  151. * function is 'easeInOutQuad'.
  152. * @param {Boolean} [byUser=false]
  153. *
  154. */
  155. Range.prototype.setRange = function(start, end, animation, byUser, event) {
  156. if (byUser !== true) {
  157. byUser = false;
  158. }
  159. var finalStart = start != undefined ? util.convert(start, 'Date').valueOf() : null;
  160. var finalEnd = end != undefined ? util.convert(end, 'Date').valueOf() : null;
  161. this._cancelAnimation();
  162. if (animation) { // true or an Object
  163. var me = this;
  164. var initStart = this.start;
  165. var initEnd = this.end;
  166. var duration = (typeof animation === 'object' && 'duration' in animation) ? animation.duration : 500;
  167. var easingName = (typeof animation === 'object' && 'easingFunction' in animation) ? animation.easingFunction : 'easeInOutQuad';
  168. var easingFunction = util.easingFunctions[easingName];
  169. if (!easingFunction) {
  170. throw new Error('Unknown easing function ' + JSON.stringify(easingName) + '. ' +
  171. 'Choose from: ' + Object.keys(util.easingFunctions).join(', '));
  172. }
  173. var initTime = new Date().valueOf();
  174. var anyChanged = false;
  175. var next = function () {
  176. if (!me.props.touch.dragging) {
  177. var now = new Date().valueOf();
  178. var time = now - initTime;
  179. var ease = easingFunction(time / duration);
  180. var done = time > duration;
  181. var s = (done || finalStart === null) ? finalStart : initStart + (finalStart - initStart) * ease;
  182. var e = (done || finalEnd === null) ? finalEnd : initEnd + (finalEnd - initEnd) * ease;
  183. changed = me._applyRange(s, e);
  184. DateUtil.updateHiddenDates(me.options.moment, me.body, me.options.hiddenDates);
  185. anyChanged = anyChanged || changed;
  186. var params = {
  187. start: new Date(me.start),
  188. end: new Date(me.end),
  189. byUser:byUser,
  190. event: util.elementsCensor(event)
  191. }
  192. if (changed) {
  193. me.body.emitter.emit('rangechange', params);
  194. }
  195. if (done) {
  196. if (anyChanged) {
  197. me.body.emitter.emit('rangechanged', params);
  198. }
  199. }
  200. else {
  201. // animate with as high as possible frame rate, leave 20 ms in between
  202. // each to prevent the browser from blocking
  203. me.animationTimer = setTimeout(next, 20);
  204. }
  205. }
  206. };
  207. return next();
  208. }
  209. else {
  210. var changed = this._applyRange(finalStart, finalEnd);
  211. DateUtil.updateHiddenDates(this.options.moment, this.body, this.options.hiddenDates);
  212. if (changed) {
  213. var params = {
  214. start: new Date(this.start),
  215. end: new Date(this.end),
  216. byUser:byUser,
  217. event: util.elementsCensor(event)
  218. };
  219. this.body.emitter.emit('rangechange', params);
  220. this.body.emitter.emit('rangechanged', params);
  221. }
  222. }
  223. };
  224. /**
  225. * Stop an animation
  226. * @private
  227. */
  228. Range.prototype._cancelAnimation = function () {
  229. if (this.animationTimer) {
  230. clearTimeout(this.animationTimer);
  231. this.animationTimer = null;
  232. }
  233. };
  234. /**
  235. * Set a new start and end range. This method is the same as setRange, but
  236. * does not trigger a range change and range changed event, and it returns
  237. * true when the range is changed
  238. * @param {Number} [start]
  239. * @param {Number} [end]
  240. * @return {Boolean} changed
  241. * @private
  242. */
  243. Range.prototype._applyRange = function(start, end) {
  244. var newStart = (start != null) ? util.convert(start, 'Date').valueOf() : this.start,
  245. newEnd = (end != null) ? util.convert(end, 'Date').valueOf() : this.end,
  246. max = (this.options.max != null) ? util.convert(this.options.max, 'Date').valueOf() : null,
  247. min = (this.options.min != null) ? util.convert(this.options.min, 'Date').valueOf() : null,
  248. diff;
  249. // check for valid number
  250. if (isNaN(newStart) || newStart === null) {
  251. throw new Error('Invalid start "' + start + '"');
  252. }
  253. if (isNaN(newEnd) || newEnd === null) {
  254. throw new Error('Invalid end "' + end + '"');
  255. }
  256. // prevent start < end
  257. if (newEnd < newStart) {
  258. newEnd = newStart;
  259. }
  260. // prevent start < min
  261. if (min !== null) {
  262. if (newStart < min) {
  263. diff = (min - newStart);
  264. newStart += diff;
  265. newEnd += diff;
  266. // prevent end > max
  267. if (max != null) {
  268. if (newEnd > max) {
  269. newEnd = max;
  270. }
  271. }
  272. }
  273. }
  274. // prevent end > max
  275. if (max !== null) {
  276. if (newEnd > max) {
  277. diff = (newEnd - max);
  278. newStart -= diff;
  279. newEnd -= diff;
  280. // prevent start < min
  281. if (min != null) {
  282. if (newStart < min) {
  283. newStart = min;
  284. }
  285. }
  286. }
  287. }
  288. // prevent (end-start) < zoomMin
  289. if (this.options.zoomMin !== null) {
  290. var zoomMin = parseFloat(this.options.zoomMin);
  291. if (zoomMin < 0) {
  292. zoomMin = 0;
  293. }
  294. if ((newEnd - newStart) < zoomMin) {
  295. if ((this.end - this.start) === zoomMin && newStart > this.start && newEnd < this.end) {
  296. // ignore this action, we are already zoomed to the minimum
  297. newStart = this.start;
  298. newEnd = this.end;
  299. }
  300. else {
  301. // zoom to the minimum
  302. diff = (zoomMin - (newEnd - newStart));
  303. newStart -= diff / 2;
  304. newEnd += diff / 2;
  305. }
  306. }
  307. }
  308. // prevent (end-start) > zoomMax
  309. if (this.options.zoomMax !== null) {
  310. var zoomMax = parseFloat(this.options.zoomMax);
  311. if (zoomMax < 0) {
  312. zoomMax = 0;
  313. }
  314. if ((newEnd - newStart) > zoomMax) {
  315. if ((this.end - this.start) === zoomMax && newStart < this.start && newEnd > this.end) {
  316. // ignore this action, we are already zoomed to the maximum
  317. newStart = this.start;
  318. newEnd = this.end;
  319. }
  320. else {
  321. // zoom to the maximum
  322. diff = ((newEnd - newStart) - zoomMax);
  323. newStart += diff / 2;
  324. newEnd -= diff / 2;
  325. }
  326. }
  327. }
  328. var changed = (this.start != newStart || this.end != newEnd);
  329. // if the new range does NOT overlap with the old range, emit checkRangedItems to avoid not showing ranged items (ranged meaning has end time, not necessarily of type Range)
  330. if (!((newStart >= this.start && newStart <= this.end) || (newEnd >= this.start && newEnd <= this.end)) &&
  331. !((this.start >= newStart && this.start <= newEnd) || (this.end >= newStart && this.end <= newEnd) )) {
  332. this.body.emitter.emit('checkRangedItems');
  333. }
  334. this.start = newStart;
  335. this.end = newEnd;
  336. return changed;
  337. };
  338. /**
  339. * Retrieve the current range.
  340. * @return {Object} An object with start and end properties
  341. */
  342. Range.prototype.getRange = function() {
  343. return {
  344. start: this.start,
  345. end: this.end
  346. };
  347. };
  348. /**
  349. * Calculate the conversion offset and scale for current range, based on
  350. * the provided width
  351. * @param {Number} width
  352. * @returns {{offset: number, scale: number}} conversion
  353. */
  354. Range.prototype.conversion = function (width, totalHidden) {
  355. return Range.conversion(this.start, this.end, width, totalHidden);
  356. };
  357. /**
  358. * Static method to calculate the conversion offset and scale for a range,
  359. * based on the provided start, end, and width
  360. * @param {Number} start
  361. * @param {Number} end
  362. * @param {Number} width
  363. * @returns {{offset: number, scale: number}} conversion
  364. */
  365. Range.conversion = function (start, end, width, totalHidden) {
  366. if (totalHidden === undefined) {
  367. totalHidden = 0;
  368. }
  369. if (width != 0 && (end - start != 0)) {
  370. return {
  371. offset: start,
  372. scale: width / (end - start - totalHidden)
  373. }
  374. }
  375. else {
  376. return {
  377. offset: 0,
  378. scale: 1
  379. };
  380. }
  381. };
  382. /**
  383. * Start dragging horizontally or vertically
  384. * @param {Event} event
  385. * @private
  386. */
  387. Range.prototype._onDragStart = function(event) {
  388. this.deltaDifference = 0;
  389. this.previousDelta = 0;
  390. // only allow dragging when configured as movable
  391. if (!this.options.moveable) return;
  392. // only start dragging when the mouse is inside the current range
  393. if (!this._isInsideRange(event)) return;
  394. // refuse to drag when we where pinching to prevent the timeline make a jump
  395. // when releasing the fingers in opposite order from the touch screen
  396. if (!this.props.touch.allowDragging) return;
  397. this.stopRolling();
  398. this.props.touch.start = this.start;
  399. this.props.touch.end = this.end;
  400. this.props.touch.dragging = true;
  401. if (this.body.dom.root) {
  402. this.body.dom.root.style.cursor = 'move';
  403. }
  404. };
  405. /**
  406. * Perform dragging operation
  407. * @param {Event} event
  408. * @private
  409. */
  410. Range.prototype._onDrag = function (event) {
  411. if (!event) return
  412. if (!this.props.touch.dragging) return;
  413. // only allow dragging when configured as movable
  414. if (!this.options.moveable) return;
  415. // TODO: this may be redundant in hammerjs2
  416. // refuse to drag when we where pinching to prevent the timeline make a jump
  417. // when releasing the fingers in opposite order from the touch screen
  418. if (!this.props.touch.allowDragging) return;
  419. var direction = this.options.direction;
  420. validateDirection(direction);
  421. var delta = (direction == 'horizontal') ? event.deltaX : event.deltaY;
  422. delta -= this.deltaDifference;
  423. var interval = (this.props.touch.end - this.props.touch.start);
  424. // normalize dragging speed if cutout is in between.
  425. var duration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  426. interval -= duration;
  427. var width = (direction == 'horizontal') ? this.body.domProps.center.width : this.body.domProps.center.height;
  428. if (this.options.rtl) {
  429. var diffRange = delta / width * interval;
  430. } else {
  431. var diffRange = -delta / width * interval;
  432. }
  433. var newStart = this.props.touch.start + diffRange;
  434. var newEnd = this.props.touch.end + diffRange;
  435. // snapping times away from hidden zones
  436. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, this.previousDelta-delta, true);
  437. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, this.previousDelta-delta, true);
  438. if (safeStart != newStart || safeEnd != newEnd) {
  439. this.deltaDifference += delta;
  440. this.props.touch.start = safeStart;
  441. this.props.touch.end = safeEnd;
  442. this._onDrag(event);
  443. return;
  444. }
  445. this.previousDelta = delta;
  446. this._applyRange(newStart, newEnd);
  447. var startDate = new Date(this.start);
  448. var endDate = new Date(this.end);
  449. // fire a rangechange event
  450. this.body.emitter.emit('rangechange', {
  451. start: startDate,
  452. end: endDate,
  453. byUser: true,
  454. event: util.elementsCensor(event)
  455. });
  456. // fire a panmove event
  457. this.body.emitter.emit('panmove');
  458. };
  459. /**
  460. * Stop dragging operation
  461. * @param {event} event
  462. * @private
  463. */
  464. Range.prototype._onDragEnd = function (event) {
  465. if (!this.props.touch.dragging) return;
  466. // only allow dragging when configured as movable
  467. if (!this.options.moveable) return;
  468. // TODO: this may be redundant in hammerjs2
  469. // refuse to drag when we where pinching to prevent the timeline make a jump
  470. // when releasing the fingers in opposite order from the touch screen
  471. if (!this.props.touch.allowDragging) return;
  472. this.props.touch.dragging = false;
  473. if (this.body.dom.root) {
  474. this.body.dom.root.style.cursor = 'auto';
  475. }
  476. // fire a rangechanged event
  477. this.body.emitter.emit('rangechanged', {
  478. start: new Date(this.start),
  479. end: new Date(this.end),
  480. byUser: true,
  481. event: util.elementsCensor(event)
  482. });
  483. };
  484. /**
  485. * Event handler for mouse wheel event, used to zoom
  486. * Code from http://adomas.org/javascript-mouse-wheel/
  487. * @param {Event} event
  488. * @private
  489. */
  490. Range.prototype._onMouseWheel = function(event) {
  491. // retrieve delta
  492. var delta = 0;
  493. if (event.wheelDelta) { /* IE/Opera. */
  494. delta = event.wheelDelta / 120;
  495. } else if (event.detail) { /* Mozilla case. */
  496. // In Mozilla, sign of delta is different than in IE.
  497. // Also, delta is multiple of 3.
  498. delta = -event.detail / 3;
  499. }
  500. // don't allow zoom when the according key is pressed and the zoomKey option or not zoomable but movable
  501. if ((this.options.zoomKey && !event[this.options.zoomKey] && this.options.zoomable)
  502. || (!this.options.zoomable && this.options.moveable)) {
  503. if (this.options.horizontalScroll) {
  504. // Prevent default actions caused by mouse wheel
  505. // (else the page and timeline both scroll)
  506. event.preventDefault();
  507. // calculate a single scroll jump relative to the range scale
  508. var diff = delta * (this.end - this.start) / 20;
  509. // calculate new start and end
  510. var newStart = this.start - diff;
  511. var newEnd = this.end - diff;
  512. this.setRange(newStart, newEnd, false, true, event);
  513. }
  514. return;
  515. }
  516. // only allow zooming when configured as zoomable and moveable
  517. if (!(this.options.zoomable && this.options.moveable)) return;
  518. // only zoom when the mouse is inside the current range
  519. if (!this._isInsideRange(event)) return;
  520. // If delta is nonzero, handle it.
  521. // Basically, delta is now positive if wheel was scrolled up,
  522. // and negative, if wheel was scrolled down.
  523. if (delta) {
  524. // perform the zoom action. Delta is normally 1 or -1
  525. // adjust a negative delta such that zooming in with delta 0.1
  526. // equals zooming out with a delta -0.1
  527. var scale;
  528. if (delta < 0) {
  529. scale = 1 - (delta / 5);
  530. }
  531. else {
  532. scale = 1 / (1 + (delta / 5)) ;
  533. }
  534. // calculate center, the date to zoom around
  535. var pointerDate
  536. if (this.rolling) {
  537. pointerDate = (this.start + this.end) / 2;
  538. } else {
  539. var pointer = this.getPointer({x: event.clientX, y: event.clientY}, this.body.dom.center);
  540. pointerDate = this._pointerToDate(pointer);
  541. }
  542. this.zoom(scale, pointerDate, delta, event);
  543. // Prevent default actions caused by mouse wheel
  544. // (else the page and timeline both scroll)
  545. event.preventDefault();
  546. }
  547. };
  548. /**
  549. * Start of a touch gesture
  550. * @private
  551. */
  552. Range.prototype._onTouch = function (event) {
  553. this.props.touch.start = this.start;
  554. this.props.touch.end = this.end;
  555. this.props.touch.allowDragging = true;
  556. this.props.touch.center = null;
  557. this.scaleOffset = 0;
  558. this.deltaDifference = 0;
  559. };
  560. /**
  561. * Handle pinch event
  562. * @param {Event} event
  563. * @private
  564. */
  565. Range.prototype._onPinch = function (event) {
  566. // only allow zooming when configured as zoomable and moveable
  567. if (!(this.options.zoomable && this.options.moveable)) return;
  568. this.props.touch.allowDragging = false;
  569. if (!this.props.touch.center) {
  570. this.props.touch.center = this.getPointer(event.center, this.body.dom.center);
  571. }
  572. this.stopRolling();
  573. var scale = 1 / (event.scale + this.scaleOffset);
  574. var centerDate = this._pointerToDate(this.props.touch.center);
  575. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  576. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.options.moment, this.body.hiddenDates, this, centerDate);
  577. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  578. // calculate new start and end
  579. var newStart = (centerDate - hiddenDurationBefore) + (this.props.touch.start - (centerDate - hiddenDurationBefore)) * scale;
  580. var newEnd = (centerDate + hiddenDurationAfter) + (this.props.touch.end - (centerDate + hiddenDurationAfter)) * scale;
  581. // snapping times away from hidden zones
  582. this.startToFront = 1 - scale <= 0; // used to do the right auto correction with periodic hidden times
  583. this.endToFront = scale - 1 <= 0; // used to do the right auto correction with periodic hidden times
  584. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, 1 - scale, true);
  585. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, scale - 1, true);
  586. if (safeStart != newStart || safeEnd != newEnd) {
  587. this.props.touch.start = safeStart;
  588. this.props.touch.end = safeEnd;
  589. this.scaleOffset = 1 - event.scale;
  590. newStart = safeStart;
  591. newEnd = safeEnd;
  592. }
  593. this.setRange(newStart, newEnd, false, true, event);
  594. this.startToFront = false; // revert to default
  595. this.endToFront = true; // revert to default
  596. };
  597. /**
  598. * Test whether the mouse from a mouse event is inside the visible window,
  599. * between the current start and end date
  600. * @param {Object} event
  601. * @return {boolean} Returns true when inside the visible window
  602. * @private
  603. */
  604. Range.prototype._isInsideRange = function(event) {
  605. // calculate the time where the mouse is, check whether inside
  606. // and no scroll action should happen.
  607. var clientX = event.center ? event.center.x : event.clientX;
  608. if (this.options.rtl) {
  609. var x = clientX - util.getAbsoluteLeft(this.body.dom.centerContainer);
  610. } else {
  611. var x = util.getAbsoluteRight(this.body.dom.centerContainer) - clientX;
  612. }
  613. var time = this.body.util.toTime(x);
  614. return time >= this.start && time <= this.end;
  615. };
  616. /**
  617. * Helper function to calculate the center date for zooming
  618. * @param {{x: Number, y: Number}} pointer
  619. * @return {number} date
  620. * @private
  621. */
  622. Range.prototype._pointerToDate = function (pointer) {
  623. var conversion;
  624. var direction = this.options.direction;
  625. validateDirection(direction);
  626. if (direction == 'horizontal') {
  627. return this.body.util.toTime(pointer.x).valueOf();
  628. }
  629. else {
  630. var height = this.body.domProps.center.height;
  631. conversion = this.conversion(height);
  632. return pointer.y / conversion.scale + conversion.offset;
  633. }
  634. };
  635. /**
  636. * Get the pointer location relative to the location of the dom element
  637. * @param {{x: Number, y: Number}} touch
  638. * @param {Element} element HTML DOM element
  639. * @return {{x: Number, y: Number}} pointer
  640. * @private
  641. */
  642. Range.prototype.getPointer = function (touch, element) {
  643. if (this.options.rtl) {
  644. return {
  645. x: util.getAbsoluteRight(element) - touch.x,
  646. y: touch.y - util.getAbsoluteTop(element)
  647. };
  648. } else {
  649. return {
  650. x: touch.x - util.getAbsoluteLeft(element),
  651. y: touch.y - util.getAbsoluteTop(element)
  652. };
  653. }
  654. }
  655. /**
  656. * Zoom the range the given scale in or out. Start and end date will
  657. * be adjusted, and the timeline will be redrawn. You can optionally give a
  658. * date around which to zoom.
  659. * For example, try scale = 0.9 or 1.1
  660. * @param {Number} scale Scaling factor. Values above 1 will zoom out,
  661. * values below 1 will zoom in.
  662. * @param {Number} [center] Value representing a date around which will
  663. * be zoomed.
  664. */
  665. Range.prototype.zoom = function(scale, center, delta, event) {
  666. // if centerDate is not provided, take it half between start Date and end Date
  667. if (center == null) {
  668. center = (this.start + this.end) / 2;
  669. }
  670. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  671. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.options.moment, this.body.hiddenDates, this, center);
  672. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  673. // calculate new start and end
  674. var newStart = (center-hiddenDurationBefore) + (this.start - (center-hiddenDurationBefore)) * scale;
  675. var newEnd = (center+hiddenDurationAfter) + (this.end - (center+hiddenDurationAfter)) * scale;
  676. // snapping times away from hidden zones
  677. this.startToFront = delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  678. this.endToFront = -delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  679. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, delta, true);
  680. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, -delta, true);
  681. if (safeStart != newStart || safeEnd != newEnd) {
  682. newStart = safeStart;
  683. newEnd = safeEnd;
  684. }
  685. this.setRange(newStart, newEnd, false, true, event);
  686. this.startToFront = false; // revert to default
  687. this.endToFront = true; // revert to default
  688. };
  689. /**
  690. * Move the range with a given delta to the left or right. Start and end
  691. * value will be adjusted. For example, try delta = 0.1 or -0.1
  692. * @param {Number} delta Moving amount. Positive value will move right,
  693. * negative value will move left
  694. */
  695. Range.prototype.move = function(delta) {
  696. // zoom start Date and end Date relative to the centerDate
  697. var diff = (this.end - this.start);
  698. // apply new values
  699. var newStart = this.start + diff * delta;
  700. var newEnd = this.end + diff * delta;
  701. // TODO: reckon with min and max range
  702. this.start = newStart;
  703. this.end = newEnd;
  704. };
  705. /**
  706. * Move the range to a new center point
  707. * @param {Number} moveTo New center point of the range
  708. */
  709. Range.prototype.moveTo = function(moveTo) {
  710. var center = (this.start + this.end) / 2;
  711. var diff = center - moveTo;
  712. // calculate new start and end
  713. var newStart = this.start - diff;
  714. var newEnd = this.end - diff;
  715. this.setRange(newStart, newEnd, false, true, event);
  716. };
  717. module.exports = Range;