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.

709 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
9 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
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', 'zoomKey'];
  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. // only zoom when the according key is pressed and the zoomKey option is set
  420. if (this.options.zoomKey && !event[this.options.zoomKey]) return;
  421. // retrieve delta
  422. var delta = 0;
  423. if (event.wheelDelta) { /* IE/Opera. */
  424. delta = event.wheelDelta / 120;
  425. } else if (event.detail) { /* Mozilla case. */
  426. // In Mozilla, sign of delta is different than in IE.
  427. // Also, delta is multiple of 3.
  428. delta = -event.detail / 3;
  429. }
  430. // If delta is nonzero, handle it.
  431. // Basically, delta is now positive if wheel was scrolled up,
  432. // and negative, if wheel was scrolled down.
  433. if (delta) {
  434. // perform the zoom action. Delta is normally 1 or -1
  435. // adjust a negative delta such that zooming in with delta 0.1
  436. // equals zooming out with a delta -0.1
  437. var scale;
  438. if (delta < 0) {
  439. scale = 1 - (delta / 5);
  440. }
  441. else {
  442. scale = 1 / (1 + (delta / 5)) ;
  443. }
  444. // calculate center, the date to zoom around
  445. var pointer = getPointer({x: event.clientX, y: event.clientY}, this.body.dom.center);
  446. var pointerDate = this._pointerToDate(pointer);
  447. this.zoom(scale, pointerDate, delta);
  448. }
  449. // Prevent default actions caused by mouse wheel
  450. // (else the page and timeline both zoom and scroll)
  451. event.preventDefault();
  452. };
  453. /**
  454. * Start of a touch gesture
  455. * @private
  456. */
  457. Range.prototype._onTouch = function (event) {
  458. this.props.touch.start = this.start;
  459. this.props.touch.end = this.end;
  460. this.props.touch.allowDragging = true;
  461. this.props.touch.center = null;
  462. this.scaleOffset = 0;
  463. this.deltaDifference = 0;
  464. };
  465. /**
  466. * Handle pinch event
  467. * @param {Event} event
  468. * @private
  469. */
  470. Range.prototype._onPinch = function (event) {
  471. // only allow zooming when configured as zoomable and moveable
  472. if (!(this.options.zoomable && this.options.moveable)) return;
  473. this.props.touch.allowDragging = false;
  474. if (!this.props.touch.center) {
  475. this.props.touch.center = getPointer(event.center, this.body.dom.center);
  476. }
  477. var scale = 1 / (event.scale + this.scaleOffset);
  478. var centerDate = this._pointerToDate(this.props.touch.center);
  479. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  480. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.body.hiddenDates, this, centerDate);
  481. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  482. // calculate new start and end
  483. var newStart = (centerDate - hiddenDurationBefore) + (this.props.touch.start - (centerDate - hiddenDurationBefore)) * scale;
  484. var newEnd = (centerDate + hiddenDurationAfter) + (this.props.touch.end - (centerDate + hiddenDurationAfter)) * scale;
  485. // snapping times away from hidden zones
  486. this.startToFront = 1 - scale <= 0; // used to do the right auto correction with periodic hidden times
  487. this.endToFront = scale - 1 <= 0; // used to do the right auto correction with periodic hidden times
  488. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, 1 - scale, true);
  489. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, scale - 1, true);
  490. if (safeStart != newStart || safeEnd != newEnd) {
  491. this.props.touch.start = safeStart;
  492. this.props.touch.end = safeEnd;
  493. this.scaleOffset = 1 - event.scale;
  494. newStart = safeStart;
  495. newEnd = safeEnd;
  496. }
  497. this.setRange(newStart, newEnd, false, true);
  498. this.startToFront = false; // revert to default
  499. this.endToFront = true; // revert to default
  500. };
  501. /**
  502. * Test whether the mouse from a mouse event is inside the visible window,
  503. * between the current start and end date
  504. * @param {Object} event
  505. * @return {boolean} Returns true when inside the visible window
  506. * @private
  507. */
  508. Range.prototype._isInsideRange = function(event) {
  509. // calculate the time where the mouse is, check whether inside
  510. // and no scroll action should happen.
  511. var clientX = event.center ? event.center.x : event.clientX;
  512. var x = clientX - util.getAbsoluteLeft(this.body.dom.centerContainer);
  513. var time = this.body.util.toTime(x);
  514. return time >= this.start && time <= this.end;
  515. };
  516. /**
  517. * Helper function to calculate the center date for zooming
  518. * @param {{x: Number, y: Number}} pointer
  519. * @return {number} date
  520. * @private
  521. */
  522. Range.prototype._pointerToDate = function (pointer) {
  523. var conversion;
  524. var direction = this.options.direction;
  525. validateDirection(direction);
  526. if (direction == 'horizontal') {
  527. return this.body.util.toTime(pointer.x).valueOf();
  528. }
  529. else {
  530. var height = this.body.domProps.center.height;
  531. conversion = this.conversion(height);
  532. return pointer.y / conversion.scale + conversion.offset;
  533. }
  534. };
  535. /**
  536. * Get the pointer location relative to the location of the dom element
  537. * @param {{x: Number, y: Number}} touch
  538. * @param {Element} element HTML DOM element
  539. * @return {{x: Number, y: Number}} pointer
  540. * @private
  541. */
  542. function getPointer (touch, element) {
  543. return {
  544. x: touch.x - util.getAbsoluteLeft(element),
  545. y: touch.y - util.getAbsoluteTop(element)
  546. };
  547. }
  548. /**
  549. * Zoom the range the given scale in or out. Start and end date will
  550. * be adjusted, and the timeline will be redrawn. You can optionally give a
  551. * date around which to zoom.
  552. * For example, try scale = 0.9 or 1.1
  553. * @param {Number} scale Scaling factor. Values above 1 will zoom out,
  554. * values below 1 will zoom in.
  555. * @param {Number} [center] Value representing a date around which will
  556. * be zoomed.
  557. */
  558. Range.prototype.zoom = function(scale, center, delta) {
  559. // if centerDate is not provided, take it half between start Date and end Date
  560. if (center == null) {
  561. center = (this.start + this.end) / 2;
  562. }
  563. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  564. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.body.hiddenDates, this, center);
  565. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  566. // calculate new start and end
  567. var newStart = (center-hiddenDurationBefore) + (this.start - (center-hiddenDurationBefore)) * scale;
  568. var newEnd = (center+hiddenDurationAfter) + (this.end - (center+hiddenDurationAfter)) * scale;
  569. // snapping times away from hidden zones
  570. this.startToFront = delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  571. this.endToFront = -delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  572. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, delta, true);
  573. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, -delta, true);
  574. if (safeStart != newStart || safeEnd != newEnd) {
  575. newStart = safeStart;
  576. newEnd = safeEnd;
  577. }
  578. this.setRange(newStart, newEnd, false, true);
  579. this.startToFront = false; // revert to default
  580. this.endToFront = true; // revert to default
  581. };
  582. /**
  583. * Move the range with a given delta to the left or right. Start and end
  584. * value will be adjusted. For example, try delta = 0.1 or -0.1
  585. * @param {Number} delta Moving amount. Positive value will move right,
  586. * negative value will move left
  587. */
  588. Range.prototype.move = function(delta) {
  589. // zoom start Date and end Date relative to the centerDate
  590. var diff = (this.end - this.start);
  591. // apply new values
  592. var newStart = this.start + diff * delta;
  593. var newEnd = this.end + diff * delta;
  594. // TODO: reckon with min and max range
  595. this.start = newStart;
  596. this.end = newEnd;
  597. };
  598. /**
  599. * Move the range to a new center point
  600. * @param {Number} moveTo New center point of the range
  601. */
  602. Range.prototype.moveTo = function(moveTo) {
  603. var center = (this.start + this.end) / 2;
  604. var diff = center - moveTo;
  605. // calculate new start and end
  606. var newStart = this.start - diff;
  607. var newEnd = this.end - diff;
  608. this.setRange(newStart, newEnd);
  609. };
  610. module.exports = Range;