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

96.87% Statements 31/32
73.68% Branches 14/19
100% Functions 11/11
96.87% Lines 31/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    17x 17x 17x 17x 17x                             60x                   60x   58x   2x                     64x               64x                   60x 2x 6x     58x               60x                     3x       60x 60x           17x   17x     17x 17x 17x     17x 17x       17x       64x 64x       3x           17x                                                  
<!--
	WikiLambda Vue component widget for transforming a string or object
	representing a type into a string with links. This is currently used
	in the FunctionEvaluator. The functionality is similar to that from
	the component ZObjectToString. However, ZObjectToString represents an
	object that is present in the store transformed into a flat table
	representation. TypeToString repsents a JS object passed as property.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-type-to-string">
		<a
			v-if="zid"
			:href="wikiUrl"
		>{{ label }}</a>
		<div
			v-if="hasArgs"
			class="ext-wikilambda-type-to-string">
			&nbsp;(
			<!-- eslint-disable vue/no-v-for-template-key -->
			<template
				v-for="( argKey, index ) in args"
				:key="argKey"
			>
				<wl-type-to-string :type="type[ argKey ]"></wl-type-to-string>
				<span v-if="hasComma( index )">,&nbsp;</span>
			</template>
			)
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-type-to-string',
	props: {
		type: {
			type: [ String, Object ],
			required: true
		}
	},
	computed: Object.assign( mapGetters( [
		'getUserLangCode',
		'getLabel'
	] ), {
		/**
		 * Whether the input type is a reference or a function call
		 *
		 * @return {boolean}
		 */
		mode: function () {
			return ( typeof this.type === 'string' ) ?
				Constants.Z_REFERENCE :
				this.type[ Constants.Z_OBJECT_TYPE ];
		},
		/**
		 * The first Zid to convert into link, either the reference
		 * terminal zid, or the ID of the function call if it's a
		 * generic type
		 *
		 * @return {string|undefined}
		 */
		zid: function () {
			switch ( this.mode ) {
				case Constants.Z_REFERENCE:
					return this.type;
				case Constants.Z_FUNCTION_CALL:
					return this.type[ Constants.Z_FUNCTION_CALL_FUNCTION ];
				default:
					return undefined;
			}
		},
		/**
		 * Returns the wiki URL for the main zid
		 *
		 * @return {string}
		 */
		wikiUrl: function () {
			return '/view/' + this.getUserLangCode + '/' + this.zid;
		},
		/**
		 * Returns the label of the main Zid
		 *
		 * @return {string}
		 */
		laEbel: function () {
			return this.zid ?
				this.getLabel( this.zid ) :
				this.$i18n( 'wikilambda-editor-default-name' ).text();
		},
		/**
		 * If the type is a generic type (and represented by a function
		 * call), returns the arguments, which are all the keys excluding
		 * Z1K1 and Z7K1
		 *
		 * @return {Array}
		 */
		args: function () {
			if ( this.mode === Constants.Z_FUNCTION_CALL ) {
				return Object.keys( this.type ).filter( ( key ) => {
					return (
						( key !== Constants.Z_OBJECT_TYPE ) &&
						( key !== Constants.Z_FUNCTION_CALL_FUNCTION )
					);
				} );
			}
			return [];
		},
		/**
		 * Whether there are any args to list after the main zid
		 *
		 * @return {boolean}
		 */
		hasArgs: function () {
			return this.args.length > 0;
		}
	} ),
	methods: Object.assign( mapActions( [
		'fetchZids'
	] ), {
		/**
		 * Whether a ZObject child needs a trailing comma given its index
		 *
		 * @param {number} index
		 * @return {boolean}
		 */
		hasComma: function ( index ) {
			return ( ( index + 1 ) < this.args.length );
		}
	} ),
	mounted: function () {
		if ( this.zid ) {
			this.fetchZids( { zids: [ this.zid ] } );
		}
	}
} );
</script>
 
<style lang="less">
.ext-wikilambda-type-to-string {
	display: flex;
	flex-flow: row wrap;
	justify-content: flex-start;
	gap: 0;
}
 
</style>