All files / ext.wikilambda.edit/components/widgets FunctionEvaluator.vue

94.89% Statements 93/98
78.78% Branches 52/66
92.5% Functions 37/40
94.84% Lines 92/97

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 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526    17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x                                                       30x                             22x               22x               510x               320x 320x               375x               375x 28x                 3x     3x 3x               3x     3x 3x               3x               375x                 375x                 375x                               302x               22x                     2x         2x                   126x 126x 126x             3x           3x       3x 1x     1x 1x 1x 1x 1x     3x 3x 3x 3x         3x 3x   3x             3x   3x             3x                           30x 30x   30x             30x 15x     15x                 30x 30x       30x 30x           126x       30x     17x   17x     17x 17x 17x         17x     17x         17x     17x     17x         17x     17x     17x       30x 30x 30x 30x 30x 30x       382x     382x                                               32x                       10x                       17x                                                                                                                                                                                                                  
<!--
	WikiLambda Vue component for evaluation of ZFunction objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-widget-base class="ext-wikilambda-function-evaluator">
		<template #header>
			{{ title }}
		</template>
 
		<template #main>
			<!-- Logged out user warning -->
			<cdx-message
				v-if="userCanRunFunction === false"
				type="warning"
				class="ext-wikilambda-function-evaluator-message"
			>
				{{ $i18n( 'wikilambda-function-evaluation-restriction-warning' ).text() }}
			</cdx-message>
 
			<!-- Not-runnable user warning -->
			<cdx-message
				v-if="!hasImplementations && selectedFunctionZid !== undefined"
				type="notice"
				class="ext-wikilambda-function-evaluator-message"
			>
				{{ $i18n( 'wikilambda-function-evaluation-restriction-notrunnable' ).text() }}
			</cdx-message>
 
			<!-- Function Call -->
			<div
				v-if="showFunctionSelector"
				class="ext-wikilambda-function-evaluator-call"
				data-testid="function-evaluator-call"
			>
				<div class="ext-wikilambda-key-block">
					<label>{{ functionCallLabel }}</label>
				</div>
				<wl-z-reference
					:row-id="selectedFunctionRowId"
					:edit="true"
					:expected-type="functionType"
					@set-value="setFunctionZid"
				></wl-z-reference>
			</div>
 
			<!-- Function Inputs -->
			<div
				v-if="hasInputs"
				class="ext-wikilambda-function-evaluator-inputs"
				data-testid="function-evaluator-inputs"
			>
				<div class="ext-wikilambda-key-block">
					<label>{{ $i18n( 'wikilambda-function-evaluator-enter-inputs' ).text() }}</label>
				</div>
				<wl-z-object-key-value
					v-for="inputRowId in inputRowIds"
					:key="'input-row-id-' + inputRowId"
					:row-id="inputRowId"
					:edit="true"
				></wl-z-object-key-value>
			</div>
 
			<!-- Run Function button -->
			<div class="ext-wikilambda-function-evaluator-run-button">
				<cdx-button
					action="progressive"
					weight="primary"
					:disabled="!canRunFunction"
					@click="waitAndCallFunction"
				>
					{{ $i18n( 'wikilambda-function-evaluator-run-function' ).text() }}
				</cdx-button>
			</div>
 
			<!-- Evaluation Result -->
			<div
				v-if="hasResult || running"
				class="ext-wikilambda-function-evaluator-result-block"
				data-testid="function-evaluator-result"
			>
				<div class="ext-wikilambda-function-evaluator-result">
					<div class="ext-wikilambda-key-block">
						<label>{{ $i18n( 'wikilambda-function-evaluator-result' ).text() }}</label>
					</div>
					<div v-if="running">
						{{ $i18n( 'wikilambda-function-evaluator-running' ).text() }}
					</div>
					<wl-z-object-key-value
						v-else
						:row-id="resultRowId"
						:skip-indent="true"
						:edit="false"
					></wl-z-object-key-value>
				</div>
			</div>
		</template>
	</wl-widget-base>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxMeIssage = require( '@wikimedia/codex' ).CdxMessage,
	WidgetBase = require( '../base/WidgetBase.vue' ),
	ZReference = require( '../default-view-types/ZReference.vue' ),
	ZObjectKeyValue = require( '../default-view-types/ZObjectKeyValue.vue' ),
	eventLogUtils = require( '../../mixins/eventLogUtils.js' ),
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-function-evaluator-widget',
	components: {
		'cdx-button': CdxButton,
		'cdxI-message': CdxMessage,
		'wl-widget-base': WidgetBase,
		'wl-z-reference': ZReference,
		'wl-z-object-key-value': ZObjectKeyValue
	},
	mixins: [ eventLogUtils ],
	props: {
		functionZid: {
			type: String,
			required: false,
			default: undefined
		},
		forImplementation: {
			type: Boolean,
			required: false,
			default: false
		},
		contentRowId: {
			type: Number,
			required: false,
			default: undefined
		}
	},
	data: function () {
		return {
			functionCallRowId: '',
			resultRowId: '',
			running: false,
			hasResult: false,
			functionType: Constants.Z_FUNCTION
		};
	},
	computed: Object.assign( mapGetters( [
		'getConnectedObjects',
		'getZFunctionCallArguments',
		'getLabel',
		'getRowByKeyPath',
		'getStoredObject',
		'getZFunctionCallFunctionId',
		'getZObjectAsJsonById',
		'getCurrentZObjectId',
		'getCurrentZObjectType',
		'getUserLangZid',
		'getMapValueByKey',
		'userCanRunFunction',
		'userCanRunUnsavedCode',
		'waitForRunningParsers'
	] ), {
		/**
		 * Whether the widget has a pre-defined function
		 *
		 * @return {boolean}
		 */
		showFunctionSelector: function () {
			return !this.forFunction && !this.forImplementation;
		},
 
		/**
		 * Whether the widget has a pre-defined function
		 *
		 * @return {boolean}
		 */
		forFunction: function () {
			return !!this.functionZid;
		},
 
		/**
		 * Returns the selected function in the function call component
		 *
		 * @return {string | undefined}
		 */
		selectedFunctionZid: function () {
			return this.getZFunctionCallFunctionId( this.functionCallRowId );
		},
 
		/**
		 * Returns the selected function in the function call component
		 *
		 * @return {string | undefined}
		 */
		selectedFunctionRowId: function () {
			const row = this.getRowByKeyPath( [ Constants.Z_FUNCTION_CALL_FUNCTION ], this.functionCallRowId );
			return row ? row.id : undefined;
		},
 
		/**
		 * Returns whether there are any inputs to enter
		 *
		 * @return {boolean}
		 */
		hasInputs: function () {
			return this.inputRowIds.length > 0;
		},
 
		/**
		 * Returns the rowIds of the inputs of a function call
		 *
		 * @return {Array}
		 */
		inputRowIds: function () {
			return this.getZFunctionCallArguments( this.functionCallRowId )
				.map( function ( row ) {
					return row.id;
				} );
		},
 
		/**
		 * Returns the rowId of the Response Envelope Metadata/Z22K2
		 *
		 * @return {string|undefined}
		 */
		metadataRowId: function () {
			if ( Ethis.resultRowId === '' ) {
				return undefined;
			}
			const row = this.getRowByKeyPath( [ Constants.Z_RESPONSEENVELOPE_METADATA ], this.resultRowId );
			return row ? row.id : undefined;
		},
 
		/**
		 * Returns the rowId of the Metadata object with key 'errors'
		 *
		 * @return {string|undefined}
		 */
		errorRowId: function () {
			if ( !this.metadataRowId ) {
				return undefined;
			}
			const row = this.getMapValueByKey( this.metadataRowId, 'errors' );
			return row ? row.id : undefined;
		},

		/**
		 * Returns whether there's an error key in the metadata of the ZEvaluationResult given by this.resultRowId
		 *
		 * @return {boolean}
		 */
		resultHasError: function () {
			return this.errorRowId !== undefined;
		},

		/**
		 * Returns the array of implementation IDs for the selected function
		 *
		 * @return {Array}
		 */
		implementations: function () {
			return this.selectedFunctionZid ?
				this.getConnectedObjects( this.selectedFunctionZid, Constants.Z_FUNCTION_IMPLEMENTATIONS ) :
				[];
		},
 
		/**
		 * Returns whether the selected Function has any implementations
		 * and hence it can be run.
		 *
		 * @return {boolean}
		 */
		hasImplementations: function () {
			return this.implementations.length > 0;
		},
 
		/**
		 * Returns whether the function can be run, which can only happen
		 * if the function has active implementations and the user is logged in.
		 *
		 * @return {boolean}
		 */
		canRunFunction: function () {
			return this.hasImplementations && this.userCanRunFunction;
		},
 
		/**
		 * Returns whether the detached Function Call object is initialized
		 *
		 * @return {boolean}
		 */
		hasFunctionCall: function () {
			return !!this.functionCallRowId;
		},
 
		/**
		 * Returns the human readable label for the function selector block
		 *
		 * @return {string}
		 */
		functionCallLabel: function () {
			return this.getLabel( Constants.Z_FUNCTION_CALL_FUNCTION );
		},
 
		/**
		 * Returns the human readable label for the function call inputs block
		 *
		 * @return {string}
		 */
		title: function () {
			return this.forImplementation ?
				this.$i18n( 'wikilambda-function-evaluator-title-implementation' ).text() :
				this.forFunction ?
					this.$i18n( 'wikilambda-function-evaluator-title-function' ).text() :
					this.$i18n( 'wikilambda-function-evaluator-title' ).text();
		}
	} ),
	methods: Object.assign( mapActions( [
		'initializeResultId',
		'changeType',
		'fetchZids',
		'callZFunction',
		'setValueByRowIdAndPath',
		'setZFunctionCallArguments'
	] ), {
 
		/**
		 * @param {Object} payload
		 * @param {Object} payload.keyPath sequence of keys till the value to edit
		 * @param {Object | Array | string} payload.value new value
		 */
		setFunctionZid: function ( payload ) {
			// We set the arguments
			this.setZFunctionCallArguments( {
				parentId: this.functionCallRowId,
				functionZid: payload.value
			} );
			// And we set the value of the function
			this.setValueByRowIdAndPath( {
				rowId: this.selectedFunctionRowId,
				keyPath: payload.keyPath ? payload.keyPath : [],
				value: payload.value
			} );
		},
 
		/**
		 * Clears the orchestration result object
		 */
		clearResult: function () {
			this.hasResult = false;
			this.running = false;
			this.initializeResultId( this.resultRowId );
		},
 
		/**
		 * Waits for running parsers to return and persist
		 * changes before going ahead and running the function call
		 */
		waitAndCallFunction: function () {
			this.waitForRunningParsers.then( () => this.callFunction() );
		},
 
		/**
		 * Performs the function call
		 */
		callFunction: function () {
			const functionCallJson = this.getZObjectAsJsonById( this.functionCallRowId );
			// If we are in an implementation page, we build raw function call with raw implementation:
			// 1. Replace Z7K1 with the whole Z8 object: we assume it's in the store
			// 2. Replace te Z8K4 with [ Z14, implementation ]
			if ( this.forImplementation ) {
				const storedFunction = this.getStoredObject( this.functionZid );
				// If user can run unsaved code, we get the raw implementation,
				// else, we use the current persisted version by using its Zid
				const implementation = this.userCanRunUnsavedCode ?
					this.getZObjectAsJsonById( this.contentRowId ) :
					this.getCurrentZObjectId;
				if ( storedFunction && implementation ) {
					const functionObject = storedFunction[ Constants.Z_PERSISTENTOBJECT_VALUE ];
					functionObject[ Constants.Z_FUNCTION_IMPLEMENTATIONS ] = [
						Constants.Z_IMPLEMENTATION,
						implementation
					];
					functionCallJson[ Constants.Z_FUNCTION_CALL_FUNCTION ] = functionObject;
				}
			}
 
			this.running = true;
			this.initializeResultId( this.resultRowId )
				.then( ( rowId ) => {
					this.resultRowId = rowId;
					return this.callZFunction( {
						functionCall: functionCallJson,
						resultRowId: this.resultRowId
					} );
				} )
				.then( () => {
					this.running = false;
					this.hasResult = true;
					// Log an event using Metrics Platform
					const customData = {
						selectedfunctionzid: this.selectedFunctionZid || null,
						zobjectid: this.getCurrentZObjectId || null,
						zobjecttype: this.getCurrentZObjectType || null,
						resulthaserror: this.resultHasError,
						zlang: this.getUserLangZid || null
					};
					this.dispatchEvent( 'wf.ui.callFunction', customData );
					// T350497 Update the WikiLambda instrumentation to use core interaction events
					const interactionData = {
						zobjecttype: this.getCurrentZObjectType || null,
						zobjectid: this.getCurrentZObjectId || null,
						zlang: this.getUserLangZid || null,
						selectedfunctionzid: this.selectedFunctionZid || null,
						haserrors: this.resultHasError
					};
					this.submitInteraction( 'call', interactionData );
				} );
		},
 
		/**
		 * Initializes the detached objects in the zobject table
		 * that will contain:
		 * 1. The function call object, and
		 * 2. The evaluator result object returned from the call
		 *
		 * @param {string|undefined} initialFunctionZid
		 */
		initializeDetachedObjects: function ( initialFunctionZid ) {
			let functionCallRowId, resultRowId;
			// Initialize detached object for the function call
			this.initializeResultId( this.functionCallRowId ).then( ( rowId ) => {
				functionCallRowId = rowId;
				// Set the Function Call scaffolding
				return this.changeType( {
					type: Constants.Z_FUNCTION_CALL,
					id: rowId,
					value: initialFunctionZid || ''
				} ).then( () => {
					// If we are initializing the evaluator with an initialFunctionZid
					// we fetch the function data and then we set the arguments
					if ( initialFunctionZid ) {
						this.fetchZids( { zids: [ initialFunctionZid ] } ).then( () => {
							this.setZFunctionCallArguments( {
								parentId: rowId,
								functionZid: initialFunctionZid
							} );
						} );
					}
				} );
			} ).then( () => {
				// Initialize detached object for the result
				return this.initializeResultId( this.resultRowId ).then( ( rowId ) => {
					resultRowId = rowId;
				} );
			} ).finally( () => {
				// Set both detached rowIds to render the components
				this.functionCallRowId = functionCallRowId;
				this.resultRowId = resultRowId;
			} );
		}
	} ),
	watch: {
		selectedFunctionZid: function () {
			this.clearResult();
		}
	},
	mounted: function () {
		this.initializeDetachedObjects( this.functionZid );
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-evaluator {
	.ext-wikilambda-function-evaluator-message {
		margin-bottom: @spacing-125;
	}
 
	.ext-wikilambda-function-evaluator-inputs,
	.ext-wikilambda-function-evaluator-call {
		margin-bottom: @spacing-125;
 
		> .ext-wikilambda-key-block {
			margin-bottom: @spacing-25;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
	}
 
	.ext-wikilambda-function-evaluator-result-block {
		margin: 0 -@spacing-75 -@spacing-75;
 
		.ext-wikilambda-function-evaluator-result {
			margin-top: @spacing-75;
			padding: @spacing-75;
			background-color: @background-color-progressive-subtle;
 
			> .ext-wikilambda-key-block {
				margin-bottom: @spacing-25;
 
				label {
					font-weight: bold;
					color: @color-base;
				}
			}
 
			> .ext-wikilambda-key-value-row {
				> .ext-wikilambda-key-value {
					margin-bottom: 0;
				}
			}
		}
	}
}
</style>