ru-se.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

typography.js (9295B)


      1 (function ($) {
      2 wp.customize.controlConstructor['kirki-typography'] = wp.customize.Control.extend({
      3 
      4 	ready: function() {
      5 
      6 		'use strict';
      7 
      8 		var control               = this,
      9 		    fontFamilySelector    = control.selector + ' .font-family select',
     10 		    variantSelector       = control.selector + ' .variant select',
     11 		    subsetSelector        = control.selector + ' .subsets select',
     12 		    textTransformSelector = control.selector + ' .text-transform select',
     13 		    hasDefault            = false,
     14 		    firstAvailable        = false,
     15 		    activeItem,
     16 		    value = {},
     17 		    renderSubControl,
     18 		    picker;
     19 
     20 		  $(control.selector + ' .addwebfont').on("click", function() {
     21 				top.wp.customize.control('web_fonts').focus()
     22 		  })
     23 
     24 		// Make sure everything we're going to need exists.
     25 		_.each( control.params['default'], function( defaultParamValue, param ) {
     26 			if ( false !== defaultParamValue ) {
     27 				value[ param ] = defaultParamValue;
     28 				if ( undefined !== control.setting._value[ param ] ) {
     29 					value[ param ] = control.setting._value[ param ];
     30 				}
     31 			}
     32 		});
     33 
     34 		_.each( control.setting._value, function( subValue, param ) {
     35 			if ( undefined === value[ param ] || 'undefined' === typeof value[ param ] ) {
     36 				value[ param ] = subValue;
     37 			}
     38 		});
     39 
     40             function updateValue() {
     41                 value = _.clone(control.setting._value);
     42                 return value;
     43             }
     44 
     45 		// Renders and refreshes selectize sub-controls.
     46 		renderSubControl = function( fontFamily, sub, startValue ) {
     47 
     48 			var subSelector = ( 'variant' === sub ) ? variantSelector : subsetSelector,
     49 			    isStandard = false,
     50 			    subList = {},
     51 			    subValue,
     52 			    subsetValues,
     53 			    subsetValuesArray,
     54 			    subSelectize;
     55 
     56 			// Destroy the selectize instance.
     57 			if ( undefined !== jQuery( subSelector ).selectize()[0] ) {
     58 				jQuery( subSelector ).selectize()[0].selectize.destroy();
     59 			}
     60 
     61 			// Get all items in the sub-list for the active font-family.
     62 			_.each( kirkiAllFonts, function( font, i ) {
     63 
     64 				// Find the font-family we've selected in the global array of fonts.
     65 				if ( fontFamily === font.family ) {
     66 
     67 					// Check if this is a standard font or a google-font.
     68 					if ( undefined !== font.isStandard && true === font.isStandard ) {
     69 						isStandard = true;
     70 					}
     71 
     72 					if ( 'variant' === sub ) {
     73 						subList = font.variants;
     74 					} else if ( 'subsets' === sub ) {
     75 						subList = font.subsets;
     76 					}
     77 
     78 				}
     79 
     80 			});
     81 
     82 			// This is a googlefont, or we're talking subsets.
     83 			if ( false === isStandard || 'subsets' !== sub ) {
     84 
     85 				// Determine the initial value we have to use
     86 				if ( null === startValue  ) {
     87 
     88 					if ( 'variant' === sub ) { // The context here is variants
     89 
     90 						// Loop the variants.
     91 						_.each( subList, function( variant ) {
     92 
     93 							var defaultValue;
     94 
     95 							if ( undefined !== variant.id ) {
     96 
     97 								activeItem = value.variant;
     98 
     99 							} else {
    100 
    101 								defaultValue = 'regular';
    102 
    103 								if ( defaultValue === variant.id ) {
    104 									hasDefault = true;
    105 								} else if ( false === firstAvailable ) {
    106 									firstAvailable = variant.id;
    107 								}
    108 
    109 							}
    110 
    111 						});
    112 
    113 					} else if ( 'subsets' === sub ) { // The context here is subsets
    114 
    115 						subsetValues = {};
    116 
    117 						_.each( subList, function( subSet ) {
    118 							if ( null !== value.subsets ) {
    119 								_.each( value.subsets, function( item ) {
    120 									if ( undefined !== subSet && item === subSet.id ) {
    121 										subsetValues[ item ] = item;
    122 									}
    123 								});
    124 							}
    125 						});
    126 
    127 						if ( 0 !== subsetValues.length ) {
    128 							subsetValuesArray = jQuery.map( subsetValues, function( value, index ) {
    129 								return [value];
    130 							});
    131 							activeItem = subsetValuesArray;
    132 						}
    133 
    134 					}
    135 
    136 					// If we have a valid setting, use it.
    137 					// If not, check if the default value exists.
    138 					// If not, then use the 1st available option.
    139 					subValue = ( undefined !== activeItem ) ? activeItem : ( false !== hasDefault ) ? 'regular' : firstAvailable;
    140 
    141 				} else {
    142 
    143 					subValue = startValue;
    144 
    145 				}
    146 
    147 				// Create
    148 				subSelectize = jQuery( subSelector ).selectize({
    149 					maxItems:    ( 'variant' === sub ) ? 1 : null,
    150 					valueField:  'id',
    151 					labelField:  'label',
    152 					searchField: ['label'],
    153 					options:     subList,
    154 					items:       ( 'variant' === sub ) ? [ subValue ] : subValue,
    155 					create:      false,
    156 					plugins:     ( 'variant' === sub ) ? '' : ['remove_button'],
    157 					render: {
    158 						item: function( item, escape ) {
    159 							return '<div>' + escape( item.label ) + '</div>';
    160 						},
    161 						option: function( item, escape ) {
    162 							return '<div>' + escape( item.label ) + '</div>';
    163 						}
    164 					}
    165 				}).data( 'selectize' );
    166 
    167 			}
    168 
    169 			if ( true === isStandard ) {
    170 
    171 				// Hide unrelated fields on standard fonts.
    172 				control.container.find( '.hide-on-standard-fonts' ).css( 'display', 'none' );
    173 			} else {
    174 
    175 				if ( 2 > subList.length ) {
    176 
    177 					// If only 1 option is available then there's no reason to show this.
    178 					control.container.find( '.kirki-' + sub + '-wrapper' ).css( 'display', 'none' );
    179 				} else {
    180 					control.container.find( '.kirki-' + sub + '-wrapper' ).css( 'display', 'block' );
    181 				}
    182 
    183 			}
    184 
    185 		};
    186 
    187 		// Render the font-family
    188 		jQuery( fontFamilySelector ).selectize({
    189 			options:     kirkiAllFonts,
    190 			items:       [ control.setting._value['font-family'] ],
    191 			persist:     false,
    192 			maxItems:    1,
    193 			valueField:  'family',
    194 			labelField:  'label',
    195 			searchField: ['family', 'label', 'subsets'],
    196 			create:      false,
    197 			render: {
    198 				item: function( item, escape ) {
    199 					return '<div>' + escape( item.label ) + '</div>';
    200 				},
    201 				option: function( item, escape ) {
    202 					return '<div>' + escape( item.label ) + '</div>';
    203 				}
    204 			}
    205 		});
    206 
    207 		// Render the variants
    208 		// Please note that when the value of font-family changes,
    209 		// this will be destroyed and re-created.
    210 		renderSubControl( value['font-family'], 'variant', value.variant );
    211 
    212 		// Render the subsets
    213 		// Please note that when the value of font-family changes,
    214 		// this will be destroyed and re-created.
    215 		renderSubControl( value['font-family'], 'subsets', value.subsets );
    216 
    217 		this.container.on( 'change', '.font-family select', function() {
    218 			updateValue();
    219 
    220 			// Add the value to the array and set the setting's value
    221 			value['font-family'] = jQuery( this ).val();
    222 			control.saveValue( value );
    223 
    224 			// Trigger changes to variants & subsets
    225 			renderSubControl( jQuery( this ).val(), 'variant', null );
    226 			renderSubControl( jQuery( this ).val(), 'subsets', null );
    227 
    228 			// Refresh the preview
    229 			// wp.customize.previewer.refresh();
    230 
    231 		});
    232 
    233 		this.container.on( 'change', '.variant select', function() {
    234 			updateValue();
    235 			// Add the value to the array and set the setting's value
    236 			value.variant = jQuery( this ).val();
    237 			control.saveValue( value );
    238 
    239 			// Refresh the preview
    240 			// wp.customize.previewer.refresh();
    241 
    242 		});
    243 
    244 		this.container.on( 'change', '.subsets select', function() {
    245 			updateValue();
    246 			// Add the value to the array and set the setting's value.
    247 			value.subsets = jQuery( this ).val();
    248 			control.saveValue( value );
    249 
    250 			// Refresh the preview
    251 			wp.customize.previewer.refresh();
    252 
    253 		});
    254 
    255 		this.container.on( 'change keyup paste', '.font-size input', function() {
    256 			updateValue();
    257 			// Add the value to the array and set the setting's value
    258 			value['font-size'] = jQuery( this ).val();
    259 			control.saveValue( value );
    260 
    261 		});
    262 
    263 		this.container.on( 'change keyup paste', '.mobile-font-size input', function() {
    264 			updateValue();
    265 			// Add the value to the array and set the setting's value
    266 			value['mobile-font-size'] = jQuery( this ).val();
    267 			control.saveValue( value );
    268 
    269 		});
    270 
    271 		this.container.on( 'change keyup paste', '.line-height input', function() {
    272 			updateValue();
    273 			// Add the value to the array and set the setting's value
    274 			value['line-height'] = jQuery( this ).val();
    275 			control.saveValue( value );
    276 
    277 		});
    278 
    279 		this.container.on( 'change keyup paste', '.letter-spacing input', function() {
    280 			updateValue();
    281 			// Add the value to the array and set the setting's value
    282 			value['letter-spacing'] = jQuery( this ).val();
    283 			control.saveValue( value );
    284 
    285 		});
    286 
    287 		this.container.on( 'change', '.text-align input', function() {
    288 			updateValue();
    289 			// Add the value to the array and set the setting's value.
    290 			value['text-align'] = jQuery( this ).val();
    291 			control.saveValue( value );
    292 
    293 		});
    294 
    295 		// Text-transform
    296 		jQuery( textTransformSelector ).selectize();
    297 		this.container.on( 'change', '.text-transform select', function() {
    298 			updateValue();
    299 			// Add the value to the array and set the setting's value.
    300 			value['text-transform'] = jQuery( this ).val();
    301 			control.saveValue( value );
    302 
    303 		});
    304 
    305 		picker = this.container.find( '.kirki-color-control' );
    306 
    307             if (picker.length) {
    308                 kirki.kirkiGetColorPicker(picker, function (color) {
    309                     value = updateValue();
    310                     value['color'] = color;
    311                     control.saveValue(value);
    312                 });
    313             }
    314 
    315 	},
    316 
    317 	/**
    318 	 * Saves the value.
    319 	 */
    320 	saveValue: function( value ) {
    321 
    322 		'use strict';
    323 
    324 		var control  = this,
    325 		    newValue = {};
    326 
    327 		_.each( value, function( newSubValue, i ) {
    328 			newValue[ i ] = newSubValue;
    329 		});
    330 
    331 		control.setting.set( newValue );
    332 	}
    333 
    334     });
    335 })(jQuery);