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

100% Statements 327/327
95.12% Branches 39/41
100% Functions 14/14
100% Lines 327/327

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 3281x 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 934x 934x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 934x 127x 127x 927x 554x 554x 924x 178x 178x 75x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 909x 909x 909x 1x 1x 1x 1x 1x 1x 1x 1x 213x 1x 1x 1x 1x 1x 1x 1x 1x 66x 1x 1x 1x 1x 1x 1x 1x 1x 1x 66x 1x 1x 1x 1x 1x 1x 1x 1x 1x 165x 165x 20x 20x 165x 79x 79x 165x 66x 66x 145x 165x 165x 165x 1x 1x 1x 1x 1x 1x 1x 1x 1x 861x 1x 1x 1x 1x 1x 1x 1x 1x 373x 373x 219x 373x 1x 1x 1x 1x 1x 1x 1x 1x 905x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 310x 108x 108x 304x 127x 127x 75x 75x 1x 1x 1x 1x 1x 1x 1x 1x 310x 310x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 221x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 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  
<!--
	WikiLambda Vue component to represent ZObjects as a string.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		v-if="hasLink"
		class="ext-wikilambda-zobject-to-string"
		role="ext-wikilambda-zobject-to-string-link">
		<div class="ext-wikilambda-zobject-to-string">
			<a
				v-if="isBlank"
				class="ext-wikilambda-zobject-to-string-blank"
				:lang="labelData.langCode"
				:dir="labelData.langDir"
				@click="expand"
			>{{ labelData.label }}</a>
			<a
				v-else
				:href="link"
				:lang="labelData.langCode"
				:dir="labelData.langDir"
			>{{ labelData.label }}</a>
		</div>
		<div
			v-if="hasChildren"
			class="ext-wikilambda-zobject-to-string">
			&nbsp;<span class="ext-wikilambda-zobject-to-string-divider">(</span>
			<!-- eslint-disable vue/no-v-for-template-key -->
			<template
				v-for="( row, index ) in childRows"
				:key="index"
			>
				<wl-z-object-to-string
					:row-id="row.id"
					@expand="expand"
				></wl-z-object-to-string>
				<span
					v-if="hasComma( index )"
					class="ext-wikilambda-zobject-to-string-divider"
				>,&nbsp;</span>
			</template><span class="ext-wikilambda-zobject-to-string-divider">)</span>
		</div>
	</div>
	<div
		v-else
		class="ext-wikilambda-zobject-to-string"
		role="ext-wikilambda-zobject-to-string-text"
		:lang="labelData.langCode"
		:dir="labelData.langDir"
	>
		"{{ labelData.label }}"
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	LabelData = require( '../../store/classes/LabelData.js' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-z-object-to-string',
	components: {
		'wl-z-object-to-string': this
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		}
	},
	computed: Object.assign(
		mapGetters( [
			'getLabelData',
			'getExpectedTypeOfKey',
			'getZFunctionCallFunctionId',
			'getZFunctionCallArguments',
			'getZReferenceTerminalValue',
			'getZStringTerminalValue',
			'getZObjectTypeByRowId',
			'getZObjectKeyByRowId',
			'getChildrenByParentRowId',
			'getUserLangCode',
			'getUserLangZid'
		] ),
		{
			/**
			 * Returns the type of the value of the the ZObject represented
			 * in this component. Depending on the type, we will decide what
			 * kind of string representation to create:
			 *
			 * Z6/String:
			 * * value: this.Z6K1
			 * * label: this.value
			 * * link: -
			 *
			 * Z9/Reference:
			 * * value: this.Z9K1
			 * * label: this.value | getLabelData.label
			 * * link: this.value | toLink
			 *
			 * Z7/Function call:
			 * * value: this.Z7K1
			 * * label: this.value | getLabelData.label
			 * * link: this.value | toLink
			 * * children: all child values except Z1K1 and Z7K1
			 *
			 * Any other object:
			 * * value: this.type
			 * * label: this.value | getLabelData.label
			 * * link: this.value | getLink
			 * * children: all child values except Z1K1
			 *
			 * @return {string}
			 */
			type: function () {
				const noArgs = true;
				return this.typeToString( this.getZObjectTypeByRowId( this.rowId ), noArgs );
			},
 
			/**
			 * Returns the value to represent in string format depending
			 * on the type:
			 * * In the case of function call, returns the Zid of the function.
			 * * In the case of terminal object, returns its terminal value.
			 * * In the case of any other object, returns its type.
			 *
			 * @return {string}
			 */
			value: function () {
				if ( this.type === Constants.Z_FUNCTION_CALL ) {
					return this.getZFunctionCallFunctionId( this.rowId );
				}
				if ( this.type === Constants.Z_STRING ) {
					return this.getZStringTerminalValue( this.rowId );
				}
				if ( this.type === Constants.Z_REFERENCE ) {
					return this.getZReferenceTerminalValue( this.rowId );
				}
				return this.type;
			},
 
			/**
			 * Returns the LabelData object for this object.
			 * If value is undefined, we generate the placeholder depending
			 * on its type.
			 *
			 * @return {LabelData}
			 */
			labelData: function () {
				return this.isBlank ?
					new LabelData( null, this.placeholder, this.getUserLangZid, this.getUserLangCode ) :
					this.getLabelData( this.value );
			},
 
			/**
			 * Returns the link to the object if it has any.
			 *
			 * @return {string}
			 */
			link: function () {
				return ( this.hasLink && !this.isBlank ) ? '/view/' + this.getUserLangCode + '/' + this.value : '';
			},
 
			/**
			 * Returns the key of the key-value pair of this component.
			 *
			 * @return {string}
			 */
			key: function () {
				return this.getZObjectKeyByRowId( this.rowId );
			},
 
			/**
			 * Returns the expected (or bound) type for the value of
			 * the key-value pair represented in this component.
			 *
			 * @return {string}
			 */
			expectedType: function () {
				return this.getExpectedTypeOfKey( this.key );
			},
 
			/**
			 * Returns a type-dependent placeholder to be printed in
			 * cases of undefined values.
			 *
			 * @return {string}
			 */
			placeholder: function () {
				let missingType = this.type;
				if ( missingType === Constants.Z_STRING ) {
					return this.$i18n( 'wikilambda-zobject-to-string-enter-string' );
				}
				if ( missingType === Constants.Z_FUNCTION_CALL ) {
					missingType = Constants.Z_FUNCTION;
				}
				if ( Constants.RESOLVER_TYPES.includes( missingType ) ) {
					missingType = this.expectedType;
				}
				const label = missingType ?
					this.getLabelData( missingType ).label :
					this.getLabelData( Constants.Z_OBJECT ).label;
				return this.$i18n( 'wikilambda-zobject-to-string-select-object', label );
			},
 
			/**
			 * Returns whether the object represented should link to somewhere
			 * or not. Only non-blank terminal strings don't have a link.
			 *
			 * @return {boolean}
			 */
			hasLink: function () {
				return this.isBlank || ( this.type !== Constants.Z_STRING );
			},
 
			/**
			 * Returns whether this object is terminal (Z6/String or Z9/Reference).
			 *
			 * @return {boolean}
			 */
			isTerminal: function () {
				return (
					( this.type === Constants.Z_REFERENCE ) ||
					( this.type === Constants.Z_STRING )
				);
			},
 
			/**
			 * Returns whether this object is blank (value is undefined)
			 *
			 * @return {boolean}
			 */
			isBlank: function () {
				return ( this.value === undefined );
			},
 
			/**
			 * Returns the array of children to represent in parenthesis.
			 * These are the arguments in case this object is a function call,
			 * or the object keys in case it's a non terminal zobject.
			 * It excludes the key Z1K1/Type.
			 *
			 * @return {Array}
			 */
			childRows: function () {
				if ( this.isTerminal ) {
					return [];
				}
				if ( this.type === Constants.Z_FUNCTION_CALL ) {
					return this.getZFunctionCallArguments( this.rowId );
				}
				return this.getChildrenByParentRowId( this.rowId )
					.filter( ( row ) => ( row.key !== Constants.Z_OBJECT_TYPE ) );
			},
 
			/**
			 * Returns whether this object has any children to represent
			 *
			 * @return {boolean}
			 */
			hasChildren: function () {
				return this.childRows.length > 0;
			}
		}
	),
	methods: {
		/**
		 * Whether a ZObject child needs a trailing comma given its index
		 *
		 * @param {number} index
		 * @return {boolean}
		 */
		hasComma: function ( index ) {
			return ( ( index + 1 ) < this.childRows.length );
		},
 
		/**
		 * Emits event 'expand' when an unselected value is clicked.
		 * This will propagate the event till the nearest ZObjectKeyValue
		 * parent, who will set the expansion flag to true.
		 */
		expand: function () {
			this.$emit( 'expand', true );
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-zobject-to-string {
	display: flex;
	white-space: pre-wrap;
	word-break: break-all;
	flex-flow: row wrap;
	justify-content: flex-start;
	gap: 0;
 
	.ext-wikilambda-zobject-to-string-divider {
		color: @color-subtle;
	}
 
	a.ext-wikilambda-zobject-to-string-blank {
		color: @color-destructive;
	}
 
	a.ext-wikilambda-zobject-to-string-blank:hover {
		color: @color-destructive--hover;
	}
 
	a.ext-wikilambda-zobject-to-string-blank:active {
		color: @color-destructive--active;
	}
 
	a.ext-wikilambda-zobject-to-string-blank:focus {
		color: @color-destructive--focus;
	}
}
</style>