All files / ext.wikilambda.app/components/function/editor FunctionEditor.vue

98.62% Statements 287/291
83.33% Branches 30/36
100% Functions 6/6
98.62% Lines 287/291

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 2921x 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 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 134x 134x 17x 17x 17x 17x 17x 17x 17x 17x 17x 48x 48x 17x 17x 17x 17x 17x 17x 17x 17x 17x 58x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x   1x 1x 1x       17x 17x 17x 17x 17x 17x 17x 21x 21x 17x 17x 17x 17x 17x 17x 17x 5x 5x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 8x 8x 17x 17x 17x 17x 17x 17x 17x 5x 5x 3x 3x 2x 2x 2x 5x 1x 1x 1x 1x 1x 1x 5x 17x 17x 17x 28x 12x 12x 12x 12x 12x 12x 12x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 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>
	<div
		class="ext-wikilambda-app-function-editor"
		data-testid="function-editor-definition"
	>
		<!-- Function Definition blocks -->
		<div class="ext-wikilambda-app-function-editor__container">
			<wl-function-editor-language-block
				v-for="( langZid, index ) in functionLanguages"
				:key="'language-block-' + index"
				:index="index"
				:z-language="langZid"
				:function-languages="functionLanguages"
				@language-changed="setLanguage"
				@labels-updated="setHasUpdatedLabels"
			></wl-function-editor-language-block>
		</div>
		<!-- Add Language Button -->
		<div class="ext-wikilambda-app-function-editor__action-add-language">
			<cdx-button
				data-testid="add-language-button"
				class="ext-wikilambda-app-function-editor__action-add-language-button"
				@click="addLanguage"
			>
				<cdx-icon :icon="iconLanguage"></cdx-icon>
				{{ i18n( 'wikilambda-function-definition-add-other-label-languages-title' ).text() }}
			</cdx-button>
		</div>
		<!-- Footer with Publish Widget -->
		<wl-publish-widget
			class="ext-wikilambda-app-function-editor__footer"
			:is-dirty="isFunctionDirty"
			:function-signature-changed="functionSignatureChanged"
			@start-publish="raiseFunctionWarnings"
		></wl-publish-widget>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, onMounted, ref, watch } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const icons = require( '../../../../lib/icons.json' );
const useEventLog = require( '../../../composables/useEventLog.js' );
const useMainStore = require( '../../../store/index.js' );
const { hybridToCanonical } = require( '../../../utils/schemata.js' );
const { typeToString } = require( '../../../utils/typeUtils.js' );
 
// Function editor componetns
const FunctionEditorLanguageBlock = require( './FunctionEditorLanguageBlock.vue' );
// Widget components
const PublishWidget = require( '../../widgets/publish/Publish.vue' );
// Codex components
const { CdxButton, CdxIcon } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor',
	components: {
		'wl-function-editor-language-block': FunctionEditorLanguageBlock,
		'wl-publish-widget': PublishWidget,
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon
	},
	setup() {
		const i18n = inject( 'i18n' );
		const { submitInteraction } = useEventLog();
		const store = useMainStore();
 
		// Constants
		const iconLanguage = icons.cdxIconLanguage;
 
		// State
		const initialInputTypes = ref( [] );
		const initialOutputType = ref( '' );
		const hasUpdatedLabels = ref( false );
		const functionLanguages = ref( [] );
 
		// Function signature data
		/**
		 * Returns an array with the string representation of the
		 * currently selected input types. Filters out the undefined
		 * values in between.
		 *
		 * @return {Array}
		 */
		const currentInputTypes = computed( () => store.getZFunctionInputs.map( ( input ) => {
			const inputType = hybridToCanonical( input[ Constants.Z_ARGUMENT_TYPE ] );
			return typeToString( inputType );
		} ).filter( ( type ) => !!type ) );
 
		/**
		 * Returns an the string representation of the
		 * currently selected output type.
		 *
		 * @return {string}
		 */
		const currentOutputType = computed( () => {
			const outputType = hybridToCanonical( store.getZFunctionOutput );
			return typeToString( outputType );
		} );
 
		/**
		 * Returns whether the input types have changed from the
		 * initial value.
		 *
		 * @return {boolean}
		 */
		const inputTypeChanged = computed( () => currentInputTypes.value.length !== initialInputTypes.value.length ||
			!!currentInputTypes.value.find( ( value, i ) => value !== initialInputTypes.value[ i ] )
		);
 
		/**
		 * Returns whether the output type has changed from the
		 * initial value.
		 *
		 * @return {boolean}
		 */
		const outputTypeChanged = computed( () => currentOutputType.value !== initialOutputType.value );
 
		/**
		 * Returns whether there have been any changes made
		 * in inputs or output types.
		 *
		 * @return {boolean}
		 */
		const functionSignatureChanged = computed( () => inputTypeChanged.value || outputTypeChanged.value );
 
		/**
		 * Returns whether there have been any changes made
		 * from the initial value of the function.
		 *
		 * @return {boolean}
		 */
		const isFunctionDirty = computed( () => functionSignatureChanged.value || hasUpdatedLabels.value );
 
		/**
		 * Returns whether the function has connected objects (implementations or tests).
		 *
		 * @return {boolean}
		 */
		const hasConnectedObjects = computed( () => !!store.getConnectedImplementations.length ||
			!!store.getConnectedTests.length );
 
		/**
		 * Returns the error code for the type of function
		 * signature warning to be shown to the user, depending on
		 * whether the inputs have changed, the output, or both.
		 *
		 * @return {string}
		 */
		const signatureWarningCode = computed( () => {
			if ( inputTypeChanged.value && outputTypeChanged.value ) {
				return 'wikilambda-publish-input-and-output-changed-impact-prompt';
			} else if ( inputTypeChanged.value ) {
				return 'wikilambda-publish-input-changed-impact-prompt';
			} else if ( outputTypeChanged.value ) {
				return 'wikilambda-publish-output-changed-impact-prompt';
			}
			return '';
		} );
 
		// Language management
		/**
		 * Sets the hasUpdatedLabels flag to true
		 */
		function setHasUpdatedLabels() {
			hasUpdatedLabels.value = true;
		}
 
		/**
		 * Adds a new entry to the functionLanguages local array.
		 * Initiates it to "unset" language, so it just adds an
		 * empty string.
		 */
		function addLanguage() {
			functionLanguages.value.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
		 */
		function setLanguage( payload ) {
			functionLanguages.value[ payload.index ] = payload.language;
		}
 
		// Warnings
		/**
		 * Set warnings when there are changes in the function signature
		 * to announce that
		 */
		function raiseFunctionWarnings() {
			// Only warn of changes if we are editing an existing function
			if ( store.isCreateNewPage ) {
				return;
			}
 
			// If there's changes in the function signature, warn that
			// implementations and testers will be detached
			if ( functionSignatureChanged.value && hasConnectedObjects.value ) {
				store.setError( {
					errorId: Constants.STORED_OBJECTS.MAIN,
					errorType: Constants.ERROR_TYPES.WARNING,
					errorMessageKey: signatureWarningCode.value
				} );
			}
		}
 
		// Watch
		watch( isFunctionDirty, ( newValue ) => {
			if ( newValue === true ) {
				const interactionData = {
					zobjectid: store.getCurrentZObjectId,
					zobjecttype: Constants.Z_FUNCTION,
					zlang: store.getUserLangZid || null
				};
				submitInteraction( 'change', interactionData );
			}
		} );
 
		// Initialization
		/**
		 * Saves the initial values for initialInputTypes and initialOutputType
		 */
		function saveInitialFunctionSignature() {
			initialInputTypes.value = currentInputTypes.value;
			initialOutputType.value = currentOutputType.value;
		}
 
		// Lifecycle
		onMounted( () => {
			// Initialize the local array with the collection of available languages
			// and initialize first label block with user lang if there are none.
			functionLanguages.value = store.getMultilingualDataLanguages.all;
 
			if ( functionLanguages.value.length === 0 ) {
				functionLanguages.value.push( store.getUserLangZid );
			}
 
			// Initialize initial state of inputs and output
			saveInitialFunctionSignature();
 
			// Log an event using Test Kitchen's core interaction events
			const interactionData = {
				zobjecttype: Constants.Z_FUNCTION,
				zobjectid: store.getCurrentZObjectId || null,
				zlang: store.getUserLangZid || null
			};
			const action = store.isCreateNewPage ? 'create' : 'edit';
			submitInteraction( action, interactionData );
		} );
 
		return {
			addLanguage,
			i18n,
			functionLanguages,
			functionSignatureChanged,
			iconLanguage,
			isFunctionDirty,
			raiseFunctionWarnings,
			setHasUpdatedLabels,
			setLanguage
		};
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-function-editor {
	.ext-wikilambda-app-function-editor__action-add-language {
		border-bottom: 1px solid @border-color-subtle;
		padding: @spacing-150 0;
	}
 
	.ext-wikilambda-app-function-editor__footer {
		margin-top: @spacing-150;
	}
}
</style>