Code Duplication    Length = 219-219 lines in 2 locations

controls/js/script.js 1 location

@@ 1151-1369 (lines=219) @@
1148
 * @see https://github.com/xwp/wp-customize-posts
1149
 */
1150
1151
( function() {
1152
	'use strict';
1153
1154
	/**
1155
	 * A dynamic color-alpha control.
1156
	 *
1157
	 * @class
1158
	 * @augments wp.customize.Control
1159
	 * @augments wp.customize.Class
1160
	 */
1161
	wp.customize.kirkiDynamicControl = wp.customize.Control.extend({
1162
1163
		initialize: function( id, options ) {
1164
			var control = this,
1165
			    args    = options || {};
1166
1167
			args.params = args.params || {};
1168
			if ( ! args.params.type ) {
1169
				args.params.type = 'kirki-generic';
1170
			}
1171
			if ( ! args.params.content ) {
1172
				args.params.content = jQuery( '<li></li>' );
1173
				args.params.content.attr( 'id', 'customize-control-' + id.replace( /]/g, '' ).replace( /\[/g, '-' ) );
1174
				args.params.content.attr( 'class', 'customize-control customize-control-' + args.params.type );
1175
			}
1176
1177
			control.propertyElements = [];
1178
			wp.customize.Control.prototype.initialize.call( control, id, args );
1179
		},
1180
1181
		/**
1182
		 * Add bidirectional data binding links between inputs and the setting(s).
1183
		 *
1184
		 * This is copied from wp.customize.Control.prototype.initialize(). It
1185
		 * should be changed in Core to be applied once the control is embedded.
1186
		 *
1187
		 * @private
1188
		 * @returns {null}
1189
		 */
1190
		_setUpSettingRootLinks: function() {
1191
			var control = this,
1192
			    nodes   = control.container.find( '[data-customize-setting-link]' );
1193
1194
			nodes.each( function() {
1195
				var node = jQuery( this );
1196
1197
				wp.customize( node.data( 'customizeSettingLink' ), function( setting ) {
1198
					var element = new wp.customize.Element( node );
1199
					control.elements.push( element );
1200
					element.sync( setting );
1201
					element.set( setting() );
1202
				});
1203
			});
1204
		},
1205
1206
		/**
1207
		 * Add bidirectional data binding links between inputs and the setting properties.
1208
		 *
1209
		 * @private
1210
		 * @returns {null}
1211
		 */
1212
		_setUpSettingPropertyLinks: function() {
1213
			var control = this,
1214
			    nodes;
1215
1216
			if ( ! control.setting ) {
1217
				return;
1218
			}
1219
1220
			nodes = control.container.find( '[data-customize-setting-property-link]' );
1221
1222
			nodes.each( function() {
1223
				var node = jQuery( this ),
1224
				    element,
1225
				    propertyName = node.data( 'customizeSettingPropertyLink' );
1226
1227
				element = new wp.customize.Element( node );
1228
				control.propertyElements.push( element );
1229
				element.set( control.setting()[ propertyName ] );
1230
1231
				element.bind( function( newPropertyValue ) {
1232
					var newSetting = control.setting();
1233
					if ( newPropertyValue === newSetting[ propertyName ] ) {
1234
						return;
1235
					}
1236
					newSetting = _.clone( newSetting );
1237
					newSetting[ propertyName ] = newPropertyValue;
1238
					control.setting.set( newSetting );
1239
				} );
1240
				control.setting.bind( function( newValue ) {
1241
					if ( newValue[ propertyName ] !== element.get() ) {
1242
						element.set( newValue[ propertyName ] );
1243
					}
1244
				} );
1245
			});
1246
		},
1247
1248
		/**
1249
		 * @inheritdoc
1250
		 */
1251
		ready: function() {
1252
			var control = this;
1253
1254
			control._setUpSettingRootLinks();
1255
			control._setUpSettingPropertyLinks();
1256
1257
			wp.customize.Control.prototype.ready.call( control );
1258
1259
			control.deferred.embedded.done( function() {
1260
				control.initKirkiControl( control );
1261
			});
1262
		},
1263
1264
		/**
1265
		 * Embed the control in the document.
1266
		 *
1267
		 * Override the embed() method to do nothing,
1268
		 * so that the control isn't embedded on load,
1269
		 * unless the containing section is already expanded.
1270
		 *
1271
		 * @returns {null}
1272
		 */
1273
		embed: function() {
1274
			var control   = this,
1275
			    sectionId = control.section();
1276
1277
			if ( ! sectionId ) {
1278
				return;
1279
			}
1280
1281
			wp.customize.section( sectionId, function( section ) {
1282
				if ( 'kirki-expanded' === section.params.type || section.expanded() || wp.customize.settings.autofocus.control === control.id ) {
1283
					control.actuallyEmbed();
1284
				} else {
1285
					section.expanded.bind( function( expanded ) {
1286
						if ( expanded ) {
1287
							control.actuallyEmbed();
1288
						}
1289
					} );
1290
				}
1291
			} );
1292
		},
1293
1294
		/**
1295
		 * Deferred embedding of control when actually
1296
		 *
1297
		 * This function is called in Section.onChangeExpanded() so the control
1298
		 * will only get embedded when the Section is first expanded.
1299
		 *
1300
		 * @returns {null}
1301
		 */
1302
		actuallyEmbed: function() {
1303
			var control = this;
1304
			if ( 'resolved' === control.deferred.embedded.state() ) {
1305
				return;
1306
			}
1307
			control.renderContent();
1308
			control.deferred.embedded.resolve(); // This triggers control.ready().
1309
		},
1310
1311
		/**
1312
		 * This is not working with autofocus.
1313
		 *
1314
		 * @param {object} [args] Args.
1315
		 * @returns {null}
1316
		 */
1317
		focus: function( args ) {
1318
			var control = this;
1319
			control.actuallyEmbed();
1320
			wp.customize.Control.prototype.focus.call( control, args );
1321
		},
1322
1323
		/**
1324
		 * Additional actions that run on ready.
1325
		 *
1326
		 * @param {object} [args] Args.
1327
		 * @returns {null}
1328
		 */
1329
		initKirkiControl: function( control ) {
1330
			if ( 'undefined' !== typeof kirki.control[ control.params.type ] ) {
1331
				kirki.control[ control.params.type ].init( control );
1332
				return;
1333
			}
1334
1335
			// Save the value
1336
			this.container.on( 'change keyup paste click', 'input', function() {
1337
				control.setting.set( jQuery( this ).val() );
1338
			});
1339
		},
1340
1341
		kirkiValidateCSSValue: function( value ) {
1342
1343
			var validUnits = ['rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax'],
1344
				numericValue,
1345
				unit;
1346
1347
			// 0 is always a valid value, and we can't check calc() values effectively.
1348
			if ( '0' === value || ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) ) {
1349
				return true;
1350
			}
1351
1352
			if ( 'auto' === value || 'inherit' === value || 'initial' === value ) {
1353
				return true;
1354
			}
1355
1356
			// Get the numeric value.
1357
			numericValue = parseFloat( value );
1358
1359
			// Get the unit
1360
			unit = value.replace( numericValue, '' );
1361
1362
			// Check the validity of the numeric value and units.
1363
			if ( isNaN( numericValue ) || -1 === jQuery.inArray( unit, validUnits ) ) {
1364
				return false;
1365
			}
1366
			return true;
1367
		}
1368
	});
1369
})();
1370
1371
_.each( kirki.control, function( obj, type ) {
1372
	wp.customize.controlConstructor[ type ] = wp.customize.kirkiDynamicControl.extend({});

controls/js/script-legacy.js 1 location

@@ 1151-1369 (lines=219) @@
1148
 * @see https://github.com/xwp/wp-customize-posts
1149
 */
1150
1151
( function() {
1152
	'use strict';
1153
1154
	/**
1155
	 * A dynamic color-alpha control.
1156
	 *
1157
	 * @class
1158
	 * @augments wp.customize.Control
1159
	 * @augments wp.customize.Class
1160
	 */
1161
	wp.customize.kirkiDynamicControl = wp.customize.Control.extend({
1162
1163
		initialize: function( id, options ) {
1164
			var control = this,
1165
			    args    = options || {};
1166
1167
			args.params = args.params || {};
1168
			if ( ! args.params.type ) {
1169
				args.params.type = 'kirki-generic';
1170
			}
1171
			if ( ! args.params.content ) {
1172
				args.params.content = jQuery( '<li></li>' );
1173
				args.params.content.attr( 'id', 'customize-control-' + id.replace( /]/g, '' ).replace( /\[/g, '-' ) );
1174
				args.params.content.attr( 'class', 'customize-control customize-control-' + args.params.type );
1175
			}
1176
1177
			control.propertyElements = [];
1178
			wp.customize.Control.prototype.initialize.call( control, id, args );
1179
		},
1180
1181
		/**
1182
		 * Add bidirectional data binding links between inputs and the setting(s).
1183
		 *
1184
		 * This is copied from wp.customize.Control.prototype.initialize(). It
1185
		 * should be changed in Core to be applied once the control is embedded.
1186
		 *
1187
		 * @private
1188
		 * @returns {null}
1189
		 */
1190
		_setUpSettingRootLinks: function() {
1191
			var control = this,
1192
			    nodes   = control.container.find( '[data-customize-setting-link]' );
1193
1194
			nodes.each( function() {
1195
				var node = jQuery( this );
1196
1197
				wp.customize( node.data( 'customizeSettingLink' ), function( setting ) {
1198
					var element = new wp.customize.Element( node );
1199
					control.elements.push( element );
1200
					element.sync( setting );
1201
					element.set( setting() );
1202
				});
1203
			});
1204
		},
1205
1206
		/**
1207
		 * Add bidirectional data binding links between inputs and the setting properties.
1208
		 *
1209
		 * @private
1210
		 * @returns {null}
1211
		 */
1212
		_setUpSettingPropertyLinks: function() {
1213
			var control = this,
1214
			    nodes;
1215
1216
			if ( ! control.setting ) {
1217
				return;
1218
			}
1219
1220
			nodes = control.container.find( '[data-customize-setting-property-link]' );
1221
1222
			nodes.each( function() {
1223
				var node = jQuery( this ),
1224
				    element,
1225
				    propertyName = node.data( 'customizeSettingPropertyLink' );
1226
1227
				element = new wp.customize.Element( node );
1228
				control.propertyElements.push( element );
1229
				element.set( control.setting()[ propertyName ] );
1230
1231
				element.bind( function( newPropertyValue ) {
1232
					var newSetting = control.setting();
1233
					if ( newPropertyValue === newSetting[ propertyName ] ) {
1234
						return;
1235
					}
1236
					newSetting = _.clone( newSetting );
1237
					newSetting[ propertyName ] = newPropertyValue;
1238
					control.setting.set( newSetting );
1239
				} );
1240
				control.setting.bind( function( newValue ) {
1241
					if ( newValue[ propertyName ] !== element.get() ) {
1242
						element.set( newValue[ propertyName ] );
1243
					}
1244
				} );
1245
			});
1246
		},
1247
1248
		/**
1249
		 * @inheritdoc
1250
		 */
1251
		ready: function() {
1252
			var control = this;
1253
1254
			control._setUpSettingRootLinks();
1255
			control._setUpSettingPropertyLinks();
1256
1257
			wp.customize.Control.prototype.ready.call( control );
1258
1259
			control.deferred.embedded.done( function() {
1260
				control.initKirkiControl( control );
1261
			});
1262
		},
1263
1264
		/**
1265
		 * Embed the control in the document.
1266
		 *
1267
		 * Override the embed() method to do nothing,
1268
		 * so that the control isn't embedded on load,
1269
		 * unless the containing section is already expanded.
1270
		 *
1271
		 * @returns {null}
1272
		 */
1273
		embed: function() {
1274
			var control   = this,
1275
			    sectionId = control.section();
1276
1277
			if ( ! sectionId ) {
1278
				return;
1279
			}
1280
1281
			wp.customize.section( sectionId, function( section ) {
1282
				if ( 'kirki-expanded' === section.params.type || section.expanded() || wp.customize.settings.autofocus.control === control.id ) {
1283
					control.actuallyEmbed();
1284
				} else {
1285
					section.expanded.bind( function( expanded ) {
1286
						if ( expanded ) {
1287
							control.actuallyEmbed();
1288
						}
1289
					} );
1290
				}
1291
			} );
1292
		},
1293
1294
		/**
1295
		 * Deferred embedding of control when actually
1296
		 *
1297
		 * This function is called in Section.onChangeExpanded() so the control
1298
		 * will only get embedded when the Section is first expanded.
1299
		 *
1300
		 * @returns {null}
1301
		 */
1302
		actuallyEmbed: function() {
1303
			var control = this;
1304
			if ( 'resolved' === control.deferred.embedded.state() ) {
1305
				return;
1306
			}
1307
			control.renderContent();
1308
			control.deferred.embedded.resolve(); // This triggers control.ready().
1309
		},
1310
1311
		/**
1312
		 * This is not working with autofocus.
1313
		 *
1314
		 * @param {object} [args] Args.
1315
		 * @returns {null}
1316
		 */
1317
		focus: function( args ) {
1318
			var control = this;
1319
			control.actuallyEmbed();
1320
			wp.customize.Control.prototype.focus.call( control, args );
1321
		},
1322
1323
		/**
1324
		 * Additional actions that run on ready.
1325
		 *
1326
		 * @param {object} [args] Args.
1327
		 * @returns {null}
1328
		 */
1329
		initKirkiControl: function( control ) {
1330
			if ( 'undefined' !== typeof kirki.control[ control.params.type ] ) {
1331
				kirki.control[ control.params.type ].init( control );
1332
				return;
1333
			}
1334
1335
			// Save the value
1336
			this.container.on( 'change keyup paste click', 'input', function() {
1337
				control.setting.set( jQuery( this ).val() );
1338
			});
1339
		},
1340
1341
		kirkiValidateCSSValue: function( value ) {
1342
1343
			var validUnits = ['rem', 'em', 'ex', '%', 'px', 'cm', 'mm', 'in', 'pt', 'pc', 'ch', 'vh', 'vw', 'vmin', 'vmax'],
1344
				numericValue,
1345
				unit;
1346
1347
			// 0 is always a valid value, and we can't check calc() values effectively.
1348
			if ( '0' === value || ( 0 <= value.indexOf( 'calc(' ) && 0 <= value.indexOf( ')' ) ) ) {
1349
				return true;
1350
			}
1351
1352
			if ( 'auto' === value || 'inherit' === value || 'initial' === value ) {
1353
				return true;
1354
			}
1355
1356
			// Get the numeric value.
1357
			numericValue = parseFloat( value );
1358
1359
			// Get the unit
1360
			unit = value.replace( numericValue, '' );
1361
1362
			// Check the validity of the numeric value and units.
1363
			if ( isNaN( numericValue ) || -1 === jQuery.inArray( unit, validUnits ) ) {
1364
				return false;
1365
			}
1366
			return true;
1367
		}
1368
	});
1369
})();
1370
1371
_.each( kirki.control, function( obj, type ) {
1372
	wp.customize.controlConstructor[ type ] = wp.customize.kirkiDynamicControl.extend({});