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

98.31% Statements 233/237
84.61% Branches 11/13
100% Functions 2/2
98.31% Lines 233/237

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 2381x 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 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 2x 1x 1x 2x 1x 1x 1x 2x 2x 2x 2x 10x 10x 10x         10x 10x 10x 10x 10x 8x 8x 8x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 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 Z6004/Wikidata Lexeme Form objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-app-wikidata-lexeme-form"
		data-testid="wikidata-lexeme-form">
		<template v-if="hasTerminalId">
			<div v-if="!edit" class="ext-wikilambda-app-wikidata-lexeme-form__read">
				<cdx-icon
					class="ext-wikilambda-app-wikidata-lexeme-form__wd-icon"
					:icon="wikidataIcon"
				></cdx-icon>
				<a
					v-if="lexemeFormLabelData && lexemeFormUrl"
					class="ext-wikilambda-app-wikidata-lexeme-form__link"
					:href="lexemeFormUrl"
					:lang="lexemeFormLabelData.langCode"
					:dir="lexemeFormLabelData.langDir"
					target="_blank"
				>{{ lexemeFormLabelData.label }}</a>
				<!-- No link: value is not a valid Qid (unknown) -->
				<span
					v-else-if="lexemeFormLabelData"
					class="ext-wikilambda-app-wikidata-lexeme-form__unknown"
					:lang="lexemeFormLabelData.langCode"
					:dir="lexemeFormLabelData.langDir"
				>{{ lexemeFormLabelData.label }}</span>
				<!-- No label: value is empty -->
				<span v-else class="ext-wikilambda-app-wikidata-lexeme-form__empty">
					{{ i18n( 'wikilambda-wikidata-entity-empty-value-placeholder' ).text() }}
				</span>
			</div>
			<wl-wikidata-entity-selector
				v-else
				:entity-id="lexemeFormId"
				:entity-label="lexemeFormLabel"
				:type="lexemeFormType"
				@select-wikidata-entity="onSelect"
			></wl-wikidata-entity-selector>
		</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 { defineComponent, computed, inject, watch, onMounted } = 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 } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-wikidata-lexeme-form',
	components: {
		'wl-wikidata-entity-selector': WikidataEntitySelector,
		'wl-z-object-to-string': ZObjectToString,
		'cdx-icon': CdxIcon
	},
	props: {
		keyPath: {
			type: String,
			required: true
		},
		objectValue: {
			type: Object,
			required: true
		},
		edit: {
			type: Boolean,
			required: true
		},
		type: {
			type: String,
			required: true
		}
	},
	emits: [ 'set-value' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const { getWikidataEntityId } = useZObject( { keyPath: props.keyPath } );
		const store = useMainStore();
 
		// Constants
		const wikidataIcon = wikidataIconSvg;
		const lexemeFormType = Constants.Z_WIKIDATA_LEXEME_FORM;
 
		// Entity ID data
		/**
		 * Returns the Wikidata Lexeme Form 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 lexemeFormId = computed( () => getWikidataEntityId( props.objectValue, lexemeFormType ) );
 
		/**
		 * 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 lexemeFormId.value === 'string' );
 
		// Entity display data
		/**
		 * Returns the Wikidata URL for the selected Lexeme Form.
		 *
		 * @return {string|undefined}
		 */
		const lexemeFormUrl = computed( () => store.getLexemeFormUrl( lexemeFormId.value ) );
 
		/**
		 * Returns the LabelData object for the selected Lexeme Form
		 *
		 * @return {LabelData|undefined}
		 */
		const lexemeFormLabelData = computed( () => store.getLexemeFormLabelData( lexemeFormId.value ) );
 
		/**
		 * Returns the string label of the selected Lexeme Form or
		 * an empty string if none is selected. This is needed
		 * for the CdxLookup component initial value.
		 *
		 * @return {string}
		 */
		const lexemeFormLabel = computed( () => lexemeFormLabelData.value ? lexemeFormLabelData.value.label : '' );
 
		// Actions
		/**
		 * Emit a set-value event to persist in the store
		 * the changes made by a new wikidata entity selection,
		 * depending on whether the existing representation is
		 * a Wikidata reference or a Function call to a Wikidata
		 * fetch function.
		 *
		 * @param {string|null} value
		 */
		function onSelect( value ) {
			const keyPath = ( props.type === Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM ) ? [
				Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM_ID,
				Constants.Z_STRING_VALUE
			] : [
				Constants.Z_WIKIDATA_FETCH_LEXEME_FORM_ID,
				Constants.Z_WIKIDATA_REFERENCE_LEXEME_FORM_ID,
				Constants.Z_STRING_VALUE
			];
 
			emit( 'set-value', { value: value || '', keyPath } );
		}
 
		// Watchers
		watch( lexemeFormId, ( id ) => {
			if ( id ) {
				const [ lexemeId ] = id.split( '-' );
				store.fetchLexemes( { ids: [ lexemeId ] } );
			}
		} );
 
		// Lifecycle hooks
		onMounted( () => {
			if ( lexemeFormId.value ) {
				const [ lexemeId ] = lexemeFormId.value.split( '-' );
				store.fetchLexemes( { ids: [ lexemeId ] } );
			}
		} );
 
		return {
			hasTerminalId,
			lexemeFormId,
			lexemeFormLabel,
			lexemeFormLabelData,
			lexemeFormType,
			lexemeFormUrl,
			onSelect,
			wikidataIcon,
			i18n
		};
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-wikidata-lexeme-form {
	--line-height-current: calc( var( --line-height-content ) * 1em );
 
	.ext-wikilambda-app-wikidata-lexeme-form__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-form__link {
		line-height: var( --line-height-current );
		word-break: normal;
		overflow-wrap: anywhere;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-form__unknown {
		color: @color-subtle;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-form__empty {
		color: @color-placeholder;
		font-style: italic;
	}
 
	.ext-wikilambda-app-wikidata-lexeme-form__wd-icon {
		margin: 0 @spacing-25;
		height: var( --line-height-current );
		flex-shrink: 0;
	}
}
</style>