@@ -12,48 +12,72 @@ import { UmbSelectionManager } from '@umbraco-cms/backoffice/utils';
12
12
13
13
import '../shared/document-variant-language-picker.element.js' ;
14
14
15
+ /**
16
+ * @function isPublished
17
+ * @param {UmbDocumentVariantOptionModel } option - the option to check.
18
+ * @returns {boolean } boolean
19
+ */
20
+ export function isPublished ( option : UmbDocumentVariantOptionModel ) : boolean {
21
+ return (
22
+ option . variant ?. state === UmbDocumentVariantState . PUBLISHED ||
23
+ option . variant ?. state === UmbDocumentVariantState . PUBLISHED_PENDING_CHANGES
24
+ ) ;
25
+ }
26
+
15
27
@customElement ( 'umb-document-unpublish-modal' )
16
28
export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement <
17
29
UmbDocumentUnpublishModalData ,
18
30
UmbDocumentUnpublishModalValue
19
31
> {
20
- #selectionManager = new UmbSelectionManager < string > ( this ) ;
32
+ protected readonly _selectionManager = new UmbSelectionManager < string > ( this ) ;
21
33
#referencesRepository = new UmbDocumentReferenceRepository ( this ) ;
22
34
23
35
@state ( )
24
36
_options : Array < UmbDocumentVariantOptionModel > = [ ] ;
25
37
38
+ @state ( )
39
+ _selection : Array < string > = [ ] ;
40
+
26
41
@state ( )
27
42
_hasReferences = false ;
28
43
29
44
@state ( )
30
45
_hasUnpublishPermission = true ;
31
46
47
+ @state ( )
48
+ _hasInvalidSelection = true ;
49
+
32
50
override firstUpdated ( ) {
33
51
this . #configureSelectionManager( ) ;
34
52
this . #getReferences( ) ;
35
53
}
36
54
37
55
async #configureSelectionManager( ) {
38
- this . #selectionManager . setMultiple ( true ) ;
39
- this . #selectionManager . setSelectable ( true ) ;
56
+ this . _selectionManager . setMultiple ( true ) ;
57
+ this . _selectionManager . setSelectable ( true ) ;
40
58
41
59
// Only display variants that are relevant to pick from, i.e. variants that are draft or published with pending changes:
42
- this . _options =
43
- this . data ?. options . filter (
44
- ( option ) =>
45
- option . variant &&
46
- ( ! option . variant . state ||
47
- option . variant . state === UmbDocumentVariantState . PUBLISHED ||
48
- option . variant . state === UmbDocumentVariantState . PUBLISHED_PENDING_CHANGES ) ,
49
- ) ?? [ ] ;
60
+ this . _options = this . data ?. options . filter ( ( option ) => isPublished ( option ) ) ?? [ ] ;
50
61
51
62
let selected = this . value ?. selection ?? [ ] ;
52
63
53
64
// Filter selection based on options:
54
65
selected = selected . filter ( ( s ) => this . _options . some ( ( o ) => o . unique === s ) ) ;
55
66
56
- this . #selectionManager. setSelection ( selected ) ;
67
+ this . _selectionManager . setSelection ( selected ) ;
68
+
69
+ this . observe (
70
+ this . _selectionManager . selection ,
71
+ ( selection ) => {
72
+ this . _selection = selection ;
73
+ const selectionHasMandatory = this . _options . some ( ( o ) => o . language . isMandatory && selection . includes ( o . unique ) ) ;
74
+ const selectionDoesNotHaveAllMandatory = this . _options . some (
75
+ ( o ) => o . language . isMandatory && ! selection . includes ( o . unique ) ,
76
+ ) ;
77
+ this . _hasInvalidSelection = selectionHasMandatory && selectionDoesNotHaveAllMandatory ;
78
+ } ,
79
+ 'observeSelection' ,
80
+ ) ;
57
81
}
58
82
59
83
async #getReferences( ) {
@@ -80,7 +104,7 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
80
104
81
105
#submit( ) {
82
106
if ( this . _hasUnpublishPermission ) {
83
- this . value = { selection : this . #selectionManager . getSelection ( ) } ;
107
+ this . value = { selection : this . _selection } ;
84
108
this . modalContext ?. submit ( ) ;
85
109
return ;
86
110
}
@@ -91,6 +115,10 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
91
115
this . modalContext ?. reject ( ) ;
92
116
}
93
117
118
+ private _requiredFilter = ( variantOption : UmbDocumentVariantOptionModel ) : boolean => {
119
+ return variantOption . language . isMandatory && ! this . _selection . includes ( variantOption . unique ) ;
120
+ } ;
121
+
94
122
override render ( ) {
95
123
return html `<umb- body- layout headline= ${ this . localize . term ( 'content_unpublish' ) } >
96
124
<p id= "subtitle" >
@@ -100,8 +128,9 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
100
128
</ p>
101
129
102
130
<umb- document- variant- language-picker
103
- .selectionManager = ${ this . #selectionManager }
131
+ .selectionManager = ${ this . _selectionManager }
104
132
.variantLanguageOptions = ${ this . _options }
133
+ .requiredFilter = ${ this . _hasInvalidSelection ? this . _requiredFilter : undefined }
105
134
.pickableFilter = ${ this . data ?. pickableFilter } > </ umb- document- variant- language-picker>
106
135
107
136
<p>
@@ -130,7 +159,7 @@ export class UmbDocumentUnpublishModalElement extends UmbModalBaseElement<
130
159
<uui- butto n label= ${ this . localize . term ( 'general_close' ) } @click = ${ this . #close} > </ uui- butto n>
131
160
<uui- butto n
132
161
label= "${ this . localize . term ( 'actions_unpublish' ) } "
133
- ?dis abled= ${ ! this . _hasUnpublishPermission || ! this . #selectionManager . getSelection ( ) . length }
162
+ ?dis abled= ${ this . _hasInvalidSelection || ! this . _hasUnpublishPermission || this . _selection . length === 0 }
134
163
look= "primary"
135
164
color = "warning"
136
165
@click = ${ this . #submit} > </ uui- butto n>
0 commit comments