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

95.87% Statements 418/436
88.37% Branches 38/43
100% Functions 5/5
95.87% Lines 418/436

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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 4371x 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 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 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 11x 11x 11x 5x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 164x 16x 16x 148x 164x 100x 100x 48x 58x 58x 58x 58x 58x 58x 58x 48x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 92x 92x 66x 16x 16x 16x 16x 16x 16x 16x 16x 40x 20x 16x 16x 16x 16x 16x 16x 16x 16x             16x 16x 16x 16x 16x 16x 16x 16x 16x 14x 14x 14x 14x 14x 14x 14x 16x 16x 16x 16x 16x 16x 16x 16x 16x 3x     3x 3x 3x 3x 3x 3x 3x 3x 3x       3x 3x 3x 16x 16x 16x 16x 16x 16x 16x 16x 2x 1x 1x 2x 1x 1x 1x 2x 2x 2x 16x 16x 16x           16x 16x 16x 43x     16x 16x 16x 16x 16x 11x 11x 11x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 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  
<!--
	WikiLambda Vue component for Z6006/Wikidata Lexeme Sense objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-app-wikidata-lexeme-sense"
		data-testid="wikidata-lexeme-sense">
		<template v-if="hasTerminalId">
			<div v-if="!edit" class="ext-wikilambda-app-wikidata-lexeme-sense__read">
				<cdx-icon
					class="ext-wikilambda-app-wikidata-lexeme-sense__wd-icon"
					:icon="wikidataIcon"
				></cdx-icon>
				<a
					v-if="lexemeSenseLabelData && lexemeSenseUrl"
					class="ext-wikilambda-app-wikidata-lexeme-sense__link"
					:href="lexemeSenseUrl"
					:lang="lexemeSenseLabelData.langCode"
					:dir="lexemeSenseLabelData.langDir"
					target="_blank"
				>{{ lexemeSenseLabelData.label }}</a>
				<!-- No link: value is not a valid Qid (unknown) -->
				<span
					v-else-if="lexemeSenseLabelData"
					class="ext-wikilambda-app-wikidata-lexeme-sense__unknown"
					:lang="lexemeSenseLabelData.langCode"
					:dir="lexemeSenseLabelData.langDir"
				>{{ lexemeSenseLabelData.label }}</span>
				<!-- No label: value is empty -->
				<span v-else class="ext-wikilambda-app-wikidata-lexeme-sense__empty">
					{{ i18n( 'wikilambda-wikidata-entity-empty-value-placeholder' ).text() }}
				</span>
			</div>
 
			<div v-else>
				<wl-wikidata-entity-selector
					v-if="isInitialized"
					class="ext-wikilambda-app-wikidata-lexeme-sense__lexeme-selector"
					:entity-id="lexemeId"
					:entity-label="lexemeLabel"
					:type="lexemeType"
					data-testid="wikidata-lexeme-select"
					@select-wikidata-entity="onSelectLexeme"
				></wl-wikidata-entity-selector>
				<cdx-select
					class="ext-wikilambda-app-wikidata-lexeme-sense__sense-selector"
					:disabled="!lexemeId || !lexemeSenseSelectMenuItems.length"
					:selected="lexemeSenseId"
					:default-label="i18n( 'wikilambda-wikidata-lexeme-sense-selector-placeholder' ).text()"
					:menu-items="lexemeSenseSelectMenuItems"
					:menu-config="lexemeSenseSelectConfig"
					data-testid="wikidata-lexeme-sense-select"
					@update:selected="onSelectLexemeSense"
				></cdx-select>
				<cdx-message
					v-if="shouldShowNoSensesMessage"
					inline
				>
					<!-- eslint-disable-next-line vue/no-v-html -->
					<div v-html="noSensesMessage"></div>
				</cdx-message>
				<cdx-message
					v-if="shouldShowFetchErrorMessage"
					type="error"
					inline
				>
					{{ i18n( 'wikilambda-wikidata-entity-fetch-error' ).text() }}
				</cdx-message>
			</div>
		</template>
		<wl-z-object-to-string
			v-else
			:key-path="keyPath"
			:object-value="objectValue"
			:edit="edit"
		></wl-z-object-to-string>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, onMounted, ref, watch } = require( 'vue' );
 
const wikidataIconSvg = require( './wikidataIconSvg.js' );
const Constants = require( '../../../Constants.js' );
const useZObject = require( '../../../composables/useZObject.js' );
const useMainStore = require( '../../../store/index.js' );
 
// Wikidata components
const WikidataEntitySelector = require( './EntitySelector.vue' );
// Type components
const ZObjectToString = require( '../ZObjectToString.vue' );
// Codex components
const { CdxIcon, CdxSelect, CdxMessage } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-wikidata-lexeme-sense',
	components: {
		'wl-wikidata-entity-selector': WikidataEntitySelector,
		'wl-z-object-to-string': ZObjectToString,
		'cdx-icon': CdxIcon,
		'cdx-message': CdxMessage,
		'cdx-select': CdxSelect
	},
	props: {
		keyPath: {
			type: String,
			required: true
		},
		objectValue: {
			type: Object,
			required: true
		},
		type: {
			type: String,
			required: true
		},
		edit: {
			type: Boolean,
			default: false
		}
	},
	emits: [ 'set-value' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const { getWikidataEntityId } = useZObject( { keyPath: props.keyPath } );
		const store = useMainStore();
 
		// Constants
		const wikidataIcon = wikidataIconSvg;
		const lexemeType = Constants.Z_WIKIDATA_LEXEME;
		const lexemeSenseType = Constants.Z_WIKIDATA_LEXEME_SENSE;
		const lexemeSenseSelectConfig = { visibleItemLimit: 5 };
 
		// State
		const lexemeId = ref( null );
		const isInitialized = ref( false );
		const isLexemeLoading = ref( false );
 
		// Lexeme Sense ID data
		/**
		 * Returns the Lexeme Sense Id string terminal value if set, or empty string if unset.
		 * If the Id is not determined by a terminal string, returns undefined.
		 *
		 * @return {string|undefined}
		 */
		const lexemeSenseId = computed( () => getWikidataEntityId( props.objectValue, lexemeSenseType ) );
 
		/**
		 * Returns whether the Wikidata Entity Id is a terminal string.
		 * If it doesn't, the component will fallback to z-object-to-string so
		 * that it informs of the object content but doesn't show blank values or
		 * fields, and doesn't allow direct edit (e.g. requires previous expnsion)
		 *
		 * @return {boolean}
		 */
		const hasTerminalId = computed( () => typeof lexemeSenseId.value === 'string' );
 
		/**
		 * Returns the initial Lexeme Id string value, if any Lexeme Sense is selected on load.
		 *
		 * @return {string|null}
		 */
		const initialLexemeId = computed( () => {
			if ( lexemeSenseId.value ) {
				const [ id ] = lexemeSenseId.value.split( '-' );
				return id;
			}
			return null;
		} );
 
		// Lexeme data
		/**
		 * Returns the LabelData object for the selected Lexeme.
		 *
		 * @return {LabelData|undefined}
		 */
		const lexemeLabelData = computed( () => store.getLexemeLabelData( lexemeId.value ) );
 
		/**
		 * Returns the string label of the selected Lexeme or an empty string if none is selected.
		 *
		 * @return {string}
		 */
		const lexemeLabel = computed( () => lexemeLabelData.value ? lexemeLabelData.value.label : '' );
 
		/**
		 * Returns the Wikidata URL for the selected lexeme.
		 *
		 * @return {string|undefined}
		 */
		const lexemeUrl = computed( () => store.getLexemeUrl( lexemeId.value ) );
 
		// Lexeme Sense data
		/**
		 * Returns the Wikidata URL for the selected Lexeme Sense.
		 *
		 * @return {string|undefined}
		 */
		const lexemeSenseUrl = computed( () => store.getLexemeSenseUrl( lexemeSenseId.value ) );
 
		/**
		 * Returns the LabelData object for the selected Lexeme Sense.
		 *
		 * @return {LabelData|undefined}
		 */
		const lexemeSenseLabelData = computed( () => store.getLexemeSenseLabelData( lexemeSenseId.value ) );
 
		// Sense selector display
		/**
		 * Returns the menu items for the senses of the selected lexeme.
		 * Each item has a label (from store.getLexemeSenseLabelData) and value (the sense id).
		 *
		 * @return {Array<{label: string, value: string}>}
		 */
		const lexemeSenseSelectMenuItems = computed( () => {
			if ( !lexemeId.value ) {
				return [];
			}
			const sensesData = store.getLexemeSensesData( lexemeId.value );
			if ( !sensesData || !Array.isArray( sensesData ) ) {
				return [];
			}
			return sensesData.map( ( sense ) => {
				const labelData = store.getLexemeSenseLabelData( sense.id );
				const ids = sense.id.split( '-' );
				return {
					label: labelData ? labelData.label : sense.id,
					value: sense.id,
					description: ids[ 1 ]
				};
			} );
		} );
 
		/**
		 * Returns true if the request for the selected lexeme failed.
		 *
		 * @return {boolean}
		 */
		const hasFetchFailed = computed( () => store.getWikidataEntityFetchFailed( lexemeId.value ) );
 
		/**
		 * Returns true if we should show the "no senses" message.
		 * Only shows when lexeme is selected, not loading, and has no senses.
		 * A failed request does not mean that the lexeme has no senses.
		 *
		 * @return {boolean}
		 */
		const shouldShowNoSensesMessage = computed( () => lexemeId.value &&
			!isLexemeLoading.value &&
			!hasFetchFailed.value &&
			!lexemeSenseSelectMenuItems.value.length
		);
 
		/**
		 * Returns true if we should show the failed request message.
		 *
		 * @return {boolean}
		 */
		const shouldShowFetchErrorMessage = computed( () => !!lexemeId.value &&
			!isLexemeLoading.value &&
			hasFetchFailed.value
		);
 
		/**
		 * Returns the localized "no senses" message with the Wikidata link.
		 *
		 * @return {string}
		 */
		const noSensesMessage = computed( () => i18n(
			'wikilambda-wikidata-lexeme-sense-no-senses-message',
			[ lexemeUrl.value ]
		)
			.parse()
			// TODO (T406155): how to open the link in a new tab using MediaWiki translations?
			.replace( '<a ', '<a target="_blank" rel="noopener" ' )
		);
 
		// Data fetching
		/**
		 * Fetch lexeme data and senses data and set loading state.
		 *
		 * @param {string} id - Lexeme ID to fetch
		 */
		function loadLexemeWithSenses( id ) {
			isLexemeLoading.value = true;
			store.fetchLexemes( { ids: [ id ] } );
			store.fetchLexemeSenses( { lexemeIds: [ id ] } )
				.finally( () => {
					isLexemeLoading.value = false;
				} );
		}
 
		// Actions
		/**
		 * Handles selection of a lexeme from the entity selector.
		 * Updates lexemeId, fetches the lexeme, and clears the sense selection.
		 *
		 * @param {string} value
		 */
		function onSelectLexeme( value ) {
			if ( value === lexemeId.value ) {
				return;
			}
			lexemeId.value = value;
			loadLexemeWithSenses( value );
 
			emit( 'set-value', {
				value: '',
				keyPath: ( props.type === Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE ) ? [
					Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE_ID,
					Constants.Z_STRING_VALUE
				] : [
					Constants.Z_WIKIDATA_FETCH_LEXEME_SENSE_ID,
					Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE_ID,
					Constants.Z_STRING_VALUE
				]
			} );
		}
 
		/**
		 * Handles selection of a lexeme sense from the senses dropdown.
		 * Emits a set-value event to persist the change.
		 *
		 * @param {string|null} value
		 */
		function onSelectLexemeSense( value ) {
			const keyPath = ( props.type === Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE ) ? [
				Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE_ID,
				Constants.Z_STRING_VALUE
			] : [
				Constants.Z_WIKIDATA_FETCH_LEXEME_SENSE_ID,
				Constants.Z_WIKIDATA_REFERENCE_LEXEME_SENSE_ID,
				Constants.Z_STRING_VALUE
			];
			emit( 'set-value', { value: value || '', keyPath } );
		}
 
		// Watch
		watch( lexemeSenseId, ( id ) => {
			if ( id ) {
				const [ newLexemeId ] = id.split( '-' );
				lexemeId.value = newLexemeId;
				loadLexemeWithSenses( newLexemeId );
			}
		} );
 
		watch( () => lexemeSenseSelectMenuItems.value.length, ( hasItems ) => {
			if ( hasItems && !isInitialized.value ) {
				isInitialized.value = true;
			}
		} );
 
		// Lifecycle
		onMounted( () => {
			if ( initialLexemeId.value ) {
				lexemeId.value = initialLexemeId.value;
				loadLexemeWithSenses( initialLexemeId.value );
			}
			// We need to set isInitialized to true after the lexemeId is set
			// because the entity selector will otherwise be rendered with an empty value.
			isInitialized.value = true;
		} );
 
		return {
			hasTerminalId,
			isInitialized,
			lexemeId,
			lexemeLabel,
			lexemeSenseId,
			lexemeSenseLabelData,
			lexemeSenseSelectConfig,
			lexemeSenseSelectMenuItems,
			i18n,
			lexemeSenseUrl,
			lexemeType,
			noSensesMessage,
			onSelectLexeme,
			onSelectLexemeSense,
			shouldShowFetchErrorMessage,
			shouldShowNoSensesMessage,
			wikidataIcon
		};
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-wikidata-lexeme-sense {
	--line-height-current: calc( var( --line-height-content ) * 1em );
 
	.ext-wikilambda-app-wikidata-lexeme-sense__read {
		display: flex;
		align-items: normal;
		min-height: @min-size-interactive-pointer;
		box-sizing: border-box;
		/* We calculate dynamically a different padding for each font size setting */
		padding-top: calc( calc( @min-size-interactive-pointer - var( --line-height-current ) ) / 2 );
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__link {
		line-height: var( --line-height-current );
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__unknown {
		color: @color-subtle;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__empty {
		color: @color-placeholder;
		font-style: italic;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__wd-icon {
		margin: 0 @spacing-25;
		height: var( --line-height-current );
		flex-shrink: 0;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__lexeme-selector {
		margin-bottom: @spacing-50;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-sense__sense-selector {
		.cdx-select-vue__handle {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
	}
}
</style>