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.

746 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 (!this.props.touch.dragging) return;
  352. // only allow dragging when configured as movable
  353. if (!this.options.moveable) return;
  354. // TODO: this may be redundant in hammerjs2
  355. // refuse to drag when we where pinching to prevent the timeline make a jump
  356. // when releasing the fingers in opposite order from the touch screen
  357. if (!this.props.touch.allowDragging) return;
  358. var direction = this.options.direction;
  359. validateDirection(direction);
  360. var delta = (direction == 'horizontal') ? event.deltaX : event.deltaY;
  361. delta -= this.deltaDifference;
  362. var interval = (this.props.touch.end - this.props.touch.start);
  363. // normalize dragging speed if cutout is in between.
  364. var duration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  365. interval -= duration;
  366. var width = (direction == 'horizontal') ? this.body.domProps.center.width : this.body.domProps.center.height;
  367. if (this.options.rtl) {
  368. var diffRange = delta / width * interval;
  369. } else {
  370. var diffRange = -delta / width * interval;
  371. }
  372. var newStart = this.props.touch.start + diffRange;
  373. var newEnd = this.props.touch.end + diffRange;
  374. // snapping times away from hidden zones
  375. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, this.previousDelta-delta, true);
  376. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, this.previousDelta-delta, true);
  377. if (safeStart != newStart || safeEnd != newEnd) {
  378. this.deltaDifference += delta;
  379. this.props.touch.start = safeStart;
  380. this.props.touch.end = safeEnd;
  381. this._onDrag(event);
  382. return;
  383. }
  384. this.previousDelta = delta;
  385. this._applyRange(newStart, newEnd);
  386. var startDate = new Date(this.start);
  387. var endDate = new Date(this.end);
  388. // fire a rangechange event
  389. this.body.emitter.emit('rangechange', {
  390. start: startDate,
  391. end: endDate,
  392. byUser: true
  393. });
  394. };
  395. /**
  396. * Stop dragging operation
  397. * @param {event} event
  398. * @private
  399. */
  400. Range.prototype._onDragEnd = function (event) {
  401. if (!this.props.touch.dragging) return;
  402. // only allow dragging when configured as movable
  403. if (!this.options.moveable) return;
  404. // TODO: this may be redundant in hammerjs2
  405. // refuse to drag when we where pinching to prevent the timeline make a jump
  406. // when releasing the fingers in opposite order from the touch screen
  407. if (!this.props.touch.allowDragging) return;
  408. this.props.touch.dragging = false;
  409. if (this.body.dom.root) {
  410. this.body.dom.root.style.cursor = 'auto';
  411. }
  412. // fire a rangechanged event
  413. this.body.emitter.emit('rangechanged', {
  414. start: new Date(this.start),
  415. end: new Date(this.end),
  416. byUser: true
  417. });
  418. };
  419. /**
  420. * Event handler for mouse wheel event, used to zoom
  421. * Code from http://adomas.org/javascript-mouse-wheel/
  422. * @param {Event} event
  423. * @private
  424. */
  425. Range.prototype._onMouseWheel = function(event) {
  426. // Prevent default actions caused by mouse wheel
  427. // (else the page and timeline both zoom and scroll)
  428. event.preventDefault();
  429. // retrieve delta
  430. var delta = 0;
  431. if (event.wheelDelta) { /* IE/Opera. */
  432. delta = event.wheelDelta / 120;
  433. } else if (event.detail) { /* Mozilla case. */
  434. // In Mozilla, sign of delta is different than in IE.
  435. // Also, delta is multiple of 3.
  436. delta = -event.detail / 3;
  437. }
  438. // don't allow zoom when the according key is pressed and the zoomKey option or not zoomable but movable
  439. if ((this.options.zoomKey && !event[this.options.zoomKey] && this.options.zoomable)
  440. || (!this.options.zoomable && this.options.moveable)) {
  441. if (this.options.horizontalScroll) {
  442. // calculate a single scroll jump relative to the range scale
  443. var diff = delta * (this.end - this.start) / 20;
  444. // calculate new start and end
  445. var newStart = this.start - diff;
  446. var newEnd = this.end - diff;
  447. this.setRange(newStart, newEnd);
  448. }
  449. return;
  450. }
  451. // only allow zooming when configured as zoomable and moveable
  452. if (!(this.options.zoomable && this.options.moveable)) return;
  453. // only zoom when the mouse is inside the current range
  454. if (!this._isInsideRange(event)) return;
  455. // If delta is nonzero, handle it.
  456. // Basically, delta is now positive if wheel was scrolled up,
  457. // and negative, if wheel was scrolled down.
  458. if (delta) {
  459. // perform the zoom action. Delta is normally 1 or -1
  460. // adjust a negative delta such that zooming in with delta 0.1
  461. // equals zooming out with a delta -0.1
  462. var scale;
  463. if (delta < 0) {
  464. scale = 1 - (delta / 5);
  465. }
  466. else {
  467. scale = 1 / (1 + (delta / 5)) ;
  468. }
  469. // calculate center, the date to zoom around
  470. var pointer = this.getPointer({x: event.clientX, y: event.clientY}, this.body.dom.center);
  471. var pointerDate = this._pointerToDate(pointer);
  472. this.zoom(scale, pointerDate, delta);
  473. }
  474. };
  475. /**
  476. * Start of a touch gesture
  477. * @private
  478. */
  479. Range.prototype._onTouch = function (event) {
  480. this.props.touch.start = this.start;
  481. this.props.touch.end = this.end;
  482. this.props.touch.allowDragging = true;
  483. this.props.touch.center = null;
  484. this.scaleOffset = 0;
  485. this.deltaDifference = 0;
  486. };
  487. /**
  488. * Handle pinch event
  489. * @param {Event} event
  490. * @private
  491. */
  492. Range.prototype._onPinch = function (event) {
  493. // only allow zooming when configured as zoomable and moveable
  494. if (!(this.options.zoomable && this.options.moveable)) return;
  495. this.props.touch.allowDragging = false;
  496. if (!this.props.touch.center) {
  497. this.props.touch.center = this.getPointer(event.center, this.body.dom.center);
  498. }
  499. var scale = 1 / (event.scale + this.scaleOffset);
  500. var centerDate = this._pointerToDate(this.props.touch.center);
  501. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  502. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.options.moment, this.body.hiddenDates, this, centerDate);
  503. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  504. // calculate new start and end
  505. var newStart = (centerDate - hiddenDurationBefore) + (this.props.touch.start - (centerDate - hiddenDurationBefore)) * scale;
  506. var newEnd = (centerDate + hiddenDurationAfter) + (this.props.touch.end - (centerDate + hiddenDurationAfter)) * scale;
  507. // snapping times away from hidden zones
  508. this.startToFront = 1 - scale <= 0; // used to do the right auto correction with periodic hidden times
  509. this.endToFront = scale - 1 <= 0; // used to do the right auto correction with periodic hidden times
  510. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, 1 - scale, true);
  511. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, scale - 1, true);
  512. if (safeStart != newStart || safeEnd != newEnd) {
  513. this.props.touch.start = safeStart;
  514. this.props.touch.end = safeEnd;
  515. this.scaleOffset = 1 - event.scale;
  516. newStart = safeStart;
  517. newEnd = safeEnd;
  518. }
  519. this.setRange(newStart, newEnd, false, true);
  520. this.startToFront = false; // revert to default
  521. this.endToFront = true; // revert to default
  522. };
  523. /**
  524. * Test whether the mouse from a mouse event is inside the visible window,
  525. * between the current start and end date
  526. * @param {Object} event
  527. * @return {boolean} Returns true when inside the visible window
  528. * @private
  529. */
  530. Range.prototype._isInsideRange = function(event) {
  531. // calculate the time where the mouse is, check whether inside
  532. // and no scroll action should happen.
  533. var clientX = event.center ? event.center.x : event.clientX;
  534. if (this.options.rtl) {
  535. var x = clientX - util.getAbsoluteLeft(this.body.dom.centerContainer);
  536. } else {
  537. var x = util.getAbsoluteRight(this.body.dom.centerContainer) - clientX;
  538. }
  539. var time = this.body.util.toTime(x);
  540. return time >= this.start && time <= this.end;
  541. };
  542. /**
  543. * Helper function to calculate the center date for zooming
  544. * @param {{x: Number, y: Number}} pointer
  545. * @return {number} date
  546. * @private
  547. */
  548. Range.prototype._pointerToDate = function (pointer) {
  549. var conversion;
  550. var direction = this.options.direction;
  551. validateDirection(direction);
  552. if (direction == 'horizontal') {
  553. return this.body.util.toTime(pointer.x).valueOf();
  554. }
  555. else {
  556. var height = this.body.domProps.center.height;
  557. conversion = this.conversion(height);
  558. return pointer.y / conversion.scale + conversion.offset;
  559. }
  560. };
  561. /**
  562. * Get the pointer location relative to the location of the dom element
  563. * @param {{x: Number, y: Number}} touch
  564. * @param {Element} element HTML DOM element
  565. * @return {{x: Number, y: Number}} pointer
  566. * @private
  567. */
  568. Range.prototype.getPointer = function (touch, element) {
  569. if (this.options.rtl) {
  570. return {
  571. x: util.getAbsoluteRight(element) - touch.x,
  572. y: touch.y - util.getAbsoluteTop(element)
  573. };
  574. } else {
  575. return {
  576. x: touch.x - util.getAbsoluteLeft(element),
  577. y: touch.y - util.getAbsoluteTop(element)
  578. };
  579. }
  580. }
  581. /**
  582. * Zoom the range the given scale in or out. Start and end date will
  583. * be adjusted, and the timeline will be redrawn. You can optionally give a
  584. * date around which to zoom.
  585. * For example, try scale = 0.9 or 1.1
  586. * @param {Number} scale Scaling factor. Values above 1 will zoom out,
  587. * values below 1 will zoom in.
  588. * @param {Number} [center] Value representing a date around which will
  589. * be zoomed.
  590. */
  591. Range.prototype.zoom = function(scale, center, delta) {
  592. // if centerDate is not provided, take it half between start Date and end Date
  593. if (center == null) {
  594. center = (this.start + this.end) / 2;
  595. }
  596. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  597. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.options.moment, this.body.hiddenDates, this, center);
  598. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  599. // calculate new start and end
  600. var newStart = (center-hiddenDurationBefore) + (this.start - (center-hiddenDurationBefore)) * scale;
  601. var newEnd = (center+hiddenDurationAfter) + (this.end - (center+hiddenDurationAfter)) * scale;
  602. // snapping times away from hidden zones
  603. this.startToFront = delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  604. this.endToFront = -delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  605. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, delta, true);
  606. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, -delta, true);
  607. if (safeStart != newStart || safeEnd != newEnd) {
  608. newStart = safeStart;
  609. newEnd = safeEnd;
  610. }
  611. this.setRange(newStart, newEnd, false, true);
  612. this.startToFront = false; // revert to default
  613. this.endToFront = true; // revert to default
  614. };
  615. /**
  616. * Move the range with a given delta to the left or right. Start and end
  617. * value will be adjusted. For example, try delta = 0.1 or -0.1
  618. * @param {Number} delta Moving amount. Positive value will move right,
  619. * negative value will move left
  620. */
  621. Range.prototype.move = function(delta) {
  622. // zoom start Date and end Date relative to the centerDate
  623. var diff = (this.end - this.start);
  624. // apply new values
  625. var newStart = this.start + diff * delta;
  626. var newEnd = this.end + diff * delta;
  627. // TODO: reckon with min and max range
  628. this.start = newStart;
  629. this.end = newEnd;
  630. };
  631. /**
  632. * Move the range to a new center point
  633. * @param {Number} moveTo New center point of the range
  634. */
  635. Range.prototype.moveTo = function(moveTo) {
  636. var center = (this.start + this.end) / 2;
  637. var diff = center - moveTo;
  638. // calculate new start and end
  639. var newStart = this.start - diff;
  640. var newEnd = this.end - diff;
  641. this.setRange(newStart, newEnd);
  642. };
  643. module.exports = Range;