­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ ­ /** * JavaScript code for the "Edit" screen * * @package TablePress * @subpackage Views JavaScript * @author Tobias Bäthge * @since 1.0.0 */ /* global alert, confirm, tp, tablepress_strings, tablepress_options, ajaxurl, wpLink, tb_show, wp, JSON */ // Ensure the global `tp` object exists. window.tp = window.tp || {}; jQuery( function( $ ) { 'use strict'; /* Wrapper to find elements in the page faster with JS-native functions */ var $id = function( element_id ) { return $( document.getElementById( element_id ) ); }; /** * TablePress object, mostly with functionality for the "Edit" screen * * @since 1.0.0 */ tp.made_changes = false; tp.table = { id: $id( 'table-id' ).val(), new_id: $id( 'table-new-id' ).val(), rows: parseInt( $id( 'number-rows' ).val(), 10 ), columns: parseInt( $id( 'number-columns' ).val(), 10 ), head: $id( 'option-table-head' ).prop( 'checked' ), foot: $id( 'option-table-foot' ).prop( 'checked' ), no_data_columns_pre: 2, no_data_columns_post: 1, body_cells_pre: '', body_cells_post: '', body_cell: '', head_cell: '', foot_cell: '', set_table_changed: function() { tp.made_changes = true; }, unset_table_changed: function() { tp.made_changes = false; $id( 'edit-form-body' ).one( 'change', 'textarea', tp.table.set_table_changed ); // @TODO: maybe use .tablepress-postbox-table here and further below $( '#tablepress_edit-table-information, #tablepress_edit-table-options, #tablepress_edit-datatables-features' ).one( 'change', 'input, textarea, select', tp.table.set_table_changed ); }, change_id: function( /* event */ ) { // empty table IDs are not allowed if ( '' === $id( 'table-new-id' ).val().toString().trim() ) { alert( tablepress_strings.table_id_not_empty ); $id( 'table-new-id' ).val( tp.table.new_id ).trigger( 'focus' ).trigger( 'select' ); return; } // the '0' table ID is not allowed if ( '0' === $id( 'table-new-id' ).val().toString().trim() ) { alert( tablepress_strings.table_id_not_zero ); $id( 'table-new-id' ).val( tp.table.new_id ).trigger( 'focus' ).trigger( 'select' ); return; } if ( this.value === tp.table.new_id ) { return; } if ( confirm( tablepress_strings.ays_change_table_id ) ) { tp.table.new_id = this.value; $( '.table-shortcode' ).val( '[' + tablepress_options.shortcode + ' id=' + tp.table.new_id + ' /]' ).trigger( 'click' ); // click() to focus and select tp.table.set_table_changed(); } else { $(this).val( tp.table.new_id ); } }, change_table_head: function( /* event */ ) { tp.table.head = $(this).prop( 'checked' ); $id( 'option-use-datatables' ).prop( 'disabled', ! tp.table.head ).trigger( 'change' ); $id( 'notice-datatables-head-row' ).toggle( ! tp.table.head ); tp.rows.stripe(); }, change_table_foot: function( /* event */ ) { tp.table.foot = $(this).prop( 'checked' ); tp.rows.stripe(); }, change_print_name_description: function( /* event */ ) { $id( this.id + '-position' ).prop( 'disabled', ! $(this).prop( 'checked' ) ); }, change_datatables: function() { var $datatables_checkbox = $id( 'option-use-datatables' ), checkboxes_disabled = ! ( $datatables_checkbox.prop( 'checked' ) && ! $datatables_checkbox.prop( 'disabled' ) ); $datatables_checkbox.closest( 'tbody' ).find( 'input' ).not( $datatables_checkbox ).prop( 'disabled', checkboxes_disabled ); tp.table.change_datatables_pagination(); }, change_datatables_pagination: function() { var $pagination_checkbox = $id( 'option-datatables-paginate' ), pagination_enabled = ( $pagination_checkbox.prop( 'checked' ) && ! $pagination_checkbox.prop( 'disabled' ) ); $id( 'option-datatables-lengthchange' ).prop( 'disabled', ! pagination_enabled ); $id( 'option-datatables-paginate_entries' ).prop( 'disabled', ! pagination_enabled ); }, prepare_ajax_request: function( wp_action, wp_nonce ) { var $table_body = $id( 'edit-form-body' ), table_data = [], table_options, table_number = { rows: tp.table.rows, columns: tp.table.columns, hidden_rows: 0, hidden_columns: 0 }, table_visibility = { rows: [], columns: [] }; $table_body.children().each( function( idx, row ) { table_data[ idx ] = $( row ).find( 'textarea' ) .map( function() { return this.value; } ) .get(); } ); table_data = JSON.stringify( table_data ); // @TODO: maybe for options saving: https://stackoverflow.com/questions/1184624/convert-form-data-to-javascript-object-with-jquery // or each()-loop through all checkboxes/textfields/selects table_options = { // Table Options table_head: tp.table.head, table_foot: tp.table.foot, alternating_row_colors: $id( 'option-alternating-row-colors' ).prop( 'checked' ), row_hover: $id( 'option-row-hover' ).prop( 'checked' ), print_name: $id( 'option-print-name' ).prop( 'checked' ), print_description: $id( 'option-print-description' ).prop( 'checked' ), print_name_position: $id( 'option-print-name-position' ).val(), print_description_position: $id( 'option-print-description-position' ).val(), extra_css_classes: $id( 'option-extra-css-classes' ).val(), // DataTables JS features use_datatables: $id( 'option-use-datatables' ).prop( 'checked' ), datatables_sort: $id( 'option-datatables-sort' ).prop( 'checked' ), datatables_filter: $id( 'option-datatables-filter' ).prop( 'checked' ), datatables_paginate: $id( 'option-datatables-paginate' ).prop( 'checked' ), datatables_lengthchange: $id( 'option-datatables-lengthchange' ).prop( 'checked' ), datatables_paginate_entries: $id( 'option-datatables-paginate_entries' ).val(), datatables_info: $id( 'option-datatables-info' ).prop( 'checked' ), datatables_scrollx: $id( 'option-datatables-scrollx' ).prop( 'checked' ), datatables_custom_commands: $id( 'option-datatables-custom-commands' ).val() }; table_options = JSON.stringify( table_options ); table_visibility.rows = $table_body.find( 'input[type="hidden"]' ) .map( function() { if ( '1' === $(this).val() ) { return 1; } table_number.hidden_rows += 1; return 0; } ) .get(); table_visibility.columns = $id( 'edit-form-foot' ).find( 'input[type="hidden"]' ) .map( function() { if ( '1' === $(this).val() ) { return 1; } table_number.hidden_columns += 1; return 0; } ) .get(); table_visibility = JSON.stringify( table_visibility ); // request_data = return { action: wp_action, _ajax_nonce : $( wp_nonce ).val(), tablepress: { id: tp.table.id, new_id: tp.table.new_id, name: $id( 'table-name' ).val(), description: $id( 'table-description' ).val(), number: table_number, data: table_data, options: table_options, visibility: table_visibility } }; }, preview: { trigger: function( /* event */ ) { if ( ! tp.made_changes ) { tp.table.preview.show( $(this).attr( 'href' ) + '&TB_iframe=true' ); return false; } // validation checks if ( $id( 'option-datatables-paginate' ).prop( 'checked' ) && ! ( /^[1-9][0-9]{0,4}$/ ).test( $id( 'option-datatables-paginate_entries' ).val() ) ) { alert( tablepress_strings.num_pagination_entries_invalid ); $id( 'option-datatables-paginate_entries' ).trigger( 'focus' ).trigger( 'select' ); return; } if ( ( /[^A-Za-z0-9- _:]/ ).test( $id( 'option-extra-css-classes' ).val() ) ) { alert( tablepress_strings.extra_css_classes_invalid ); $id( 'option-extra-css-classes' ).trigger( 'focus' ).trigger( 'select' ); return; } $(this).closest( 'p' ).append( '' ); $( 'body' ).addClass( 'wait' ); $id( 'table-preview' ).empty(); // clear preview $.ajax({ 'type': 'POST', 'url': ajaxurl, 'data': tp.table.prepare_ajax_request( 'tablepress_preview_table', '#nonce-preview-table' ), 'success': tp.table.preview.ajax_success, 'error': tp.table.preview.ajax_error, 'dataType': 'json' } ); return false; }, ajax_success: function( data, status /*, jqXHR */ ) { if ( ( 'undefined' === typeof status ) || ( 'success' !== status ) ) { tp.table.preview.error( 'AJAX call successful, but unclear status.' ); } else if ( ( 'undefined' === typeof data ) || ( null === data ) || ( '-1' === data ) || ( 'undefined' === typeof data.success ) || ( true !== data.success ) ) { tp.table.preview.error( 'AJAX call successful, but unclear data.' ); } else { tp.table.preview.success( data ); } }, ajax_error: function( jqXHR, status, error_thrown ) { tp.table.preview.error( 'AJAX call failed: ' + status + ' - ' + error_thrown ); }, success: function( data ) { $id( 'table-preview' ).empty(); $( '