sliderMove.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  41. * Calculate slider move result.
  42. * Usage:
  43. * (1) If both handle0 and handle1 are needed to be moved, set minSpan the same as
  44. * maxSpan and the same as `Math.abs(handleEnd[1] - handleEnds[0])`.
  45. * (2) If handle0 is forbidden to cross handle1, set minSpan as `0`.
  46. *
  47. * @param delta Move length.
  48. * @param handleEnds handleEnds[0] can be bigger then handleEnds[1].
  49. * handleEnds will be modified in this method.
  50. * @param extent handleEnds is restricted by extent.
  51. * extent[0] should less or equals than extent[1].
  52. * @param handleIndex Can be 'all', means that both move the two handleEnds.
  53. * @param minSpan The range of dataZoom can not be smaller than that.
  54. * If not set, handle0 and cross handle1. If set as a non-negative
  55. * number (including `0`), handles will push each other when reaching
  56. * the minSpan.
  57. * @param maxSpan The range of dataZoom can not be larger than that.
  58. * @return The input handleEnds.
  59. */
  60. export default function sliderMove(delta, handleEnds, extent, handleIndex, minSpan, maxSpan) {
  61. delta = delta || 0;
  62. var extentSpan = extent[1] - extent[0];
  63. // Notice maxSpan and minSpan can be null/undefined.
  64. if (minSpan != null) {
  65. minSpan = restrict(minSpan, [0, extentSpan]);
  66. }
  67. if (maxSpan != null) {
  68. maxSpan = Math.max(maxSpan, minSpan != null ? minSpan : 0);
  69. }
  70. if (handleIndex === 'all') {
  71. var handleSpan = Math.abs(handleEnds[1] - handleEnds[0]);
  72. handleSpan = restrict(handleSpan, [0, extentSpan]);
  73. minSpan = maxSpan = restrict(handleSpan, [minSpan, maxSpan]);
  74. handleIndex = 0;
  75. }
  76. handleEnds[0] = restrict(handleEnds[0], extent);
  77. handleEnds[1] = restrict(handleEnds[1], extent);
  78. var originalDistSign = getSpanSign(handleEnds, handleIndex);
  79. handleEnds[handleIndex] += delta;
  80. // Restrict in extent.
  81. var extentMinSpan = minSpan || 0;
  82. var realExtent = extent.slice();
  83. originalDistSign.sign < 0 ? realExtent[0] += extentMinSpan : realExtent[1] -= extentMinSpan;
  84. handleEnds[handleIndex] = restrict(handleEnds[handleIndex], realExtent);
  85. // Expand span.
  86. var currDistSign;
  87. currDistSign = getSpanSign(handleEnds, handleIndex);
  88. if (minSpan != null && (currDistSign.sign !== originalDistSign.sign || currDistSign.span < minSpan)) {
  89. // If minSpan exists, 'cross' is forbidden.
  90. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + originalDistSign.sign * minSpan;
  91. }
  92. // Shrink span.
  93. currDistSign = getSpanSign(handleEnds, handleIndex);
  94. if (maxSpan != null && currDistSign.span > maxSpan) {
  95. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + currDistSign.sign * maxSpan;
  96. }
  97. return handleEnds;
  98. }
  99. function getSpanSign(handleEnds, handleIndex) {
  100. var dist = handleEnds[handleIndex] - handleEnds[1 - handleIndex];
  101. // If `handleEnds[0] === handleEnds[1]`, always believe that handleEnd[0]
  102. // is at left of handleEnds[1] for non-cross case.
  103. return {
  104. span: Math.abs(dist),
  105. sign: dist > 0 ? -1 : dist < 0 ? 1 : handleIndex ? -1 : 1
  106. };
  107. }
  108. function restrict(value, extend) {
  109. return Math.min(extend[1] != null ? extend[1] : Infinity, Math.max(extend[0] != null ? extend[0] : -Infinity, value));
  110. }