Commit 09526e44 authored by Ahmed Awan's avatar Ahmed Awan
Browse files

[24.2] Fix drag and drop for dataset collection elements

parent fdee4c64
Loading
Loading
Loading
Loading
+41 −18
Original line number Diff line number Diff line
@@ -338,24 +338,22 @@ function handleIncoming(incoming: Record<string, unknown>, partial = true) {
        if (!canAcceptDatatype(extensions)) {
            return false;
        }
        if (values.some((v) => !canAcceptSrc(v.history_content_type, v.collection_type))) {
        if (
            values.some((v) => {
                const { historyContentType } = getSrcAndContentType(v);
                const collectionType = v.collection_type as string | undefined;
                return !canAcceptSrc(historyContentType, collectionType);
            })
        ) {
            return false;
        }
        if (values.length > 0) {
            const incomingValues: Array<DataOption> = [];
            values.forEach((v) => {
                // Map incoming objects to data option values
                let newSrc;
                if (isDCE(v)) {
                    if (isDatasetElement(v)) {
                        newSrc = SOURCE.DATASET;
                const { newSrc, isADatasetElement } = getSrcAndContentType(v);
                if (isADatasetElement) {
                    v = v.object;
                    } else {
                        newSrc = SOURCE.COLLECTION_ELEMENT;
                    }
                } else {
                    newSrc =
                        v.src || (v.history_content_type === "dataset_collection" ? SOURCE.COLLECTION : SOURCE.DATASET);
                }
                const newHid = v.hid;
                const newId = v.id;
@@ -455,6 +453,34 @@ function canAcceptDatatype(itemDatatypes: string | Array<string>) {
    return true;
}

/**
 * Given an element, determine the source and content type.
 * Also determine if the element is a dataset element object.
 */
function getSrcAndContentType(element: any): {
    historyContentType: "dataset" | "dataset_collection";
    newSrc: string;
    isADatasetElement: boolean;
} {
    let historyContentType: "dataset" | "dataset_collection";
    let newSrc: string;
    let isADatasetElement = false;
    if (isDCE(element)) {
        if (isDatasetElement(element)) {
            historyContentType = "dataset";
            newSrc = SOURCE.DATASET;
            isADatasetElement = true;
        } else {
            historyContentType = "dataset_collection";
            newSrc = SOURCE.COLLECTION_ELEMENT;
        }
    } else {
        historyContentType = element.history_content_type;
        newSrc = element.src || (historyContentType === "dataset_collection" ? SOURCE.COLLECTION : SOURCE.DATASET);
    }
    return { historyContentType, newSrc, isADatasetElement };
}

function canAcceptSrc(historyContentType: "dataset" | "dataset_collection", collectionType?: string) {
    if (historyContentType === "dataset") {
        // HDA can only be fed into data parameters, not collection parameters
@@ -517,18 +543,15 @@ function createdCollection(collection: any) {
// Drag/Drop event handlers
function onDragEnter(evt: MouseEvent) {
    const eventData = eventStore.getDragData();
    const { historyContentType } = getSrcAndContentType(eventData);
    const collectionType = eventData?.collection_type as string | undefined;
    if (eventData) {
        const extensions = (eventData.extension as string) || (eventData.elements_datatypes as Array<string>);
        let highlightingState = "success";
        if (!canAcceptDatatype(extensions)) {
            highlightingState = "warning";
            $emit("alert", `${extensions} is not an acceptable format for this parameter.`);
        } else if (
            !canAcceptSrc(
                eventData.history_content_type as "dataset" | "dataset_collection",
                eventData.collection_type as string
            )
        ) {
        } else if (!canAcceptSrc(historyContentType, collectionType)) {
            highlightingState = "warning";
            $emit("alert", `${eventData.history_content_type} is not an acceptable input type for this parameter.`);
        }