1
|
|
|
import template from './template.html'; |
2
|
|
|
import './style.scss'; |
3
|
|
|
|
4
|
|
|
export class Slider { |
5
|
|
|
constructor( options ) { |
6
|
|
|
|
7
|
|
|
this.options = _.defaults( options || {}, { |
|
|
|
|
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 => { |
|
|
|
|
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' ) ), { |
|
|
|
|
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. |
|
|
|
|
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
|
|
|
|
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.