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.

680 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. };
  332. /**
  333. * Perform dragging operation
  334. * @param {Event} event
  335. * @private
  336. */
  337. Range.prototype._onDrag = function (event) {
  338. // only allow dragging when configured as movable
  339. if (!this.options.moveable) return;
  340. // TODO: this may be redundant in hammerjs2
  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.deltaX : event.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. // TODO: this may be redundant in hammerjs2
  384. // refuse to drag when we where pinching to prevent the timeline make a jump
  385. // when releasing the fingers in opposite order from the touch screen
  386. if (!this.props.touch.allowDragging) return;
  387. this.props.touch.dragging = false;
  388. if (this.body.dom.root) {
  389. this.body.dom.root.style.cursor = 'auto';
  390. }
  391. // fire a rangechanged event
  392. this.body.emitter.emit('rangechanged', {
  393. start: new Date(this.start),
  394. end: new Date(this.end),
  395. byUser: true
  396. });
  397. };
  398. /**
  399. * Event handler for mouse wheel event, used to zoom
  400. * Code from http://adomas.org/javascript-mouse-wheel/
  401. * @param {Event} event
  402. * @private
  403. */
  404. Range.prototype._onMouseWheel = function(event) {
  405. // only allow zooming when configured as zoomable and moveable
  406. if (!(this.options.zoomable && this.options.moveable)) return;
  407. // retrieve delta
  408. var delta = 0;
  409. if (event.wheelDelta) { /* IE/Opera. */
  410. delta = event.wheelDelta / 120;
  411. } else if (event.detail) { /* Mozilla case. */
  412. // In Mozilla, sign of delta is different than in IE.
  413. // Also, delta is multiple of 3.
  414. delta = -event.detail / 3;
  415. }
  416. // If delta is nonzero, handle it.
  417. // Basically, delta is now positive if wheel was scrolled up,
  418. // and negative, if wheel was scrolled down.
  419. if (delta) {
  420. // perform the zoom action. Delta is normally 1 or -1
  421. // adjust a negative delta such that zooming in with delta 0.1
  422. // equals zooming out with a delta -0.1
  423. var scale;
  424. if (delta < 0) {
  425. scale = 1 - (delta / 5);
  426. }
  427. else {
  428. scale = 1 / (1 + (delta / 5)) ;
  429. }
  430. // calculate center, the date to zoom around
  431. var pointer = getPointer({x: event.pageX, y: event.pageY}, this.body.dom.center);
  432. var 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 (!this.props.touch.center) {
  468. this.props.touch.center = getPointer(event.center, this.body.dom.center);
  469. }
  470. var scale = 1 / (event.scale + this.scaleOffset);
  471. var centerDate = this._pointerToDate(this.props.touch.center);
  472. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  473. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.body.hiddenDates, this, centerDate);
  474. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  475. // calculate new start and end
  476. var newStart = (centerDate - hiddenDurationBefore) + (this.props.touch.start - (centerDate - hiddenDurationBefore)) * scale;
  477. var newEnd = (centerDate + hiddenDurationAfter) + (this.props.touch.end - (centerDate + hiddenDurationAfter)) * scale;
  478. // snapping times away from hidden zones
  479. this.startToFront = 1 - scale <= 0; // used to do the right auto correction with periodic hidden times
  480. this.endToFront = scale - 1 <= 0; // used to do the right auto correction with periodic hidden times
  481. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, 1 - scale, true);
  482. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, scale - 1, true);
  483. if (safeStart != newStart || safeEnd != newEnd) {
  484. this.props.touch.start = safeStart;
  485. this.props.touch.end = safeEnd;
  486. this.scaleOffset = 1 - event.scale;
  487. newStart = safeStart;
  488. newEnd = safeEnd;
  489. }
  490. this.setRange(newStart, newEnd, false, true);
  491. this.startToFront = false; // revert to default
  492. this.endToFront = true; // revert to default
  493. };
  494. /**
  495. * Helper function to calculate the center date for zooming
  496. * @param {{x: Number, y: Number}} pointer
  497. * @return {number} date
  498. * @private
  499. */
  500. Range.prototype._pointerToDate = function (pointer) {
  501. var conversion;
  502. var direction = this.options.direction;
  503. validateDirection(direction);
  504. if (direction == 'horizontal') {
  505. return this.body.util.toTime(pointer.x).valueOf();
  506. }
  507. else {
  508. var height = this.body.domProps.center.height;
  509. conversion = this.conversion(height);
  510. return pointer.y / conversion.scale + conversion.offset;
  511. }
  512. };
  513. /**
  514. * Get the pointer location relative to the location of the dom element
  515. * @param {{x: Number, y: Number}} touch
  516. * @param {Element} element HTML DOM element
  517. * @return {{x: Number, y: Number}} pointer
  518. * @private
  519. */
  520. function getPointer (touch, element) {
  521. return {
  522. x: touch.x - util.getAbsoluteLeft(element),
  523. y: touch.y - util.getAbsoluteTop(element)
  524. };
  525. }
  526. /**
  527. * Zoom the range the given scale in or out. Start and end date will
  528. * be adjusted, and the timeline will be redrawn. You can optionally give a
  529. * date around which to zoom.
  530. * For example, try scale = 0.9 or 1.1
  531. * @param {Number} scale Scaling factor. Values above 1 will zoom out,
  532. * values below 1 will zoom in.
  533. * @param {Number} [center] Value representing a date around which will
  534. * be zoomed.
  535. */
  536. Range.prototype.zoom = function(scale, center, delta) {
  537. // if centerDate is not provided, take it half between start Date and end Date
  538. if (center == null) {
  539. center = (this.start + this.end) / 2;
  540. }
  541. var hiddenDuration = DateUtil.getHiddenDurationBetween(this.body.hiddenDates, this.start, this.end);
  542. var hiddenDurationBefore = DateUtil.getHiddenDurationBefore(this.body.hiddenDates, this, center);
  543. var hiddenDurationAfter = hiddenDuration - hiddenDurationBefore;
  544. // calculate new start and end
  545. var newStart = (center-hiddenDurationBefore) + (this.start - (center-hiddenDurationBefore)) * scale;
  546. var newEnd = (center+hiddenDurationAfter) + (this.end - (center+hiddenDurationAfter)) * scale;
  547. // snapping times away from hidden zones
  548. this.startToFront = delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  549. this.endToFront = -delta > 0 ? false : true; // used to do the right autocorrection with periodic hidden times
  550. var safeStart = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newStart, delta, true);
  551. var safeEnd = DateUtil.snapAwayFromHidden(this.body.hiddenDates, newEnd, -delta, true);
  552. if (safeStart != newStart || safeEnd != newEnd) {
  553. newStart = safeStart;
  554. newEnd = safeEnd;
  555. }
  556. this.setRange(newStart, newEnd, false, true);
  557. this.startToFront = false; // revert to default
  558. this.endToFront = true; // revert to default
  559. };
  560. /**
  561. * Move the range with a given delta to the left or right. Start and end
  562. * value will be adjusted. For example, try delta = 0.1 or -0.1
  563. * @param {Number} delta Moving amount. Positive value will move right,
  564. * negative value will move left
  565. */
  566. Range.prototype.move = function(delta) {
  567. // zoom start Date and end Date relative to the centerDate
  568. var diff = (this.end - this.start);
  569. // apply new values
  570. var newStart = this.start + diff * delta;
  571. var newEnd = this.end + diff * delta;
  572. // TODO: reckon with min and max range
  573. this.start = newStart;
  574. this.end = newEnd;
  575. };
  576. /**
  577. * Move the range to a new center point
  578. * @param {Number} moveTo New center point of the range
  579. */
  580. Range.prototype.moveTo = function(moveTo) {
  581. var center = (this.start + this.end) / 2;
  582. var diff = center - moveTo;
  583. // calculate new start and end
  584. var newStart = this.start - diff;
  585. var newEnd = this.end - diff;
  586. this.setRange(newStart, newEnd);
  587. };
  588. module.exports = Range;