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.

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