All files / ext.wikilambda.edit/components/function/editor FunctionEditorDefinition.vue

99.19% Statements 247/249
75.86% Branches 22/29
100% Functions 14/14
99.19% Lines 247/249

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 2501x 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 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x 12x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 63x 63x 63x 63x 37x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 40x 40x 40x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 1x 1x 1x 1x 1x 1x 3x 3x 1x 1x 1x 1x 1x 19x 1x 1x 1x 1x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 100x 100x 100x 1x 1x 3x 3x 3x 3x     3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for the definition tab in the ZFunction Editor.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<main
		class="ext-wikilambda-function-definition"
		data-testid="function-editor-definition"
	>
		<!-- Function Definition blocks -->
		<div class="ext-wikilambda-function-definition__container">
			<wl-function-editor-language-block
				v-for="( langZid, index ) in functionLanguages"
				:key="'language-block-' + langZid"
				:index="index"
				:z-language="langZid"
				@set-language="setLanguage"
				@updated-labels="setHasUpdatedLabels"
			></wl-function-editor-language-block>
		</div>
		<!-- Add Language Button -->
		<div class="ext-wikilambda-function-definition__action-add-language">
			<cdx-button
				data-testid="add-language-button"
				class="ext-wikilambda-function-definition__action-add-language-button"
				@click="addLanguage"
			>
				<cdx-icon :icon="icons.cdxIconLanguage"></cdx-icon>
				{{ addLanguageButtonText }}
			</cdx-button>
		</div>
		<!-- Footer with Publish Widget -->
		<!-- FIXME: No need to add a footer component -->
		<wl-function-editor-footer
			:is-function-dirty="isFunctionDirty"
			:function-input-changed="inputTypeChanged"
			:function-output-changed="outputTypeChanged"
			:function-signature-changed="functionSignatureChanged"
		></wl-function-editor-footer>
	</main>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const FunctionEditorLanguageBlock = require( './FunctionEditorLanguageBlock.vue' ),
	FunctionEditorFooter = require( './FunctionEditorFooter.vue' ),
	icons = require( '../../../../lib/icons.json' ),
	Constants = require( '../../../Constants.js' ),
	eventLogUtils = require( '../../../mixins/eventLogUtils.js' ),
	typeUtils = require( '../../../mixins/typeUtils.js' ),
	hybridToCanonical = require( '../../../mixins/schemata.js' ).methods.hybridToCanonical,
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-definition',
	components: {
		'wl-function-editor-language-block': FunctionEditorLanguageBlock,
		'wl-function-editor-footer': FunctionEditorFooter,
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon
	},
	mixins: [ eventLogUtils, typeUtils ],
	data: function () {
		return {
			rowId: 0,
			icons: icons,
			initialInputTypes: [],
			initialOutputType: '',
			hasUpdatedLabels: false,
			functionLanguages: []
		};
	},
	computed: Object.assign( mapGetters( [
		'getCurrentZObjectId',
		'getRowByKeyPath',
		'getUserLangZid',
		'getZFunctionInputs',
		'getMetadataLanguages',
		'getZFunctionOutput',
		'getZObjectAsJsonById',
		'isCreateNewPage'
	] ),
	{
		/**
		 * Returns whether there have been any changes made
		 * in inputs or output types.
		 *
		 * @return {boolean}
		 */
		functionSignatureChanged: function () {
			return this.inputTypeChanged || this.outputTypeChanged;
		},
		/**
		 * Returns whether there have been any changes made
		 * from the initial value of the function.
		 *
		 * @return {boolean}
		 */
		isFunctionDirty: function () {
			return this.functionSignatureChanged || this.hasUpdatedLabels;
		},
		/**
		 * Returns the text for the button to add more languages
		 *
		 * @return {string}
		 */
		addLanguageButtonText: function () {
			return this.$i18n( 'wikilambda-function-definition-add-other-label-languages-title' ).text();
		},
		/**
		 * Returns an array with the string representation of the
		 * currently selected input types. Filters out the undefined
		 * values in between.
		 *
		 * @return {Array}
		 */
		currentInputTypes: function () {
			return this.getZFunctionInputs().map( ( inputRow ) => {
				const inputTypeRow = this.getRowByKeyPath( [ Constants.Z_ARGUMENT_TYPE ], inputRow.id );
				return inputTypeRow ?
					this.getTypeStringRepresentation( inputTypeRow.id ) :
					undefined;
			} ).filter( ( type ) => !!type );
		},
		/**
		 * Returns an the string representation of the
		 * currently selected output type.
		 *
		 * @return {string}
		 */
		currentOutputType: function () {
			const outputRow = this.getZFunctionOutput();
			return outputRow ?
				this.getTypeStringRepresentation( outputRow.id ) :
				undefined;
		},
		/**
		 * Returns whether the input types have changed from the
		 * initial value.
		 *
		 * @return {boolean}
		 */
		inputTypeChanged: function () {
			// Return true if length is different, or if at least an item is found to be different
			return ( this.currentInputTypes.length !== this.initialInputTypes.length ) ||
				!!this.currentInputTypes.find( ( value, i ) => value !== this.initialInputTypes[ i ] );
		},
		/**
		 * Returns whether the output type has changed from the
		 * initial value.
		 *
		 * @return {boolean}
		 */
		outputTypeChanged: function () {
			return ( this.currentOutputType !== this.initialOutputType );
		}
	} ),
	methods: {
		/**
		 * Saves the initial values for initialInputTypes and initialOutputType
		 */
		saveInitialFunctionSignature: function () {
			this.initialInputTypes = this.currentInputTypes;
			this.initialOutputType = this.currentOutputType;
		},
		/**
		 * Sets the hasUpdatedLabels flag to true
		 */
		setHasUpdatedLabels: function () {
			this.hasUpdatedLabels = true;
		},
		/**
		 * Adds a new entry to the functionLanguages local array.
		 * Initiates it to "unset" language, so it just adds an
		 * empty string.
		 */
		addLanguage: function () {
			this.functionLanguages.push( '' );
		},
		/**
		 * Sets a function definition unset language to a given
		 * value, given its index in the functionLanguages array.
		 *
		 * @param {Object} payload
		 * @param {number} payload.index array index
		 * @param {string} payload.language zid
		 */
		setLanguage: function ( payload ) {
			this.functionLanguages[ payload.index ] = payload.language;
		},
		/**
		 * Return the string representation of the object under
		 * the given rowId. If it's a function call, includes args (E.g.
		 * Z881(Z6)
		 *
		 * @param {number} rowId
		 * @return {string}
		 */
		getTypeStringRepresentation: function ( rowId ) {
			const canonical = hybridToCanonical( this.getZObjectAsJsonById( rowId ) );
			return this.typeToString( canonical );
		}
	},
	mounted: function () {
		// Initializ the local array with the collection of available languages
		// and initialize first label block with user lang if there are none.
		this.functionLanguages = this.getMetadataLanguages( this.rowId );
		if ( this.functionLanguages.length === 0 ) {
			this.functionLanguages.push( this.getUserLangZid );
		}
 
		// Initialize initial state of inputs and output
		this.saveInitialFunctionSignature();
 
		// Dispatch an editFunction load event
		// TODO (T352141): Consider counting "About info" editing separately
		this.dispatchEvent( 'wf.ui.editFunction.load', {
			edit: true,
			isnewzobject: this.isCreateNewPage,
			zobjectid: this.getCurrentZObjectId || null,
			zobjecttype: Constants.Z_FUNCTION,
			zlang: this.getUserLangZid || null
		} );
		// T350495 Update the WikiLambda instrumentation to use core interaction events
		const interactionData = {
			zobjecttype: Constants.Z_FUNCTION,
			zobjectid: this.getCurrentZObjectId || null,
			zlang: this.getUserLangZid || null
		};
		const action = this.isCreateNewPage ? 'create' : 'edit';
		this.submitInteraction( action, interactionData );
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-definition {
	&__action-add-language {
		border-bottom: 1px solid @border-color-subtle;
		padding: @spacing-150 0;
	}
}
</style>