Issues (94)

src/controls/slider/control.js (4 issues)

1
var $ = window.jQuery;
2
import template from './template.html';
3
import './style.scss';
4
5
export class Slider {
6
	constructor( options ) {
7
		this.options = _.defaults( options || {}, {
0 ignored issues
show
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
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
		this.$slider.slider(
84
			_.defaults( this.getSliderConfig( this.$slider.data( 'name' ) ), {
0 ignored issues
show
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...
85
				animate: 'fast',
86
				slide: () => this._onSliderChange(),
87
				change: () => this._onSliderChange()
88
			} )
89
		);
90
91
		this._updateInput( this.$slider );
92
	}
93
94
	/**
95
	 * When the slider changes.
96
	 *
97
	 * @since 1.0
98
	 *
99
	 * @param  {DOM Event} event When the slider changes.
0 ignored issues
show
The parameter event does not exist. Did you maybe forget to remove this comment?
Loading history...
100
	 */
101
	_onSliderChange() {
102
		this._updateInput( this.$slider );
103
		this.$control.trigger( 'slide-change', {
104
			name: this.options.name,
105
			value: this.$slider.slider( 'value' )
106
		} );
107
	}
108
}
109
110
export { Slider as default };
111