-
Notifications
You must be signed in to change notification settings - Fork 98
/
neon-animatable-behavior.js
144 lines (127 loc) · 4.39 KB
/
neon-animatable-behavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
/**
* `NeonAnimatableBehavior` is implemented by elements containing
* animations for use with elements implementing
* `NeonAnimationRunnerBehavior`.
* @polymerBehavior
*/
export const NeonAnimatableBehavior = {
properties: {
/**
* Animation configuration. See README for more info.
*/
animationConfig: {type: Object},
/**
* Convenience property for setting an 'entry' animation. Do not set
* `animationConfig.entry` manually if using this. The animated node is set
* to `this` if using this property.
*/
entryAnimation: {
observer: '_entryAnimationChanged',
type: String,
},
/**
* Convenience property for setting an 'exit' animation. Do not set
* `animationConfig.exit` manually if using this. The animated node is set
* to `this` if using this property.
*/
exitAnimation: {
observer: '_exitAnimationChanged',
type: String,
},
},
_entryAnimationChanged: function() {
this.animationConfig = this.animationConfig || {};
this.animationConfig['entry'] = [{name: this.entryAnimation, node: this}];
},
_exitAnimationChanged: function() {
this.animationConfig = this.animationConfig || {};
this.animationConfig['exit'] = [{name: this.exitAnimation, node: this}];
},
_copyProperties: function(config1, config2) {
// shallowly copy properties from config2 to config1
for (var property in config2) {
config1[property] = config2[property];
}
},
_cloneConfig: function(config) {
var clone = {isClone: true};
this._copyProperties(clone, config);
return clone;
},
_getAnimationConfigRecursive: function(type, map, allConfigs) {
if (!this.animationConfig) {
return;
}
if (this.animationConfig.value &&
typeof this.animationConfig.value === 'function') {
this._warn(this._logf(
'playAnimation',
'Please put \'animationConfig\' inside of your components \'properties\' object instead of outside of it.'));
return;
}
// type is optional
var thisConfig;
if (type) {
thisConfig = this.animationConfig[type];
} else {
thisConfig = this.animationConfig;
}
if (!Array.isArray(thisConfig)) {
thisConfig = [thisConfig];
}
// iterate animations and recurse to process configurations from child nodes
if (thisConfig) {
for (var config, index = 0; config = thisConfig[index]; index++) {
if (config.animatable) {
config.animatable._getAnimationConfigRecursive(
config.type || type, map, allConfigs);
} else {
if (config.id) {
var cachedConfig = map[config.id];
if (cachedConfig) {
// merge configurations with the same id, making a clone lazily
if (!cachedConfig.isClone) {
map[config.id] = this._cloneConfig(cachedConfig);
cachedConfig = map[config.id];
}
this._copyProperties(cachedConfig, config);
} else {
// put any configs with an id into a map
map[config.id] = config;
}
} else {
allConfigs.push(config);
}
}
}
}
},
/**
* An element implementing `NeonAnimationRunnerBehavior` calls this
* method to configure an animation with an optional type. Elements
* implementing `NeonAnimatableBehavior` should define the property
* `animationConfig`, which is either a configuration object or a map of
* animation type to array of configuration objects.
*/
getAnimationConfig: function(type) {
var map = {};
var allConfigs = [];
this._getAnimationConfigRecursive(type, map, allConfigs);
// append the configurations saved in the map to the array
for (var key in map) {
allConfigs.push(map[key]);
}
return allConfigs;
}
};