all files / lib/src/instance/ constructor.js

100% Statements 35/35
93.75% Branches 15/16
100% Functions 6/6
100% Lines 32/32
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                                  11×           11×           13×                                                
import defaults from './defaults'
import noop from './noop'
 
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, transformSupported, transitionSupported } from '../utils/browser'
import { getNode, logger } from '../utils/core'
import { deepAssign } from '../utils/generic'
 
import { version } from '../../package.json'
 
 
export default function ScrollReveal (options) {
	if ( options === void 0 ) options = {};
 
 
	/**
	 * Support instantiation without the `new` keyword.
	 */
	if (typeof this === 'undefined' || Object.getPrototypeOf(this) !== ScrollReveal.prototype) {
		return new ScrollReveal(options)
	}
 
	if (!ScrollReveal.isSupported()) {
		logger('Instantiation aborted.', 'This browser is not supported.')
		return noop
	}
 
	try {
		Object.defineProperty(this, 'defaults', {
			get: (function () {
				var config = {}
				deepAssign(config, defaults, options)
				return function () { return config; }
			})(),
		})
	} catch (error) {
		logger('Instantiation failed.', 'Invalid configuration provided.', error.message)
		return noop
	}
 
	var container = getNode(this.defaults.container)
	if (!container) {
		logger('Instantiation failed.', 'Invalid or missing container.')
		return noop
	}
 
	Eif (this.defaults.mobile === isMobile() || this.defaults.desktop === !isMobile()) {
		document.documentElement.classList.add('sr')
	}
 
	this.store = {
		containers: {},
		elements: {},
		history: [],
		sequences: {},
	}
 
	this.pristine = true
	this.delegate = delegate.bind(this)
 
	Object.defineProperty(this, 'version', {
		get: function () { return version; },
	})
 
	Object.defineProperty(this, 'noop', {
		get: function () { return false; },
	})
}
 
ScrollReveal.isSupported = function () { return transformSupported() && transitionSupported(); }
 
ScrollReveal.prototype.clean = clean
ScrollReveal.prototype.destroy = destroy
ScrollReveal.prototype.reveal = reveal
ScrollReveal.prototype.sync = sync