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

100% Statements 58/58
93.47% Branches 43/46
100% Functions 18/18
100% Lines 58/58

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 32529x 29x 29x     29x                                                                                       958x 958x                       958x 127x   831x 554x 554x   277x 202x   75x                   958x 203x   755x               258x               620x                 66x                 203x 203x 57x   146x 80x   146x 66x   146x 146x                 958x               461x               958x                     461x 259x   202x 127x   75x 150x                 461x                     221x               4x       29x   29x     29x 29x 29x         29x     29x 29x       29x     29x       29x     29x           958x 958x       4x           221x             29x                                                                                                                                  
<!--
	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"
				@click="expand"
			>{{ name }}</a>
			<a
				v-else
				:href="link"
			>{{ name }}</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"
	>
		{{ name }}
	</div>
</template>
 
<script>
const Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-object-to-string',
	components: {
		'wl-z-object-to-string': this
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		}
	},
	computed: $.extend(
		mapGetters( [
			'getLabel',
			'getExpectedTypeOfKey',
			'getZFunctionCallFunctionId',
			'getZFunctionCallArguments',
			'getZReferenceTerminalValue',
			'getZStringTerminalValue',
			'getZObjectTypeByRowId',
			'getZObjectKeyByRowId',
			'getChildrenByParentRowId'
		] ),
		{
			/**
			 * 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
			 * * name: this.value
			 * * link: -
			 *
			 * Z9/Reference:
			 * * value: this.Z9K1
			 * * name: this.value | getLabel
			 * * link: this.value | toLink
			 *
			 * Z7/Function call:
			 * * value: this.Z7K1
			 * * name: this.value | getLabel
			 * * link: this.value | toLink
			 * * children: all child values except Z1K1 and Z7K1
			 *
			 * Any other object:
			 * * value: this.type
			 * * name: this.value | getLabel
			 * * 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 ) {
					const value = this.getZStringTerminalValue( this.rowId );
					return ( this.key === Constants.Z_ARGUMENT_REFERENCE_KEY ) ?
						this.getLabel( value ) :
						value;
				}
				if ( this.type === Constants.Z_REFERENCE ) {
					return this.getZReferenceTerminalValue( this.rowId );
				}
				return this.type;
			},
 
			/**
			 * Returns the human readable string that identifies this object.
			 * If value is undefined, we generate the placeholder depending
			 * on its type.
			 *
			 * @return {string}
			 */
			name: function () {
				if ( this.isBlank ) {
					return this.placeholder;
				}
				return ( this.type === Constants.Z_STRING ) ?
					( '"' + this.value + '"' ) :
					this.getLabel( this.value );
			},
 
			/**
			 * Returns the link to the object if it has any.
			 *
			 * @return {string}
			 */
			link: function () {
				return ( this.hasLink && !this.isBlank ) ? '/view/' + ( mw.language.getFallbackLanguageChain()[ 0 ] || 'en' ) + '/' + 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.getLabel( missingType ) : this.getLabel( Constants.Z_OBJECT );
				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 ) => {
						return ( 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>