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.

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