All files functions.js

100% Statements 52/52
100% Branches 21/21
100% Functions 16/16
100% Lines 46/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x 3x 2x 2x   3x             1x 3x 1x 4x   1x   3x 3x               1x 3x 12x   3x 2x 4x 2x   2x       3x 3x             11x           6x           6x                       4x                       2x                       1x 1x 1x 1x             1x 2x 2x             1x 2x 2x               1x 2x       2x 1x   2x               1x 2x 1x   2x    
import { RESET_KEYS, MARKER_DATA, ZERO_SEC, NAMESPACE, INVIEW_EVENT, REFRESH_EVENT } from './constants';
 
/**
 * @param {object} target target
 * @returns {object} target
 */
export const reset = target => {
	if ( target.attr( MARKER_DATA ) ) {
		destroy( target );
		removeEvent( target );
	}
	return target;
};
 
/**
 * @param {object} target target
 * @returns {object} target
 */
export const destroy = target => {
	if ( target[ 0 ].resetValues ) {
		RESET_KEYS.forEach( key => {
			target.css( key, target[ 0 ].resetValues[ key ] );
		} );
		delete target[ 0 ].resetValues;
	}
	target.attr( MARKER_DATA, false );
	return target;
};
 
/**
 * @param {object} target target
 * @param {object} options options
 * @returns {object} target
 */
export const create = ( target, options ) => {
	target[ 0 ].resetValues = {};
	RESET_KEYS.forEach( key => target[ 0 ].resetValues[ key ] = target.css( key ) );
 
	if ( ! isStatic( options ) ) {
		target.data( 'inview', false ).on( INVIEW_EVENT, ( event, isInView ) => {
			if ( isInView ) {
				onInView( target, options );
			} else {
				offInView( target, options );
			}
		} );
	}
	target.css( getCss( options ) ).attr( MARKER_DATA, true );
	return target;
};
 
/**
 * @param {object} options options
 * @returns {boolean} is static?
 */
export const isStatic = options => options.stripe || ( ZERO_SEC === options.delay && ZERO_SEC === options.duration );
 
/**
 * @param {object} options options
 * @returns {object} css
 */
export const getCss = options => options.cssFilter( options.stripe ? getStripeCss( options ) : getMarkerCss( options ) );
 
/**
 * @param {object} options options
 * @returns {object} css
 */
const getCommonCss = options => Object.assign( {
	'display': 'inline',
	'background-position': 'left 0 center',
	'padding-bottom': options.padding_bottom,
}, options.font_weight ? {
	'font-weight': options.font_weight,
} : {} );
 
/**
 * @param {object} options options
 * @returns {object} css
 */
const getMarkerCss = options => Object.assign( {}, getCommonCss( options ), {
	'background-size': `200% ${ options.thickness }`,
	'background-repeat': 'repeat-x',
	'background-image': `linear-gradient(to right, rgba(255,255,255,0) 50%, ${ options.color } 50%)`,
}, isStatic( options ) ? {
	'background-position': 'left -100% center',
} : {} );
 
/**
 * @param {object} options options
 * @returns {object} css
 */
const getStripeCss = options => Object.assign( {}, getCommonCss( options ), {
	'background-size': `100% ${ options.thickness }`,
	'background-repeat': 'no-repeat',
	// eslint-disable-next-line no-magic-numbers
	'background-image': `repeating-linear-gradient(-45deg, ${ options.color }, ${ options.color } ${ options.stripe_thickness }px, transparent ${ options.stripe_thickness }px, transparent ${ options.stripe_thickness * 2 }px)`,
} );
 
/**
 * @param {object} target target
 * @param {object} options options
 * @returns {object} target
 */
export const refresh = ( target, options ) => {
	destroy( target );
	stop( target );
	return create( target, options );
};
 
/**
 * @param {object} target target
 * @returns {object} target
 */
export const removeEvent = target => {
	target.off( `.${ NAMESPACE }` );
	return target;
};
 
/**
 * @param {object} target target
 * @returns {object} target
 */
export const stop = target => {
	target.off( INVIEW_EVENT );
	return target;
};
 
/**
 * @param {object} target target
 * @param {object} options options
 * @returns {object} target
 */
export const onInView = ( target, options ) => {
	target.stop( true, true ).css( {
		transition: `background-position ${ options.duration } ${ options.function } ${ options.delay }`,
		'background-position': 'left -100% center',
	} );
	if ( ! options.repeat ) {
		stop( target );
	}
	return target;
};
 
/**
 * @param {object} target target
 * @param {object} options options
 * @returns {object} target
 */
export const offInView = ( target, options ) => {
	if ( options.repeat ) {
		target.trigger( REFRESH_EVENT );
	}
	return target;
};