Completed
Pull Request — master (#1710)
by Aristeides
14:05 queued 02:09
created

controls/js/src/kirki.control.js   A

Complexity

Total Complexity 16
Complexity/F 2

Size

Lines of Code 248
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
wmc 16
nc 32
mnd 1
bc 10
fnc 8
dl 0
loc 248
rs 10
bpm 1.25
cpm 2
noi 10
c 0
b 0
f 0
1
var kirki = kirki || {};
2
kirki = jQuery.extend( kirki, {
3
4
	/**
5
	 * An object containing definitions for controls.
6
	 *
7
	 * @since 3.0.16
8
	 */
9
	control: {
10
11
		/**
12
		 * The radio control.
13
		 *
14
		 * @since 3.0.17
15
		 */
16
		'kirki-radio': {
17
18
			/**
19
			 * Init the control.
20
			 *
21
			 * @since 3.0.17
22
			 * @param {Object} control - The customizer control object.
23
			 * @returns {null}
24
			 */
25
			init: function( control ) {
26
				var self = this;
27
28
				// Render the template.
29
				self.template( control );
30
31
				// Init the control.
32
				kirki.input.radio.init( control );
33
34
			},
35
36
			/**
37
			 * Render the template.
38
			 *
39
			 * @since 3.0.17
40
			 * @param {Object} control - The customizer control object.
41
			 * @param {Object} control.params - The control parameters.
42
			 * @param {string} control.params.label - The control label.
43
			 * @param {string} control.params.description - The control description.
44
			 * @param {string} control.params.inputAttrs - extra input arguments.
45
			 * @param {string} control.params.default - The default value.
46
			 * @param {Object} control.params.choices - Any extra choices we may need.
47
			 * @param {string} control.id - The setting.
48
			 * @returns {null}
49
			 */
50
			template: function( control ) {
51
				var template = wp.template( 'kirki-input-radio' );
52
				control.container.html( template( {
53
					label: control.params.label,
54
					description: control.params.description,
55
					'data-id': control.id,
56
					inputAttrs: control.params.inputAttrs,
57
					'default': control.params['default'],
58
					value: kirki.setting.get( control.id ),
59
					choices: control.params.choices
60
				} ) );
61
			}
62
		},
63
64
		/**
65
		 * The color control.
66
		 *
67
		 * @since 3.0.16
68
		 */
69
		'kirki-color': {
70
71
			/**
72
			 * Init the control.
73
			 *
74
			 * @since 3.0.16
75
			 * @param {Object} control - The customizer control object.
76
			 * @returns {null}
77
			 */
78
			init: function( control ) {
79
				var self = this;
80
81
				// Render the template.
82
				self.template( control );
83
84
				// Init the control.
85
				kirki.input.color.init( control );
86
87
			},
88
89
			/**
90
			 * Render the template.
91
			 *
92
			 * @since 3.0.16
93
			 * @param {Object}     control - The customizer control object.
94
			 * @param {Object}     control.params - The control parameters.
95
			 * @param {string}     control.params.label - The control label.
96
			 * @param {string}     control.params.description - The control description.
97
			 * @param {string}     control.params.mode - The colorpicker mode. Can be 'full' or 'hue'.
98
			 * @param {bool|array} control.params.palette - false if we don't want a palette,
99
			 *                                              true to use the default palette,
100
			 *                                              array of custom hex colors if we want a custom palette.
101
			 * @param {string}     control.params.inputAttrs - extra input arguments.
102
			 * @param {string}     control.params.default - The default value.
103
			 * @param {Object}     control.params.choices - Any extra choices we may need.
104
			 * @param {boolean}    control.params.choices.alpha - should we add an alpha channel?
105
			 * @param {string}     control.id - The setting.
106
			 * @returns {null}
107
			 */
108
			template: function( control ) {
109
				var template = wp.template( 'kirki-input-color' );
110
				control.container.html( template( {
111
					label: control.params.label,
112
					description: control.params.description,
113
					'data-id': control.id,
114
					mode: control.params.mode,
115
					inputAttrs: control.params.inputAttrs,
116
					'data-palette': control.params.palette,
117
					'data-default-color': control.params['default'],
118
					'data-alpha': control.params.choices.alpha,
119
					value: kirki.setting.get( control.id )
120
				} ) );
121
			}
122
		},
123
124
		/**
125
		 * The generic control.
126
		 *
127
		 * @since 3.0.16
128
		 */
129
		'kirki-generic': {
130
131
			/**
132
			 * Init the control.
133
			 *
134
			 * @since 3.0.17
135
			 * @param {Object} control - The customizer control object.
136
			 * @param {Object} control.params - Control parameters.
137
			 * @param {Object} control.params.choices - Define the specifics for this input.
138
			 * @param {string} control.params.choices.element - The HTML element we want to use ('input', 'div', 'span' etc).
139
			 * @returns {null}
140
			 */
141
			init: function( control ) {
142
				var self = this;
143
144
				// Render the template.
145
				self.template( control );
146
147
				// Init the control.
148
				if ( ! _.isUndefined( control.params ) && ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.element ) && 'textarea' === control.params.choices.element ) {
149
					kirki.input.textarea.init( control );
150
					return;
151
				}
152
				kirki.input.genericInput.init( control );
153
			},
154
155
			/**
156
			 * Render the template.
157
			 *
158
			 * @since 3.0.17
159
			 * @param {Object}  control - The customizer control object.
160
			 * @param {Object}  control.params - The control parameters.
161
			 * @param {string}  control.params.label - The control label.
162
			 * @param {string}  control.params.description - The control description.
163
			 * @param {string}  control.params.inputAttrs - extra input arguments.
164
			 * @param {string}  control.params.default - The default value.
165
			 * @param {Object}  control.params.choices - Any extra choices we may need.
166
			 * @param {boolean} control.params.choices.alpha - should we add an alpha channel?
167
			 * @param {string}  control.id - The setting.
168
			 * @returns {null}
169
			 */
170
			template: function( control ) {
171
				var args = {
172
						label: control.params.label,
173
						description: control.params.description,
174
						'data-id': control.id,
175
						inputAttrs: control.params.inputAttrs,
176
						choices: control.params.choices,
177
						value: kirki.setting.get( control.id )
178
				    },
179
				    template;
180
181
				if ( ! _.isUndefined( control.params ) && ! _.isUndefined( control.params.choices ) && ! _.isUndefined( control.params.choices.element ) && 'textarea' === control.params.choices.element ) {
182
					template = wp.template( 'kirki-input-textarea' );
183
					control.container.html( template( args ) );
184
					return;
185
				}
186
				template = wp.template( 'kirki-input-generic' );
187
				control.container.html( template( args ) );
188
			}
189
		},
190
191
		'kirki-select': {
192
193
			/**
194
			 * Init the control.
195
			 *
196
			 * @since 3.0.17
197
			 * @param {Object} control - The customizer control object.
198
			 * @returns {null}
199
			 */
200
			init: function( control ) {
201
				var self = this;
202
203
				// Render the template.
204
				self.template( control );
205
206
				// Init the control.
207
				kirki.input.select.init( control );
208
			},
209
210
			/**
211
			 * Render the template.
212
			 *
213
			 * @since 3.0.17
214
			 * @param {Object}  control - The customizer control object.
215
			 * @param {Object}  control.params - The control parameters.
216
			 * @param {string}  control.params.label - The control label.
217
			 * @param {string}  control.params.description - The control description.
218
			 * @param {string}  control.params.inputAttrs - extra input arguments.
219
			 * @param {Object}  control.params.default - The default value.
220
			 * @param {Object}  control.params.choices - The choices for the select dropdown.
221
			 * @param {string}  control.id - The setting.
222
			 * @returns {null}
223
			 */
224
			template: function( control ) {
225
				var template = wp.template( 'kirki-input-select' );
226
227
				control.container.html( template( {
228
					label: control.params.label,
229
					description: control.params.description,
230
					'data-id': control.id,
231
					inputAttrs: control.params.inputAttrs,
232
					choices: control.params.choices,
233
					value: kirki.setting.get( control.id ),
234
					multiple: control.params.multiple || 1,
235
					placeholder: control.params.placeholder
236
			    } ) );
237
			}
238
		}
239
	}
240
} );
241