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

97.88% Statements 231/236
81.81% Branches 18/22
100% Functions 4/4
97.88% Lines 231/236

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 2371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 34x 34x 34x 34x 34x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 6x 1x 1x 5x 5x 5x 5x 6x           6x 5x 5x 5x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for selecting a Z6095/Wikidata reference item.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-app-wikidata-enum"
		data-testid="wikidata-enum"
	>
		<wl-z-object-key-value
			v-if="!edit"
			:key-path="`${ keyPath }.${ selectedEntityKey }`"
			:object-value="objectValue[ selectedEntityKey ]"
			:edit="edit"
			:skip-key="true"
		></wl-z-object-key-value>
		<div v-else class="ext-wikilambda-app-wikidata-enum__select">
			<cdx-select
				:selected="selectedEntityId"
				:default-label="enumSelectPlaceholder"
				:menu-items="enumSelectMenuItems"
				:menu-config="enumSelectConfig"
				data-testid="wikidata-enum-select"
				@update:selected="onSelect"
			></cdx-select>
			<cdx-progress-indicator
				v-if="isLoading"
				class="ext-wikilambda-app-wikidata-enum__loader"
			>
				{{ i18n( 'wikilambda-loading' ).text() }}
			</cdx-progress-indicator>
		</div>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, onMounted, ref, watch } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const useZObject = require( '../../../composables/useZObject.js' );
const useMainStore = require( '../../../store/index.js' );
 
// Codex components
const { CdxSelect, CdxProgressIndicator } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-wikidata-enum',
	components: {
		'cdx-select': CdxSelect,
		'cdx-progress-indicator': CdxProgressIndicator
	},
	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 enumSelectConfig = { visibleItemLimit: 5 };
 
		// State
		const loadingCount = ref( 0 );
 
		// Enum data
		/**
		 * Returns the type of Wikidata entities of this enum type. It does that by
		 * getting the type object from the library store and returning its Z6884K1.
		 * Example: returns 'Z6095' for a Wikidata reference item enum.
		 *
		 * @return {string|undefined}
		 */
		const entityType = computed( () => store.getTypeOfWikidataEnum( props.type ) );
 
		/**
		 * Returns the array of Wikidata entity Ids of this in enum type.
		 * It does that by getting the type object from the library store and
		 * returning the terminal values of its Z6884K2 key.
		 * E.g. [ 'L123', 'L345', 'L567' ]
		 *
		 * @return {Array<string>}
		 */
		const wikidataIds = computed( () => store.getReferencesIdsOfWikidataEnum( props.type ) );
 
		/**
		 * Returns the key for the selected entity
		 * (key K1 of the wikidata enum type)
		 *
		 * @return {string}
		 */
		const selectedEntityKey = computed( () => `${ props.type }K1` );
 
		/**
		 * Returns the terminal Wikidata Id of the selected entity, or undefined
		 * if nothing is selected yet.
		 * E.g. 'L313289'
		 *
		 * @return {string|null}
		 */
		const selectedEntityId = computed( () => {
			const selectedEntity = props.objectValue[ selectedEntityKey.value ];
			const simplifiedType = Constants.WIKIDATA_SIMPLIFIED_TYPES[ entityType.value ];
			return selectedEntity ?
				getWikidataEntityId( selectedEntity, simplifiedType ) || null :
				null;
		} );
 
		// Selector display
		/**
		 * Returns the placeholder text for the Wikidata enum selector.
		 * Falls back to a generic message if no type-specific placeholder is found.
		 *
		 * @return {string}
		 */
		const enumSelectPlaceholder = computed( () => {
			const type = Constants.WIKIDATA_SIMPLIFIED_TYPES[ entityType.value ];
			const msg = Constants.WIKIDATA_SELECTOR_PLACEHOLDER_MSG[ type ];
 
			return i18n( msg || 'wikilambda-wikidata-entity-selector-placeholder' ).text();
		} );
 
		/**
		 * Builds the menu items representing the available enum values.
		 * Each item contains a label (from Wikidata or the ID) and the Wikidata ID as value.
		 *
		 * @return {Array<{label: string, value: string}>}
		 */
		const enumSelectMenuItems = computed( () => wikidataIds.value.map( ( id ) => {
			const labelData = store.getWikidataEntityLabelData( entityType.value, id );
			return {
				label: labelData ? labelData.label : id,
				value: id
			};
		} ) );
 
		/**
		 * Returns true if currently loading Wikidata entities.
		 *
		 * @return {boolean}
		 */
		const isLoading = computed( () => loadingCount.value > 0 );
 
		// Actions
		/**
		 * Emit a set-value event to persist in the store
		 * the changes made by a new Wikidata enum selection.
		 *
		 * @param {string} value
		 */
		function onSelect( value ) {
			const keyPath = [
				`${ props.type }K1`,
				`${ entityType.value }K1`,
				Constants.Z_STRING_VALUE
			];
			emit( 'set-value', { value, keyPath } );
		}
 
		// Data fetching
		/**
		 * Fetches all the Wikidata entities in the selector component (only for edit)
		 */
		function fetchEntities() {
			if ( !props.edit ) {
				return;
			}
			const promise = store.fetchWikidataEntitiesByType( {
				type: entityType.value,
				ids: wikidataIds.value
			} );
			if ( promise && typeof promise.then === 'function' ) {
				loadingCount.value++;
				promise.finally( () => {
					loadingCount.value = Math.max( 0, loadingCount.value - 1 );
				} );
			}
		}
 
		// Watchers
		watch( wikidataIds, () => {
			fetchEntities();
		} );
 
		// Lifecycle hooks
		onMounted( () => {
			fetchEntities();
		} );
 
		return {
			enumSelectConfig,
			enumSelectMenuItems,
			enumSelectPlaceholder,
			isLoading,
			onSelect,
			selectedEntityId,
			selectedEntityKey,
			i18n
		};
	},
	beforeCreate: function () {
		this.$options.components[ 'wl-z-object-key-value' ] = require( './../ZObjectKeyValue.vue' );
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-wikidata-enum {
	.ext-wikilambda-app-wikidata-enum__select {
		position: relative;
		display: flex;
		gap: @spacing-100;
		align-items: center;
	}
}
</style>