All files / ext.wikilambda.edit/components/default-view-types ZArgumentReference.vue

100% Statements 32/32
100% Branches 10/10
100% Functions 13/13
100% Lines 32/32

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    25x 25x 25x 25x 25x 25x 25x                                   12x                     4x               81x 81x                 83x                 49x           32x               32x                     32x 93x                         8x                             4x 3x   1x   4x             25x   25x     25x 25x 25x         83x 83x 83x               4x         25x                                  
<!--
	WikiLambda Vue component for Z18/Argument Reference objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		data-testid="argument-reference-key"
		class="ext-wikilambda-argument-reference"
	>
		<template v-if="!edit">
			<cdx-icon :icon="icon"></cdx-icon>
			{{ argumentLabel }}
		</template>
		<cdx-select
			v-else
			v-model:selected="argumentKey"
			:menu-items="argumentOptions"
			:default-label="argumentSelectorPlaceholder"
			@update:selected="setValue"
		></cdx-select>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	CdxSelect = require( '@wikimedia/codex' ).CdxSelect,
	icons = require( '../../../lib/icons.json' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-z-argument-reference',
	components: {
		'cdx-select': CdxSelect,
		'cdx-icon': CdxIcon
	},
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		}
	},
	data: function () {
		return {
			icon: icons.cdxIconFunctionArgument
		};
	},
	computed: Object.assign(
		mapGetters( [
			'getLabel',
			'getRowByKeyPath',
			'getInputsOfFunctionZid',
			'getZImplementationFunctionZid',
			'getZStringTerminalValue',
			'getZObjectKeyByRowId',
			'getZPersistentContentRowId'
		] ),
		{
			/**
			 * Returns the key of the key-value pair of this component.
			 *
			 * @return {string}
			 */
			key: function () {
				return this.getZObjectKeyByRowId( this.rowId );
			},
 
			/**
			 * Returns the row Id of the argument reference key/Z18K1.
			 *
			 * @return {number}
			 */
			argumentKeyRowId: function () {
				const row = this.getRowByKeyPath( [ Constants.Z_ARGUMENT_REFERENCE_KEY ], this.rowId );
				return row ? row.id : this.rowId;
			},
 
			/**
			 * Returns the key of the selected argument reference,
			 * if any. Else, returns empty string.
			 *
			 * @return {string}
			 */
			argumentKey: function () {
				return this.getZStringTerminalValue( this.argumentKeyRowId ) || '';
			},
 
			/**
			 * Returns the label of the selected argument reference,
			 * if any. Else returns the argument reference key.
			 *
			 * @return {string}
			 */
			argumentLabel: function () {
				return this.getLabel( this.argumentKey );
			},
 
			/**
			 * @return {string | undefined}
			 */
			implementationRowId: function () {
				return this.getZPersistentContentRowId();
			},
 
			/**
			 * Zid of the target function of the current implementation.
			 *
			 * @return {string | undefined}
			 */
			functionZid: function () {
				return this.getZImplementationFunctionZid( this.implementationRowId );
			},
 
			/**
			 * Returns the available argument references options
			 * formatted for the CdxSelect component.
			 * The options will be the union of all the argument references
			 * of the function being implemented in the current composition.
			 *
			 * @return {Array} Array of codex MenuItemData objects
			 */
			argumentOptions: function () {
				return this.getInputsOfFunctionZid( this.functionZid )
					.map( ( arg ) => {
						return {
							value: arg[ Constants.Z_ARGUMENT_KEY ],
							label: this.getLabel( arg[ Constants.Z_ARGUMENT_KEY ] ),
							icon: icons.cdxIconFunctionArgument
						};
					} );
			},
 
			/**
			 * Returns the placeholder for the argument reference selector
			 *
			 * @return {string}
			 */
			argumentSelectorPlaceholder: function () {
				return this.$i18n( 'wikilambda-argument-reference-selector-placeholder' ).text();
			}
		}
	),
	methods: {
		/**
		 * Emits the event setValue so that ZObjectKey can update
		 * the terminal value in the ZObject data table. This component can
		 * be shown both in the collapsed version (hence set the value of the
		 * Z18K1->Z6K1 sequence of keys) or in the expanded one (hence just
		 * setting the string value Z6K1 of the current Z18K1 key).
		 *
		 * @param {string} value
		 */
		setValue: function ( value ) {
			let keyPath;
			if ( this.key === Constants.Z_ARGUMENT_REFERENCE_KEY ) {
				keyPath = [ Constants.Z_STRING_VALUE ];
			} else {
				keyPath = [
					Constants.Z_ARGUMENT_REFERENCE_KEY,
					Constants.Z_STRING_VALUE
				];
			}
			this.$emit( 'set-value', { keyPath, value } );
		}
	}
} );