| 1 |
<template> |
| 2 |
<cdx-field> |
| 3 |
<template #label> |
| 4 |
{{ $i18n( 'ipb-pages-label' ).text() }} |
| 5 |
</template> |
| 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="$i18n( 'block-pages-placeholder' ).text()" |
| 15 |
@input="onInput" |
| 16 |
@update:input-chips="onUpdateChips" |
| 17 |
></cdx-multiselect-lookup> |
| 18 |
</cdx-field> |
| 19 |
</template> |
| 20 |
|
| 21 |
<script> |
| 22 |
const { 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 |
|
| 28 |
module.exports = exports = defineComponent( { |
| 29 |
name: 'PagesField', |
| 30 |
components: { |
| 31 |
CdxField, |
| 32 |
CdxMultiselectLookup |
| 33 |
}, |
| 34 |
setup() { |
| 35 |
const { pages } = storeToRefs( useBlockStore() ); |
| 36 |
const chips = ref( |
| 37 |
pages.value.map( ( page ) => ( { value: page, label: page } ) ) |
| 38 |
); |
| 39 |
const selection = ref( [] ); |
| 40 |
const inputValue = ref( '' ); |
| 41 |
const menuItems = ref( [] ); |
| 42 |
const menuConfig = { visibleItemLimit: 10 }; |
| 43 |
|
| 44 |
/** |
| 45 |
* Get search results. |
| 46 |
* |
| 47 |
* @param {string} searchTerm |
| 48 |
* |
| 49 |
* @return {Promise} |
| 50 |
*/ |
| 51 |
function fetchResults( searchTerm ) { |
| 52 |
return api.get( { |
| 53 |
action: 'query', |
| 54 |
list: 'prefixsearch', |
| 55 |
pssearch: searchTerm, |
| 56 |
pslimit: 10, |
| 57 |
format: 'json' |
| 58 |
} ).then( ( response ) => response.query ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Handle lookup input. |
| 63 |
* |
| 64 |
* @param {string} value |
| 65 |
*/ |
| 66 |
const onInput = mw.util.debounce( ( value ) => { |
| 67 |
// Clear menu items if the input was cleared. |
| 68 |
if ( !value ) { |
| 69 |
menuItems.value = []; |
| 70 |
return; |
| 71 |
} |
| 72 |
|
| 73 |
fetchResults( value ).then( ( data ) => { |
| 74 |
// Make sure this data is still relevant first. |
| 75 |
if ( inputValue.value !== value ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
// Reset the menu items if there are no results. |
| 80 |
if ( !data || data.prefixsearch.length === 0 ) { |
| 81 |
menuItems.value = []; |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Build and update the array of menu items. |
| 86 |
menuItems.value = data.prefixsearch.map( ( result ) => ( { |
| 87 |
label: result.title, |
| 88 |
value: result.title |
| 89 |
} ) ); |
| 90 |
} ).catch( () => { |
| 91 |
// On error, set results to empty. |
| 92 |
menuItems.value = []; |
| 93 |
} ); |
| 94 |
}, 300 ); |
| 95 |
|
| 96 |
/** |
| 97 |
* Update the store with the new chip values. |
| 98 |
* |
| 99 |
* @param {ChipInputItem[]} newChips |
| 100 |
*/ |
| 101 |
function onUpdateChips( newChips ) { |
| 102 |
pages.value = newChips.map( ( chip ) => chip.value ); |
| 103 |
} |
| 104 |
|
| 105 |
return { |
| 106 |
chips, |
| 107 |
selection, |
| 108 |
inputValue, |
| 109 |
menuItems, |
| 110 |
menuConfig, |
| 111 |
onInput, |
| 112 |
onUpdateChips |
| 113 |
}; |
| 114 |
} |
| 115 |
} ); |
| 116 |
</script> |