Source: ui/dnd/util.js

import { generateUniqueId } from '../../utils/utils.js';

// static data object that stores all registered datasets
const data_ = {};

/**
 * Register a dataset, generate an ID and store it in a static
 * location to retrieve it later using the ID.
 * 
 * This is needed, because complex data types 
 * cannot be transferred using a drag/drop event
 * 
 * @author rhess <robin.hess@awi.de>
 * @memberof vef.ui.dnd
 * 
 * @param {string} value
 * @returns {string} id
 */
export function registerDataTransfer(value) {
    const id = generateUniqueId();
    data_[id] = value;
    return id;
}

/**
 * Get a dataset using its ID and remove it from
 *
 * @author rhess <robin.hess@awi.de>
 * @memberof vef.ui.dnd
 * 
 * @param {string} id
 * @param {boolean} keep
 * @returns {any} value
 */
export function retrieveDataTransfer(id, keep) {
    if (id in data_) {
        const value = data_[id];
        if (!keep) delete data_[id];
        return value;
    }
}