/**
* Functionality for settings page
*/
(function($) {
$(document).ready( function() {
/**
* For responsive design
*/
pdfprnt_add_labels();
$( window ).resize( function() {
pdfprnt_add_labels();
});
/**
* Ajax request for load additional fonts
*/
var input = $( 'input[name="pdfprnt_load_fonts"]' );
input.click( function() {
input.attr( 'disabled', true );
$.ajax({
url: ajaxurl,
type: "POST",
data: { action: 'pdfprnt_load_fonts', pdfprnt_ajax_nonce: pdfprnt_var['ajax_nonce'] },
beforeSend: function() {
$( '#pdfprnt_font_loader' ).css( 'display', 'inline-block' );
$( '.updated, .error' ).hide();
$( '
' + pdfprnt_var['loading_fonts'] + '.
' ).insertAfter( ".nav-tab-wrapper" );
/* display 'warning'-window while fonts loading */
window.onbeforeunload = function(e) {
if ( $( '#pdfprnt_font_loader' ).is( ':visible' ) )
return true;
};
},
success: function( result ) {
$( '#pdfprnt_font_loader, .updated, .error' ).hide();
try {
var message = $.parseJSON( result );
} catch ( e ) {
$( '' + result + pdfprnt_var['need_reload'] + '.
' ).insertAfter( ".nav-tab-wrapper" );
input.attr( 'disabled', false );
return false;
}
if ( message['done'] ) {
$( '' ).insertAfter( ".nav-tab-wrapper" );
$( '#pdfprnt_load_fonts_button' ).hide();
}
if ( message['error'] ) {
$( '' + message['error'] + pdfprnt_var['need_reload'] + '.
' ).insertAfter( ".nav-tab-wrapper" );
input.attr( 'disabled', false );
}
}
});
return false;
});
if ( $( 'input[name="pdfprnt_use_custom_styles"]' ).length ) {
var textarea = $( '.pdfprnt_custom_styles' ),
add_editor = false;
if ( textarea.is( ':visible' ) && ! add_editor ) {
pdfprnt_add_editor();
add_editor = true;
}
$( 'input[name="pdfprnt_use_custom_styles"]' ).click( function() {
if ( $( this ).is( ':checked' ) ) {
textarea.show();
if ( ! add_editor ) {
pdfprnt_add_editor();
add_editor = true;
}
} else
textarea.hide();
});
}
});
})(jQuery);
/**
* Add labels to 'position of buttons'-table on settings page
*/
function pdfprnt_add_labels() {
(function($) {
var labels = [],
i = 0;
if ( $(window).width() <= 785 ) {
if ( ! $( '.pdfprnt_label' ).length ) {
/* get text of column headers */
$( '.pdfprnt_table_head' ).children().each( function() {
labels[i] = $( this ).text();
i ++;
});
/* add labels */
for ( i = 1; i < 5; i ++ ) {
html = '';
$( '.pdfprnt_pdf_buttton td:nth-child(' + i + '), .pdfprnt_print_buttton td:nth-child(' + i + ')' ).append( html );
$( '.pdfprnt_position_buttton td:nth-child(' + i + '), .pdfprnt_layout td:nth-child(' + i + ')' ).prepend( html );
}
}
}
})(jQuery);
}
/**
* Initialize CSS highlighter
*/
function pdfprnt_add_editor() {
var editor = CodeMirror.fromTextArea(
document.getElementById( "pdfprnt_custom_styles" ), {
mode: "css",
theme: "default",
styleActiveLine: true,
matchBrackets: true,
lineNumbers: true
}
);
}