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.

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