• File: customizer.js
  • Full Path: /home/chassiw/www/wp-content/themes/reon/customize/assets/customizer.js
  • File size: 2.31 KB
  • MIME-type: text/plain
  • Charset: utf-8
jQuery( document ).ready(function($) {
	"use strict";

	
	/**
	 * Googe Font Select Custom Control
	 *
	 * @author Anthony Hortin <http://maddisondesigns.com>
	 * @license http://www.gnu.org/licenses/gpl-2.0.html
	 * @link https://github.com/maddisondesigns
	 */

	$('.google-fonts-list').each(function (i, obj) {
		if (!$(obj).hasClass('select2-hidden-accessible')) {
			$(obj).select2();
		}
	});

	$('.google-fonts-list').on('change', function() {
		var elementRegularWeight = $(this).parent().parent().find('.google-fonts-regularweight-style');

		
		

		var selectedFont = $(this).val();
		var customizerControlName = $(this).attr('control-name');

		// Clear Weight/Style dropdowns
		elementRegularWeight.empty();
		
		
		
		

		// Get the Google Fonts control object
		var bodyfontcontrol = _wpCustomizeSettings.controls[customizerControlName];

		// Find the index of the selected font
		var indexes = $.map(bodyfontcontrol.ovafontslist, function(obj, index) {
			if(obj.family === selectedFont) {
				return index;
			}
		});
		var index = indexes[0];

		// For the selected Google font show the available weight/style variants
		$.each(bodyfontcontrol.ovafontslist[index].variants, function(val, text) {
			elementRegularWeight.append(
				$('<option></option>').val(text).html(text)
			);
		});

		$(this).parent().parent().find( '.google-fonts-weight-string' ).val( bodyfontcontrol.ovafontslist[index].variants.toString() );

		// Update the font category based on the selected font
		$(this).parent().parent().find('.google-fonts-category').val(bodyfontcontrol.ovafontslist[index].category);
		

		ovaGetAllSelects($(this).parent().parent());
	});

	$('.google_fonts_select_control select').on('change', function() {
		ovaGetAllSelects($(this).parent().parent());
	});

	function ovaGetAllSelects($element) {
		var regularweight = $element.find('.google-fonts-regularweight-style').val();
		if( regularweight != null ) regularweight = regularweight.join();

		var selectedFont = {
			font: $element.find('.google-fonts-list').val(),
			regularweight: regularweight,
			category: $element.find('.google-fonts-category').val(),
		};
		

		// Important! Make sure to trigger change event so Customizer knows it has to save the field
		$element.find('.customize-control-google-font-selection').val(JSON.stringify(selectedFont)).trigger('change');
	}

});