Completed
Push — master ( fe9946...c73065 )
by Aristeides
18s
created

controls/js/src/kirki.input.js   B

Complexity

Total Complexity 37
Complexity/F 2.31

Size

Lines of Code 243
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
nc 1536
dl 0
loc 243
rs 8.6
c 0
b 0
f 0
wmc 37
mnd 2
bc 28
fnc 16
bpm 1.75
cpm 2.3125
noi 11
1
/* global kirkiL10n */
2
var kirki = kirki || {};
3
kirki = jQuery.extend( kirki, {
4
	/**
5
	 * An object containing definitions for input fields.
6
	 *
7
	 * @since 3.0.16
8
	 */
9
	input: {
10
11
		/**
12
		 * Radio input fields.
13
		 *
14
		 * @since 3.0.17
15
		 */
16
		radio: {
17
18
			/**
19
			 * Init the control.
20
			 *
21
			 * @since 3.0.17
22
			 * @param {Object} control - The control object.
23
			 * @param {Object} control.id - The setting.
24
			 * @returns {null}
25
			 */
26
			init: function( control ) {
27
				var input = jQuery( 'input[data-id="' + control.id + '"]' );
28
29
				// Save the value
30
				input.on( 'change keyup paste click', function() {
31
					kirki.setting.set( control.id, jQuery( this ).val() );
32
				});
33
			}
34
		},
35
36
		/**
37
		 * Color input fields.
38
		 *
39
		 * @since 3.0.16
40
		 */
41
		color: {
42
43
			/**
44
			 * Init the control.
45
			 *
46
			 * @since 3.0.16
47
			 * @param {Object} control - The control object.
48
			 * @param {Object} control.id - The setting.
49
			 * @param {Object} control.choices - Additional options for the colorpickers.
50
			 * @param {Object} control.params - Control parameters.
51
			 * @param {Object} control.params.choices - alias for control.choices.
52
53
			 * @returns {null}
54
			 */
55
			init: function( control ) {
56
				var picker = jQuery( '.kirki-color-control[data-id="' + control.id + '"]' ),
57
				    clear;
58
59
				control.choices = control.choices || {};
60
				if ( _.isEmpty( control.choices ) && control.params.choices ) {
61
					control.choices = control.params.choices;
62
				}
63
64
				// If we have defined any extra choices, make sure they are passed-on to Iris.
65
				if ( ! _.isEmpty( control.choices ) ) {
66
					picker.wpColorPicker( control.choices );
67
				}
68
69
				// Tweaks to make the "clear" buttons work.
70
				setTimeout( function() {
71
					clear = jQuery( '.kirki-input-container[data-id="' + control.id + '"] .wp-picker-clear' );
72
					if ( clear.length ) {
73
						clear.click( function() {
74
							kirki.setting.set( control.id, '' );
75
						});
76
					}
77
				}, 200 );
78
79
				// Saves our settings to the WP API
80
				picker.wpColorPicker({
81
					change: function() {
82
83
						// Small hack: the picker needs a small delay
84
						setTimeout( function() {
85
							kirki.setting.set( control.id, picker.val() );
86
						}, 20 );
87
					}
88
				});
89
			}
90
		},
91
92
		/**
93
		 * Generic input fields.
94
		 *
95
		 * @since 3.0.17
96
		 */
97
		genericInput: {
98
99
			/**
100
			 * Init the control.
101
			 *
102
			 * @since 3.0.17
103
			 * @param {Object} control - The control object.
104
			 * @param {Object} control.id - The setting.
105
			 * @returns {null}
106
			 */
107
			init: function( control ) {
108
				var input = jQuery( 'input[data-id="' + control.id + '"]' );
109
110
				// Save the value
111
				input.on( 'change keyup paste click', function() {
112
					kirki.setting.set( control.id, jQuery( this ).val() );
113
				});
114
			}
115
		},
116
117
		/**
118
		 * Generic input fields.
119
		 *
120
		 * @since 3.0.17
121
		 */
122
		textarea: {
123
124
			/**
125
			 * Init the control.
126
			 *
127
			 * @since 3.0.17
128
			 * @param {Object} control - The control object.
129
			 * @param {Object} control.id - The setting.
130
			 * @returns {null}
131
			 */
132
			init: function( control ) {
133
				var textarea = jQuery( 'textarea[data-id="' + control.id + '"]' );
134
135
				// Save the value
136
				textarea.on( 'change keyup paste click', function() {
137
					kirki.setting.set( control.id, jQuery( this ).val() );
138
				});
139
			}
140
		},
141
142
		select: {
143
144
			/**
145
			 * Init the control.
146
			 *
147
			 * @since 3.0.17
148
			 * @param {Object} control - The control object.
149
			 * @param {Object} control.id - The setting.
150
			 * @returns {null}
151
			 */
152
			init: function( control ) {
153
				var element  = jQuery( 'select[data-id="' + control.id + '"]' ),
154
				    multiple = parseInt( element.data( 'multiple' ), 10 ),
155
				    selectValue,
156
				    selectWooOptions = {
157
						escapeMarkup: function( markup ) {
158
							return markup;
159
						}
160
				    };
161
					if ( control.params.placeholder ) {
162
						selectWooOptions.placeholder = control.params.placeholder;
163
						selectWooOptions.allowClear = true;
164
					}
165
166
				if ( 1 < multiple ) {
167
					selectWooOptions.maximumSelectionLength = multiple;
168
				}
169
				jQuery( element ).selectWoo( selectWooOptions ).on( 'change', function() {
170
					selectValue = jQuery( this ).val();
171
					selectValue = ( null === selectValue && 1 < multiple ) ? [] : selectValue;
172
					kirki.setting.set( control.id, selectValue );
173
				});
174
			}
175
		},
176
177
		image: {
178
179
			/**
180
			 * Get the HTML for image inputs.
181
			 *
182
			 * @since 3.0.17
183
			 * @param {Object} data - The arguments.
184
			 * @returns {string}
185
			 */
186
			getTemplate: function( data ) {
187
				var html   = '',
188
				    saveAs = 'url',
189
				    url;
190
191
				data = _.defaults( data, {
192
					label: '',
193
					description: '',
194
					inputAttrs: '',
195
					'data-id': '',
196
					choices: {},
197
					value: ''
198
				} );
199
200
				if ( ! _.isUndefined( data.choices ) && ! _.isUndefined( data.choices.save_as ) ) {
201
					saveAs = data.choices.save_as;
202
				}
203
				url = data.value;
204
				if ( _.isObject( data.value ) && ! _.isUndefined( data.value.url ) ) {
205
					url = data.value.url;
206
				}
207
208
				html += '<label>';
209
				if ( data.label ) {
210
					html += '<span class="customize-control-title">' + data.label + '</span>';
211
				}
212
				if ( data.description ) {
213
					html += '<span class="description customize-control-description">' + data.description + '</span>';
214
				}
215
				html += '</label>';
216
				html += '<div class="image-wrapper attachment-media-view image-upload">';
217
				if ( data.value.url || '' !== url ) {
218
					html += '<div class="thumbnail thumbnail-image"><img src="' + url + '" alt="" /></div>';
219
				} else {
220
					html += '<div class="placeholder">' + kirkiL10n.noFileSelected + '</div>';
221
				}
222
				html += '<div class="actions">';
223
				html += '<button class="button image-upload-remove-button' + ( '' === url ? ' hidden' : '' ) + '">' + kirkiL10n.remove + '</button>';
224
				if ( data['default'] && '' !== data['default'] ) {
225
					html += '<button type="button" class="button image-default-button"';
226
					if ( data['default'] === data.value || ( ! _.isUndefined( data.value.url ) && data['default'] === data.value.url ) ) {
227
						html += ' style="display:none;"';
228
					}
229
					html += '>' + kirkiL10n['default'] + '</button>';
230
				}
231
				html += '<button type="button" class="button image-upload-button">' + kirkiL10n.selectFile + '</button>';
232
				html += '</div></div>';
233
234
				return '<div class="kirki-input-container" data-id="' + data.id + '">' + html + '</div>';
235
			},
236
237
			/**
238
			 * Init the control.
239
			 *
240
			 * @since 3.0.17
241
			 * @param {Object} control - The control object.
242
			 * @returns {null}
243
			 */
244
			init: function( control ) { // jshint ignore:line
245
			}
246
		}
247
	}
248
} );
249