All files / ext.wikilambda.edit/components/function ZLabelBlockAliases.vue

18.18% Statements 6/33
0% Branches 0/9
0% Functions 0/9
18.18% Lines 6/33

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                                                              17x 17x 17x 17x 17x     17x                                                                                                                                                                                                                                                                                                                                              
<template>
	<!--
		WikiLambda Vue component for the collection of label aliases of ZPersistent objects.
 
		@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
		@license MIT
	-->
	<div>
		<div v-for="( alias, index ) in languageAliases" :key="index">
			<cdx-button
				v-if="!viewmode"
				:destructive="true"
				@click="removeAlias( alias )"
			>
				{{ $i18n( 'wikilambda-editor-removeitem' ).text() }}
			</cdx-button>
			<wl-z-string
				:zobject-id="alias"
			></wl-z-string>
		</div>
		<div v-if="!viewmode">
			<cdx-button
				@click="addAliasForLanguage"
			>
				{{ $i18n( 'wikilambda-editor-additem' ).text() }}
			</cdx-button> {{ $i18n( 'wikilambda-metadata-add-alias' ).text() }}
		</div>
	</div>
</template>
 
<script>
var Constants = require( '../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	ZString = require( '../main-types/ZString.vue' );
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-label-block-aliases',
	components: {
		'cdx-button': CdxButton,
		'wl-z-string': ZString
	},
	inject: {
		viewmode: { default: false }
	},
	props: {
		zobjectId: {
			type: Number,
			required: true
		},
		zObjectAliasId: {
			type: Number,
			required: true
		},
		language: {
			type: Object,
			required: true
		},
		languageAliases: {
			type: Array,
			required: true
		}
	},
	computed: $.extend( mapGetters( [
		'getAllItemsFromListById',
		'getNestedZObjectById',
		'getNextObjectId'
	] ), {
		Constants: function () {
			return Constants;
		},
		/**
		 * Returns the table ID of the MonolingualStringSet Value
		 * containing the list of aliases for a given language
		 *
		 * @return {string}
		 */
		getLanguageAliasStringsetId: function () {
			const aliasForLang = this.getZObjectAliases.find( function ( alias ) {
				const aliasLang = this.getNestedZObjectById( alias.id, [
					Constants.Z_MONOLINGUALSTRINGSET_LANGUAGE,
					Constants.Z_REFERENCE_ID
				] );
				return ( aliasLang.value === this.getLanguageWithReference );
			}.bind( this ) );
 
			return aliasForLang && aliasForLang.id ?
				this.getNestedZObjectById( aliasForLang.id, [ Constants.Z_MONOLINGUALSTRINGSET_VALUE ] ).id :
				null;
		},
		/**
		 * Returns a list of MonolingualStringSet objects.
		 *
		 * @return {Array}
		 */
		getZObjectAliases: function () {
			return this.getAllItemsFromListById(
				this.getNestedZObjectById( this.zobjectId, [
					Constants.Z_PERSISTENTOBJECT_ALIASES,
					Constants.Z_MULTILINGUALSTRINGSET_VALUE
				] ).id );
		},
		getLanguageWithReference: function () {
			return this.language[ Constants.Z_REFERENCE_ID ];
		}
	} ),
	methods: $.extend( mapActions( [
		'addZObject',
		'addZString',
		'injectZObject',
		'removeZObjectChildren',
		'removeZObject',
		'recalculateZListIndex'
	] ), {
		/**
		 * Returns the list of elements contained in the
		 * MonolingualStringSet value typed list of strings
		 * for a given language.
		 *
		 * @return {Array}
		 */
		getLanguageAliases: function () {
			const aliases = this.getAllItemsFromListById( this.getLanguageAliasStringsetId );
			return aliases.map( function ( alias ) {
				return alias.id;
			} );
		},
		/**
		 * Remove one alias from the alias list of a given language
		 * by providing its ZObject internal table id
		 *
		 * @param {number} alias
		 */
		removeAlias: function ( alias ) {
			this.removeZObjectChildren( alias );
			this.removeZObject( alias );
 
			this.recalculateZListIndex( this.getLanguageAliasStringsetId );
 
			// If we removed the last alias, delete the Z31 empty alias block.
			Iif ( this.languageAliases.length === 1 ) {
				var monoLingualStringSetId = this.getAllItemsFromListById(
					this.getNestedZObjectById( this.zobjectId, [
						Constants.Z_PERSISTENTOBJECT_ALIASES,
						Constants.Z_MULTILINGUALSTRINGSET_VALUE
					] ).id )[ 0 ].id;
				this.removeZObject( monoLingualStringSetId );
			}
		},
		/**
		 * Add an alias in a language. The language already exists for
		 * labels, but there might or might not already exists aliases or
		 * even an alias block for this language.
		 *
		 */
		addAliasForLanguage: function () {
			const nextId = this.getNextObjectId;
 
			if ( this.languageAliases.length > 0 && this.getLanguageAliasStringsetId ) {
				// If the monolingualStringSet for the given language exists,
				// add one more string to the list.
				const nextIndexLanguageAliases = this.getLanguageAliases( this.getLanguageWithReference ).length + 1;
 
				const payload = {
					key: nextIndexLanguageAliases.toString(),
					value: 'object',
					parent: this.getLanguageAliasStringsetId
				};
 
				this.addZObject( payload );
				this.addZString( { id: nextId } );
 
			} 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 nextIndexAliases = this.getZObjectAliases.length + 1;
				const multilingualAliasesId = this.getNestedZObjectById( this.zObjectAliasId, [
					Constants.Z_MULTILINGUALSTRINGSET_VALUE
				] ).id;
 
				const payload = {
					key: nextIndexAliases.toString(),
					value: 'object',
					parent: multilingualAliasesId
				};
 
				this.addZObject( payload );
				this.injectZObject( {
					zobject: {
						Z1K1: Constants.Z_MONOLINGUALSTRINGSET,
						Z31K1: this.getLanguageWithReference,
						Z31K2: [ Constants.Z_STRING, '' ]
					},
					key: nextIndexAliases.toString(),
					id: nextId,
					parent: multilingualAliasesId
				} );
			}
		}
	} )
};
</script>