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

93.54% Statements 87/93
73.33% Branches 33/45
83.33% Functions 25/30
93.4% Lines 85/91

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    25x 25x 25x 25x 25x 25x 25x 25x 25x                                                 12x                       126x                 126x               125x 125x               125x               126x               125x 125x               125x                     111x 52x 30x   81x   111x                     127x 45x 45x   153x 82x   127x                 111x     5x 5x                   125x               5x                 5x 10x                   111x 111x 110x 220x           111x                     5x     5x 5x 2x         5x           5x 5x   3x 3x   2x 2x       5x                           2x 2x 2x                                                 13x 13x 13x           12x     12x 1x       25x   25x     25x 25x 25x     25x     25x     25x     25x       25x       25x 25x     25x     25x     25x     25x   128x 128x 128x 128x       5x                                                                                   25x                                                                                                                                                      
<!--
	WikiLambda Vue component for Z16/Code objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-code">
		<!-- Programming language block -->
		<div class="ext-wikilambda-key-value">
			<div class="ext-wikilambda-key-value-main">
				<div class="ext-wikilambda-key-block">
					<label>{{ programmingLanguageLabel }}</label>
				</div>
				<div
					class="ext-wikilambda-value-block"
					data-testid="language-dropdown"
				>
					<span
						v-if="!edit"
						class="ext-wikilambda-value-text"
					>{{ programmingLanguageLiteral }}</span>
					<cdx-select
						v-else
						v-model:selected="programmingLanguageValue"
						class="ext-wikilambda-value-input ext-wikilambda-code__language-selector"
						:menu-items="programmingLanguageMenuItems"
						:default-label="$i18n( 'wikilambda-editor-label-select-programming-language-label' ).text()"
						:status="( programmingLanguageErrors.length > 0 ) ? 'error' : 'default'"
					>
					</cdx-select>
				</div>
				<cdx-message
					v-for="( error, index ) in programmingLanguageErrors"
					:key="'language-error-' + index"
					class="ext-wikilambda-key-value-inline-error"
					:type="error.type"
					:inline="true"
				>
					<!-- eslint-disable vue/no-v-html -->
					<div v-html="getErrorMessage( error )"></div>
				</cdx-message>
			</div>
		</div>
		<!-- Code editor block -->
		<div class="ext-wikilambda-key-value">
			<div class="ext-wikilambda-key-value-main">
				<div class="ext-wikilambda-key-block">
					<label>{{ codeLabel }}</label>
				</div>
				<div class="ext-wikilambda-value-block">
					<code-editor
						class="ext-wikilambda-code__code-editor"
						:mode="programmingLanguageLiteral"
						:read-only="!edit"
						:value="editorValue"
						data-testid="code-editor"
						@change="updateCode"
					></code-editor>
				</div>
				<cdx-message
					v-for="( error, index ) in codeErrors"
					:key="'code-error-' + index"
					class="ext-wikilambda-key-value-inline-error"
					:type="error.type"
					:inline="true"
				>
					<!-- eslint-disable vue/no-v-html -->
					<div v-html="getErrorMessage( error )"></div>
				</cdx-message>
			</div>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	CdxMessage = require( '@wikimedia/codex' ).CdxMessage,
	CodeEditor = require( '../base/CodeEditor.vue' ),
	CdxSelect = require( '@wikimedia/codex' ).CdxSelect,
	errorUtils = require( '../../mixins/errorUtils.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions;
 
module.exports = exports = defineComponent( {
	name: 'wl-z-code',
	components: {
		'cdx-message': CdxMessage,
		'cdx-select': CdxSelect,
		'code-editor': CodeEditor
	},
	mixins: [ errorUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		parentId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		}
	},
	data: function () {
		return {
			editorValue: '',
			allowSetEditorValue: true
		};
	},
	computed: Object.assign(
		mapGetters( [
			'getAllProgrammingLangs',
			'getErrors',
			'getLabel',
			'getRowByKeyPath',
			'getZCodeProgrammingLanguageRow',
			'getZCodeString',
			'getZObjectTypeByRowId',
			'getZReferenceTerminalValue',
			'getInputsOfFunctionZid',
			'getZImplementationFunctionZid'
		] ),
		{
			/**
			 * Returns the label of the key Z16K2
			 *
			 * @return {string}
			 */
			codeLabel: function () {
				return this.getLabel( Constants.Z_CODE_CODE );
			},
 
			/**
			 * Terminal string with the code of the function
			 * implementation or undefined if not found
			 *
			 * @return {string | undefined}
			 */
			codeValue: function () {
				retuErn this.getZCodeString( this.rowId );
			},
 
			/**
			 * Returns the rowId for the code value string
			 *
			 * @return {number | undefined}
			 */
			codeValueRowId: function () {
				const codeRow = this.getRowByKeyPath( [
					Constants.Z_CODE_CODE,
					Constants.Z_STRING_VALUE
				], this.rowId );
				return codeRow ? codeRow.id : undefined;
			},
 
			/**
			 * Returns code field errors
			 *
			 * @return {Array}
			 */
			codeErrors: function () {
				return this.codeValueRowId ? this.getErrors( this.codeValueRowId ) : [];
			},
 
			/**
			 * Returns the label of the key Z16K1
			 *
			 * @return {string}
			 */
			programmingLanguageLabel: function () {
				return this.getLabel( Constants.Z_CODE_LANGUAGE );
			},
 
			/**
			 * Returns the programming language (Z16K1) row id
			 *
			 * @return {number}
			 */
			programmingLanguageRowId: function () {
				const programmingLangRow = this.getZCodeProgrammingLanguageRow( this.rowId );
				return programmingLangRow ? programmingLangRow.id : undefined;
			},
 
			/**
			 * Returns whether the programming language field has a literal/Z61 or a reference/Z9
			 *
			 * @return {string}
			 */
			programmingLanguageType: function () {
				return this.getZObjectTypeByRowId( this.programmingLanguageRowId );
			},
 
			/**
			 * Returns the Zid of the programming language selected, or undefined if unselected.
			 * When the programming language is a literal, finds the Zid in the Menu Items array by its literal.
			 *
			 * @return {string | undefined}
			 */
			programmingLanguageZid: function () {
				// TODO: remove options for literal programming language when the time comes
				let zId;
				if ( this.programmingLanguageType === Constants.Z_PROGRAMMING_LANGUAGE ) {
					const menuObject = this.programmingLanguageMenuItems.find( ( item ) =>
						item.label === this.programmingLanguageLiteral
					);
					zId = menuObject ? menuObject.value : undefined;
				} else {
					zId = this.getZReferenceTerminalValue( this.programmingLanguageRowId );
				}
				return zId;
			},
 
			/**
			 * Returns the literal of the programming language selected, or empty string if unselected.
			 * When the programming language is a reference, finds the literal in the Menu Items array by
			 * its zid.
			 *
			 * @return {string}
			 */
			programmingLanguageLiteral: function () {
				let literal;
				if ( this.programmingLanguageType === Constants.Z_PROGRAMMING_LANGUAGE ) {
					const langObject = this.getRowByKeyPath( [
						Constants.Z_PROGRAMMING_LANGUAGE_CODE,
						Constants.Z_STRING_VALUE
					], this.programmingLanguageRowId );
					literal = langObject ? langObject.value : undefined;
				} else {
					const menuObject = this.programmingLanguageMenuItems.find( ( item ) =>
						item.value === this.programmingLanguageValue
					);
					literal = menuObject ? menuObject.label : undefined;
				}
				return literal;
			},
 
			/**
			 * Zid of the selected programming language or empty string if not set
			 *
			 * E@return {string}
			 */
			programmingLanguageValue: {
				get: function () {
					return this.programmingLanguageZid === undefined ? '' : this.programmingLanguageZid;
				},
				set: function ( val ) {
					if ( val !== this.programmingLanguageValue ) {
						this.selectLanguage( val );
					}
				}
			},
 
			/**
			 * Returns programming language errors
			 *
			 * @return {Array}
			 */
			programmingLanguageErrors: function () {
				return this.programmingLanguageRowId ? this.getErrors( this.programmingLanguageRowId ) : [];
			},
 
			/**
			 * Zid of the target function that this code will implement
			 *
			 * @return {string | undefined }
			 */
			functEionZid: function () {
				return this.getZImplementationFunctionZid( this.parentId );
			},
 
			/**
			 * Returns an array of strings with the keys of the selected
			 * target function for the implementation (Z14K1)
			 *
			 * @return {Array}
			 */
			functionArgumentKeys: function () {
				return this.getInputsOfFunctionZid( this.functionZid )
					.map( function ( arg ) {
						return arg[ Constants.Z_ARGUMENT_KEY ];
					} );
			},
 
			/**
			 * Returns the appropriately formatted menu items to load the cdx-select
			 * component with the different programming language options
			 *
			 * @return {Array}
			 */
			programmingLanguageMenuItems: function () {
				const programmingLangs = [];
				if ( this.getAllProgrammingLangs.length > 0 ) {
					for ( const lang of this.getAllProgrammingLangs ) {
						programmingLangs.push(
							{
								label: lang[
									Constants.Z_PERSISTENTOBJECT_VALUE
								][
									Constants.Z_PROGRAMMING_LANGUAGE_CODE
								],
								value: lang[
									Constants.Z_PERSISTENTOBJECT_ID
								]
							}
						);
					}
				}
				return programmingLangs;
			}
		}
	),
	methods: Object.assign(
		mapActions( [
			'fetchZids',
			'fetchAllZProgrammingLanguages',
			'changeType',
			'clearErrors'
		] ),
		{
			/**
			 * Sets the value of the Code programming language key (Z16K1) and
			 * initializes the value of the Code content key (Z16K2)
			 *
			 * @param {string} value
			 */
			selectLanguage: function ( value ) {
				this.allowSetEditorValue = true;
 
				// clear errors for language field
				this.clearErrors( this.programmingLanguageRowId );
 
				if ( this.programmingLanguageType === Constants.Z_PROGRAMMING_LANGUAGE ) {
					this.changeType( {
						id: this.programmingLanguageRowId,
						type: Constants.Z_REFERENCE
					} );
				}

				this.$emit( 'set-value', {
					keyPath: [
						Constants.Z_CODE_LANGUAGE,
						Constants.Z_REFERENCE_ID
					],
					value
				} );

				// update boiler plate code
				let updatedBoilerPlateCode = '';
				switch ( value ) {
					case Constants.Z_PROGRAMMING_LANGUAGES.JAVASCRIPT:
						updatedBoilerPlateCode = 'function ' +
							this.functionZid + '( ' +
							this.functionArgumentKeys.join( ', ' ) + ' ) {\n\n}';
						break;
					case Constants.Z_PROGRAMMING_LANGUAGES.PYTHON:
						updatedBoilerPlateCode = 'def ' +
							this.functionZid + '(' +
							this.functionArgumentKeys.join( ', ' ) + '):\n\t';
						break;
					default:
						break;
				}
 
				this.$emit( 'set-value', {
					keyPath: [ Constants.Z_CODE_CODE, Constants.Z_STRING_VALUE ],
					value: updatedBoilerPlateCode
				} );
			},
 
			/**
			 * Updates the value of the Code content key (Z16K2)
			 *
			 * @param {string} code
			 */
			updateCode: function ( code ) {
				// there is an edge case where acejs will trigger an empty change that we process as an event object
				// we don't want to update our object with that bad data
				// TODO (T324605): this deserves a deeper investigation
				if ( typeof code !== 'object' ) {
					this.clearErrors( this.codeValueRowId );
					this.$emit( 'set-value', {
						keyPath: [ Constants.Z_CODE_CODE, Constants.Z_STRING_VALUE ],
						value: code
					} );
				}
			},
 
			/**
			 * Returns the translated message for a given error code.
			 * Error messages can have html tags.
			 *
			 * @param {Object} error
			 * @return {string}
			 */
			getErrorMessage: function ( error ) {
				// eslint-disable-next-line mediawiki/msg-doc
				return error.message || this.$i18n( error.code ).text();
			}
		} ),
	watch: {
		codeValue: {
			immediate: true,
			handler: function () {
				// Check allowSetEditorValue to ensure we only set value in the editor when its current value should be
				// overridden (e.g. when the editor is first loaded, or when the language changes). Ensuring this
				// prevents a bug that moves the cursor to the end of the editor on every keypress.
				if ( this.allowSetEditorValue ) {
					this.editorValue = this.codeValue || '';
					this.allowSetEditorValue = false;
				}
			}
		}
	},
	mounted: function () {
		this.fetchZids( { zids: [ Constants.Z_CODE ] } );
		if ( this.getAllProgrammingLangs.length <= 0 ) {
			this.fetchAllZProgrammingLanguages();
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-code {
	& > .ext-wikilambda-key-value {
		& > .ext-wikilambda-key-value-main {
			& > .ext-wikilambda-key-block {
				margin-bottom: 0;
 
				label {
					font-weight: bold;
					color: @color-base;
				}
			}
 
			& > .ext-wikilambda-value-block {
				margin-bottom: @spacing-50;
 
				.ext-wikilambda-value-input {
					margin-top: @spacing-25;
				}
			}
		}
	}
}
</style>