/** * Theme Name: BlogCentral * Theme URI: http://4bzthemes.com * Author: 4bzthemes * Author URI: http://4bzthemes.com * File Description: javascript admin file included on the theme options, post edit, and user profile pages. * * @since BlogCentral 1.0.0 * * @package BlogCentral * @subpackage admin.js ------------------------------------------------------------------------ Table of Contents 1. Utility Functions 2. Theme Options Page Functions 3. Event Handlers -------------------------------------------------------------------------*/ /** * 1. Utility Functions *-----------------------------------------------------------------------*/ /** * Split an id by '-' * * @since 1.0.0 * @param string id. Required. Id to be parsed. * @return Array. The id parsed into parts. */ function blogcentral_parse_id( id ) { return id.split( "-" ); } /** * Get the index of an element by parsing its id. * * @since 1.0.0 * * @param string id. Required. Id to be parsed. * @param int offset. Required. Array offset. * @return Int. The index. */ function blogcentral_get_index( id, offset ) { var split = blogcentral_parse_id( id ); return parseInt( split[split.length-offset] ); } /** * Get the prefix of an element's id. * * Used when there's a group of elements with the same base id, differing only in its prefix. * * @since 1.0.0 * * @param string id. Id to be parsed. * @return string. The id's prefix. */ function blogcentral_get_name( id ) { var split = blogcentral_parse_id( id ); return split[0]; } ( function( $ ) { "use strict"; /** * 2. Theme Options Pages Functions *-----------------------------------------------------------------------*/ /** * Handle font selection. * * @since 1.0.0 * * @param object event. Object passed to handler by jquery. */ function blogcentral_font_options( event ) { var target = $( event.target ), val = target.val(), id = target.attr( 'id' ), i = blogcentral_get_name( id ), parent = target.parent(), font_class = target.find( ":selected" ).attr( 'class' ), type = '', paramsJson = '', url = target.data( 'blogcentral-font-url' ); if ( 'no_font' === val ) { $( '#' + i + '-font-variant-cont' ).text( '' ); $( '#' + i + '-font-subsets-cont' ).text( '' ); return; } if ( 'system_fonts' === font_class ) { type = 'system'; } else { if ( 'google_fonts' === font_class ) { type = 'google'; } } $( 'input[type=hidden]', parent ).val( type ); $( '#' + i + '-font-variant-cont' ).text( '' ); $( '#' + i + '-font-subsets-cont' ).text( '' ); paramsJson = { 'font': val, 'name': i, 'type': font_class }; // Make the ajax call to retrieve the font options for the font selected. $.ajax( { url: url, type: "POST", timeout: 5000, dataType: "json", error: function( xhr ) { alert( 'error in ajax '+ xhr.status + ' ' + xhr.statusText ); }, data: paramsJson, beforeSend: function() { $( '.loader', parent ).show(); }, complete: function(){ $( '.loader', parent ).hide(); }, success: function( data ) { /* * data should hold the variants and subsets fragment that should be appended to * the fonts container. */ $( '#' + i + '-font-variant-cont' ).append( data.variants ); $( '#' + i + '-font-subsets-cont' ).append( data.subsets ); } } ); // End of ajax call. } /** * Show component layout specific options. * * @since 1.0.0 * @param object event. Object passed to handler by jquery. */ function blogcentral_show_sub_options( event ) { var target = $( event.target ), val = target.val(), name = target.data( 'blogcentral-name' ), html = '', img_file = ''; $( '.' + name + '-layout-sub-layout' ).css( 'display', 'none' ); $( '.' + name + '-layout-sub-' + val, $( this ).closest( 'td' ) ).css( 'display', 'block' ); } /** * Handle border selection * * @since 1.0.0 * @param object event. Object passed to handler by jquery. */ function blogcentral_show_border( event ) { var target = $( event.target ), parent = target.parent(), val = target.val(), border = $( '.border-ex', parent ); border .text( '' ) .removeClass( 'div1 div2 div3' ); if ( 'default' !== val && '0' !== val ) { border .text( val ) .addClass( val ); } } /** * Handle slide template selection. * * @since 1.0.0 * @param object event. Object passed to handler by jquery. */ function blogcentral_get_template( event ) { var target = $( event.target ), val = target.val(), parent = target.parent(), url = blogcentral_object.admin_template_ajax, paramsJson = ''; // Send the selected template. paramsJson = { 'template': val }; // Make the ajax call to retrieve the template code for the template selected. $.ajax( { url: url, type: "POST", timeout: 5000, dataType: "json", error: function( xhr ) { alert( 'error in ajax '+ xhr.status + ' ' + xhr.statusText ); }, data: paramsJson, beforeSend: function() { $( '.loader', parent ).show(); }, complete: function(){ $( '.loader', parent ).hide(); }, success: function( data ) { // Insert the template code into the textarea. $( '.slide-html', parent ).val( data.template ); } } ); } /** * 3. Event Handlers *-----------------------------------------------------------------------*/ $( document ).on( 'ready', function() { // Tabs $( '.tabs-layout1' ).tabs( { show: { effect: "fadeIn", duration:100 }} ); $( '.tabs-layout2' ).tabs( { show: { effect: "fadeIn", duration:100 }} ); /** * WP Color Picker *-----------------------------------------------------------------------*/ // For theme options page and shortcode builder. $( '.theme-options .blogcentral-my-color-field' ).wpColorPicker(); /** * General Theme Options *-----------------------------------------------------------------------*/ $( 'body' ).on( 'click', '.layout-show', blogcentral_show_sub_options ); $( 'body' ).on( 'change', '.header-border-select', blogcentral_show_border ); $( 'body' ).on( 'change', '.template-select', blogcentral_get_template ); $( '.font-select' ).change( blogcentral_font_options ); }); })( jQuery );