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.

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