| 1 |
/** |
| 2 |
* Functionality for settings page |
| 3 |
*/ |
| 4 |
( function( $ ) { |
| 5 |
$( document ).ready( function() { |
| 6 |
/** |
| 7 |
* For responsive design |
| 8 |
*/ |
| 9 |
pdfprnt_add_labels(); |
| 10 |
$( window ).resize( function() { |
| 11 |
pdfprnt_add_labels(); |
| 12 |
} ); |
| 13 |
/** |
| 14 |
* Ajax request for load additional fonts |
| 15 |
*/ |
| 16 |
var input = $( 'input[name="pdfprnt_load_fonts"]' ); |
| 17 |
input.click( function() { |
| 18 |
input.attr( 'disabled', true ); |
| 19 |
$.ajax( { |
| 20 |
url: ajaxurl, |
| 21 |
type: "POST", |
| 22 |
data: { action: 'pdfprnt_load_fonts', pdfprnt_ajax_nonce: pdfprnt_var['ajax_nonce'] }, |
| 23 |
beforeSend: function() { |
| 24 |
$( '#pdfprnt_font_loader' ).css( 'display', 'inline-block' ); |
| 25 |
$( '.updated, .error' ).hide(); |
| 26 |
$( '<div class="updated fade"><p><strong>' + pdfprnt_var['loading_fonts'] + '.</strong></p></div>' ).insertAfter( ".nav-tab-wrapper" ); |
| 27 |
/* display 'warning'-window while fonts loading */ |
| 28 |
window.onbeforeunload = function(e) { |
| 29 |
if ( $( '#pdfprnt_font_loader' ).is( ':visible' ) ) |
| 30 |
return true; |
| 31 |
}; |
| 32 |
}, |
| 33 |
success: function( result ) { |
| 34 |
$( '#pdfprnt_font_loader, .updated, .error' ).hide(); |
| 35 |
try { |
| 36 |
var message = $.parseJSON( result ); |
| 37 |
} catch ( e ) { |
| 38 |
$( '<div class="error"><p><strong>' + result + pdfprnt_var['need_reload'] + '.</strong></p></div>' ).insertAfter( ".nav-tab-wrapper" ); |
| 39 |
input.attr( 'disabled', false ); |
| 40 |
return false; |
| 41 |
} |
| 42 |
if ( message['done'] ) { |
| 43 |
$( '<div class="updated fade"><p><strong>' + message['done'] + '.</strong></p></div>' ).insertAfter( ".nav-tab-wrapper" ); |
| 44 |
$( '#pdfprnt_load_fonts_button' ).hide(); |
| 45 |
} |
| 46 |
if ( message['error'] ) { |
| 47 |
$( '<div class="error"><p><strong>' + message['error'] + pdfprnt_var['need_reload'] + '.</strong></p></div>' ).insertAfter( ".nav-tab-wrapper" ); |
| 48 |
input.attr( 'disabled', false ); |
| 49 |
} |
| 50 |
} |
| 51 |
} ); |
| 52 |
return false; |
| 53 |
} ); |
| 54 |
|
| 55 |
if ( $( 'input[name="pdfprnt_use_custom_styles"]' ).length ) { |
| 56 |
var textarea = $( '.pdfprnt_custom_styles' ), |
| 57 |
add_editor = false; |
| 58 |
if ( textarea.is( ':visible' ) && ! textarea.parents( 'body' ).hasClass( 'rtl' ) && ! add_editor ) { /* excluding .rtl pages because codeMirror doesn`t work properly in case textarea is inside table td */ |
| 59 |
pdfprnt_add_editor(); |
| 60 |
add_editor = true; |
| 61 |
} |
| 62 |
$( 'input[name="pdfprnt_use_custom_styles"]' ).click( function() { |
| 63 |
if ( $( this ).is( ':checked' ) && ! $( this ).parents( 'body' ).hasClass( 'rtl' ) ) { /* excluding .rtl pages because codeMirror doesn`t work properly in case textarea is inside table td */ |
| 64 |
textarea.show(); |
| 65 |
if ( ! add_editor ) { |
| 66 |
pdfprnt_add_editor(); |
| 67 |
add_editor = true; |
| 68 |
} |
| 69 |
} else if ( $( this ).is( ':checked' ) && $( this ).parents( 'body' ).hasClass( 'rtl' ) ) { /* excluding .rtl pages because codeMirror doesn`t work properly in case textarea is inside table td */ |
| 70 |
textarea.show(); |
| 71 |
} else { |
| 72 |
textarea.hide(); |
| 73 |
} |
| 74 |
} ); |
| 75 |
} |
| 76 |
} ); |
| 77 |
} )( jQuery ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Add labels to 'position of buttons'-table on settings page |
| 81 |
*/ |
| 82 |
function pdfprnt_add_labels() { |
| 83 |
( function( $ ) { |
| 84 |
var labels = [], |
| 85 |
i = 0; |
| 86 |
if ( $( window ).width() <= 785 ) { |
| 87 |
if ( ! $( '.pdfprnt_label' ).length ) { |
| 88 |
/* get text of column headers */ |
| 89 |
$( '.pdfprnt_table_head' ).children().each( function() { |
| 90 |
labels[i] = $( this ).text(); |
| 91 |
i ++; |
| 92 |
} ); |
| 93 |
/* add labels */ |
| 94 |
for ( i = 1; i < 5; i ++ ) { |
| 95 |
html = '<label class="pdfprnt_label">' + labels[ i - 1 ] +'</label>'; |
| 96 |
$( '.pdfprnt_pdf_button td:nth-child(' + i + '), .pdfprnt_print_button td:nth-child(' + i + ')' ).append( html ); |
| 97 |
$( '.pdfprnt_position_button td:nth-child(' + i + '), .pdfprnt_layout td:nth-child(' + i + ')' ).prepend( html ); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} )( jQuery ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Initialize CSS highlighter |
| 106 |
*/ |
| 107 |
function pdfprnt_add_editor() { |
| 108 |
var editor = CodeMirror.fromTextArea( |
| 109 |
document.getElementById( "pdfprnt_custom_styles" ), { |
| 110 |
mode: "css", |
| 111 |
theme: "default", |
| 112 |
styleActiveLine: true, |
| 113 |
matchBrackets: true, |
| 114 |
lineNumbers: true |
| 115 |
} |
| 116 |
); |
| 117 |
} |