All files / ext.wikilambda.app/components/visualeditor FunctionInputSetup.vue

99.49% Statements 392/394
97.29% Branches 36/37
100% Functions 10/10
99.49% Lines 392/394

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 3951x 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 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 17x 17x 31x 31x 17x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 113x 113x 98x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 23x 23x 23x 23x 23x 13x 13x 13x 13x 13x 13x 13x 8x 10x 10x 8x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 23x 23x 23x 23x 23x 23x 23x 2x 2x 1x 1x 1x 2x 23x 23x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x 1x 1x 1x 1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 2x 2x 2x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 205x 205x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 130x 130x 130x 130x 130x 130x     130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 130x 13x 13x 13x 13x 13x 13x 13x 13x 13x 14x 14x 13x 13x 13x 13x 13x 13x 13x 117x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 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  
<!--
	WikiLambda Vue component for Visual Editor Wikifunctions function call
	insertion and edit plugin.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-app-function-input-setup">
		<wl-function-input-preview
			v-if="allTypesFetched"
			:payload="areInputFieldsValid ? functionCallPayload : undefined">
		</wl-function-input-preview>
		<div class="ext-wikilambda-app-function-input-setup__body">
			<cdx-message v-if="hasMissingContent">
				<span
					v-i18n-html:wikilambda-visualeditor-wikifunctionscall-info-missing-content="[ functionZid ]"
				></span>
			</cdx-message>
			<wl-expandable-description
				v-if="functionDescription"
				:description="functionDescription"
				class="ext-wikilambda-app-function-input-setup__description"
			></wl-expandable-description>
			<p v-else class="ext-wikilambda-app-function-input-setup__description--empty">
				{{ i18n( 'brackets',
					i18n( 'wikilambda-visualeditor-wikifunctionscall-no-description' ).text()
				).text() }}
			</p>
			<div v-if="allTypesFetched" class="ext-wikilambda-app-function-input-setup__fields">
				<wl-function-input-field
					v-for="( field, index ) in inputFields"
					:key="field.inputKey"
					v-model="field.value"
					:input-type="field.inputType"
					:label-data="field.labelData"
					:error="field.error"
					:show-validation="showValidation( field.hasChanged )"
					@update="value => handleUpdate( index, value )"
					@validate="payload => handleValidation( index, payload )"
					@loading-start="$emit( 'loading-start' )"
					@loading-end="$emit( 'loading-end' )"
				></wl-function-input-field>
			</div>
		</div>
		<div class="ext-wikilambda-app-function-input-setup__footer">
			<cdx-icon :icon="icon"></cdx-icon>
			<!-- eslint-disable-next-line vue/no-v-html -->
			<span class="ext-wikilambda-app-function-input-setup__link" v-html="functionLink"></span>
		</div>
	</div>
</template>
 
<script>
const { CdxIcon, CdxMessage } = require( '../../../codex.js' );
const { computed, defineComponent, inject, onMounted, ref, watch } = require( 'vue' );
const useMainStore = require( '../../store/index.js' );
const Constants = require( '../../Constants.js' );
const FunctionInputField = require( './FunctionInputField.vue' );
const ExpandableDescription = require( './ExpandableDescription.vue' );
const FunctionInputPreview = require( './FunctionInputPreview.vue' );
const wikifunctionsIconSvg = require( './wikifunctionsIconSvg.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-input-setup',
	components: {
		'wl-function-input-field': FunctionInputField,
		'wl-expandable-description': ExpandableDescription,
		'wl-function-input-preview': FunctionInputPreview,
		'cdx-icon': CdxIcon,
		'cdx-message': CdxMessage
	},
	emits: [ 'update', 'loading-start', 'loading-end' ],
	setup( _, { emit } ) {
		const i18n = inject( 'i18n' );
		const store = useMainStore();
 
		// Constants
		const icon = wikifunctionsIconSvg;
 
		// State
		const inputFields = ref( [] );
		const allTypesFetched = ref( false );
 
		// Function data
		/**
		 * Returns the VisualEditor function ID.
		 *
		 * @return {string}
		 */
		const functionZid = computed( () => store.getVEFunctionId );
 
		/**
		 * Returns the text for the link to the function in Wikifunctions.
		 *
		 * @return {string}
		 */
		const functionLink = computed( () => i18n(
			'wikilambda-visualeditor-wikifunctionscall-dialog-function-link-footer',
			functionZid.value
		)
			.parse()
			// TODO (T406155): how to open the link in a new tab using MediaWiki translations?
			.replace( '<a ', '<a target="_blank" rel="noopener" ' )
		);
 
		/**
		 * Returns the LabelData object for the function name
		 *
		 * @return {LabelData}
		 */
		const functionName = computed( () => store.getLabelData( functionZid.value ) );
 
		/**
		 * Returns the description of the function.
		 *
		 * @return {LabelData}
		 */
		const functionDescription = computed( () => store.getDescription( functionZid.value ) );
 
		/**
		 * Returns the inputs of the function.
		 *
		 * @return {Array}
		 */
		const functionInputs = computed( () => store.getInputsOfFunctionZid( functionZid.value ) );
 
		/**
		 * Returns the output type of the function.
		 *
		 * @return {string}
		 */
		const functionOutputType = computed( () => store.getOutputTypeOfFunctionZid( functionZid.value ) );
 
		// Function call payload
		/**
		 * Prepares the payload for the function call.
		 *
		 * @return {Object} - The payload object.
		 */
		const functionCallPayload = computed( () => ( {
			functionZid: store.getVEFunctionId,
			params: functionInputs.value.map( ( arg, index ) => ( {
				type: arg[ Constants.Z_ARGUMENT_TYPE ],
				value: store.getVEFunctionParams[ index ]
			} ) )
		} ) );
 
		// Validation
		/**
		 * Checks if all input fields are valid.
		 *
		 * @return {boolean} - True if all fields are valid, otherwise false.
		 */
		const areInputFieldsValid = computed( () => inputFields.value.every( ( field ) => field.isValid ) );
 
		// Content status
		/**
		 * Returns whether any of the shown multilingual labels
		 * are missing in the user language. Will determine whether
		 * to display an info box with a call to action to translate.
		 *
		 * @return {boolean}
		 */
		const hasMissingContent = computed( () => (
			!functionName.value || !functionName.value.isUserLang ||
			!functionDescription.value || !functionDescription.value.isUserLang ||
			!inputFields.value.every( ( item ) => item.labelData.isUserLang )
		) );
 
		// Input field initialisation
		/**
		 * Builds a single input field object from an argument definition and value.
		 *
		 * @param {Object} arg - The argument definition.
		 * @param {string} value - The value for the field.
		 * @return {Object} The input field object.
		 */
		const buildInputField = ( arg, value ) => ( {
			inputKey: arg[ Constants.Z_ARGUMENT_KEY ],
			inputType: arg[ Constants.Z_ARGUMENT_TYPE ],
			labelData: store.getLabelData( arg[ Constants.Z_ARGUMENT_KEY ] ),
			value,
			hasChanged: false
		} );
 
		/**
		 * Pads veFunctionParams so it's at least as long as functionInputs.
		 */
		function syncFunctionParamsLength() {
			if ( functionInputs.value.length > store.getVEFunctionParams.length ) {
				for ( let i = store.getVEFunctionParams.length; i < functionInputs.value.length; i++ ) {
					store.setVEFunctionParam( i, '' );
				}
			}
		}
 
		/**
		 * Initializes the function input fields with the current Visual Editor function params.
		 */
		function initializeInputFields() {
			// Ensure veFunctionParams matches functionInputs length
			syncFunctionParamsLength();
 
			// Check if there's only one input field and use selected text as default
			// when there is no existing value in the store
			const hasOnlyOneInput = functionInputs.value.length === 1;
 
			// Set local inputFields array
			inputFields.value = functionInputs.value.map( ( arg, index ) => {
				// Get the existing value from the store
				let defaultValue = store.getVEFunctionParams[ index ];
 
				// Only prefill with selected text if:
				// 1) only one input field
				// 2) no existing store value
				if ( hasOnlyOneInput && !defaultValue ) {
					const selectedText = store.getVESelectedText;
					if ( selectedText ) {
						defaultValue = selectedText;
						store.setVEFunctionParam( index, selectedText );
					}
				}
 
				return buildInputField( arg, defaultValue );
			} );
		}
 
		// Field actions
		/**
		 * Updates the value of a specific input field in the Visual Editor.
		 *
		 * @param {number} index - The index of the field to update.
		 * @param {string} value - The new value for the field.
		 */
		function handleUpdate( index, value ) {
			inputFields.value[ index ].hasChanged = true;
			store.setVEFunctionParam( index, value );
			store.setVEFunctionParamsDirty();
			emit( 'update' );
		}
 
		/**
		 * Handles the validation event for a specific input field.
		 *
		 * Fields are validated even when the validation result doesn't
		 * need to be shown, so the isValid field is always present
		 * in every field object, and the veFunctionParamsValid store
		 * flag is always updated with the validity of the form.
		 *
		 * @param {number} index - The index of the field to validate.
		 * @param {Object} payload
		 * @param {boolean} payload.isValid - The validation status.
		 * @param {string|undefined} payload.error - The error message to set, if any.
		 */
		function handleValidation( index, payload ) {
			inputFields.value[ index ].isValid = payload.isValid;
			inputFields.value[ index ].error = payload.error;
		}
 
		/**
		 * Show validation message if:
		 * * is not a new parameter setup screen (initialized with values)
		 * * is blank param screen and the field has changed
		 *
		 * @param {boolean} hasChanged
		 * @return {boolean}
		 */
		function showValidation( hasChanged ) {
			return !store.isNewParameterSetup || hasChanged;
		}
 
		// Data fetching
		/**
		 * Fetches the ZIDs for all input types and the output type.
		 * Show a loading state in VisualEditor while fetching.
		 *
		 * @param {Array} inputs - The list of function inputs.
		 * @param {string} outputType - The output type of the function.
		 */
		function fetchInputAndOutputTypes( inputs, outputType ) {
			const zidsToFetch = [
				...inputs.map( ( arg ) => arg[ Constants.Z_ARGUMENT_TYPE ] ),
				outputType
			];
 
			if ( !zidsToFetch.length ) {
				return;
			}
 
			emit( 'loading-start' );
 
			store.fetchZids( { zids: zidsToFetch } )
				.then( () => {
					allTypesFetched.value = true;
				} )
				.finally( () => {
					emit( 'loading-end' );
				} );
		}
 
		// Watch
		/**
		 * Watches the form validity and updates the VisualEditor validity state
		 * so the submit button can be enabled/disabled depending on the state.
		 *
		 * @param {boolean} isValid - The form validity status.
		 */
		watch( areInputFieldsValid, ( isValid ) => {
			store.setVEFunctionParamsValid( isValid );
			emit( 'update' );
		} );
 
		/**
		 * Watches the function inputs and output type, and fetches the ZIDs for both.
		 * Do this immediately and when the inputs or output type change.
		 */
		watch( functionInputs, ( newInputs ) => {
			fetchInputAndOutputTypes( newInputs, functionOutputType.value );
		}, { immediate: true } );
		watch( functionOutputType, ( newOutputType ) => {
			fetchInputAndOutputTypes( functionInputs.value, newOutputType );
		}, { immediate: true } );
 
		// Lifecycle
		onMounted( () => {
			initializeInputFields();
			// Fetch language zid for content page
			const langCode = mw.config.get( 'wgContentLanguage' );
			store.ensureLanguageCodes( { codes: [ langCode ] } );
		} );
 
		return {
			allTypesFetched,
			areInputFieldsValid,
			functionCallPayload,
			functionDescription,
			functionLink,
			functionZid,
			handleUpdate,
			handleValidation,
			hasMissingContent,
			icon,
			inputFields,
			showValidation,
			i18n
		};
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-function-input-setup {
	.ext-wikilambda-app-function-input-setup__body {
		background-color: @background-color-neutral-subtle;
		padding: @spacing-75 @spacing-100 @spacing-100;
	}
 
	.ext-wikilambda-app-function-input-setup__description {
		margin-top: @spacing-75;
		margin-bottom: @spacing-75;
 
		.ext-wikilambda-app-expandable-description__toggle-button {
			background-color: @background-color-neutral-subtle;
 
			&::before {
				background: linear-gradient( to right, transparent, @background-color-neutral-subtle );
			}
		}
	}
 
	.ext-wikilambda-app-function-input-setup__description--empty {
		margin-top: @spacing-75;
		margin-bottom: @spacing-75;
		color: @color-placeholder;
	}
 
	.ext-wikilambda-app-function-input-setup__footer {
		display: flex;
		background-color: @background-color-base;
		padding: @spacing-75 @spacing-100;
	}
 
	.ext-wikilambda-app-function-input-setup__link {
		margin-left: @spacing-25;
 
		& > a {
			font-weight: @font-weight-bold;
		}
	}
}
</style>