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.

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