vakata / jstree

jquery tree plugin
http://jstree.com
MIT License
5.13k stars 1.39k forks source link

Cancel node opening with callback style. #2751

Closed Aycon3296 closed 10 months ago

Aycon3296 commented 10 months ago

I'm using tree update with a callback because my node loading logic is complex. Specifically, if a certain error occurs on the server while trying to fetch a node, I want to send another request to update the server state and retry loading the node automatically. Here is my jstree config:

function resolve_password_required(response, callback) {
    if (!response.ok && response.status == 409){ // Conflict
        response.json()
        .then(json_data => {
            console.info(json_data)
            ask_pass_form_change_archive_name(json_data.archive_name)
        })

        let form = get_modal_form(ask_pass_form_selector)
        modal_form_show(form)
        callback() // <---- THERE PROBLEM

    } else {
        response.json()
        .then(json_data => {
            callback(json_data)
        })

    }
}
async function initFileSystemView(source_is_user) {
    file_system_tree.jstree({
        'core': {
            'multiple': false,
            'check_callback': true,
            'data': async function (node, callback){
                let destination = `http://${window.location.hostname}/main/files/${source_is_user ? 'user' : 'server'}`
                await fetch(destination)
                .then(response => resolve_password_required(response, callback))
            },
            'force_text': true,
        },
        'types': {
            '#': {
                'max_children': 1536,
                'max_depth': 384,
                'valid_children' : ['folder', 'file']
            },
            'default': {
                'max_children': 1536,
                'max_depth': 384,
                'valid_children': ['folder', 'file']
            },
            'folder': {
                'max_children': 1536,
                'max_depth': 384,
                'valid_children': ['folder', 'file']
            },
            'file': {
                'max_children': 0,
                'max_depth': 0,
                'icon': 'jstree-file',
                'valid_children': ['folder', 'file']
            }
        },
        'plugins': [
            //'search',
            //'state',
            'types',
            'wholerow'
        ]
    });
}

I highlighted the problematic line, I don't know what it should look like.

I want that in the event that the server returns a 409, then I can cancel loading the node and leave the expand checkbox on that node so that the user can open it again later. If I pass an empty array to the callback, the checkbox will disappear, and if I don't pass anything, the loader spinner will continue to spin.

Sorry for my English. Any help ? Thanks!

vakata commented 10 months ago

Returning false should do the trick - this means - the node was not loaded, you can try to open again. Here is a demo: https://jsfiddle.net/pjbwhfx9/

Aycon3296 commented 10 months ago

Thank you very much!