Jump to content

PW panel - button click inside panel should close panel and run action in main window - possible?


Gadgetto
 Share

Recommended Posts

I have a PW panel with a button element inside. When this button is clicked, the panel should close and the button action (href is triggered via JavaScript) should be executed in the main window (from where the panel was opened). In the current state, when the button is clicked, the action is processed within the panel, which is not what I want.

563477263_Bildschirmfoto2020-01-11um12_43_34.thumb.png.0cb5769799a3205562f450e33524c9f9.png

Here is the button code:

        /** @var InputfieldSubmit $btn */
        $btn = $modules->get('InputfieldButton');
        $btn->attr('name', 'delete_discount');
        $btn->href = $this->snipWireRootUrl . 'discounts/?id=' . $item['id'] . '&action=delete_discount';
        $btn->aclass = 'DeleteDiscountButton';
        $btn->text = $this->_('Delete discount');
        $btn->icon = 'trash';
        $deleteButton = $btn->render();

Here is the JS part which is triggered when button is clicked:

    $('.DeleteDiscountButton').on('click', function(e) {
        e.preventDefault();        
        var a_href = $(this).attr('href');
        ProcessWire.confirm(
            discountActionStrings.confirm_delete_discount,
            function() {
                // dialogue OK click
                window.location.href = a_href;
            }
        );
    });

Any hints on how this could be done?

Link to comment
Share on other sites

As the content of the panel is a totally different document (Iframe), than in the opening document your function has to be modified.

 $('.DeleteDiscountButton').on('click', function(e) {
        e.preventDefault();        
        var a_href = $(this).attr('href');
        ProcessWire.confirm(
            discountActionStrings.confirm_delete_discount,
            function() {
                // dialogue OK click
                window.parent.document.location.href = a_href;
            }
        );
    });

The key is using window.parent.document, which refers to the document that opened  the panel.

 

  • Like 1
Link to comment
Share on other sites

4 hours ago, Jens Martsch - dotnetic said:

As the content of the panel is a totally different document (Iframe), than in the opening document your function has to be modified.


 $('.DeleteDiscountButton').on('click', function(e) {
        e.preventDefault();        
        var a_href = $(this).attr('href');
        ProcessWire.confirm(
            discountActionStrings.confirm_delete_discount,
            function() {
                // dialogue OK click
                window.parent.document.location.href = a_href;
            }
        );
    });

The key is using window.parent.document, which refers to the document that opened  the panel.

Great, this works - thank you!

  • Like 1
Link to comment
Share on other sites

4 hours ago, Jens Martsch - dotnetic said:

If you don't need to support old browsers like IE, than it is also better to use "let" to define javascript variables.

I know Jens, but I'd like to use same references as PW uses. An PW is not yet on ECMAScript 6.

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...