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.

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