all files / lib/src/instance/methods/ reveal.js

1.56% Statements 1/64
0% Branches 0/36
0% Functions 0/4
1.56% Lines 1/64
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                                                                                                                                                                                                                                                                                                                             
import style from '../functions/style'
import initialize from '../functions/initialize'
 
import { getNode, getNodes, logger } from '../../utils/core'
import { deepAssign, each, nextUniqueId } from '../../utils/generic'
import { isMobile } from '../../utils/browser'
 
 
export default function reveal (target, options, interval, sync) {
	var this$1 = this;
 
 
	/**
	 * The reveal method has an optional 2nd parameter,
	 * so here we just shuffle things around to accept
	 * the interval being passed as the 2nd argument.
	 */
	if (typeof options === 'number') {
		interval = Math.abs(parseInt(options))
		options = {}
	} else {
		interval = Math.abs(parseInt(interval))
		options = options || {}
	}
 
	var config = deepAssign({}, this.defaults, options)
	var containers = this.store.containers
 
	var container
	var targets
	try {
		container = getNode(config.container)
		if (!container) {
			throw new Error('Invalid container.')
		}
		targets = getNodes(target, container)
		if (!targets) {
			throw new Error('Nothing to animate.')
		}
	} catch (e) {
		return logger.call(this, 'Reveal failed.', e.message)
	}
 
	/**
	 * Verify our platform matches our platform configuration.
	 */
	if (!config.mobile && isMobile() || !config.desktop && !isMobile()) {
		return logger.call(this, 'Reveal aborted.', 'This platform has been disabled.')
	}
 
	/**
	 * Sequence intervals must be at least 16ms (60fps).
	 */
	var sequence
	if (interval) {
		if (interval >= 16) {
			var sequenceId = nextUniqueId()
			sequence = {
				elementIds: [],
				nose: { blocked: false, index: null, pointer: null },
				tail: { blocked: false, index: null, pointer: null },
				id: sequenceId,
				interval: Math.abs(interval),
			}
		} else {
			return logger.call(this, 'Reveal failed.', 'Sequence interval must be at least 16ms.')
		}
	}
 
	var containerId
	each(containers, function (storedContainer) {
		if (!containerId && storedContainer.node === container) {
			containerId = storedContainer.id
		}
	})
 
	if (isNaN(containerId)) {
		containerId = nextUniqueId()
	}
 
	try {
		var elements = targets.map(function (node) {
			var element = {}
			var existingId = node.getAttribute('data-sr-id')
 
			if (existingId) {
				deepAssign(element, this$1.store.elements[existingId])
 
				/**
				 * In order to prevent previously generated styles
				 * from throwing off the new styles, the style tag
				 * has to be reverted to it's pre-reveal state.
				 */
				element.node.setAttribute('style', element.styles.inline)
 
			} else {
				element.id = nextUniqueId()
				element.node = node
				element.seen = false
				element.revealed = false
				element.visible = false
			}
 
			element.config = config
			element.containerId = containerId
			element.styles = style(element)
 
			if (sequence) {
				element.sequence = {
					id: sequence.id,
					index: sequence.elementIds.length,
				}
				sequence.elementIds.push(element.id)
			}
 
			return element
		})
 
		/**
		* Modifying the DOM via setAttribute needs to be handled
		* separately from reading computed styles in the map above
		* for the browser to batch DOM changes (limiting reflows)
		*/
		each(elements, function (element) {
			this$1.store.elements[element.id] = element
			element.node.setAttribute('data-sr-id', element.id)
		})
 
	} catch (e) {
		return logger.call(this, 'Reveal failed.', e.message)
	}
 
	containers[containerId] = containers[containerId] || {
		id: containerId,
		node: container,
	}
 
	if (sequence) {
		this.store.sequences[sequence.id] = sequence
	}
 
	/**
	* If reveal wasn't invoked by sync, we want to
	* make sure to add this call to the history.
	*/
	if (!sync) {
		this.store.history.push({ target: target, options: options, interval: interval })
 
		/**
		* Push initialization to the event queue, giving
		* multiple reveal calls time to be interpretted.
		*/
		if (this.initTimeout) {
			window.clearTimeout(this.initTimeout)
		}
		this.initTimeout = window.setTimeout(initialize.bind(this), 0)
	}
}