imports/vx/client/code/vxapp.js
/**
     * Generalized drop handler for cross-list drag/drop.
     *
     * @param {object} parameteters Parameters object with source and target information.
     */
Generalized drop handler for cross-list drag/drop.
@param {object} parameteters Parameters object with source and target information.
Generalized drop handler for cross - list drag / drop . @param { object } parameteters Parameters object with source and target information .
handleDropMultiPrime(parameters) {
        try {
            const singleUpdate =
                parameters.sourceCollection === parameters.targetCollection &&
                parameters.sourceRecord._id === parameters.targetRecord._id
            const singleList = singleUpdate &&
                parameters.sourceRowsPath === parameters.targetRowsPath
            const dropInfo = parameters.dropInfo
            const sourceRows = get(parameters.sourceRecord, parameters.sourceRowsPath, [])
            const targetRows = get(parameters.targetRecord, parameters.targetRowsPath, [])
            if (dropInfo.clone) {
                dropInfo.items.forEach((item, index) => {
                    const newRow = parameters.newItemHandler(item, index, parameters)
                    if (!newRow) {
                        return
                    }
                    const newIndex = dropInfo.targetIndex + index
                    OLog.debug("vxapp.js handleDropMultiPrime *splicing* single row " +
                        `newIndex=${newIndex} newRow=${OLog.debugString(newRow)}`)
                    targetRows.splice(newIndex, 0, newRow)
                })
            }
            else {
                const insertArray = dropInfo.items.map(item => {
                    return _.findWhere(sourceRows, { [parameters.rowId]: item["data-item-id"] })
                })
                let spliceIndex = dropInfo.targetIndex
                if (singleList && dropInfo.targetIndex > dropInfo.items[0].sourceIndex) {
                    spliceIndex++
                }
                const insertedIds = _.pluck(dropInfo.items, "data-item-id")
                const insertStartIndex = spliceIndex
                const insertEndIndex = insertStartIndex + dropInfo.items.length - 1
                OLog.debug(`vxapp.js handleDropMultiPrime *splicing* singleList=${singleList} ${insertArray.length} rows ` +
                    `into ${targetRows.length} existng rows first item sourceIndex=${dropInfo.items[0].sourceIndex} ` +
                    `targetIndex=${dropInfo.targetIndex} spliceIndex=${spliceIndex} ` +
                    `dropInfo.items=${OLog.debugString(dropInfo.items)}`)
                targetRows.splice(spliceIndex, 0, ...insertArray)
                if (singleList) {
                    for (let purgeIndex = targetRows.length - 1; purgeIndex >= 0; purgeIndex--) {
                        if (purgeIndex < insertStartIndex || purgeIndex > insertEndIndex) {
                            const targetRow = targetRows[purgeIndex]
                            if (insertedIds.includes(targetRow[parameters.rowId])) {
                                targetRows.splice(purgeIndex, 1)
                            }
                        }
                    }
                }
                else {
                    for (let purgeIndex = sourceRows.length - 1; purgeIndex >= 0; purgeIndex--) {
                        const sourceRow = sourceRows[purgeIndex]
                        if (insertedIds.includes(sourceRow[parameters.rowId])) {
                            sourceRows.splice(purgeIndex, 1)
                        }
                    }
                }
            }
            if (singleUpdate) {
                const modifier = {}
                modifier.$set = {}
                if (!singleList) {
                    const sourceMongoPath = Util.toMongoPath(parameters.sourceRowsPath)
                    modifier.$set[sourceMongoPath] = sourceRows
                }
                const targetMongoPath = Util.toMongoPath(parameters.targetRowsPath)
                modifier.$set[targetMongoPath] = targetRows
                OLog.debug(`vxapp.js handleDropMultiPrime recordId=${parameters.targetRecord._id} ` +
                    `rowsPath=${parameters.targetRowsPath} rowId=${parameters.rowId} ` +
                    `modifier=${OLog.debugString(modifier)}`)
                parameters.targetCollection.update(parameters.targetRecord._id, modifier)
            }
            else {
                throw new Error("vxapp.js handleDropMultiPrime only single update is currently supported")
            }
        }
        catch (error) {
            UX.notifyForError(error)
        }
    }
handleDropMultiPrime ( parameters ) { try { const singleUpdate = parameters . sourceCollection === parameters . targetCollection && parameters . sourceRecord . _id === parameters . targetRecord . _id const singleList = singleUpdate && parameters . sourceRowsPath === parameters . targetRowsPath const dropInfo = parameters . dropInfo const sourceRows = get ( parameters . sourceRecord , parameters . sourceRowsPath , [ ] ) const targetRows = get ( parameters . targetRecord , parameters . targetRowsPath , [ ] ) if ( dropInfo . clone ) { dropInfo . items . forEach ( ( item , index ) => { const newRow = parameters . newItemHandler ( item , index , parameters ) if ( ! newRow ) { return } const newIndex = dropInfo . targetIndex + index OLog . debug ( "vxapp.js handleDropMultiPrime *splicing* single row " + ` ${ newIndex } ${ OLog . debugString ( newRow ) } ` ) targetRows . splice ( newIndex , 0 , newRow ) } ) } else { const insertArray = dropInfo . items . map ( item => { return _ . findWhere ( sourceRows , { [ parameters . rowId ] : item [ "data-item-id" ] } ) } ) let spliceIndex = dropInfo . targetIndex if ( singleList && dropInfo . targetIndex > dropInfo . items [ 0 ] . sourceIndex ) { spliceIndex ++ } const insertedIds = _ . pluck ( dropInfo . items , "data-item-id" ) const insertStartIndex = spliceIndex const insertEndIndex = insertStartIndex + dropInfo . items . length - 1 OLog . debug ( ` ${ singleList } ${ insertArray . length } ` + ` ${ targetRows . length } ${ dropInfo . items [ 0 ] . sourceIndex } ` + ` ${ dropInfo . targetIndex } ${ spliceIndex } ` + ` ${ OLog . debugString ( dropInfo . items ) } ` ) targetRows . splice ( spliceIndex , 0 , ... insertArray ) if ( singleList ) { for ( let purgeIndex = targetRows . length - 1 ; purgeIndex >= 0 ; purgeIndex -- ) { if ( purgeIndex < insertStartIndex || purgeIndex > insertEndIndex ) { const targetRow = targetRows [ purgeIndex ] if ( insertedIds . includes ( targetRow [ parameters . rowId ] ) ) { targetRows . splice ( purgeIndex , 1 ) } } } } else { for ( let purgeIndex = sourceRows . length - 1 ; purgeIndex >= 0 ; purgeIndex -- ) { const sourceRow = sourceRows [ purgeIndex ] if ( insertedIds . includes ( sourceRow [ parameters . rowId ] ) ) { sourceRows . splice ( purgeIndex , 1 ) } } } } if ( singleUpdate ) { const modifier = { } modifier . $set = { } if ( ! singleList ) { const sourceMongoPath = Util . toMongoPath ( parameters . sourceRowsPath ) modifier . $set [ sourceMongoPath ] = sourceRows } const targetMongoPath = Util . toMongoPath ( parameters . targetRowsPath ) modifier . $set [ targetMongoPath ] = targetRows OLog . debug ( ` ${ parameters . targetRecord . _id } ` + ` ${ parameters . targetRowsPath } ${ parameters . rowId } ` + ` ${ OLog . debugString ( modifier ) } ` ) parameters . targetCollection . update ( parameters . targetRecord . _id , modifier ) } else { throw new Error ( "vxapp.js handleDropMultiPrime only single update is currently supported" ) } } catch ( error ) { UX . notifyForError ( error ) } }
Generalized drop handler for cross-list drag/drop.
Generalized drop handler for cross - list drag / drop .