animation.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * Animate multiple elements with a single done-callback.
  42. *
  43. * @example
  44. * animation
  45. * .createWrap()
  46. * .add(el1, {x: 10, y: 10})
  47. * .add(el2, {shape: {width: 500}, style: {fill: 'red'}}, 400)
  48. * .done(function () { // done })
  49. * .start('cubicOut');
  50. */
  51. var AnimationWrap =
  52. /** @class */
  53. function () {
  54. function AnimationWrap() {
  55. this._storage = [];
  56. this._elExistsMap = {};
  57. }
  58. /**
  59. * Caution: a el can only be added once, otherwise 'done'
  60. * might not be called. This method checks this (by el.id),
  61. * suppresses adding and returns false when existing el found.
  62. *
  63. * @return Whether adding succeeded.
  64. */
  65. AnimationWrap.prototype.add = function (el, target, duration, delay, easing) {
  66. if (this._elExistsMap[el.id]) {
  67. return false;
  68. }
  69. this._elExistsMap[el.id] = true;
  70. this._storage.push({
  71. el: el,
  72. target: target,
  73. duration: duration,
  74. delay: delay,
  75. easing: easing
  76. });
  77. return true;
  78. };
  79. /**
  80. * Only execute when animation done/aborted.
  81. */
  82. AnimationWrap.prototype.finished = function (callback) {
  83. this._finishedCallback = callback;
  84. return this;
  85. };
  86. /**
  87. * Will stop exist animation firstly.
  88. */
  89. AnimationWrap.prototype.start = function () {
  90. var _this = this;
  91. var count = this._storage.length;
  92. var checkTerminate = function () {
  93. count--;
  94. if (count <= 0) {
  95. // Guard.
  96. _this._storage.length = 0;
  97. _this._elExistsMap = {};
  98. _this._finishedCallback && _this._finishedCallback();
  99. }
  100. };
  101. for (var i = 0, len = this._storage.length; i < len; i++) {
  102. var item = this._storage[i];
  103. item.el.animateTo(item.target, {
  104. duration: item.duration,
  105. delay: item.delay,
  106. easing: item.easing,
  107. setToFinal: true,
  108. done: checkTerminate,
  109. aborted: checkTerminate
  110. });
  111. }
  112. return this;
  113. };
  114. return AnimationWrap;
  115. }();
  116. export function createWrap() {
  117. return new AnimationWrap();
  118. }