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.

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