sliderMove.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Calculate slider move result.
  3. * Usage:
  4. * (1) If both handle0 and handle1 are needed to be moved, set minSpan the same as
  5. * maxSpan and the same as `Math.abs(handleEnd[1] - handleEnds[0])`.
  6. * (2) If handle0 is forbidden to cross handle1, set minSpan as `0`.
  7. *
  8. * @param {number} delta Move length.
  9. * @param {Array.<number>} handleEnds handleEnds[0] can be bigger then handleEnds[1].
  10. * handleEnds will be modified in this method.
  11. * @param {Array.<number>} extent handleEnds is restricted by extent.
  12. * extent[0] should less or equals than extent[1].
  13. * @param {number|string} handleIndex Can be 'all', means that both move the two handleEnds,
  14. * where the input minSpan and maxSpan will not work.
  15. * @param {number} [minSpan] The range of dataZoom can not be smaller than that.
  16. * If not set, handle0 and cross handle1. If set as a non-negative
  17. * number (including `0`), handles will push each other when reaching
  18. * the minSpan.
  19. * @param {number} [maxSpan] The range of dataZoom can not be larger than that.
  20. * @return {Array.<number>} The input handleEnds.
  21. */
  22. function _default(delta, handleEnds, extent, handleIndex, minSpan, maxSpan) {
  23. // Normalize firstly.
  24. handleEnds[0] = restrict(handleEnds[0], extent);
  25. handleEnds[1] = restrict(handleEnds[1], extent);
  26. delta = delta || 0;
  27. var extentSpan = extent[1] - extent[0]; // Notice maxSpan and minSpan can be null/undefined.
  28. if (minSpan != null) {
  29. minSpan = restrict(minSpan, [0, extentSpan]);
  30. }
  31. if (maxSpan != null) {
  32. maxSpan = Math.max(maxSpan, minSpan != null ? minSpan : 0);
  33. }
  34. if (handleIndex === 'all') {
  35. minSpan = maxSpan = Math.abs(handleEnds[1] - handleEnds[0]);
  36. handleIndex = 0;
  37. }
  38. var originalDistSign = getSpanSign(handleEnds, handleIndex);
  39. handleEnds[handleIndex] += delta; // Restrict in extent.
  40. var extentMinSpan = minSpan || 0;
  41. var realExtent = extent.slice();
  42. originalDistSign.sign < 0 ? realExtent[0] += extentMinSpan : realExtent[1] -= extentMinSpan;
  43. handleEnds[handleIndex] = restrict(handleEnds[handleIndex], realExtent); // Expand span.
  44. var currDistSign = getSpanSign(handleEnds, handleIndex);
  45. if (minSpan != null && (currDistSign.sign !== originalDistSign.sign || currDistSign.span < minSpan)) {
  46. // If minSpan exists, 'cross' is forbinden.
  47. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + originalDistSign.sign * minSpan;
  48. } // Shrink span.
  49. var currDistSign = getSpanSign(handleEnds, handleIndex);
  50. if (maxSpan != null && currDistSign.span > maxSpan) {
  51. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + currDistSign.sign * maxSpan;
  52. }
  53. return handleEnds;
  54. }
  55. function getSpanSign(handleEnds, handleIndex) {
  56. var dist = handleEnds[handleIndex] - handleEnds[1 - handleIndex]; // If `handleEnds[0] === handleEnds[1]`, always believe that handleEnd[0]
  57. // is at left of handleEnds[1] for non-cross case.
  58. return {
  59. span: Math.abs(dist),
  60. sign: dist > 0 ? -1 : dist < 0 ? 1 : handleIndex ? -1 : 1
  61. };
  62. }
  63. function restrict(value, extend) {
  64. return Math.min(extend[1], Math.max(extend[0], value));
  65. }
  66. module.exports = _default;