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