Passed
Push — master ( 8f169b...36019d )
by Rafael
01:27
created

control.js ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
nc 1
dl 0
loc 47
rs 9.0303
cc 1
nop 1
1
var $ = window.jQuery;
2
3
import { MultiSlider } from '../multi-slider';
4
import template from './template.html';
5
import './style.scss';
6
7
export class Border extends MultiSlider {
8
	constructor( options ) {
9
		super( options );
10
11
		this.controlOptions = {
12
			control: {
13
				title: 'Border',
14
				name: 'border-width',
15
				borderStyle: {
16
					default: ''
17
				},
18
				units: {
19
					enabled: [ 'px', 'em' ]
20
				}
21
			},
22
			setting: {
23
				css: '',
24
				settings: [
25
					{
26
						media: [ 'base', 'phone', 'tablet', 'desktop', 'large' ],
27
						unit: 'px',
28
						isLinked: false,
29
						type: '',
30
						values: {
31
							top: 0,
32
							right: 0,
33
							bottom: 0,
34
							left: 0
35
						}
36
					}
37
				]
38
			},
39
			slider: {
40
				px: {
41
					step: 0.1,
42
					min: 0,
43
					max: 15
44
				},
45
				em: {
46
					min: 0,
47
					max: 5,
48
					step: 0.1
49
				}
50
			}
51
		};
52
53
		this.borderDirections = [ 'left', 'top', 'right', 'bottom' ];
54
	}
55
56
	/**
57
	 * Create a control.
58
	 *
59
	 * @since 1.0.0
60
	 *
61
	 * @return {jQuery} Control.
62
	 */
63
	render() {
64
		let $control;
65
66
		this.$typeControl = $( template );
67
		super.render();
68
		this.bindEvents();
69
		this._sortControls();
70
71
		$control = this.$typeControl.append( this.$control );
72
73
		return $control;
74
	}
75
76
	/**
77
	 * Get CSS rules. Override to append style.
78
	 *
79
	 * @since 1.0.0
80
	 *
81
	 * @return {string} CSS rules.
82
	 */
83
	getCssRule() {
84
		let css = super.getCssRule(),
85
			style = this._getBorderStyle();
86
87
		if ( style ) {
88
			css += 'border-style: ' + this._getBorderStyle() + ';';
89
		} else {
90
			css = 'border: 0;';
91
		}
92
93
		return css;
94
	}
95
96
	/**
97
	 * Bind all events.
98
	 *
99
	 * @since 1.0.0
100
	 */
101
	bindEvents() {
102
		this._bindTypeChange();
103
		this.refreshValues();
104
		this._setType( this._getBorderStyle() );
105
	}
106
107
	/**
108
	 * Get the current settings.
109
	 *
110
	 * @since 1.0.0
111
	 *
112
	 * @return {object} Settings for a control.
113
	 */
114
	getSettings() {
115
		let settings = super.getSettings();
116
		settings.type = this._getBorderStyle();
117
		return settings;
118
	}
119
120
	/**
121
	 * Arange the controls.
122
	 *
123
	 * @since 1.0.0
124
	 */
125
	_sortControls() {
126
		this.$sliderGroup = this.$control.find( '.slider-group' );
127
		this.$sliderGroup
128
			.before( this.$typeControl.find( '.border-type-control' ) )
129
			.prepend( '<h4 class="control-name">Width</h4>' );
130
	}
131
132
	/**
133
	 * Save the default values for reverts.
134
	 *
135
	 * @since 1.0
136
	 */
137
	_storeDefaultValues() {
138
		super._storeDefaultValues();
139
		this.defaultValues.type = this._getDefaultBorderStyle();
140
	}
141
142
	/**
143
	 * Given an object of settings, change the inputs.
144
	 *
145
	 * @since 1.0.0
146
	 *
147
	 * @param  {object} settings Settings for control.
148
	 */
149
	applySettings( settings ) {
150
		super.applySettings( settings );
151
		this._setType( settings.type ).change();
152
		this._toggleWidthControl( settings.type );
153
		this.applyCssRules( { 'border-style': settings.type } );
154
	}
155
156
	/**
157
	 * Get the default borer style to use.
158
	 *
159
	 * @since 1.0.0
160
	 *
161
	 * @return {string} Default border style.
162
	 */
163
	_getDefaultBorderStyle() {
164
		let defaultBorderStyle = this._getBorderStyle();
165
166
		if ( this.options.defaults && this.options.defaults.type ) {
167
			defaultBorderStyle = this.options.defaults.type;
168
		}
169
170
		return defaultBorderStyle;
171
	}
172
173
	/**
174
	 * Set the input type to the type of the target.
175
	 *
176
	 * @since 1.0.0
177
	 */
178
	_setType( setting ) {
179
		setting = 'none' !== setting ? setting : '';
180
		return this.$typeControl
181
			.find( '.border-type-control input' )
182
			.filter( '[value="' + setting + '"]' )
183
			.prop( 'checked', true );
184
	}
185
186
	/**
187
	 * Get the currently set border style.
188
	 *
189
	 * This was added for cross browser support. FF does not return anything for border-style.
190
	 *
191
	 * @since 0.8.7
192
	 *
193
	 * @return {string} Currently applied style.
194
	 */
195
	_getBorderStyle() {
196
		let style = '';
197
		for ( let direction of this.borderDirections ) {
198
			let directionalStyle = this.$target.css( 'border-' + direction + '-style' );
199
			if ( 'none' !== directionalStyle && directionalStyle ) {
200
				style = directionalStyle;
201
				break;
202
			}
203
		}
204
205
		return style;
206
	}
207
208
	/**
209
	 * Refresh the values of the input to the values of the target.
210
	 *
211
	 * @since 1.0.0
212
	 */
213
	refreshValues() {
214
		let $radio;
215
		super.refreshValues();
216
		$radio = this._setType( this._getBorderStyle() );
217
		this._toggleWidthControl( $radio.val() );
218
	}
219
220
	/**
221
	 * Toggle the visibility of the width controls.
222
	 *
223
	 * @since 1.0.0
224
	 *
225
	 * @param  {string} val Current value of the control.
226
	 */
227
	_toggleWidthControl( val ) {
228
		if ( val ) {
229
			this.$sliderGroup.show();
230
		} else {
231
			this.$sliderGroup.hide();
232
		}
233
	}
234
235
	/**
236
	 * When the border type changes, update the css.
237
	 *
238
	 * @since 1.0.0
239
	 */
240
	_bindTypeChange() {
241
		this.$typeControl.find( 'input' ).on( 'change', e => {
242
			let $this = $( e.target ),
243
				val = $this.val();
244
245
			this.applyCssRules( {
246
				'border-style': val
247
			} );
248
249
			this._toggleWidthControl( val );
250
			this.$control.trigger( 'type-change', val );
251
			this._triggerChangeEvent();
252
		} );
253
	}
254
}
255
256
export { Border as default };
257