Skip to content

Commit 9e3555b

Browse files
authored
Merge pull request #2298 from umbraco/14/bugfix/only-publish-writebale-cultures
Bugfix: Prevent publishing of readonly cultures
2 parents c1c9940 + f2b0d5b commit 9e3555b

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

src/packages/core/utils/selection-manager/selection.manager.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ describe('UmbSelectionManager', () => {
7676
it('has a clearSelection method', () => {
7777
expect(manager).to.have.property('clearSelection').that.is.a('function');
7878
});
79+
80+
it('has a setAllowLimitation method', () => {
81+
expect(manager).to.have.property('setAllowLimitation').that.is.a('function');
82+
});
7983
});
8084
});
8185

@@ -150,6 +154,15 @@ describe('UmbSelectionManager', () => {
150154
manager.select('3');
151155
expect(manager.getSelection()).to.deep.equal([]);
152156
});
157+
158+
it('can not select an item if it does not pass the allow function', () => {
159+
manager.setAllowLimitation((item) => item !== '2');
160+
expect(() => manager.select('2')).to.throw();
161+
expect(manager.getSelection()).to.deep.equal([]);
162+
163+
manager.select('1');
164+
expect(manager.getSelection()).to.deep.equal(['1']);
165+
});
153166
});
154167

155168
describe('Deselect', () => {

src/packages/core/utils/selection-manager/selection.manager.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export class UmbSelectionManager<ValueType extends string | null = string | null
1818
#multiple = new UmbBooleanState(false);
1919
public readonly multiple = this.#multiple.asObservable();
2020

21+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
22+
#allow = (unique: ValueType) => true;
23+
2124
constructor(host: UmbControllerHost) {
2225
super(host);
2326
}
@@ -109,6 +112,9 @@ export class UmbSelectionManager<ValueType extends string | null = string | null
109112
public select(unique: ValueType) {
110113
if (this.getSelectable() === false) return;
111114
if (this.isSelected(unique)) return;
115+
if (this.#allow(unique) === false) {
116+
throw new Error('The item is now allowed to be selected');
117+
}
112118
const newSelection = this.getMultiple() ? [...this.getSelection(), unique] : [unique];
113119
this.#selection.setValue(newSelection);
114120
this.getHostElement().dispatchEvent(new UmbSelectedEvent(unique));
@@ -146,4 +152,13 @@ export class UmbSelectionManager<ValueType extends string | null = string | null
146152
if (this.getSelectable() === false) return;
147153
this.#selection.setValue([]);
148154
}
155+
156+
/**
157+
* Sets a function that determines if an item is selectable or not.
158+
* @param compareFn A function that determines if an item is selectable or not.
159+
* @memberof UmbSelectionManager
160+
*/
161+
public setAllowLimitation(compareFn: (unique: ValueType) => boolean): void {
162+
this.#allow = compareFn;
163+
}
149164
}

src/packages/documents/documents/modals/schedule-modal/document-schedule-modal.element.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ export class UmbDocumentScheduleModalElement extends UmbModalBaseElement<
4141
this.#selectionManager.setMultiple(true);
4242
this.#selectionManager.setSelectable(true);
4343

44+
const pickableFilter = this.data?.pickableFilter;
45+
46+
if (pickableFilter) {
47+
this.#selectionManager.setAllowLimitation((unique) => {
48+
const option = this.data?.options.find((o) => o.unique === unique);
49+
return option ? pickableFilter(option) : true;
50+
});
51+
}
52+
4453
// Only display variants that are relevant to pick from, i.e. variants that are draft or published with pending changes:
4554
this._options =
4655
this.data?.options.filter(

src/packages/documents/documents/modals/shared/document-variant-language-picker.element.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { UmbDocumentVariantState, type UmbDocumentVariantOptionModel } from '../../types.js';
2-
import { css, customElement, html, nothing, property, repeat, state } from '@umbraco-cms/backoffice/external/lit';
2+
import {
3+
css,
4+
customElement,
5+
html,
6+
nothing,
7+
property,
8+
repeat,
9+
state,
10+
type PropertyValues,
11+
} from '@umbraco-cms/backoffice/external/lit';
312
import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element';
413
import { UmbTextStyles } from '@umbraco-cms/backoffice/style';
514
import type { UmbSelectionManager } from '@umbraco-cms/backoffice/utils';
@@ -37,6 +46,17 @@ export class UmbDocumentVariantLanguagePickerElement extends UmbLitElement {
3746
@property({ attribute: false })
3847
public pickableFilter?: (item: UmbDocumentVariantOptionModel) => boolean;
3948

49+
protected override updated(_changedProperties: PropertyValues): void {
50+
super.updated(_changedProperties);
51+
52+
if (this.selectionManager && this.pickableFilter) {
53+
this.#selectionManager.setAllowLimitation((unique) => {
54+
const option = this.variantLanguageOptions.find((o) => o.unique === unique);
55+
return option ? this.pickableFilter!(option) : true;
56+
});
57+
}
58+
}
59+
4060
override render() {
4161
return this.variantLanguageOptions.length
4262
? repeat(

src/packages/documents/documents/workspace/document-workspace.context.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,15 @@ export class UmbDocumentWorkspaceContext
536536
const selected = activeVariantIds.concat(changedVariantIds);
537537
// Selected can contain entries that are not part of the options, therefor the modal filters selection based on options.
538538

539+
const readOnlyCultures = this.readOnlyState.getStates().map((s) => s.variantId.culture);
540+
const selectedCultures = selected.map((x) => x.toString()).filter((v, i, a) => a.indexOf(v) === i);
541+
const writable = selectedCultures.filter((x) => readOnlyCultures.includes(x) === false);
542+
539543
const options = await firstValueFrom(this.variantOptions);
540544

541545
return {
542546
options,
543-
selected: selected.map((x) => x.toString()).filter((v, i, a) => a.indexOf(v) === i),
547+
selected: writable,
544548
};
545549
}
546550

0 commit comments

Comments
 (0)