gasparesganga / jquery-popup-window

The ultimate popup/dialog/modal jQuery plugin
https://gasparesganga.com/labs/jquery-popup-window/
MIT License
24 stars 12 forks source link

Enforce window destroy on close events #5

Open centurianii opened 6 years ago

centurianii commented 6 years ago

If we want to catch the close event and destroy the window we get en error only when the window is minimized!

That's because function function _Close(popupWindow) tries to un-minimize it that results in deferreds at function function _Unminimize(popupWindow) while at the same time it triggers the close event. I'm reffering to these 2 lines at the last function:

    var defPosition = _RestoreSavedPosition(popupWindow);
    var defSize     = _RestoreSavedSize(popupWindow);

At the time we catch the event and destroy the window with function _Destroy(popupWindow) it seems that jquery hasn't finished with the 1st deferred which calls function _ChangePosition(popupWindow, params) where line var settings = popupWindow.data("settings"); fires an error (although Firefox dev tools give me an error from the same command but at function _CheckPosition(popupWindow)!?).

To make a long story short, by the time you introduce deferreds in your code everything else is dependent upon these especially if the code that creates deferreds at the same time triggers events like function _Close(popupWindow), i.e. my code should question the internal deferred:

have you ended with success?

Yes (resolved)? Now, execute my function

No (rejected)? Abandon

The solution without applying big modifications to the structure: add another key in var _defaults:

destroyOnClose      : false,

and change function _Close(popupWindow) like:

    function _Close(popupWindow){
        if (!_CheckPopupWindow(popupWindow) || !popupWindow.data("opened")) return;
        if(!popupWindow.data("settings").destroyOnClose){
            if (popupWindow.data("minimized")) _Unminimize(popupWindow);
            popupWindow.data("overlay").hide();
            popupWindow.data("opened", false);
        }
        _TriggerEvent(popupWindow, "close");
    }

Now I can destroy minimized windows from the close button!

centurianii commented 6 years ago

...and yes, I added an internal id counter/storage in my code that produces the windows - a kind of a higher code over your code which is not bad as an approach. Of course you can add such a counter internally but I have some doubts about it's usability from that lower end.

That reminded me another issue: a window should change stack order on click events moving from a lower layer to the front one. Hopefully, the solution can be done from a higher end again by applying z-index at .popupwindow_overlays so you don't have to update again-and-again the basic project but instead, create usability modules based on it.