/*global redux_change, redux*/
/**
* Typography
* Dependencies: google.com, jquery, select2
* Feature added by: Dovy Paukstys - http://simplerain.com/
* Date: 06.14.2013
*
* Rewrite: Kevin Provance (kprovance)
* Date: May 25, 2014
*/
(function( $ ) {
"use strict";
redux.field_objects = redux.field_objects || {};
redux.field_objects.typography = redux.field_objects.typography || {};
var selVals = [];
var isSelecting = false;
var default_params = {
width: 'resolve',
triggerChange: true,
allowClear: true
};
$( document ).ready(
function() {
//redux.field_objects.typography.init();
}
);
redux.field_objects.typography.init = function( selector, skipCheck) {
if ( !selector ) {
selector = $( document ).find( ".redux-group-tab:visible" ).find( '.redux-container-typography:visible' );
}
$( selector ).each(
function() {
var el = $( this );
var parent = el;
if ( !el.hasClass( 'redux-field-container' ) ) {
parent = el.parents( '.redux-field-container:first' );
}
if ( parent.is( ":hidden" ) ) { // Skip hidden fields
return;
}
if ( parent.hasClass( 'redux-field-init' ) ) {
parent.removeClass( 'redux-field-init' );
} else {
return;
}
var fontClear;
el.each(
function() {
// init each typography field
$( this ).find( '.redux-typography-container' ).each(
function() {
var family = $( this ).find( '.redux-typography-family' );
if ( family.data( 'value' ) === undefined ) {
family = $( this );
} else if ( family.data( 'value' ) !== "" ) {
$( family ).val( family.data( 'value' ) );
}
var select2_handle = $( this ).find( '.select2_params' );
if ( select2_handle.size() > 0 ) {
var select2_params = select2_handle.val();
select2_params = JSON.parse( select2_params );
default_params = $.extend( {}, default_params, select2_params );
}
fontClear = Boolean( $( this ).find( '.redux-font-clear' ).val() );
redux.field_objects.typography.select( family, true );
window.onbeforeunload = null;
}
);
//init when value is changed
$( this ).find( '.redux-typography' ).on(
'change', function() {
redux.field_objects.typography.select( $( this ) ); //.parents('.redux-container-typography:first'));
}
);
//init when value is changed
$( this ).find( '.redux-typography-size, .redux-typography-height, .redux-typography-word, .redux-typography-letter, .redux-typography-align, .redux-typography-transform, .redux-typography-font-variant, .redux-typography-decoration' ).keyup(
function() {
redux.field_objects.typography.select( $( this ).parents( '.redux-container-typography:first' ) );
}
);
// Have to redeclare the wpColorPicker to get a callback function
$( this ).find( '.redux-typography-color' ).wpColorPicker(
{
change: function( e, ui ) {
$( this ).val( ui.color.toString() );
redux.field_objects.typography.select( $( this ).parents( '.redux-container-typography:first' ) );
}
}
);
// Don't allow negative numbers for size field
$( this ).find( ".redux-typography-size" ).numeric(
{
allowMinus: false
}
);
// Allow negative numbers for indicated fields
$( this ).find( ".redux-typography-height, .redux-typography-word, .redux-typography-letter" ).numeric(
{
allowMinus: true
}
);
// select2 magic, to load font-family dynamically
var data = [{id: 'none', text: 'none'}];
$( this ).find( ".redux-typography-family" ).select2(
{
matcher: function( term, text ) {
return text.toUpperCase().indexOf( term.toUpperCase() ) === 0;
},
query: function( query ) {
return window.Select2.query.local( data )( query );
},
initSelection: function( element, callback ) {
var data = {id: element.val(), text: element.val()};
callback( data );
},
allowClear: fontClear,
// when one clicks on the font-family select box
}
).on(
"select2-opening", function( e ) {
// Get field ID
var thisID = $( this ).parents( '.redux-container-typography:first' ).attr( 'data-id' );
// User included fonts?
var isUserFonts = $( '#' + thisID + ' .redux-typography-font-family' ).data( 'user-fonts' );
isUserFonts = isUserFonts ? 1 : 0;
// Google font isn use?
var usingGoogleFonts = $( '#' + thisID + ' .redux-typography-google' ).val();
usingGoogleFonts = usingGoogleFonts ? 1 : 0;
// Set up data array
var buildData = [];
// If custom fonts, push onto array
if ( redux.customfonts !== undefined ) {
buildData.push( redux.customfonts );
}
// If standard fonts, push onto array
if ( redux.stdfonts !== undefined && isUserFonts === 0 ) {
buildData.push( redux.stdfonts );
}
// If user fonts, pull from localize and push into array
if ( isUserFonts == 1 ) {
var fontKids = [];
//