All files / ext.wikilambda.app/components/types/wikidata ReferenceSelector.vue

100% Statements 124/124
93.33% Branches 14/15
100% Functions 2/2
100% Lines 124/124

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 1251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 19x 8x 8x 8x 8x 8x 8x 8x 8x 24x 24x 120x 120x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 5x 1x 1x 4x 4x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x  
<!--
	WikiLambda Vue component for selection of Wikidata Reference types.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-app-wikidata-reference-selector"
		data-testid="z-wikidata-reference-selector">
		<cdx-select
			:default-label="i18n( 'wikilambda-typeselector-label' ).text()"
			:selected="selectedValue"
			:disabled="disabled || !enumsFetched"
			:menu-items="enumValues"
			:menu-config="selectConfig"
			data-testid="z-wikidata-enum-type-select"
			@update:selected="onSelect"
		></cdx-select>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, onMounted } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const useMainStore = require( '../../../store/index.js' );
 
// Codex components
const { CdxSelect } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-wikidata-reference-selector',
	components: {
		'cdx-select': CdxSelect
	},
	props: {
		selectedZid: {
			type: String,
			default: ''
		},
		disabled: {
			type: Boolean,
			default: false
		}
	},
	emits: [ 'select-item' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const store = useMainStore();
 
		// Constants
		const selectConfig = {
			visibleItemLimit: 5
		};
 
		// Enum data
		/**
		 * Determines whether all Wikidata enum reference types have been fetched.
		 *
		 * @return {boolean} True if all enum reference types are fetched, false otherwise.
		 */
		const enumsFetched = computed( () => Object.keys( Constants.WIKIDATA_REFERENCE_TYPES )
			.map( ( k ) => Constants.WIKIDATA_REFERENCE_TYPES[ k ] )
			.every( ( zid ) => store.getStoredObject( zid ) ) );
 
		/**
		 * Retrieves an array of Wikidata enum reference values with their labels and IDs.
		 *
		 * @return {Array} An array of objects containing labels and values.
		 */
		const enumValues = computed( () => Object.keys( Constants.WIKIDATA_REFERENCE_TYPES )
			.map( ( k ) => Constants.WIKIDATA_REFERENCE_TYPES[ k ] )
			.map( ( zid ) => ( {
				label: store.getLabelData( zid ).label,
				value: zid
			} ) ) );
 
		// Selector display
 
		/**
		 * Retrieves the value of the selected reference.
		 *
		 * @return {string|null} The selected reference value, or null if none is selected.
		 */
		const selectedValue = computed( () => props.selectedZid || null );
 
		// Actions
		/**
		 * Handles the selection of a value from the dropdown.
		 * Emits an event to update the parent component with the selected value.
		 *
		 * @param {string|null} value - The selected value from the dropdown.
		 */
		function onSelect( value ) {
			// If the already selected value is selected again, exit early
			if ( selectedValue.value === value ) {
				return;
			}
 
			// If a new value is selected, emit the select event.
			emit( 'select-item', value || '' );
		}
 
		// Lifecycle hooks
		onMounted( () => {
			// Fetches the Zids for the Wikidata enum reference types when the component is mounted.
			const references = Object.keys( Constants.WIKIDATA_REFERENCE_TYPES )
				.map( ( k ) => Constants.WIKIDATA_REFERENCE_TYPES[ k ] );
			store.fetchZids( { zids: references } );
		} );
 
		return {
			enumValues,
			enumsFetched,
			onSelect,
			i18n,
			selectConfig,
			selectedValue
		};
	}
} );
 
</script>