All files index.js

82.93% Statements 68/82
80.43% Branches 37/46
64.71% Functions 11/17
82.5% Lines 66/80

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 156 157 158 159 1601x 1x   48x 12x 12x 12x 12x 12x                         5x                   6x     8x 8x 6x 24x   6x   8x     6x 6x 6x         6x 1x 1x   1x   5x 5x 5x   6x 5x   6x 6x 24x   6x 6x 1x     5x 2x       3x                             8x                                       12x 6x 6x     6x 2x 2x     6x 6x 66x 66x 6x   66x 12x 12x 6x 1x 5x 3x   6x 1x 1x 1x         6x   6x 6x 6x         6x 5x        
require( 'jquery-inview' );
import $ from 'jquery';
 
$.fn.markerAnimation = function( ...args ) {
	return this.each( function() {
		const target = $( this );
		const namespace = 'markerAnimation';
		const overridePrefix = 'ma_';
		const markerAnimationObj = {
			op: {
				'color': '#fe9',
				'padding_bottom': '.6em',
				'thickness': '.6em',
				'delay': '.1s',
				'duration': '2s',
				'function': 'ease',
				'font_weight': 'bold',
				'repeat': false,
				'stripe': false,
				'stripe_thickness': 2,
				'cssFilter': function( css ) {
					return css;
				},
			},
			resetKeys: [
				'background',
				'padding-bottom',
				'font-weight',
				'transition',
			],
			setOption: function( op ) {
				$.extend( this.op, op );
			},
			destroy: function() {
				const $this = this;
				if ( target[ 0 ].resetValues ) {
					$this.resetKeys.forEach( function( key ) {
						target.css( key, target[ 0 ].resetValues[ key ] );
					} );
					target[ 0 ].resetValues = null;
				}
				target.attr( 'data-marker_animation', false );
			},
			create: function( op ) {
				const $this = this;
				this.setOption( op );
				let css = {
					'display': 'inline',
					'background-position': 'left 0 center',
					'padding-bottom': $this.op.padding_bottom,
				};
				if ( $this.op.stripe ) {
					css [ 'background-size' ] = '0 ' + $this.op.thickness;
					css [ 'background-repeat' ] = 'no-repeat';
					// eslint-disable-next-line no-magic-numbers
					css[ 'background-image' ] = 'repeating-linear-gradient(-45deg, ' + $this.op.color + ', ' + $this.op.color + ' ' + $this.op.stripe_thickness + 'px,transparent ' + $this.op.stripe_thickness + 'px,transparent ' + ( $this.op.stripe_thickness * 2 ) + 'px)';
				} else {
					css[ 'background-size' ] = '200% ' + $this.op.thickness;
					css[ 'background-repeat' ] = 'repeat-x';
					css[ 'background-image' ] = 'linear-gradient(to right, rgba(255,255,255,0) 50%, ' + $this.op.color + ' 50%)';
				}
				if ( $this.op.font_weight ) {
					css[ 'font-weight' ] = $this.op.font_weight;
				}
				target[ 0 ].resetValues = {};
				$this.resetKeys.forEach( function( key ) {
					target[ 0 ].resetValues[ key ] = target.css( key );
				} );
				css = $this.op.cssFilter( css );
				if ( $this.op.stripe ) {
					target.css( css ).css( {
						'background-size': '100% ' + $this.op.thickness,
					} ).attr( 'data-marker_animation', true );
				} else if ( '0s' === $this.op.delay && '0s' === $this.op.duration ) {
					target.css( css ).css( {
						'background-position': 'left -100% center',
					} ).attr( 'data-marker_animation', true );
				} else {
					target.data( 'inview', false ).on( 'inview.' + namespace, function( event, isInView ) {
						if ( isInView ) {
							$this.onInView();
						} else {
							$this.offInView();
						}
					} ).css( css ).attr( 'data-marker_animation', true );
				}
			},
			refresh: function() {
				this.destroy();
				this.stop();
				this.create();
			},
			removeEvent: function() {
				target.off( '.' + namespace );
			},
			stop: function() {
				target.off( 'inview.' + namespace );
			},
			onInView: function() {
				target.stop( true, true ).css( {
					'transition': 'background-position ' + this.op.duration + ' ' + this.op.function + ' ' + this.op.delay,
					'background-position': 'left -100% center',
				} );
				if ( ! this.op.repeat ) {
					this.stop();
				}
			},
			offInView: function() {
				if ( this.op.repeat ) {
					target.trigger( 'refresh.' + namespace );
				}
			},
		};
		if ( typeof args[ 0 ] === 'string' && args[ 0 ] === 'destroy' ) {
			target.trigger( 'destroy.' + namespace );
		} else Iif ( typeof args[ 0 ] === 'string' && args[ 0 ] === 'refresh' ) {
			target.trigger( 'refresh.' + namespace );
		} else {
			if ( target.attr( 'data-marker_animation' ) ) {
				markerAnimationObj.destroy();
				markerAnimationObj.removeEvent();
			}
 
			const options = $.extend( {}, args[ 0 ] );
			Object.keys( markerAnimationObj.op ).forEach( function( key ) {
				const data = target.data( overridePrefix + key );
				if ( undefined !== data ) {
					options[ key ] = data;
				}
				if ( 'string' === typeof options[ key ] ) {
					options[ key ] = options[ key ].trim();
					if ( 'delay' === key || 'duration' === key ) {
						if ( ! /^-?\d*\.?\d+m?s$/.test( options[ key ] ) ) {
							options[ key ] = '0s';
						} else if ( /^-?0?\.?0+m?s$/.test( options[ key ] ) ) {
							options[ key ] = '0s';
						}
					} else if ( 'stripe_thickness' === key ) {
						options[ key ] = parseInt( options[ key ], 10 );
						Eif ( isNaN( options[ key ] ) ) {
							options[ key ] = 2;
						}
					}
				}
			} );
			markerAnimationObj.create( options );
 
			target.on( 'destroy.' + namespace, function() {
				markerAnimationObj.destroy();
				markerAnimationObj.removeEvent();
			} ).on( 'refresh.' + namespace, function() {
				markerAnimationObj.refresh();
			} );
 
			if ( options.test ) {
				target[ 0 ].markerAnimationObj = markerAnimationObj;
			}
		}
	} );
};