Skip to content

Commit 046f929

Browse files
committed
cleanup and add additional ParallaxController comments
1 parent 727bfea commit 046f929

File tree

1 file changed

+74
-24
lines changed

1 file changed

+74
-24
lines changed

src/libs/ParallaxController.js

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import {
55
} from '../utils/index';
66

77
/**
8+
* -------------------------------------------------------
9+
* Parallax Controller
10+
* -------------------------------------------------------
811
*
912
* The global controller for setting up window scroll/resize
1013
* listeners, managing and caching parallax element positions,
@@ -81,13 +84,19 @@ function ParallaxController() {
8184
}
8285

8386
/**
84-
* Creates a unique ID
87+
* Creates a unique id to distinguish parallax elements.
88+
* @return {Number}
8589
*/
8690
function _createID() {
8791
++id;
8892
return id;
8993
}
9094

95+
/**
96+
* Update element positions.
97+
* Determines if the element is in view based on the cached
98+
* attributes, if so set the elements parallax styles.
99+
*/
91100
function _updateElementPositions() {
92101
elements.forEach(element => {
93102
if (element.props.disabled) return;
@@ -103,6 +112,12 @@ function ParallaxController() {
103112
});
104113
}
105114

115+
/**
116+
* Update element attributes.
117+
* Sets up the elements offsets based on the props passed from
118+
* the component then caches the elements current position and
119+
* other important attributes.
120+
*/
106121
function _updateElementAttributes() {
107122
elements.forEach(element => {
108123
if (element.props.disabled) return;
@@ -113,21 +128,27 @@ function ParallaxController() {
113128
});
114129
}
115130

131+
/**
132+
* Remove parallax styles from all elements.
133+
*/
116134
function _removeParallaxStyles() {
117135
elements.forEach(element => {
118136
_resetStyles(element);
119137
});
120138
}
121139

140+
/**
141+
* Cache the window height.
142+
*/
122143
function _setWindowHeight() {
123144
const html = document.documentElement;
124145
windowHeight = window.innerHeight || html.clientHeight;
125146
}
126147

127148
/**
128-
* Takes a parallax element and caches important values
129-
* as an attribute object on the element
130-
*
149+
* Takes a parallax element and caches important values that
150+
* cause layout reflow and paints. Stores the values as an
151+
* attribute object accesible on the parallax element.
131152
* @param {object} element
132153
*/
133154
function _cacheAttributes(element) {
@@ -180,7 +201,7 @@ function ParallaxController() {
180201
// NOTE: must add the current scroll position when the
181202
// element is checked so that we get its absolute position
182203
// relative to the document and not the viewport then
183-
// add the min/max offsets calculated above
204+
// add the min/max offsets calculated above.
184205
let top = 0;
185206
let bottom = 0;
186207

@@ -192,9 +213,9 @@ function ParallaxController() {
192213
bottom = rect.bottom + scrollY + (yMinPx * -1);
193214
}
194215

195-
// Total distance the element will move from when
216+
// NOTE: Total distance the element will move from when
196217
// the top enters the view to the bottom leaving
197-
// accounting for elements height and max/min offsets
218+
// accounting for elements height and max/min offsets.
198219
const totalDist = windowHeight + (elHeight + Math.abs(yMinPx) + yMaxPx);
199220

200221
element.attributes = {
@@ -211,10 +232,8 @@ function ParallaxController() {
211232
}
212233

213234
/**
214-
* Takes a parallax element and parses the offset props
215-
* to get the value and unit (if any). Sets these offsets
216-
* on the element
217-
*
235+
* Takes a parallax element and parses the offset props to get the value
236+
* and unit. Sets these values as offset object accessible on the element.
218237
* @param {object} element
219238
*/
220239
function _setupOffsets(element) {
@@ -230,7 +249,6 @@ function ParallaxController() {
230249
const xMin = parseValueAndUnit(offsetXMax);
231250
const xMax = parseValueAndUnit(offsetXMin);
232251

233-
// @TODO: Move error to component proptypes
234252
if (xMin.unit !== xMax.unit || yMin.unit !== yMax.unit) {
235253
throw new Error('Must provide matching units for the min and max offset values of each axis.');
236254
}
@@ -248,6 +266,13 @@ function ParallaxController() {
248266
};
249267
}
250268

269+
/**
270+
* Takes a parallax element and returns whether the element
271+
* is in view based on the cached position of the element,
272+
* current scroll position and the window height.
273+
* @param {object} element
274+
* @return {boolean} isInView
275+
*/
251276
function _isElementInView(element) {
252277
const top = element.attributes.top - scrollY;
253278
const bottom = element.attributes.bottom - scrollY;
@@ -261,6 +286,11 @@ function ParallaxController() {
261286
return isInView;
262287
}
263288

289+
/**
290+
* Takes a parallax element and set the styles based on the
291+
* offsets and percent the element has moved though the viewport.
292+
* @param {object} element
293+
*/
264294
function _setParallaxStyles(element) {
265295
const top = element.attributes.top - scrollY;
266296
const { totalDist } = element.attributes;
@@ -282,9 +312,11 @@ function ParallaxController() {
282312
transform:translate3d(${offsets.x.value}${offsets.x.unit}, ${offsets.y.value}${offsets.y.unit}, 0)`;
283313
}
284314

315+
/**
316+
* Takes a parallax element and removes parallax offset styles.
317+
* @param {object} element
318+
*/
285319
function _resetStyles(element) {
286-
// Resets any styles that may be left over when
287-
// resizing from desktop to mobile apply styles
288320
const el = element.elInner;
289321
el.style.cssText =
290322
`will-change:none;
@@ -293,11 +325,17 @@ function ParallaxController() {
293325
}
294326

295327
/**
296-
* --------------------------------------
328+
* -------------------------------------------------------
297329
* Public methods
298-
* --------------------------------------
330+
* -------------------------------------------------------
299331
*/
300332

333+
/**
334+
* Creates a new parallax element object with new id
335+
* and options to store in the 'elements' array.
336+
* @param {object} options
337+
* @return {object} element
338+
*/
301339
this.createElement = function(options) {
302340
const id = _createID();
303341
const element = {
@@ -311,32 +349,44 @@ function ParallaxController() {
311349
return element;
312350
};
313351

352+
/**
353+
* Creates a new parallax element object with new id
354+
* and options to store in the 'elements' array.
355+
* @param {object} element
356+
*/
314357
this.removeElement = function(element) {
315358
const index = elements.indexOf(element);
316359
if (index !== -1) {
317360
elements.splice(index, 1);
318361
}
319362
};
320363

364+
/**
365+
* Updates an existing parallax element object with new options.
366+
* @param {object} element
367+
* @param {object} options
368+
*/
321369
this.updateElement = function(element, options) {
322-
// update props of a given element
370+
// gets the index of the element to update based on id
323371
const index = elements.findIndex(el => el.id === element.id);
324372

325-
// create new element with options and replace old
373+
// create new element with options and replaces the old
326374
elements[index] = Object.assign({}, elements[index], options);
327375

376+
// call update to set attributes and positions based on the new options
328377
this.update();
329378
};
330379

331380
/**
332-
* Remove element styles
381+
* Remove element styles.
382+
* @param {object} element
333383
*/
334384
this.resetElementStyles = function(element) {
335385
_resetStyles(element);
336386
};
337387

338388
/**
339-
* Updates all parallax element attributes and postitions
389+
* Updates all parallax element attributes and postitions.
340390
*/
341391
this.update = function() {
342392
_setWindowHeight();
@@ -345,7 +395,7 @@ function ParallaxController() {
345395
};
346396

347397
/**
348-
* Removes listeners, resets all styles, nullifies self
398+
* Removes listeners, reset all styles then nullifies the global ParallaxController.
349399
*/
350400
this.destroy = function() {
351401
_removeListeners();
@@ -355,8 +405,9 @@ function ParallaxController() {
355405
}
356406

357407
/**
358-
* Static method to instantiate the ParallaxController
359-
* @returns {ParallaxController} A new or existing instance of the ParallaxController
408+
* Static method to instantiate the ParallaxController.
409+
* Returns a new or existing instance of the ParallaxController.
410+
* @returns {Object} ParallaxController
360411
*/
361412
ParallaxController.init = function() {
362413
if (!window.ParallaxController) {
@@ -366,4 +417,3 @@ ParallaxController.init = function() {
366417
};
367418

368419
export default ParallaxController;
369-

0 commit comments

Comments
 (0)