forked from jlmakes/scrollreveal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.js
More file actions
112 lines (90 loc) · 2.84 KB
/
constructor.js
File metadata and controls
112 lines (90 loc) · 2.84 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
import defaults from './defaults'
import mount from './mount'
import clean from './methods/clean'
import destroy from './methods/destroy'
import reveal from './methods/reveal'
import sync from './methods/sync'
import delegate from './functions/delegate'
import isMobile from '../utils/is-mobile'
import isTransformSupported from '../utils/is-transform-supported'
import isTransitionSupported from '../utils/is-transition-supported'
import deepAssign from '../utils/deep-assign'
import logger from '../utils/logger'
import $ from 'tealight'
import { version } from '../../package.json'
let boundDelegate
let boundDestroy
let boundReveal
let boundClean
let boundSync
let config
let debug
let instance
export default function ScrollReveal(options = {}) {
const invokedWithoutNew =
typeof this === 'undefined' ||
Object.getPrototypeOf(this) !== ScrollReveal.prototype
if (invokedWithoutNew) {
return new ScrollReveal(options)
}
if (!ScrollReveal.isSupported()) {
logger.call(this, 'Instantiation failed.', 'This browser is not supported.')
return mount.failure()
}
let buffer
try {
buffer = config
? deepAssign({}, config, options)
: deepAssign({}, defaults, options)
} catch (e) {
logger.call(this, 'Invalid configuration.', e.message)
return mount.failure()
}
try {
const container = $(buffer.container)[0]
if (!container) {
throw new Error('Invalid container.')
}
} catch (e) {
logger.call(this, e.message)
return mount.failure()
}
config = buffer
if ((!config.mobile && isMobile()) || (!config.desktop && !isMobile())) {
logger.call(
this,
'This device is disabled.',
`desktop: ${config.desktop}`,
`mobile: ${config.mobile}`
)
return mount.failure()
}
mount.success()
this.store = {
containers: {},
elements: {},
history: [],
sequences: {}
}
this.pristine = true
boundDelegate = boundDelegate || delegate.bind(this)
boundDestroy = boundDestroy || destroy.bind(this)
boundReveal = boundReveal || reveal.bind(this)
boundClean = boundClean || clean.bind(this)
boundSync = boundSync || sync.bind(this)
Object.defineProperty(this, 'delegate', { get: () => boundDelegate })
Object.defineProperty(this, 'destroy', { get: () => boundDestroy })
Object.defineProperty(this, 'reveal', { get: () => boundReveal })
Object.defineProperty(this, 'clean', { get: () => boundClean })
Object.defineProperty(this, 'sync', { get: () => boundSync })
Object.defineProperty(this, 'defaults', { get: () => config })
Object.defineProperty(this, 'version', { get: () => version })
Object.defineProperty(this, 'noop', { get: () => false })
return instance ? instance : (instance = this)
}
ScrollReveal.isSupported = () =>
isTransformSupported() && isTransitionSupported()
Object.defineProperty(ScrollReveal, 'debug', {
get: () => debug || false,
set: value => (debug = typeof value === 'boolean' ? value : debug)
})