All files / ext.wikilambda.app/components/function/editor FunctionEditorAliases.vue

100% Statements 207/207
100% Branches 28/28
100% Functions 11/11
100% Lines 207/207

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 2081x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 112x 112x 112x 1x 1x 1x 1x 1x 1x 1x 89x 89x 37x 37x 89x 1x 1x 1x 1x 1x 1x 1x 89x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x 1x 1x 1x 22x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 5x 2x 5x 3x 3x 3x 3x 3x 3x 11x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 10x 10x 10x 1x 1x 1x  
<!--
	WikiLambda Vue component for setting the aliases of a ZFunction in the Function editor.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-function-editor-field class="ext-wikilambda-app-function-editor-aliases">
		<template #label>
			<label :for="aliasFieldId">
				{{ aliasLabel }}
				<span>{{ aliasOptional }}</span>
			</label>
		</template>
		<template #description>
			{{ aliasFieldDescription }}
		</template>
		<template #body>
			<cdx-chip-input
				:id="aliasFieldId"
				:aria-label="aliasInputLabel"
				:input-chips="aliases"
				:placeholder="aliasFieldPlaceholder"
				@update:input-chips="persistAliases"
			></cdx-chip-input>
		</template>
	</wl-function-editor-field>
</template>
 
<script>
const { CdxChipInput } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	FunctionEditorField = require( './FunctionEditorField.vue' ),
	{ mapGetters, mapActions } = require( 'vuex' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-aliases',
	components: {
		'wl-function-editor-field': FunctionEditorField,
		'cdx-chip-input': CdxChipInput
	},
	props: {
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			required: true
		}
	},
	computed: Object.assign( mapGetters( [
		'getRowByKeyPath',
		'getZPersistentAlias',
		'getZMonolingualStringsetValues'
	] ), {
		/**
		 * Returns the Alias (Z2K4) row for the selected language.
		 *
		 * @return {Object|undefined}
		 */
		aliasRow: function () {
			return this.zLanguage ?
				this.getZPersistentAlias( this.zLanguage ) :
				undefined;
		},
		/**
		 * Returns the array of string aliases for the selected language
		 *
		 * @return {Array}
		 */
		aliases: function () {
			return ( this.aliasRow ? this.getZMonolingualStringsetValues( this.aliasRow.id ) : [] )
				.map( ( value ) => ( {
					id: value.rowId,
					value: value.value
				} ) );
		},
		/**
		 * Returns whether there are any aliases for the selected language
		 *
		 * @return {boolean}
		 */
		hasAliases: function () {
			return this.aliases.length > 0;
		},
		/**
		 * Returns the label for the alias field
		 *
		 * @return {string}
		 */
		aliasLabel: function () {
			// TODO (T335583): Replace i18n message with key label
			// return this.getLabelData( Constants.Z_PERSISTENTOBJECT_ALIASES );
			return this.$i18n( 'wikilambda-function-definition-alias-label' ).text();
		},
		/**
		 * Returns the label for the alias input field
		 *
		 * @return {string}
		 */
		aliasInputLabel: function () {
			return this.$i18n( 'wikilambda-function-definition-alias-label-new' ).text();
		},
		/**
		 * Returns the "optional" caption for the alias field
		 *
		 * @return {string}
		 */
		aliasOptional: function () {
			return this.$i18n( 'parentheses', [ this.$i18n( 'wikilambda-optional' ).text() ] ).text();
		},
		/**
		 * Returns the placeholder for the chip input field
		 * if there are no aliases for the selected language.
		 * Else, returns empty string.
		 *
		 * @return {string}
		 */
		aliasFieldPlaceholder: function () {
			return !this.hasAliases ?
				this.$i18n( 'wikilambda-function-definition-alias-placeholder' ).text() :
				'';
		},
		/**
		 * Returns the description for the alias field
		 *
		 * @return {string}
		 */
		aliasFieldDescription: function () {
			return this.$i18n( 'wikilambda-function-definition-alias-description' ).text();
		},
		/**
		 * Returns the id for the alias field
		 *
		 * @return {string}
		 */
		aliasFieldId: function () {
			return `ext-wikilambda-app-function-editor-aliases__input${ this.zLanguage }`;
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'removeItemFromTypedList',
		'setValueByRowIdAndPath'
	] ), {
		/**
		 * Persists the aliases in the data store.
		 *
		 * @param {Array} aliases list of aliases to persist
		 * @return {void}
		 */
		persistAliases: function ( aliases ) {
			const rowId = this.aliasRow ? this.aliasRow.id : undefined;
			this.persistZMonolingualStringSet(
				rowId,
				aliases.map( ( alias ) => alias.value )
			);
		},
		/**
		 * Persists ZMonolingualStringset changes in the data store.
		 * These correspond to the Aliases/Z2K4 field.
		 * TODO: When About Widget 2.0 is finished, move this (and persistZMonolingualString)
		 * into a mixin or into the zobject store actions.
		 *
		 * @param {number|undefined} currentRowId current monolingual stringset row Id
		 * @param {Array} values list of aliases to persist
		 */
		persistZMonolingualStringSet: function ( currentRowId, values ) {
			if ( currentRowId ) {
				if ( values.length === 0 ) {
					this.removeItemFromTypedList( { rowId: currentRowId } );
				} else {
					this.setValueByRowIdAndPath( {
						rowId: currentRowId,
						keyPath: [ Constants.Z_MONOLINGUALSTRINGSET_VALUE ],
						value: [ Constants.Z_STRING, ...values ]
					} );
				}
			} else {
				// If currentRowId is undefined, there's no monolingual stringset
				// for the given language, so we create a new monolingual stringset
				// with the new value and append to the parent list.
				const parentRow = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_ALIASES,
					Constants.Z_MULTILINGUALSTRINGSET_VALUE
				] );
				if ( !parentRow ) {
					// This should never happen because all Z2Kn's are initialized
					return;
				}
				this.changeType( {
					id: parentRow.id,
					type: Constants.Z_MONOLINGUALSTRINGSET,
					lang: this.zLanguage,
					value: values,
					append: true
				} );
			}
 
			this.$emit( 'updated-alias' );
		}
	} )
} );
</script>