Passed
Push — master ( 78b380...a2100d )
by Rafael
01:41
created

control.js ➔ ???   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 1
c 8
b 0
f 0
nc 2
dl 0
loc 13
rs 9.4285
nop 1
1
import template from './template.html';
2
import './style.scss';
3
4
export class Slider {
5
	constructor( options ) {
6
7
		this.options = _.defaults( options || {}, {
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...
8
			name: 'slider-name',
9
			label: 'Slider Title',
10
			uiSettings: {
11
				min: 0,
12
				max: 100
13
			}
14
		} );
15
16
		this.template = _.template( template );
17
	}
18
19
	/**
20
	 * Render a slider.
21
	 *
22
	 * @since 1.0.0
23
	 *
24
	 * @return {jQuery} The control.
25
	 */
26
	render() {
27
		this.$control = $( this.template( this.options ) );
28
29
		this.$input = this.$control.find( 'input' );
30
		this.$slider = this.$control.find( '.slider' );
31
32
		this._bindInput();
33
		this._bindSlider();
34
35
		return this.$control;
36
	}
37
38
	/**
39
	 * Bind the inputs change events.
40
	 *
41
	 * @since 1.0.0
42
	 */
43
	_bindInput() {
44
		this.$input.on( 'input', event => {
0 ignored issues
show
Unused Code introduced by
The parameter event is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
45
			let val = this.$input.val();
46
47
			val = Math.min( this.$input.val(), this.$slider.slider( 'option', 'max' ) );
48
			val = Math.max( val, this.$slider.slider( 'option', 'min' ) );
49
50
			this.$input.val( val );
51
52
			this.$slider.slider( 'value', val );
53
		} );
54
	}
55
56
	/**
57
	 * Update the input.
58
	 *
59
	 * @since 1.0.0
60
	 *
61
	 * @param  {$} $slider Slider element jquery.
62
	 */
63
	_updateInput( $slider ) {
64
		this.$input.val( this.$slider.slider( 'value' ) );
65
	}
66
67
	/**
68
	 * Get a slider configuration.
69
	 *
70
	 * @since 1.0.0
71
	 * @return {object} jquery ui slider config.
72
	 */
73
	getSliderConfig() {
74
		return this.options.uiSettings;
75
	}
76
77
	/**
78
	 * Bind the slider events.
79
	 *
80
	 * @since 1.0
81
	 */
82
	_bindSlider() {
83
84
		this.$slider.slider(
85
			_.defaults( this.getSliderConfig( this.$slider.data( 'name' ) ), {
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...
86
				animate: 'fast',
87
				slide: () => this._onSliderChange(),
88
				change: () => this._onSliderChange()
89
			} )
90
		);
91
92
		this._updateInput( this.$slider );
93
	}
94
95
	/**
96
	 * When the slider changes.
97
	 *
98
	 * @since 1.0
99
	 *
100
	 * @param  {DOM Event} event When the slider changes.
0 ignored issues
show
Documentation introduced by
The parameter event does not exist. Did you maybe forget to remove this comment?
Loading history...
101
	 */
102
	_onSliderChange() {
103
		this._updateInput( this.$slider );
104
		this.$control.trigger( 'slide-change', {
105
			name: this.options.name,
106
			value: this.$slider.slider( 'value' )
107
		} );
108
	}
109
}
110
111
export { Slider as default };
112