• Resolved poppydev

    (@poppydev)


    Dear Admin,

    Had to hack your plugin for a customer to use that allowed me to hide specific options on the jexcel popup menu when viewing Table Contend on the plugin admin.

    This small script is handy for other users who would like to keep this for their customers but want to hide tools to prevent removal of changes to the table.

    All you need to do is enqueue a custom admin script

    Example of custom CSS/JS for WP admin:

    // WordPress - Add custom css for WP admin
    function custom_admin_styles() {
    // Check if the current user has the Editor role. Change this so that it doesn't effect Admin
    if (current_user_can('editor')) {
    wp_enqueue_style('custom-admin', plugin_dir_url(__FILE__) . 'assets/css/style-admin.css');
    }
    }
    add_action('admin_enqueue_scripts', 'custom_admin_styles');

    jQuery to allow for class to be added to menu in Tablepress:

    jQuery(document).ready(function($) {
    function addClassesToDivs() {
    // Select all divs within .jexcel_contextmenu that contain an <a> tag
    $('.jexcel_contextmenu div').each(function() {
    var $link = $(this).find('a').first(); // Find the first <a> tag within the div
    if ($link.length > 0) {
    var linkText = $link.text().trim(); // Get the text content and trim any extra whitespace
    var className = textToClassName(linkText); // Generate a valid class name from the link text
    $(this).addClass(className); // Add the generated class to the div
    }
    });
    }

    // Function to sanitize the text and convert it to a valid class name
    function textToClassName(text) {
    return text.toLowerCase().replace(/[^a-z0-9]+/g, '-');
    }

    // Initial run
    addClassesToDivs();

    // Watch for changes using MutationObserver for dynamic content updates
    var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
    if (mutation.type === 'childList') {
    addClassesToDivs(); // Re-run the function when DOM changes occur
    }
    });
    });

    // Configure and start the observer to watch the body (or a specific container if needed)
    observer.observe(document.body, { childList: true, subtree: true });
    });
    Example of DOM changes

    From: <div><a>Cut</a><span>Ctrl+X</span></div>

    To: <div class="cut"><a>Cut</a><span>Ctrl+X</span></div>

    To target this selector using CSS, you can do the following:

    .jexcel_contextmenu .cut {
    display: none;
    }

    This might be something you add to your plugin as a feature update, or implement this somehow to the edit.js.

    Enjoy. P.S. Please do share if you find a cleaner solution to the above.

    • This topic was modified 8 months ago by poppydev.
    • This topic was modified 8 months ago by poppydev.
    • This topic was modified 8 months ago by poppydev.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi!

    Thanks for your post and sorry for the trouble!

    That’s an interesting challenge and I can see that it can be useful to hide certain elements of the context menu (or other UI).

    Right now, I can’t think of an easy (and existing) approach for this, so your idea is probably the best solution for now.

    In the future, I think that this can be solved better by using plugin filter hooks in the JavaScript code. I’m already using these quite a bit in the PHP code parts of TablePress, but not so much in the JS context (as it’s a bit more complex to use them). Staying with the context menu as an example, I could imagine running a plugin filter hook on the context menu elements whenever it is opened, so that a filter hook handler function could intercept that call and modify the context menu as desired. I’ll add this to my list for TablePress 3.0, currently planned for Q4 of this year ??

    Best wishes,
    Tobias

    Thread Starter poppydev

    (@poppydev)

    Hi Tobias,

    Thank you for getting back to me on this. Would definitely welcome this as a core feature. Look forward to seeing this implemented.

    Apologies for using the word ‘Hack’. Its not a nice word really. I should have used ‘Improvement’ as I didn’t hack any of your code when I think about it :/.

    Regards

    Plugin Author Tobias B?thge

    (@tobiasbg)

    Hi,

    sure, no problem! Good to hear that this approach might help!

    And no worries about the term hack ?? I think for such initial “proofs of concepts”, that’s totally fine ??

    Best wishes,
    Tobias

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Tablepress – Admin table content improvements’ is closed to new replies.