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.

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