All files / ext.wikilambda.edit/components/base TypeSelector.vue

100% Statements 35/35
92.85% Branches 13/14
100% Functions 10/10
100% Lines 35/35

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    21x 21x 21x 21x 21x 21x 21x 21x                                                                         202x                 202x                   202x               202x               26x                               20x 20x     2x   18x       18x 18x             18x 4x               21x   21x     21x 21x 21x     21x         202x 202x 202x 202x   155x     202x                         30x                   21x                                                          
<!--
	WikiLambda Vue interface module for selecting a generic type, which can be terminal
	(reference) or non terminal (function call which requires arguments)
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-function-type-selector">
		<!-- Main type selector -->
		<cdx-field>
			<template #label>
				{{ label }}
			</template>
			<wl-z-object-selector
				:disabled="disabled"
				:placeholder="placeholder"
				:row-id="rowId"
				:selected-zid="selectedZid"
				:type="type"
				:return-type="type"
				:strict-type="true"
				@input="setValue"
			></wl-z-object-selector>
		</cdx-field>
		<!-- Argument selectors -->
		<div
			v-if="hasSelected && !selectedIsTerminal"
			class="ext-wikilambda-function-type-selector__args"
		>
			<wl-type-selector
				v-for="arg of genericTypeArgs"
				:key="'type-' + rowId + '-arg-' + arg.id"
				data-testid="type-selector-arg"
				:row-id="arg.id"
				:label="getLabel( arg.key )"
				:type="getExpectedTypeOfKey( arg.key )"
				:disabled="disabled"
				:placeholder="placeholder"
			></wl-type-selector>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	ZObjectSelector = require( './ZObjectSelector.vue' ),
	CdxField = require( '@wikimedia/codex' ).CdxField,
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-type-selector',
	components: {
		'cdx-field': CdxField,
		'wl-z-object-selector': ZObjectSelector
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: true
		},
		disabled: {
			type: Boolean,
			default: false
		},
		type: {
			type: String,
			default: ''
		},
		placeholder: {
			type: String,
			default: ''
		},
		label: {
			type: String,
			default: ''
		}
	},
	computed: Object.assign( mapGetters( [
		'getExpectedTypeOfKey',
		'getLabel',
		'getStoredObject',
		'getZObjectTypeByRowId',
		'getZFunctionCallFunctionId',
		'getZFunctionCallArguments',
		'getZReferenceTerminalValue'
	] ), {
		/**
		 * Returns the string type (mode) of the selected value,
		 * which can be a Reference/Z9 or a Function call/Z7.
		 *
		 * @return {string}
		 */
		selectedMode: function () {
			return this.typeToString( this.getZObjectTypeByRowId( this.rowId ) );
		},
		/**
		 * Returns whether the selected value is terminal
		 * (Reference/Z9) or non-terminal (Function call/Z7).
		 *
		 * @return {boolean}
		 */
		selectedIsTerminal: function () {
			return this.selectedMode === Constants.Z_REFERENCE;
		},
		/**
		 * Returns the selected value.
		 *
		 * @return {string}
		 */
		selectedZid: function () {
			// If terminal, return the Zid of the selected reference;
			// else, return the Zid of the selected function call.
			return this.selectedIsTerminal ?
				this.getZReferenceTerminalValue( this.rowId ) :
				this.getZFunctionCallFunctionId( this.rowId );
		},
		/**
		 * Returns whether the field has any selected value.
		 *
		 * @return {boolean}
		 */
		hasSelected: function () {
			return !!this.selectedZid;
		},
		/**
		 * Returns the arguments of generic type function call
		 *
		 * @return {Array}
		 */
		genericTypeArgs: function () {
			return this.selectedIsTerminal ? [] : this.getZFunctionCallArguments( this.rowId );
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'setZFunctionCallArguments'
	] ), {
		/**
		 * Persists the selected value in the global store.
		 * If the selected value is of the required type, we persist as a reference
		 * Else, the selected value is a function that returns the required type,
		 * so we persist as a function call and fill up with the necessary arguments.
		 *
		 * @param {string} value
		 */
		setValue: function ( value ) {
			// TODO: what happens when we clear the field but we don't
			// select a new value? What happens when we write something in the
			// field but we don't select a value?
			const zobject = this.getStoredObject( value );
			if ( !zobject ) {
				// This should not happen, the objects are requested as soon as the
				// lookup menu is displayed, so they should be available by now.
				return;
			}
			const type = this.getZObjectType( zobject[ Constants.Z_PERSISTENTOBJECT_VALUE ] );
 
			// If the selected object type is equal to this.type, the selection is
			// terminal: we set a reference. If not terminal, we set a function call.
			const mode = ( type === this.type ) ? Constants.Z_REFERENCE : Constants.Z_FUNCTION_CALL;
			this.changeType( {
				id: this.rowId,
				type: mode,
				value
			} );
 
			// If the selected object is a function call, we set up its arguments:
			if ( mode === Constants.Z_FUNCTION_CALL ) {
				this.setZFunctionCallArguments( {
					parentId: this.rowId,
					functionZid: value
				} );
			}
		}
	} )
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-type-selector {
	margin-bottom: @spacing-100;
 
	&:last-child {
		margin-bottom: 0;
	}
 
	&__args {
		margin-top: @spacing-100;
		margin-left: @spacing-100;
	}
}
</style>