kartik-v / yii2-tree-manager

An advanced tree management module using nested sets for Yii 2.
http://demos.krajee.com/tree-manager
Other
150 stars 107 forks source link

Confirmation to abandon changes before changing node #188

Closed douglas-srs closed 6 years ago

douglas-srs commented 6 years ago

I am trying to implement this without override the kv-tree.js itself, I tried this:

#('#arvore').on('treeview.beforeselect', function(event, key, jqXHR, settings) {
    if (confirm('Do you want to abandon changes?')) {
        console.log('Ok, nothing changes');
    } else {
        console.log('Now I try to stop node selection but it doesn't stop at all');
        event.preventDefault();
        return false;
    }        
});

Anyway to implement this?

douglas-srs commented 6 years ago

This is how I managed to do it, I saved every node "click" handler in an array, unbinded it and then binded my own click handler, asked for confirmation and if positive I bind the old click handler and trigger click again, I am sure this is not optimal but it works, now I just have to implement something to check if data has actually changed before popping up this confirmation dialog. I hope this helps someone else.

oldClickEvents = {};
firstClick = true;

newClickEvent = function (event, index, elem) {
    if (firstClick == true){
        if (confirm('You changed data, are you sure you want to continue?')) {
            //YES
            $(elem).bind( 'click', oldClickEvents[index].handler);
            firstClick = false;                    
            $(elem).triggerHandler('click');
        } else {
            //NO
            firstClick = true;
            $(elem).blur();            
            event.preventDefault();
        }

    } else {
        firstClick = true;
        $(elem).off('click');
        $(elem).on('click', function (event) {
            newClickEvent(event, index, elem);
        });
    }
};

$('#arvore-tree').find('.kv-node-detail').each(function (index) {
    var oldClickEvent = jQuery._data(this, 'events').click[0];
    oldClickEvents[index] = oldClickEvent;

    $(this).off('click');
    $(this).on('click', function (event) {
        newClickEvent(event, index, this);
    });

});
kartik-v commented 6 years ago

I have provided an enhancement for you to do an event.preventDefault and abort events.

douglas-srs commented 6 years ago

Thank you :)