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

95.7% Statements 334/349
82.92% Branches 34/41
92.85% Functions 13/14
95.7% Lines 334/349

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 3501x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x     20x 20x 20x 20x 20x 20x 17x 5x 5x 17x 20x 20x 20x 20x 20x 20x 20x 16x 16x 16x 16x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 35x 35x 35x 35x 35x 35x 35x 20x 20x 20x 20x 20x 20x 20x 20x 15x 15x 15x 20x 20x 20x 20x 20x 20x 20x 20x 15x 15x     15x 15x 15x 15x 15x 14x 14x 15x 15x 15x 15x 15x 15x     15x 15x 15x 15x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x     1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 20x 20x 20x 20x 20x 20x 20x 20x 20x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 15x 15x 15x 15x 15x 15x 15x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 12x 12x 5x 5x 7x 7x 7x 12x 1x 1x 1x 6x 12x 12x 20x 20x 20x 20x 20x 20x 20x 20x         20x 20x 20x 20x 20x 20x 3x 3x 1x 1x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 1x 3x 20x 20x 20x 20x 20x 20x 1x       1x 1x 1x 1x 20x 20x 20x 8x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component to search and select Wikidata Entities
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<cdx-lookup
		:input-value="inputValue"
		class="ext-wikilambda-app-wikidata-entity-selector"
		data-testid="wikidata-entity-selector"
		:selected="entityId"
		:placeholder="lookupPlaceholder"
		:menu-items="lookupResults"
		:menu-config="lookupConfig"
		:start-icon="wikidataIcon"
		@update:selected="onSelect"
		@update:input-value="onInput"
		@blur="onBlur"
		@focus="onFocus"
		@load-more="onLoadMore"
	>
		<template #no-results>
			{{ i18n( 'wikilambda-zobjectselector-no-results' ).text() }}
		</template>
	</cdx-lookup>
</template>
 
<script>
const { computed, defineComponent, inject, onMounted, ref, watch } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const useMainStore = require( '../../../store/index.js' );
const wikidataIconSvg = require( './wikidataIconSvg.js' );
const { extractIdFromUrl } = require( '../../../utils/urlUtils.js' );
 
// Codex components
const { CdxLookup } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-wikidata-entity-selector',
	components: {
		'cdx-lookup': CdxLookup
	},
	props: {
		entityId: {
			type: [ String, null ],
			required: true
		},
		entityLabel: {
			type: String,
			required: true
		},
		type: {
			type: String,
			required: true
		},
		placeholder: {
			type: String,
			required: false,
			default: ''
		}
	},
	emits: [ 'select-wikidata-entity' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const store = useMainStore();
 
		// Constants
		const wikidataIcon = wikidataIconSvg;
 
		// Lookup
		const inputValue = ref( '' );
		const lookupResults = ref( [] );
		const lookupConfig = ref( {
			boldLabel: true,
			searchQuery: '',
			visibleItemLimit: 5,
			searchContinue: null
		} );
		let lookupDelayTimer = null;
		const lookupDelayMs = 300;
		let lookupAbortController = null;
 
		/**
		 * Return the placeholder string depending on the item to select
		 *
		 * @return {string}
		 */
		const lookupPlaceholder = computed( () => {
			if ( props.placeholder ) {
				return props.placeholder;
			}
			const msg = Constants.WIKIDATA_SELECTOR_PLACEHOLDER_MSG[ props.type ];
 
			return i18n( msg || 'wikilambda-wikidata-entity-selector-placeholder' ).text();
		} );
 
		function abortLookup() {
			if ( lookupAbortController ) {
				lookupAbortController.abort();
			}
		}
 
		/**
		 * Reset the abort controller and return the signal.
		 *
		 * @return {AbortSignal}
		 */
		function resetAbortController() {
			abortLookup();
			lookupAbortController = new AbortController();
			return lookupAbortController.signal;
		}
 
		/**
		 * Builds a menu item object from a Wikidata entity result.
		 *
		 * @param {Object} entity - The entity result from the API
		 * @param {string} entity.id
		 * @param {string} entity.label
		 * @param {string} entity.description
		 * @return {Object} A menu item object for Codex
		 */
		function buildMenuItem( { id, label, description } ) {
			return {
				value: id,
				label,
				description,
				title: description
			};
		}
 
		/**
		 * Updates the lookup configuration with new search data.
		 *
		 * @param {string} searchQuery - The search query string
		 * @param {string|null} searchContinue - The search continuation token
		 */
		function updateLookupConfig( searchQuery, searchContinue ) {
			lookupConfig.value.searchQuery = searchQuery;
			lookupConfig.value.searchContinue = searchContinue;
		}
 
		/**
		 * Processes lookup response data and updates the lookup results.
		 *
		 * @param {Object} data - The response data from the API
		 * @param {string} searchTerm - The original search term
		 */
		function handleLookupResponse( data, searchTerm ) {
			// Discard responses from superseded searches.
			if ( searchTerm !== inputValue.value ) {
				return;
			}
 
			const { searchContinue, search } = data;
 
			// Clear results if this is a new search (not a continuation)
			if ( !lookupConfig.value.searchContinue ) {
				lookupResults.value = [];
			}
 
			// Update lookup configuration
			updateLookupConfig( searchTerm, searchContinue );
 
			// Process search results if available
			if ( !search || search.length === 0 ) {
				return;
			}
 
			const newMenuItems = search.map( ( entity ) => buildMenuItem( entity ) );
			lookupResults.value.push( ...newMenuItems );
		}
 
		/**
		 * Handle lookup error.
		 *
		 * @param {Error} error
		 * @param {string} searchTerm
		 */
		function handleLookupError( error, searchTerm ) {
			// Discard errors from superseded searches or aborted requests.
			if ( error.code === 'abort' || searchTerm !== inputValue.value ) {
				return;
			}
			lookupConfig.value.searchQuery = searchTerm;
			if ( !lookupConfig.value.searchContinue ) {
				lookupResults.value = [];
			}
		}
 
		/**
		 * Perform Wikidata Entity lookup given a search term.
		 *
		 * @param {string} searchTerm
		 */
		function getLookupResults( searchTerm ) {
			const signal = resetAbortController();
			const payload = {
				// Users often paste a link to an entity instead of typing its Id
				search: extractIdFromUrl( searchTerm ) || searchTerm,
				type: Constants.WIKIDATA_API_TYPE_VALUES[ props.type ],
				searchContinue: lookupConfig.value.searchContinue,
				signal
			};
 
			store.lookupWikidataEntities( payload )
				.then( ( data ) => handleLookupResponse( data, searchTerm ) )
				.catch( ( error ) => handleLookupError( error, searchTerm ) );
		}
 
		// Input handlers
		/**
		 * On field input, perform a backend lookup and
		 * save the returned objects in lookupResults array.
		 *
		 * @param {string} input
		 */
		function onInput( input ) {
			inputValue.value = input;
 
			// Reset searchContinue when a new search is initiated
			lookupConfig.value.searchContinue = null;
 
			// If empty input, do nothing
			if ( !input ) {
				abortLookup();
				lookupResults.value = [];
				return;
			}
 
			// Search after 300 ms
			clearTimeout( lookupDelayTimer );
			lookupDelayTimer = setTimeout( () => {
				getLookupResults( input );
			}, lookupDelayMs );
		}
 
		/**
		 * When lookup selected value updates, emit a set-value
		 * event so that parent ZObjectKeyValue sets the value
		 * of the Fetch Wikidata Entity function call.
		 * If the field is cleared, set value as empty string.
		 *
		 * @param {string} value
		 */
		function onSelect( value ) {
			// T374246: Disable clear strategy
			if ( value === null ) {
				return;
			}
 
			// If the already selected value is selected again, exit early
			// and reset the input value to the selected value (T382755).
			if ( props.entityId === value ) {
				inputValue.value = props.entityLabel;
				return;
			}
 
			emit( 'select-wikidata-entity', value || '' );
		}
 
		/**
		 * On focus, if there is an inputValue and the lookupResults are empty,
		 * get the lookup results for the inputValue.
		 * This ensures that the lookup results are populated when the field is focused,
		 * especially when the field is mounted.
		 */
		function onFocus() {
			if ( inputValue.value && !lookupResults.value.length ) {
				getLookupResults( inputValue.value );
			}
		}
 
		/**
		 * On blur, select the value that matches the inputValue if valid;
		 * else, restore the previous selected value.
		 */
		function onBlur() {
			// If current inputValue matches selected lexeme, do nothing:
			if ( inputValue.value === props.entityLabel ) {
				return;
			}
 
			// Match current inputValue with available menu options:
			const match = lookupResults.value.find( ( option ) => option.label === inputValue.value );
			if ( match ) {
				// Select new value
				onSelect( match.value );
			} else {
				// Reset to old value
				inputValue.value = props.entityLabel;
				lookupConfig.value.searchQuery = props.entityLabel;
			}
		}
 
		/**
		 * Load more Wikidata Entities when the user scrolls to the bottom of the list
		 * and there are more results to load.
		 */
		function onLoadMore() {
			if ( !lookupConfig.value.searchContinue ) {
				// No more results to load
				return;
			}
 
			// Use the existing search term stored in lookupConfig.searchQuery
			getLookupResults( lookupConfig.value.searchQuery );
		}
 
		// Watch
		watch( () => props.entityLabel, ( label ) => {
			inputValue.value = label;
		} );
 
		// Lifecycle
		onMounted( () => {
			inputValue.value = props.entityLabel;
		} );
 
		return {
			inputValue,
			lookupConfig,
			lookupPlaceholder,
			lookupResults,
			onBlur,
			onFocus,
			onInput,
			onLoadMore,
			onSelect,
			wikidataIcon,
			i18n
		};
	}
} );
</script>
 
<style lang="less">
.ext-wikilambda-app-wikidata-entity-selector {
	.cdx-menu-item__text__description {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		line-clamp: 2;
		overflow: hidden;
	}
}
</style>