| 1 |
<template> |
| 2 |
<cdx-field |
| 3 |
class="mw-block-expiry-field" |
| 4 |
:is-fieldset="true" |
| 5 |
> |
| 6 |
<template #label> |
| 7 |
{{ $i18n( 'block-expiry' ).text() }} |
| 8 |
</template> |
| 9 |
|
| 10 |
<cdx-radio |
| 11 |
v-model="expiryType" |
| 12 |
name="expiryType" |
| 13 |
input-value="preset-duration" |
| 14 |
> |
| 15 |
{{ $i18n( 'block-expiry-preset' ).text() }} |
| 16 |
<template v-if="expiryType === 'preset-duration'" #custom-input> |
| 17 |
<cdx-field |
| 18 |
class="mw-block-expiry-field__preset-duration" |
| 19 |
:status="presetDurationStatus" |
| 20 |
:messages="presetDurationMessages" |
| 21 |
> |
| 22 |
<cdx-select |
| 23 |
v-if="expiryType === 'preset-duration'" |
| 24 |
v-model:selected="presetDuration" |
| 25 |
:menu-items="presetDurationOptions" |
| 26 |
:default-label="$i18n( 'block-expiry-preset-placeholder' ).text()" |
| 27 |
@update:selected="() => { |
| 28 |
presetDurationStatus = 'default'; |
| 29 |
presetDurationMessages = {}; |
| 30 |
}" |
| 31 |
></cdx-select> |
| 32 |
</cdx-field> |
| 33 |
</template> |
| 34 |
</cdx-radio> |
| 35 |
|
| 36 |
<cdx-radio |
| 37 |
v-model="expiryType" |
| 38 |
name="expiryType" |
| 39 |
input-value="custom-duration" |
| 40 |
> |
| 41 |
{{ $i18n( 'block-expiry-custom' ).text() }} |
| 42 |
<template v-if="expiryType === 'custom-duration'" #custom-input> |
| 43 |
<cdx-field |
| 44 |
class="mw-block-expiry-field__custom-duration" |
| 45 |
:status="customDurationStatus" |
| 46 |
:messages="customDurationMessages" |
| 47 |
> |
| 48 |
<validating-text-input |
| 49 |
v-model="customDurationNumber" |
| 50 |
input-type="number" |
| 51 |
min="1" |
| 52 |
required |
| 53 |
@update:status="( status, message ) => { |
| 54 |
customDurationMessages = message; |
| 55 |
customDurationStatus = status; |
| 56 |
}" |
| 57 |
></validating-text-input> |
| 58 |
<cdx-select |
| 59 |
v-model:selected="customDurationUnit" |
| 60 |
:menu-items="customDurationOptions" |
| 61 |
></cdx-select> |
| 62 |
</cdx-field> |
| 63 |
</template> |
| 64 |
</cdx-radio> |
| 65 |
|
| 66 |
<cdx-radio |
| 67 |
v-model="expiryType" |
| 68 |
name="expiryType" |
| 69 |
input-value="datetime" |
| 70 |
> |
| 71 |
{{ $i18n( 'block-expiry-datetime' ).text() }} |
| 72 |
<template v-if="expiryType === 'datetime'" #custom-input> |
| 73 |
<cdx-field |
| 74 |
class="mw-block-expiry-field__datetime" |
| 75 |
:status="datetimeStatus" |
| 76 |
:messages="datetimeMessages" |
| 77 |
> |
| 78 |
<validating-text-input |
| 79 |
v-if="expiryType === 'datetime'" |
| 80 |
v-model="datetime" |
| 81 |
input-type="datetime-local" |
| 82 |
name="wpExpiry-other" |
| 83 |
:min="new Date().toISOString().slice( 0, 16 )" |
| 84 |
required |
| 85 |
@update:status="( status, message ) => { |
| 86 |
datetimeMessages = message; |
| 87 |
datetimeStatus = status; |
| 88 |
}" |
| 89 |
></validating-text-input> |
| 90 |
</cdx-field> |
| 91 |
</template> |
| 92 |
</cdx-radio> |
| 93 |
</cdx-field> |
| 94 |
</template> |
| 95 |
|
| 96 |
<script> |
| 97 |
const { defineComponent, ref, computed, watch } = require( 'vue' ); |
| 98 |
const { CdxField, CdxRadio, CdxSelect } = require( '@wikimedia/codex' ); |
| 99 |
const { storeToRefs } = require( 'pinia' ); |
| 100 |
const ValidatingTextInput = require( './ValidatingTextInput.js' ); |
| 101 |
const useBlockStore = require( '../stores/block.js' ); |
| 102 |
|
| 103 |
module.exports = exports = defineComponent( { |
| 104 |
name: 'ExpiryField', |
| 105 |
components: { |
| 106 |
CdxField, |
| 107 |
CdxRadio, |
| 108 |
CdxSelect, |
| 109 |
ValidatingTextInput |
| 110 |
}, |
| 111 |
props: { |
| 112 |
/** |
| 113 |
* Whether the form has been submitted yet. This is used to show |
| 114 |
* validation messages only after the form has been submitted. |
| 115 |
*/ |
| 116 |
formSubmitted: { |
| 117 |
type: Boolean, |
| 118 |
default: false |
| 119 |
} |
| 120 |
}, |
| 121 |
setup( props ) { |
| 122 |
const store = useBlockStore(); |
| 123 |
const blockExpiryOptions = mw.config.get( 'blockExpiryOptions' ); |
| 124 |
const presetDurationOptions = Object.keys( blockExpiryOptions ) |
| 125 |
.map( ( key ) => ( { label: key, value: blockExpiryOptions[ key ] } ) ) |
| 126 |
// Don't include "other" in the preset options as it's handled separately in the new UI |
| 127 |
.filter( ( option ) => option.value !== 'other' ); |
| 128 |
|
| 129 |
const customDurationOptions = [ |
| 130 |
{ value: 'minutes', label: mw.msg( 'block-expiry-custom-minutes' ) }, |
| 131 |
{ value: 'hours', label: mw.msg( 'block-expiry-custom-hours' ) }, |
| 132 |
{ value: 'days', label: mw.msg( 'block-expiry-custom-days' ) }, |
| 133 |
{ value: 'weeks', label: mw.msg( 'block-expiry-custom-weeks' ) }, |
| 134 |
{ value: 'months', label: mw.msg( 'block-expiry-custom-months' ) }, |
| 135 |
{ value: 'years', label: mw.msg( 'block-expiry-custom-years' ) } |
| 136 |
]; |
| 137 |
|
| 138 |
const presetDuration = ref( null ); |
| 139 |
const presetDurationStatus = ref( 'default' ); |
| 140 |
const presetDurationMessages = ref( {} ); |
| 141 |
const customDurationNumber = ref( 1 ); |
| 142 |
const customDurationUnit = ref( 'hours' ); |
| 143 |
const customDurationStatus = ref( 'default' ); |
| 144 |
const customDurationMessages = ref( {} ); |
| 145 |
const datetime = ref( '' ); |
| 146 |
const datetimeStatus = ref( 'default' ); |
| 147 |
const datetimeMessages = ref( {} ); |
| 148 |
const expiryType = ref( 'preset-duration' ); |
| 149 |
|
| 150 |
const computedModelValue = computed( () => { |
| 151 |
if ( expiryType.value === 'preset-duration' ) { |
| 152 |
return presetDuration.value; |
| 153 |
} else if ( expiryType.value === 'custom-duration' ) { |
| 154 |
return `${ Number( customDurationNumber.value ) } ${ customDurationUnit.value }`; |
| 155 |
} else { |
| 156 |
return datetime.value; |
| 157 |
} |
| 158 |
} ); |
| 159 |
|
| 160 |
watch( computedModelValue, ( newValue ) => { |
| 161 |
const { expiry } = storeToRefs( store ); |
| 162 |
expiry.value = newValue; |
| 163 |
} ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Set the form fields according to the given expiry. |
| 167 |
* |
| 168 |
* @param {string} given |
| 169 |
* @return {boolean} false if the given expiry is invalid or of an unsupported format. |
| 170 |
*/ |
| 171 |
function setDurationFromGiven( given ) { |
| 172 |
const optionsContainsValue = ( opts, v ) => opts.some( ( option ) => option.value === v ); |
| 173 |
if ( mw.util.isInfinity( given ) ) { |
| 174 |
expiryType.value = 'preset-duration'; |
| 175 |
// FIXME: Assumes that the "infinite" option exists. |
| 176 |
// (It has to be for this form as there's no other way to specify infinite) |
| 177 |
presetDuration.value = 'infinite'; |
| 178 |
} else if ( optionsContainsValue( presetDurationOptions, given ) ) { |
| 179 |
expiryType.value = 'preset-duration'; |
| 180 |
presetDuration.value = given; |
| 181 |
} else if ( /^\d+ [a-z]+$/.test( given ) ) { |
| 182 |
const [ number, unit ] = given.split( ' ' ); |
| 183 |
// Normalize the unit to plural form, as used by customDurationOptions. |
| 184 |
const unitPlural = unit.replace( /s?$/, 's' ); |
| 185 |
if ( optionsContainsValue( customDurationOptions, unitPlural ) ) { |
| 186 |
expiryType.value = 'custom-duration'; |
| 187 |
customDurationNumber.value = Number( number ); |
| 188 |
customDurationUnit.value = unitPlural; |
| 189 |
} else { |
| 190 |
return false; |
| 191 |
} |
| 192 |
} else if ( /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}$/.test( given ) ) { |
| 193 |
expiryType.value = 'datetime'; |
| 194 |
datetime.value = given; |
| 195 |
} else { |
| 196 |
// Unsupported format. |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
// Set the form fields according to the preselected expiry (?wpExpiry=). |
| 204 |
if ( !setDurationFromGiven( mw.config.get( 'blockExpiryPreset' ) ) ) { |
| 205 |
// If no expiry is preselected, attempt to go by [[MediaWiki:Ipb-default-expiry]]. |
| 206 |
setDurationFromGiven( mw.config.get( 'blockExpiryDefault' ) ); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* The preset duration field is a dropdown that requires custom validation. |
| 211 |
* We simply need to assert something is selected, but only do so after form submission. |
| 212 |
*/ |
| 213 |
watch( () => props.formSubmitted, () => { |
| 214 |
if ( expiryType.value === 'preset-duration' && !presetDuration.value ) { |
| 215 |
presetDurationStatus.value = 'error'; |
| 216 |
presetDurationMessages.value = { error: mw.msg( 'ipb_expiry_invalid' ) }; |
| 217 |
} |
| 218 |
} ); |
| 219 |
|
| 220 |
return { |
| 221 |
presetDurationOptions, |
| 222 |
presetDurationStatus, |
| 223 |
presetDurationMessages, |
| 224 |
customDurationStatus, |
| 225 |
customDurationMessages, |
| 226 |
customDurationOptions, |
| 227 |
presetDuration, |
| 228 |
customDurationNumber, |
| 229 |
customDurationUnit, |
| 230 |
datetime, |
| 231 |
datetimeStatus, |
| 232 |
datetimeMessages, |
| 233 |
expiryType |
| 234 |
}; |
| 235 |
} |
| 236 |
} ); |
| 237 |
</script> |
| 238 |
|
| 239 |
<style lang="less"> |
| 240 |
@import 'mediawiki.skin.variables.less'; |
| 241 |
|
| 242 |
.mw-block-expiry-field { |
| 243 |
.cdx-radio__custom-input .cdx-label { |
| 244 |
display: none; |
| 245 |
} |
| 246 |
|
| 247 |
.cdx-select-vue { |
| 248 |
margin-bottom: 0; |
| 249 |
} |
| 250 |
|
| 251 |
&__custom-duration .cdx-field__control { |
| 252 |
display: flex; |
| 253 |
gap: @spacing-50; |
| 254 |
|
| 255 |
&__number, |
| 256 |
&__unit { |
| 257 |
flex-grow: 1; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
</style> |