Skip to content

Commit f55b4ec

Browse files
committed
componentize core utility functions
1 parent 6ddaa61 commit f55b4ec

10 files changed

Lines changed: 87 additions & 87 deletions

File tree

src/instance/constructor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import transformSupported from '../utils/transform-supported'
1313
import transitionSupported from '../utils/transition-supported'
1414

1515
import deepAssign from '../utils/deep-assign'
16-
import { logger } from '../utils/core'
16+
import logger from '../utils/logger'
1717
import $ from 'tealight'
1818

1919
import { version } from '../../package.json'

src/instance/functions/delegate.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import sequence from './sequence'
33
import mathSign from '../../polyfills/math-sign'
44
import raf from '../../polyfills/raf'
55
import each from '../../utils/each'
6-
import { getGeometry, getScrolled, isElementVisible } from '../../utils/core'
6+
import getGeometry from '../../utils/get-geometry'
7+
import getScrolled from '../../utils/get-scrolled'
8+
import isElementVisible from '../../utils/is-element-visible'
79

810
export default function delegate (
911
event = { type: 'init' },

src/instance/methods/clean.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import $ from 'tealight'
22
import each from '../../utils/each'
3-
import { logger } from '../../utils/core'
3+
import logger from '../../utils/logger'
44
import rinse from '../functions/rinse'
55

66
export default function clean (target) {

src/instance/methods/reveal.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Sequence } from '../functions/sequence'
55
import $ from 'tealight'
66
import each from '../../utils/each'
77
import deepAssign from '../../utils/deep-assign'
8-
import { logger } from '../../utils/core'
8+
import logger from '../../utils/logger'
99
import nextUniqueId from '../../utils/next-unique-id'
1010
import isMobile from '../../utils/is-mobile'
1111

src/utils/core.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/utils/get-geometry.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
export default function getGeometry (target, isContainer) {
2+
/**
3+
* We want to ignore padding and scrollbars for container elements.
4+
* More information here: https://goo.gl/vOZpbz
5+
*/
6+
const height = isContainer ? target.node.clientHeight : target.node.offsetHeight
7+
const width = isContainer ? target.node.clientWidth : target.node.offsetWidth
8+
9+
let offsetTop = 0
10+
let offsetLeft = 0
11+
let node = target.node
12+
13+
do {
14+
if (!isNaN(node.offsetTop)) {
15+
offsetTop += node.offsetTop
16+
}
17+
if (!isNaN(node.offsetLeft)) {
18+
offsetLeft += node.offsetLeft
19+
}
20+
node = node.offsetParent
21+
} while (node)
22+
23+
return {
24+
bounds: {
25+
top: offsetTop,
26+
right: offsetLeft + width,
27+
bottom: offsetTop + height,
28+
left: offsetLeft,
29+
},
30+
height,
31+
width,
32+
}
33+
}

src/utils/get-scrolled.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default function getScrolled (container) {
2+
return container.node === document.documentElement
3+
? {
4+
top: window.pageYOffset,
5+
left: window.pageXOffset,
6+
}
7+
: {
8+
top: container.node.scrollTop,
9+
left: container.node.scrollLeft,
10+
}
11+
}

src/utils/is-element-visible.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export default function isElementVisible (element) {
2+
const container = this.store.containers[element.containerId]
3+
const viewFactor = Math.max(0, Math.min(1, element.config.viewFactor))
4+
const viewOffset = element.config.viewOffset
5+
6+
const elementBounds = {
7+
top: element.geometry.bounds.top + element.geometry.height * viewFactor,
8+
right: element.geometry.bounds.right - element.geometry.width * viewFactor,
9+
bottom: element.geometry.bounds.bottom - element.geometry.height * viewFactor,
10+
left: element.geometry.bounds.left + element.geometry.width * viewFactor,
11+
}
12+
13+
const containerBounds = {
14+
top: container.geometry.bounds.top + container.scroll.top + viewOffset.top,
15+
right: container.geometry.bounds.right + container.scroll.left - viewOffset.right,
16+
bottom:
17+
container.geometry.bounds.bottom + container.scroll.top - viewOffset.bottom,
18+
left: container.geometry.bounds.left + container.scroll.left + viewOffset.left,
19+
}
20+
21+
return (
22+
(elementBounds.top < containerBounds.bottom &&
23+
elementBounds.right > containerBounds.left &&
24+
elementBounds.bottom > containerBounds.top &&
25+
elementBounds.left < containerBounds.right) ||
26+
element.styles.position === 'fixed'
27+
)
28+
}

src/utils/logger.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function logger (message, ...details) {
2+
if (this.constructor.debug && console) {
3+
let report = `%cScrollReveal: ${message}`
4+
details.forEach(detail => (report += `\n — ${detail}`))
5+
console.log(report, 'color: #ea654b;') // eslint-disable-line no-console
6+
}
7+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { logger } from '../../src/utils/core'
1+
import logger from '../../src/utils/logger'
22

3-
describe('Core Utilities', () => {
3+
describe('Utilities', () => {
44
describe('logger()', () => {
55
const mock = { constructor: { debug: true } }
66

0 commit comments

Comments
 (0)