Skip to content

Commit 467db77

Browse files
authored
Content picker: Remove multi-node content picker entries by UDI to account for index not aligning with stored values (#20950)
* Remove multi-node content picker entries by udi to account for index not aligning with stored values. * Handled issues raised in code review.
1 parent aced691 commit 467db77

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/Umbraco.Web.UI.Client/src/views/propertyeditors/contentpicker/contentpicker.controller.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/**
32
* The controller that is used for a couple different Property Editors: Multi Node Tree Picker, Content Picker,
43
* since this is used by MNTP and it supports content, media and members, there is code to deal with all 3 of those types
@@ -345,17 +344,24 @@ function contentPickerController($scope, $q, $routeParams, $location, entityReso
345344

346345
};
347346

348-
$scope.remove = function (index) {
347+
$scope.remove = function (id) {
349348
if (!$scope.allowRemove) return;
350349

351-
var currIds = $scope.model.value ? $scope.model.value.split(',') : [];
352-
if (currIds.length > 0) {
353-
currIds.splice(index, 1);
354-
setDirty();
355-
$scope.model.value = currIds.join();
350+
var currUdis = $scope.model.value ? $scope.model.value.split(',') : [];
351+
if (currUdis.length > 0) {
352+
353+
// Remove the node with the provided UDI.
354+
var index = currUdis.indexOf(id.toString()); // id may be an integer ID or a UDI string, split() always returns strings.
355+
if (index >= 0) {
356+
currUdis.splice(index, 1);
357+
setDirty();
358+
359+
// If no ids left, set value to null to match `clear()` behavior.
360+
$scope.model.value = currUdis.length > 0 ? currUdis.join() : null;
361+
}
356362
}
357363

358-
removeAllEntriesAction.isDisabled = currIds.length === 0;
364+
removeAllEntriesAction.isDisabled = currUdis.length === 0;
359365
};
360366

361367
$scope.showNode = function (index) {

src/Umbraco.Web.UI.Client/src/views/propertyeditors/contentpicker/contentpicker.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
sortable="!sortableOptions.disabled"
1515
allow-remove="allowRemove"
1616
allow-open="model.config.showOpenButton && allowOpen && !dialogEditor"
17-
on-remove="remove($index)"
17+
on-remove="remove(model.config.idType === 'udi' ? node.udi : node.id)"
1818
on-open="openEditor(node)">
1919
</umb-node-preview>
2020
</div>

src/Umbraco.Web.UI.Client/test/unit/app/propertyeditors/content-picker-controller.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('Content picker controller tests', function () {
8181
});
8282

8383
it("Removing an item should update renderModel, ids and model.value", function(){
84-
scope.remove(1);
84+
scope.remove(1231);
8585
scope.$apply();
8686
expect(scope.renderModel.length).toBe(2);
8787
expect(scope.model.value).toBe("1233,23121");

0 commit comments

Comments
 (0)