sliderMove.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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]; // Notice maxSpan and minSpan can be null/undefined.
  63. if (minSpan != null) {
  64. minSpan = restrict(minSpan, [0, extentSpan]);
  65. }
  66. if (maxSpan != null) {
  67. maxSpan = Math.max(maxSpan, minSpan != null ? minSpan : 0);
  68. }
  69. if (handleIndex === 'all') {
  70. var handleSpan = Math.abs(handleEnds[1] - handleEnds[0]);
  71. handleSpan = restrict(handleSpan, [0, extentSpan]);
  72. minSpan = maxSpan = restrict(handleSpan, [minSpan, maxSpan]);
  73. handleIndex = 0;
  74. }
  75. handleEnds[0] = restrict(handleEnds[0], extent);
  76. handleEnds[1] = restrict(handleEnds[1], extent);
  77. var originalDistSign = getSpanSign(handleEnds, handleIndex);
  78. handleEnds[handleIndex] += delta; // Restrict in extent.
  79. var extentMinSpan = minSpan || 0;
  80. var realExtent = extent.slice();
  81. originalDistSign.sign < 0 ? realExtent[0] += extentMinSpan : realExtent[1] -= extentMinSpan;
  82. handleEnds[handleIndex] = restrict(handleEnds[handleIndex], realExtent); // Expand span.
  83. var currDistSign;
  84. currDistSign = getSpanSign(handleEnds, handleIndex);
  85. if (minSpan != null && (currDistSign.sign !== originalDistSign.sign || currDistSign.span < minSpan)) {
  86. // If minSpan exists, 'cross' is forbidden.
  87. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + originalDistSign.sign * minSpan;
  88. } // Shrink span.
  89. currDistSign = getSpanSign(handleEnds, handleIndex);
  90. if (maxSpan != null && currDistSign.span > maxSpan) {
  91. handleEnds[1 - handleIndex] = handleEnds[handleIndex] + currDistSign.sign * maxSpan;
  92. }
  93. return handleEnds;
  94. }
  95. function getSpanSign(handleEnds, handleIndex) {
  96. var dist = handleEnds[handleIndex] - handleEnds[1 - handleIndex]; // If `handleEnds[0] === handleEnds[1]`, always believe that handleEnd[0]
  97. // is at left of handleEnds[1] for non-cross case.
  98. return {
  99. span: Math.abs(dist),
  100. sign: dist > 0 ? -1 : dist < 0 ? 1 : handleIndex ? -1 : 1
  101. };
  102. }
  103. function restrict(value, extend) {
  104. return Math.min(extend[1] != null ? extend[1] : Infinity, Math.max(extend[0] != null ? extend[0] : -Infinity, value));
  105. }