diff --git a/lib/utils/1m.js b/lib/utils/1m.js index 74236f92..6de18a90 100644 --- a/lib/utils/1m.js +++ b/lib/utils/1m.js @@ -406,10 +406,10 @@ function hasDuplicates(array) { return uniqueValues.size !== array.length; } -const cellsInColumnUnique = (dh) => (shared_key_name, new_value = null) => { +const cellsInColumnUnique = (dh) => (shared_key_name, filter_value = null) => { const columnIndex = dh.getColumnIndexByFieldName(shared_key_name); const columnValues = getNonEmptyColumnValues(dh.hot, columnIndex); - if (new_value) columnValues.push(new_value); + if (filter_value !== null) columnValues.filter(el => el === filter_value); return hasDuplicates(columnValues); } @@ -552,16 +552,24 @@ const bindChangeEmitter = (appContext) => (dh) => { : ACTION.UPDATE; // TODO: modal continuation interface - - const cellIsUnique = cellsInColumnUnique(dh)(key_name); - const willHaveDuplicateValues = cellsInColumnUnique(dh)(key_name, Object.values(row_changes[row].newValues)[0]); - - // propagate deletion when it is the final key + const currentValue = row_changes[row].oldValues[column_index]; + + // propagate deletion when the deleted cell was unique of its kind // always propagate updates const propagates = updateActionType === ACTION.DELETE ? - !cellIsUnique + // if deletion is triggering, the current non empty column values should still be one down from the original value + // NOTE: if this is problematic, need to replace *all* handsontable with CRUD + getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length === 0 : true; + // console.warn( + // updateActionType, + // currentValue, + // futureValue, + // getNonEmptyColumnValues(dh.hot, column_index), + // getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length === 0, + // propagates); + const details = { emitted_by: dh.class_assignment, target: dh.class_assignment, @@ -579,15 +587,17 @@ const bindChangeEmitter = (appContext) => (dh) => { propagates }; - // warn about deletion if it's the final key - if (updateActionType === ACTION.DELETE && !cellIsUnique && propagates) { - console.warn("PROPAGATION:", "Row collections with key will be deleted", details); - } + if (propagates) { + // warn about deletion if it's the final key + if (updateActionType === ACTION.DELETE) { + console.warn("PROPAGATION:", "Row collections with key will be deleted", details); + } - // // warn if update will cause a duplicate that merges entries in child tables which share that same key - // if (updateActionType === ACTION.UPDATE && willHaveDuplicateValues && propagates) { - // console.warn("PROPAGATION:", "Row collections will merge with existing those of existing key value", details); - // } + // // warn if update will cause a duplicate that merges entries in child tables which share that same key + if (updateActionType === ACTION.UPDATE && getNonEmptyColumnValues(dh.hot, column_index).filter(el => el === currentValue).length + 1 > 1) { + console.warn("PROPAGATION:", "Row collections will merge with existing those of existing key value", details); + } + } dispatchHandsontableUpdate(updateActionType, details); @@ -796,6 +806,7 @@ const handleAction = (appContext) => (dh) => (action, details) => { } }; } else { + // propagation for (let col_index in shared_key_details.key_indexes) { updateRows(dh, [ col_index,