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.

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