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

100% Statements 242/242
100% Branches 27/27
100% Functions 14/14
100% Lines 242/242

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 2431x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 105x 105x 105x 1x 1x 1x 1x 1x 1x 1x 89x 75x 51x 51x 89x 1x 1x 1x 1x 1x 1x 1x 89x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 38x 38x 38x 1x 1x 1x 1x 1x 1x 1x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 1x 1x 1x 7x 7x 7x 8x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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>
	<div class="ext-wikilambda-function-definition-aliases">
		<div class="ext-wikilambda-function-block__label">
			<label :for="aliasFieldId">
				{{ aliasLabel }}
				<span>{{ aliasOptional }}</span>
			</label>
			<span class="ext-wikilambda-function-block__label__description">
				{{ aliasFieldDescription }}
			</span>
		</div>
		<div class="ext-wikilambda-function-block__body">
			<wl-chip-container
				:id="aliasFieldId"
				:chips="aliases"
				:input-placeholder="aliasFieldPlaceholder"
				:input-aria-label="aliasInputLabel"
				@add-chip="addAlias"
				@remove-chip="removeAlias"
			></wl-chip-container>
			<p
				v-if="repeatedAlias"
				class="ext-wikilambda-function-definition-aliases__error"
			>
				{{ aliasErrorMessage }}
			</p>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	WlChipContainer = require( '../../base/ChipContainer.vue' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-aliases',
	components: {
		// TODO: replace with codex filter chip when available
		'wl-chip-container': WlChipContainer
	},
	props: {
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			required: true
		}
	},
	data: function () {
		return {
			repeatedAlias: null
		};
	},
	computed: Object.assign( mapGetters( [
		'getRowByKeyPath',
		'getZPersistentAlias',
		'getZMonolingualStringsetValues'
	] ), {
		/**
		 * Returns the Alias (Z2K4) row for the selected language.
		 *
		 * @return {Object|undefined}
		 */
		aliasObject: 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.aliasObject ? [] : this.getZMonolingualStringsetValues( this.aliasObject.rowId )
				.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 error message for when an alias is repeated
		 *
		 * @return {string}
		 */
		aliasErrorMessage: function () {
			return this.$i18n( 'wikilambda-metadata-duplicate-alias-error', this.repeatedAlias ).text();
		},
		/**
		 * Returns the id for the alias field
		 *
		 * @return {string}
		 */
		aliasFieldId: function () {
			return `ext-wikilambda-function-definition-aliases__input${ this.zLanguage }`;
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'removeItemFromTypedList'
	] ), {
		/**
		 * Add a new string to the alias stringset
		 *
		 * @param {string} alias
		 */
		addAlias: function ( alias ) {
			// show an error message if a user enters a duplicate alias
			// TODO (T317990): should duplication be case sensitive?
			if ( this.aliases.some( ( a ) => a.value === alias ) ) {
				this.repeatedAlias = alias;
				return;
			}
			// clear the error message
			this.clearAliasError();
 
			if ( this.aliasObject ) {
				// If monolingual string set for the given language exists,
				// find the monolingual string set value and add new string to the list.
				const stringSet = this.getRowByKeyPath( [
					Constants.Z_MONOLINGUALSTRINGSET_VALUE
				], this.aliasObject.rowId );
				this.changeType( {
					type: Constants.Z_STRING,
					id: stringSet.id,
					value: alias,
					append: true
				} );
			} else {
				// If the monolingualStringSet for the given language does not exist,
				// create a monolingualStringSet object with an empty array of strings
				// and add it to the list.
				const parentRow = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_ALIASES,
					Constants.Z_MULTILINGUALSTRINGSET_VALUE
				] );
				this.changeType( {
					id: parentRow.id,
					type: Constants.Z_MONOLINGUALSTRINGSET,
					lang: this.zLanguage,
					value: [ alias ],
					append: true
				} );
			}
			this.$emit( 'updated-alias' );
		},
		/**
		 * Remove an alias from the monolingual stringset
		 * value list given the alias row Id.
		 *
		 * @param {string} rowId
		 */
		removeAlias: function ( rowId ) {
			this.clearAliasError();
			this.removeItemFromTypedList( { rowId } );
			this.$emit( 'updated-alias' );
		},
		/**
		 * Clear the local error for repeated alias
		 */
		clearAliasError: function () {
			this.repeatedAlias = null;
		}
	} )
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-definition-aliases {
	&__error {
		color: @color-error;
	}
}
</style>