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.

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