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

100% Statements 176/176
100% Branches 16/16
100% Functions 10/10
100% Lines 176/176

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 1771x 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 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 1x 1x 1x 1x 1x 1x 1x 1x 84x 84x 1x 1x 1x 1x 1x 1x 1x 1x 1x 86x 1x 1x 1x 1x 1x 1x 1x 1x 1x 52x 1x 1x 1x 1x 1x 1x 32x 1x 1x 1x 1x 1x 1x 1x 1x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 91x 91x 91x 31x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for Z18/Argument Reference objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-app-argument-reference"
		data-testid="z-argument-reference"
	>
		<template v-if="!edit">
			<cdx-icon :icon="icon"></cdx-icon>
			<span
				:lang="argumentLabelData.langCode"
				:dir="argumentLabelData.langDir"
			>{{ argumentLabelData.label }}</span>
		</template>
		<cdx-select
			v-else
			:selected="argumentKey"
			:menu-items="argumentOptions"
			:default-label="argumentSelectorPlaceholder"
			@update:selected="setValue"
		></cdx-select>
	</div>
</template>
 
<script>
const { CdxIcon, CdxSelect } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	icons = require( '../../../lib/icons.json' ),
	{ mapGetters } = require( 'vuex' );
 
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( [
			'getLabelData',
			'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 {LabelData}
			 */
			argumentLabelData: function () {
				return this.getLabelData( 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 ) => ( {
						value: arg[ Constants.Z_ARGUMENT_KEY ],
						label: this.getLabelData( arg[ Constants.Z_ARGUMENT_KEY ] ).label,
						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 } );
		}
	}
} );
 
</script>