Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

animator-factory.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import _typeof from 'babel-runtime/helpers/typeof';
  2. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  3. import _createClass from 'babel-runtime/helpers/createClass';
  4. /*
  5. Copyright 2013-2015 ASIAL CORPORATION
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16. import util from '../util';
  17. import internal from '../internal';
  18. var AnimatorFactory = function () {
  19. /**
  20. * @param {Object} opts
  21. * @param {Object} opts.animators The dictionary for animator classes
  22. * @param {Function} opts.baseClass The base class of animators
  23. * @param {String} [opts.baseClassName] The name of the base class of animators
  24. * @param {String} [opts.defaultAnimation] The default animation name
  25. * @param {Object} [opts.defaultAnimationOptions] The default animation options
  26. */
  27. function AnimatorFactory(opts) {
  28. _classCallCheck(this, AnimatorFactory);
  29. this._animators = opts.animators;
  30. this._baseClass = opts.baseClass;
  31. this._baseClassName = opts.baseClassName || opts.baseClass.name;
  32. this._animation = opts.defaultAnimation || 'default';
  33. this._animationOptions = opts.defaultAnimationOptions || {};
  34. if (!this._animators[this._animation]) {
  35. util.throw('No such animation: ' + this._animation);
  36. }
  37. }
  38. /**
  39. * @param {String} jsonString
  40. * @return {Object/null}
  41. */
  42. _createClass(AnimatorFactory, [{
  43. key: 'setAnimationOptions',
  44. /**
  45. * @param {Object} options
  46. */
  47. value: function setAnimationOptions(options) {
  48. this._animationOptions = options;
  49. }
  50. /**
  51. * @param {Object} options
  52. * @param {String} [options.animation] The animation name
  53. * @param {Object} [options.animationOptions] The animation options
  54. * @param {Object} defaultAnimator The default animator instance
  55. * @return {Object} An animator instance
  56. */
  57. }, {
  58. key: 'newAnimator',
  59. value: function newAnimator() {
  60. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  61. var defaultAnimator = arguments[1];
  62. var animator = null;
  63. if (options.animation instanceof this._baseClass) {
  64. return options.animation;
  65. }
  66. var Animator = null;
  67. if (typeof options.animation === 'string') {
  68. Animator = this._animators[options.animation];
  69. }
  70. if (!Animator && defaultAnimator) {
  71. animator = defaultAnimator;
  72. } else {
  73. Animator = Animator || this._animators[this._animation];
  74. var animationOpts = util.extend({}, this._animationOptions, options.animationOptions || {}, internal.config.animationsDisabled ? { duration: 0, delay: 0 } : {});
  75. animator = new Animator(animationOpts);
  76. if (typeof animator === 'function') {
  77. animator = new animator(animationOpts); // eslint-disable-line new-cap
  78. }
  79. }
  80. if (!(animator instanceof this._baseClass)) {
  81. util.throw('"animator" is not an instance of ' + this._baseClassName);
  82. }
  83. return animator;
  84. }
  85. }], [{
  86. key: 'parseAnimationOptionsString',
  87. value: function parseAnimationOptionsString(jsonString) {
  88. try {
  89. if (typeof jsonString === 'string') {
  90. var result = util.animationOptionsParse(jsonString);
  91. if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) === 'object' && result !== null) {
  92. return result;
  93. } else {
  94. console.error('"animation-options" attribute must be a JSON object string: ' + jsonString);
  95. }
  96. }
  97. return {};
  98. } catch (e) {
  99. console.error('"animation-options" attribute must be a JSON object string: ' + jsonString);
  100. return {};
  101. }
  102. }
  103. }]);
  104. return AnimatorFactory;
  105. }();
  106. export default AnimatorFactory;