Debian Codegraph @

resources/src/mediawiki.special.block/SpecialBlock.vue

raw
1 <template>
2 <cdx-field
3 class="mw-block-fieldset"
4 :is-fieldset="true"
5 :disabled="formDisabled"
6 >
7 <div ref="messagesContainer" class="mw-block-messages">
8 <cdx-message
9 v-if="success"
10 type="success"
11 :allow-user-dismiss="true"
12 class="mw-block-success"
13 >
14 <p><strong>{{ $i18n( 'blockipsuccesssub' ) }}</strong></p>
15 <!-- eslint-disable-next-line vue/no-v-html -->
16 <p v-html="$i18n( 'block-success', store.targetUser ).parse()"></p>
17 </cdx-message>
18 <cdx-message
19 v-for="( formError, index ) in formErrors"
20 :key="index"
21 type="error"
22 class="mw-block-error"
23 inline
24 >
25 {{ formError }}
26 </cdx-message>
27 </div>
28 <user-lookup
29 v-model="store.targetUser"
30 :form-submitted="formSubmitted"
31 @input="store.alreadyBlocked = false"
32 ></user-lookup>
33 <target-active-blocks
34 v-if="blockEnableMultiblocks"
35 :target-user="store.targetUser"
36 ></target-active-blocks>
37 <target-block-log
38 :target-user="store.targetUser"
39 ></target-block-log>
40 <block-type-field></block-type-field>
41 <expiry-field :form-submitted="formSubmitted"></expiry-field>
42 <reason-field
43 v-model:selected="store.reason"
44 v-model:other="store.reasonOther"
45 ></reason-field>
46 <block-details-field
47 v-model="store.details"
48 :checkboxes="blockDetailsOptions"
49 :label="$i18n( 'block-details' ).text()"
50 :description="$i18n( 'block-details-description' ).text()"
51 ></block-details-field>
52 <block-details-field
53 v-model="store.additionalDetails"
54 :checkboxes="additionalDetailsOptions"
55 :label="$i18n( 'block-options' ).text()"
56 :description="$i18n( 'block-options-description' ).text()"
57 ></block-details-field>
58 <cdx-field v-if="store.confirmationRequired">
59 <cdx-checkbox
60 v-model="store.confirmationChecked"
61 class="mw-block-confirm"
62 >
63 {{ $i18n( 'ipb-confirm' ).text() }}
64 </cdx-checkbox>
65 </cdx-field>
66 <hr class="mw-block-hr">
67 <cdx-button
68 action="destructive"
69 weight="primary"
70 class="mw-block-submit"
71 :disabled="formDisabled || ( store.confirmationRequired && !store.confirmationChecked )"
72 @click="handleSubmit"
73 >
74 {{ submitButtonMessage }}
75 </cdx-button>
76 </cdx-field>
77 </template>
78
79 <script>
80 const { computed, defineComponent, nextTick, ref, Ref, watch } = require( 'vue' );
81 const { storeToRefs } = require( 'pinia' );
82 const { CdxButton, CdxCheckbox, CdxField, CdxMessage } = require( '@wikimedia/codex' );
83 const useBlockStore = require( './stores/block.js' );
84 const UserLookup = require( './components/UserLookup.vue' );
85 const TargetActiveBlocks = require( './components/TargetActiveBlocks.vue' );
86 const TargetBlockLog = require( './components/TargetBlockLog.vue' );
87 const BlockTypeField = require( './components/BlockTypeField.vue' );
88 const ExpiryField = require( './components/ExpiryField.vue' );
89 const ReasonField = require( './components/ReasonField.vue' );
90 const BlockDetailsField = require( './components/BlockDetailsOptions.vue' );
91
92 module.exports = exports = defineComponent( {
93 name: 'SpecialBlock',
94 components: {
95 UserLookup,
96 TargetActiveBlocks,
97 TargetBlockLog,
98 BlockTypeField,
99 ExpiryField,
100 ReasonField,
101 BlockDetailsField,
102 CdxButton,
103 CdxCheckbox,
104 CdxField,
105 CdxMessage
106 },
107 setup() {
108 const store = useBlockStore();
109 const blockEnableMultiblocks = mw.config.get( 'blockEnableMultiblocks' ) || false;
110 const success = ref( false );
111 /**
112 * Whether the form has been submitted. This is used to only show error states
113 * after the user has attempted to submit the form.
114 *
115 * @type {Ref<boolean>}
116 */
117 const formSubmitted = ref( false );
118 const formDisabled = ref( false );
119 const messagesContainer = ref();
120 // eslint-disable-next-line arrow-body-style
121 const submitButtonMessage = computed( () => {
122 return mw.message( store.alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit' ).text();
123 } );
124 const blockAllowsUTEdit = mw.config.get( 'blockAllowsUTEdit' ) || false;
125 const blockEmailBan = mw.config.get( 'blockAllowsEmailBan' ) || false;
126 const blockAutoblockExpiry = mw.config.get( 'blockAutoblockExpiry' );
127 const blockHideUser = mw.config.get( 'blockHideUser' ) || false;
128 const blockDetailsOptions = [
129 {
130 label: mw.message( 'ipbcreateaccount' ),
131 value: 'wpCreateAccount'
132 }
133 ];
134
135 if ( blockEmailBan ) {
136 blockDetailsOptions.push( {
137 label: mw.message( 'ipbemailban' ),
138 value: 'wpDisableEmail'
139 } );
140 }
141
142 if ( blockAllowsUTEdit ) {
143 blockDetailsOptions.push( {
144 label: mw.message( 'ipb-disableusertalk' ),
145 value: 'wpDisableUTEdit'
146 } );
147 }
148
149 const additionalDetailsOptions = [ {
150 label: mw.message( 'ipbenableautoblock', blockAutoblockExpiry ),
151 value: 'wpAutoBlock',
152 disabled: false
153 } ];
154
155 if ( blockHideUser ) {
156 additionalDetailsOptions.push( {
157 label: mw.message( 'ipbhidename' ),
158 value: 'wpHideName',
159 class: 'mw-block-hideuser'
160 } );
161 }
162
163 // Show an error message if the target user is the current user.
164 const { formErrors, targetUser } = storeToRefs( store );
165 watch( targetUser, ( newValue ) => {
166 if ( newValue === mw.config.get( 'wgUserName' ) ) {
167 formErrors.value.push( mw.msg( 'ipb-blockingself', 'ipb-confirmaction' ) );
168 } else {
169 formErrors.value = [];
170 }
171 } );
172
173 additionalDetailsOptions.push( {
174 label: mw.message( 'ipbwatchuser' ),
175 value: 'wpWatch',
176 disabled: false
177 } );
178
179 additionalDetailsOptions.push( {
180 label: mw.message( 'ipb-hardblock' ),
181 value: 'wpHardBlock',
182 disabled: false
183 } );
184
185 /**
186 * Handle form submission. If the form is invalid, show the browser's
187 * validation messages along with any custom validation messages, and
188 * scroll to the first error message to ensure the user sees it.
189 *
190 * If the form is valid, send the block request to the server,
191 * add the success message and scroll to it.
192 *
193 * @param {Event} event
194 */
195 function handleSubmit( event ) {
196 event.preventDefault();
197 formSubmitted.value = true;
198 success.value = false;
199
200 // checkValidity() executes browser form validation, which triggers automatic
201 // validation states on applicable components (e.g. fields with `required` attr).
202 if ( event.target.form.checkValidity() && store.expiry ) {
203 store.doBlock()
204 .done( () => {
205 success.value = true;
206 } )
207 .fail( ( _, errorObj ) => {
208 formErrors.value = [ errorObj.error.info ];
209 success.value = false;
210 } )
211 .always( () => {
212 formDisabled.value = false;
213 messagesContainer.value.scrollIntoView( { behavior: 'smooth' } );
214 } );
215 } else {
216 // nextTick() needed to ensure error messages are rendered before scrolling.
217 nextTick( () => {
218 // Currently, only the expiry field has custom validations.
219 // Scrolling to `cdx-message--error` is merely future-proofing to
220 // ensure the user sees the error message, wherever it may be.
221 // Actual validation logic should live in the respective component,
222 // and only apply after the form has been submitted via a `formSubmitted` prop.
223 document.querySelector( '.cdx-message--error' )
224 .scrollIntoView( { behavior: 'smooth' } );
225 } );
226 }
227 }
228
229 return {
230 store,
231 formSubmitted,
232 formDisabled,
233 messagesContainer,
234 formErrors,
235 success,
236 submitButtonMessage,
237 handleSubmit,
238 blockDetailsOptions,
239 additionalDetailsOptions,
240 blockEnableMultiblocks
241 };
242 }
243 } );
244 </script>
245
246 <style lang="less">
247 @import 'mediawiki.skin.variables.less';
248
249 // HACK: The accordions within the form need to be full width.
250 .mw-htmlform-codex {
251 max-width: unset;
252 }
253
254 // HACK: Set the max-width of the fields back to what they should be.
255 .cdx-field:not( .mw-block-fieldset ) {
256 max-width: @size-4000;
257 }
258
259 .mw-block-hideuser,
260 .mw-block-confirm {
261 .cdx-checkbox__label .cdx-label__label__text {
262 font-weight: @font-weight-bold;
263 }
264 }
265
266 .mw-block-hr {
267 margin-top: @spacing-200;
268 }
269
270 .mw-block-submit.cdx-button {
271 margin-top: @spacing-100;
272 }
273
274 .mw-block-error {
275 margin-left: @spacing-75;
276 }
277 </style>