| 1 |
<template> |
| 2 |
<cdx-field> |
| 3 |
<template #label> |
| 4 |
{{ $i18n( 'ipb-namespaces-label' ).text() }} |
| 5 |
</template> |
| 6 |
<cdx-multiselect-lookup |
| 7 |
v-model:input-chips="chips" |
| 8 |
v-model:selected="selection" |
| 9 |
class="mw-block-namespaces" |
| 10 |
:menu-items="menuItems" |
| 11 |
:menu-config="menuConfig" |
| 12 |
:aria-label="$i18n( 'ipb-namespaces-label' ).text()" |
| 13 |
:placeholder="$i18n( 'block-namespaces-placeholder' ).text()" |
| 14 |
@input="onInput" |
| 15 |
@update:input-chips="onUpdateChips" |
| 16 |
></cdx-multiselect-lookup> |
| 17 |
</cdx-field> |
| 18 |
</template> |
| 19 |
|
| 20 |
<script> |
| 21 |
const { defineComponent, ref } = require( 'vue' ); |
| 22 |
const { storeToRefs } = require( 'pinia' ); |
| 23 |
const { CdxField, ChipInputItem, CdxMultiselectLookup } = require( '@wikimedia/codex' ); |
| 24 |
const useBlockStore = require( '../stores/block.js' ); |
| 25 |
|
| 26 |
module.exports = exports = defineComponent( { |
| 27 |
name: 'NamespacesField', |
| 28 |
components: { |
| 29 |
CdxField, |
| 30 |
CdxMultiselectLookup |
| 31 |
}, |
| 32 |
setup() { |
| 33 |
const { namespaces } = storeToRefs( useBlockStore() ); |
| 34 |
const selection = ref( [] ); |
| 35 |
const mwNamespaces = mw.config.get( 'wgFormattedNamespaces' ); |
| 36 |
mwNamespaces[ '0' ] = mw.msg( 'blanknamespace' ); |
| 37 |
const initialMenuItems = Object.keys( mwNamespaces ).map( ( id ) => ( { |
| 38 |
value: Number( id ), |
| 39 |
label: mwNamespaces[ id ] |
| 40 |
} ) ); |
| 41 |
const chips = ref( |
| 42 |
namespaces.value.map( ( nsId ) => ( { value: nsId, label: mwNamespaces[ nsId ] } ) ) |
| 43 |
); |
| 44 |
const menuItems = ref( initialMenuItems ); |
| 45 |
const menuConfig = { visibleItemLimit: 10 }; |
| 46 |
|
| 47 |
/** |
| 48 |
* On input, filter the menu items. |
| 49 |
* |
| 50 |
* @param {string} value |
| 51 |
*/ |
| 52 |
function onInput( value ) { |
| 53 |
if ( value ) { |
| 54 |
// eslint-disable-next-line arrow-body-style |
| 55 |
menuItems.value = initialMenuItems.filter( ( item ) => { |
| 56 |
return item.label.toLowerCase().indexOf( value.toLowerCase() ) !== -1; |
| 57 |
} ); |
| 58 |
} else { |
| 59 |
menuItems.value = initialMenuItems; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Update the store with the new chip values. |
| 65 |
* |
| 66 |
* @param {ChipInputItem[]} newChips |
| 67 |
*/ |
| 68 |
function onUpdateChips( newChips ) { |
| 69 |
namespaces.value = newChips.map( ( chip ) => chip.value ); |
| 70 |
} |
| 71 |
|
| 72 |
return { |
| 73 |
chips, |
| 74 |
selection, |
| 75 |
menuItems, |
| 76 |
menuConfig, |
| 77 |
onInput, |
| 78 |
onUpdateChips |
| 79 |
}; |
| 80 |
} |
| 81 |
} ); |
| 82 |
</script> |