UNPKG

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