forked from FrontendMatter/material-design-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscroll-effect-behavior.js
More file actions
215 lines (193 loc) · 5.57 KB
/
Copy pathscroll-effect-behavior.js
File metadata and controls
215 lines (193 loc) · 5.57 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { util } from 'dom-factory'
/**
* Allows a scrollTargetBehavior consumer to use scroll effects.
* Use from a consumer via dom-factory mixins i.e.
*
* import {
* scrollTargetBehavior,
* scrollEffectBehavior
* } from 'material-design-kit'
*
* const anotherComponent = () => ({
* mixins: [
* scrollTargetBehavior,
* scrollEffectBehavior
* ]
* })
*
* @param {HTMLElement} element The element which should respond to scroll events
* @return {Object}
*/
export const scrollEffectBehavior = () => ({
// List of registered scroll effects
_scrollEffects: {},
// List of effects handlers that will take place during scroll
_effectsRunFn: [],
// List of the effects definitions
_effects: [],
// Effects config
_effectsConfig: null,
/**
* Get the list of effect names to run
* @return {Array}
*/
get effects () {
return this.element.hasAttribute('effects')
? (this.element.getAttribute('effects') || '').split(' ')
: []
},
/**
* Get the effects config object
* @return {Object}
*/
get effectsConfig () {
if (this._effectsConfig) {
return this._effectsConfig
}
if (this.element.hasAttribute('effects-config')) {
try {
return JSON.parse(this.element.getAttribute('effects-config'))
}
catch (e) {}
}
return {}
},
/**
* Set the effects config object
* @param {Object} value
*/
set effectsConfig (value) {
this._effectsConfig = value
},
/**
* The clamped value of `_scrollTop`.
* @return {number}
*/
get _clampedScrollTop () {
return Math.max(0, this._scrollTop)
},
/**
* Registers a scroll effect
* @param {string} effectName The effect name
* @param {Object} effectDef The effect definition
*/
registerEffect (effectName, effectDef) {
if (this._scrollEffects[effectName] !== undefined) {
throw new Error(`effect ${ effectName } is already registered.`)
}
this._scrollEffects[effectName] = effectDef
this._setUpEffects()
},
/**
* Returns true if the element is visible in the current viewport.
* This method should be overridden by the consumer of this behavior.
* @return {Boolean}
*/
isOnScreen () {
return false
},
/**
* Returns true if there's content below the element.
* This method should be overridden by the consumer of this behavior.
* @return {Boolean}
*/
isContentBelow () {
return false
},
/**
* Creates an effect object from an effect's name that can be used to run
* effects programmatically.
* @param {string} effectName The effect name
* @param {Object} effectConfig The effect config (optional)
* @return {Object} An effect object with the following functions:
*
* `effect.setUp()`, Sets up the requirements for the effect
* `effect.run(progress, top)`, Runs the effect given a `progress`
* `effect.tearDown()`, Clean up
*/
createEffect (effectName, effectConfig = {}) {
const effectDef = this._scrollEffects[effectName]
if (typeof effectDef === undefined) {
throw new ReferenceError(`Scroll effect ${ effectName } was not registered`)
}
const prop = this._boundEffect(effectDef, effectConfig)
prop.setUp()
return prop
},
/**
* Returns an effect object bound to the current context.
* @param {Object} effectDef The effect definition
* @param {Object} effectConfig The effect config (optional)
* @return {Object}
*/
_boundEffect (effectDef, effectConfig = {}) {
let startsAt = parseFloat(effectConfig.startsAt || 0)
let endsAt = parseFloat(effectConfig.endsAt || 1)
let deltaS = endsAt - startsAt
let noop = Function()
let runFn = (startsAt === 0 && endsAt === 1) ? effectDef.run : function (progress, top) {
effectDef.run.call(this, Math.max(0, (progress - startsAt) / deltaS), top)
}
return {
setUp: effectDef.setUp ? effectDef.setUp.bind(this, effectConfig) : noop,
run: effectDef.run ? runFn.bind(this) : noop,
tearDown: effectDef.tearDown ? effectDef.tearDown.bind(this) : noop
}
},
/**
* Sets up the effects.
*/
_setUpEffects () {
this._tearDownEffects()
this.effects.forEach((effectName) => {
let effectDef
if ((effectDef = this._scrollEffects[effectName])) {
this._effects.push(this._boundEffect(effectDef, this.effectsConfig[effectName]))
}
})
this._effects.forEach((effectDef) => {
if (effectDef.setUp() !== false) {
this._effectsRunFn.push(effectDef.run)
}
})
},
/**
* Tears down the effects.
*/
_tearDownEffects () {
this._effects.forEach((effectDef) => {
effectDef.tearDown()
})
this._effectsRunFn = []
this._effects = []
},
/**
* Runs the effects.
* @param {number} progress The progress
* @param {number} top The top position of the current element relative to the viewport
*/
_runEffects (progress, top) {
this._effectsRunFn.forEach(run => run(progress, top))
},
/**
* Overrides `scrollTargetBehavior._scrollHandler`
*/
_scrollHandler () {
this._updateScrollState(this._clampedScrollTop)
},
/**
* Updates the scroll state.
* Should be overriden from the consumer of the behavior.
* @param {number} scrollTop
*/
_updateScrollState (scrollTop) {},
/**
* Transform style
* @param {String} value The transform value
* @param {HTMLElement} element The element to apply transforms to (optional)
*/
_transform (value, element) {
element = element || this.element
util.transform(value, element)
}
})