1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import util from '../util.js';
|
19 | import internal from '../internal/index.js';
|
20 |
|
21 | export default class AnimatorFactory {
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
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 |
|
45 |
|
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 |
|
66 |
|
67 | setAnimationOptions(options) {
|
68 | this._animationOptions = options;
|
69 | }
|
70 |
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
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);
|
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 | }
|