4 {{ $i18n( 'ipb-pages-label' ).text() }}
6 <cdx-multiselect-lookup
7 v-model:input-chips="chips"
8 v-model:selected="selection"
9 v-model:input-value="inputValue"
10 class="mw-block-pages"
11 :menu-items="menuItems"
12 :menu-config="menuConfig"
13 :aria-label="$i18n( 'ipb-pages-label' ).text()"
14 :placeholder="placeholder"
16 @update:input-chips="onUpdateChips"
17 ></cdx-multiselect-lookup>
22 const { computed, defineComponent, ref } = require( 'vue' );
23 const { storeToRefs } = require( 'pinia' );
24 const { CdxField, ChipInputItem, CdxMultiselectLookup } = require( '@wikimedia/codex' );
25 const useBlockStore = require( '../stores/block.js' );
26 const api = new mw.Api();
27 const useMultiblocks = mw.config.get( 'blockEnableMultiblocks' );
28 const MAX_CHIPS = useMultiblocks ? 50 : 10;
31 * Field component for selecting pages to block.
33 * @todo Abstract for general use in MediaWiki (T375220)
35 module.exports = exports = defineComponent( {
42 const { pages } = storeToRefs( useBlockStore() );
43 if ( pages.value.length > MAX_CHIPS ) {
44 pages.value = pages.value.slice( 0, MAX_CHIPS );
47 pages.value.map( ( page ) => ( { value: page, label: page } ) )
49 const placeholder = computed( () => {
50 if ( chips.value.length === MAX_CHIPS ) {
53 return mw.msg( 'block-pages-placeholder' );
55 const selection = ref( pages.value );
56 const inputValue = ref( '' );
57 const menuItems = ref( [] );
58 const menuConfig = { visibleItemLimit: 10 };
63 * @param {string} searchTerm
67 function fetchResults( searchTerm ) {
74 } ).then( ( response ) => response.query );
78 * Handle lookup input.
80 * @param {string} value
82 const onInput = mw.util.debounce( ( value ) => {
83 if ( chips.value.length >= MAX_CHIPS ) {
88 // Clear menu items if the input was cleared.
94 fetchResults( value ).then( ( data ) => {
95 // Make sure this data is still relevant first.
96 if ( inputValue.value !== value ) {
100 // Reset the menu items if there are no results.
101 if ( !data || data.prefixsearch.length === 0 ) {
102 menuItems.value = [];
106 // Build and update the array of menu items.
107 menuItems.value = data.prefixsearch.map( ( result ) => ( {
112 // On error, set results to empty.
113 menuItems.value = [];
118 * Update the store with the new chip values.
120 * @param {ChipInputItem[]} newChips
122 function onUpdateChips( newChips ) {
123 pages.value = newChips.map( ( chip ) => chip.value );