sliderMove.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /*
  20. * Licensed to the Apache Software Foundation (ASF) under one
  21. * or more contributor license agreements. See the NOTICE file
  22. * distributed with this work for additional information
  23. * regarding copyright ownership. The ASF licenses this file
  24. * to you under the Apache License, Version 2.0 (the
  25. * "License"); you may not use this file except in compliance
  26. * with the License. You may obtain a copy of the License at
  27. *
  28. * http://www.apache.org/licenses/LICENSE-2.0
  29. *
  30. * Unless required by applicable law or agreed to in writing,
  31. * software distributed under the License is distributed on an
  32. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  33. * KIND, either express or implied. See the License for the
  34. * specific language governing permissions and limitations
  35. * under the License.
  36. */
  37. /**
  38. * Calculate slider move result.
  39. * Usage:
  40. * (1) If both handle0 and handle1 are needed to be moved, set minSpan the same as
  41. * maxSpan and the same as `Math.abs(handleEnd[1] - handleEnds[0])`.
  42. * (2) If handle0 is forbidden to cross handle1, set minSpan as `0`.
  43. *
  44. * @param {number} delta Move length.
  45. * @param {Array.<number>} handleEnds handleEnds[0] can be bigger then handleEnds[1].
  46. * handleEnds will be modified in this method.
  47. * @param {Array.<number>} extent handleEnds is restricted by extent.
  48. * extent[0] should less or equals than extent[1].
  49. * @param {number|string} handleIndex Can be 'all', means that both move the two handleEnds.
  50. * @param {number} [minSpan] The range of dataZoom can not be smaller than that.
  51. * If not set, handle0 and cross handle1. If set as a non-negative
  52. * number (including `0`), handles will push each other when reaching
  53. * the minSpan.
  54. * @param {number} [maxSpan] The range of dataZoom can not be larger than that.
  55. * @return {Array.<number>} The input handleEnds.
  56. */
  57. function _default(delta, handleEnds, extent, handleIndex, minSpan, maxSpan) {
  58. delta = delta || 0;
  59. var extentSpan = extent[1] - extent[0]; // Notice maxSpan and minSpan can be null/undefined.
  60. if (minSpan != null) {
  61. minSpan = restrict(minSpan, [0, extentSpan]);
  62. }
  63. if (maxSpan != null) {
  64. maxSpan = Math.max(maxSpan, minSpan != null ? minSpan : 0);
  65. }
  66. if (handleIndex === 'all') {
  67. var handleSpan = Math.abs(handleEnds[1] - handleEnds[0]);
  68. handleSpan = restrict(handleSpan, [0, extentSpan]);
  69. minSpan = maxSpan = restrict(handleSpan, [minSpan, maxSpan]);
  70. handleIndex = 0;
  71. }
  72. handleEnds[0] = restrict(handleEnds[0], extent);
  73. handleEnds[1] = restrict(handleEnds[1], extent);
  74. var originalDistSign = getSpanSign(handleEnds, handleIndex);
  75. handleEnds[handleIndex] += delta; // Restrict in extent.
  76. var extentMinSpan = minSpan || 0;
  77. var realExtent = extent.slice();
  78. originalDistSign.sign < 0 ? realExtent[0] += extentMinSpan : realExtent[1] -= extentMinSpan;
  79. handleEnds[handleIndex] = restrict(handleEnds[handleIndex], realExtent); // Expand span.
  80. var currDistSign = getSpanSign(handleEnds, handleIndex);
  81. if (minSpan != null && (currDistSign.sign !== originalDistSign.sign || currDistSign.span < minSpan)) {
  82. // If minSpan exists, 'cross' is forbidden.
  83. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + originalDistSign.sign * minSpan;
  84. } // Shrink span.
  85. var currDistSign = getSpanSign(handleEnds, handleIndex);
  86. if (maxSpan != null && currDistSign.span > maxSpan) {
  87. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + currDistSign.sign * maxSpan;
  88. }
  89. return handleEnds;
  90. }
  91. function getSpanSign(handleEnds, handleIndex) {
  92. var dist = handleEnds[handleIndex] - handleEnds[1 - handleIndex]; // If `handleEnds[0] === handleEnds[1]`, always believe that handleEnd[0]
  93. // is at left of handleEnds[1] for non-cross case.
  94. return {
  95. span: Math.abs(dist),
  96. sign: dist > 0 ? -1 : dist < 0 ? 1 : handleIndex ? -1 : 1
  97. };
  98. }
  99. function restrict(value, extend) {
  100. return Math.min(extend[1] != null ? extend[1] : Infinity, Math.max(extend[0] != null ? extend[0] : -Infinity, value));
  101. }
  102. module.exports = _default;