Passed
Push — master ( e92062...bf99ee )
by Rafael
01:11
created

control.js ➔ ???   B

Complexity

Conditions 1
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
nc 4
dl 0
loc 28
rs 8.8571
c 9
b 0
f 0
cc 1
nop 1
1
import template from './template.html';
2
import './style.scss';
3
import $ from 'jquery';
4
import 'select2/dist/css/select2.min.css';
5
import 'select2/dist/js/select2.min.js';
6
import 'animate.css/animate.css';
7
import { Slider } from '../slider';
8
import animateConfig from 'animate.css/animate-config.json';
9
import titleCase from 'title-case';
10
11
export class Control {
12
	constructor( options ) {
13
		this.options = options || {};
14
		this.$target = this.options.target;
15
		this.animationClasses = _.flatten( _.values( animateConfig ) );
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
16
		this.animationClassesString = this.animationClasses.join( ' ' );
17
18
		this.delayControl = new Slider( {
19
			name: 'animation-delay',
20
			label: 'Delay (secs)',
21
			uiSettings: {
22
				min: 0,
23
				max: 4,
24
				value: this.getDefaultDelay(),
25
				step: .1
26
			}
27
		} );
28
29
		this.durationControl = new Slider( {
30
			name: 'animation-duration',
31
			label: 'Duration (secs)',
32
			uiSettings: {
33
				min: .5,
34
				max: 4,
35
				value: this.$target.attr( 'data-wow-duration' ).replace( 's', '' ) || 1,
36
				step: .1
37
			}
38
		} );
39
	}
40
41
	/**
42
	 * Get the default value for delay.
43
	 *
44
	 * @since 0.10.0
45
	 *
46
	 * @return {string} Delay time.
47
	 */
48
	getDefaultDelay() {
49
		let delay = this.$target.attr( 'data-wow-delay' );
50
51
		if ( 'undefined' === typeof delay ) {
52
			delay = '1';
53
		}
54
55
		return delay.replace( 's', '' );
56
	}
57
58
	/**
59
	 * Create a control.
60
	 *
61
	 * @since 0.10.0
62
	 *
63
	 * @return {jQuery} Control.
64
	 */
65
	render() {
66
		this.$durationSlider = this.durationControl.render();
67
		this.$delaySlider = this.delayControl.render();
68
69
		this.$control = $( _.template( template )( {
0 ignored issues
show
Bug introduced by
The variable _ seems to be never declared. If this is a global, consider adding a /** global: _ */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
70
			animateConfig: animateConfig,
71
			titleCase: titleCase
72
		} ) );
73
74
		this.$control.find( 'durationSlider' ).replaceWith( this.$durationSlider );
75
		this.$control.find( 'delaySlider' ).replaceWith( this.$delaySlider );
76
		this.$typeControl = this.$control.find( 'select' );
77
78
		this.$typeControl.select2();
79
		this._bindEvents();
80
81
		return this.$control;
82
	}
83
84
	/**
85
	 * Remove the saved settings.
86
	 *
87
	 * @since 0.10.0
88
	 */
89
	removeSettings() {
90
		this.$target.removeClass( 'wow' );
91
		this.$target.removeAttr( 'data-wow-duration' );
92
		this.$target.removeAttr( 'data-wow-delay' );
93
		this._removeAnimationClasses();
94
	}
95
96
	/**
97
	 * Given an animation value, Save animation.
98
	 *
99
	 * @since 0.10.0
100
	 *
101
	 * @param  {string} val Animation Value.
102
	 */
103
	saveAnimation( val ) {
104
		let duration = this.durationControl.$input.val() + 's',
105
			delay = this.delayControl.$input.val() + 's';
106
107
		this.$target.addClass( 'wow' );
108
		this._removeAnimationClasses();
109
		this.$target.addClass( 'animated' );
110
		this.$target.addClass( val );
111
		this.$target.css( 'animation-duration', duration );
112
		this.$target.attr( 'data-wow-duration', duration );
113
		this.$target.attr( 'data-wow-delay', delay );
114
115
		this.$target.one( 'webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', () => {
116
			this._removeAnimationClasses();
117
			this.$target.addClass( val );
118
		} );
119
	}
120
121
	/**
122
	 * Trigger the selected animation.
123
	 *
124
	 * @since 0.10.0
125
	 */
126
	triggerAnimation() {
127
		let val = this.$typeControl.val();
128
129
		if ( val ) {
130
			this.saveAnimation( val );
131
		} else {
132
			this.removeSettings();
133
		}
134
	}
135
136
	/**
137
	 * Bind all events.
138
	 *
139
	 * @since 0.10.0
140
	 */
141
	_bindEvents() {
142
		this.$typeControl.on( 'change', () => this.triggerAnimation() );
143
		this.durationControl.$control.on( 'slide-change', () => this._updateAnimationSettings() );
144
		this.delayControl.$control.on( 'slide-change', () => this._updateAnimationSettings() );
145
	}
146
147
	/**
148
	 * Update the animation settings.
149
	 *
150
	 * @since 0.10.0
151
	 */
152
	_updateAnimationSettings() {
153
		let duration = this.durationControl.$input.val() + 's',
154
			delay = this.delayControl.$input.val() + 's';
155
156
		if ( this.$typeControl.val() ) {
157
			this.$target.attr( 'data-wow-duration', duration );
158
			this.$target.attr( 'data-wow-delay', delay );
159
		}
160
	}
161
162
	/**
163
	 * Remove any animation classes.
164
	 *
165
	 * @since 0.10.0
166
	 */
167
	_removeAnimationClasses() {
168
		this.$target.removeClass( 'animated' );
169
		this.$target.removeClass( this.animationClassesString );
170
		this.$target.css( 'animation-duration', '' );
171
	}
172
173
}
174
175
export { Control as default };
176