All files / ext.wikilambda.edit/store/modules zfunction.js

100% Statements 478/478
100% Branches 71/71
100% Functions 25/25
100% Lines 478/478

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 4797x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 115x 115x 115x 115x 115x 194x 194x 194x 194x 194x 13x 13x 194x 194x 194x 194x 115x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 86x 86x 86x 86x 86x 86x 86x 86x 135x 135x 135x 135x 135x 135x 135x 135x 135x 86x 86x 86x 86x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 24x 24x 24x 24x 24x 104x 104x 104x 104x 104x 24x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 15x 15x 15x 15x 15x 97x 97x 97x 15x 7x 7x 7x 7x 7x 7x 7x 7x 7x 8x 8x 8x 8x 8x 44x 44x 44x 8x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 25x 25x 25x 25x 25x 25x 110x 110x 110x 110x 110x 110x 110x 110x 97x 97x 110x 110x 110x 25x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 14x 14x 14x 14x 14x 14x 1x 1x 13x 13x 6x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 15x 15x 15x 15x 15x 15x 1x 1x 14x 14x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 11x 11x 11x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 3x 3x 20x 20x 20x 20x 20x 11x 7x 7x 7x 7x 7x 7x 7x 7x 7x 12x 12x 12x 12x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 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 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 4x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 7x  
/*!
 * WikiLambda Vue editor: Function Editor and Viewer Vuex module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
const Constants = require( '../../Constants.js' ),
	createConnectedItemsChangesSummaryMessage = require( '../../mixins/utilsMixins.js' ).methods.createConnectedItemsChangesSummaryMessage,
	apiUtils = require( '../../mixins/api.js' ).methods;
 
module.exports = exports = {
	getters: {
		/**
		 * Returns the function Zid of a given function given
		 * the function rowId. If not set, returns undefined
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionIdentity: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {string | undefined}
			 */
			function findIdentity( rowId = 0 ) {
				const functionZid = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_IDENTITY
				], rowId );
				return functionZid ?
					getters.getZReferenceTerminalValue( functionZid.id ) :
					undefined;
			}
			return findIdentity;
		},
		/**
		 * Returns the list of input rows for the function given
		 * given the root object rowId. If no rowId given, starts
		 * from 0, which is safe to use.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionInputs: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			function findZFunctionInputs( rowId = 0 ) {
				const inputsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_ARGUMENTS
				], rowId );
				if ( inputsRow === undefined ) {
					return [];
				}
				const inputs = getters.getChildrenByParentRowId( inputsRow.id );
				// Remove benjamin type item
				return inputs.slice( 1 );
			}
			return findZFunctionInputs;
		},
		/**
		 * Returns the array of language Zids that are present
		 * in a function's input labels. If no arguments or no
		 * labels, returns an empty array.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionInputLangs: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			function findInputLangs( rowId = 0 ) {
				let languages = [];
				const inputs = getters.getZFunctionInputs( rowId );
				for ( const inputRow of inputs ) {
					const inputLabelsRow = getters.getRowByKeyPath( [
						Constants.Z_ARGUMENT_LABEL,
						Constants.Z_MULTILINGUALSTRING_VALUE
					], inputRow.id );
					const inputLangs = inputLabelsRow ?
						getters.getZMultilingualLanguageList( inputLabelsRow.id ) :
						[];
					languages = languages.concat( inputLangs );
				}
				return languages;
			}
 
			return findInputLangs;
		},
		/**
		 * Returns the output type row for the function given
		 * given the root object rowId. If no rowId given, starts
		 * from 0, which is safe to use.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZFunctionOutput: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Object|undefined}
			 */
			function findZFunctionOutput( rowId = 0 ) {
				return getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_RETURN_TYPE
				], rowId );
			}
			return findZFunctionOutput;
		},
		/**
		 * Returns the input type row of an input or
		 * argument declaration, given the rowId of the
		 * argument/Z17 object.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentTypeRowId: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {number|undefined}
			 */
			function findArgumentType( rowId ) {
				const argType = getters.getRowByKeyPath( [ Constants.Z_ARGUMENT_TYPE ], rowId );
				return argType ? argType.id : undefined;
			}
			return findArgumentType;
		},
		/**
		 * Returns the terminal string value of the input key
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentKey: function ( _state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {string|undefined}
			 */
			function findArgumentKey( rowId ) {
				const argKey = getters.getRowByKeyPath( [ Constants.Z_ARGUMENT_KEY ], rowId );
				return argKey ? getters.getZStringTerminalValue( argKey.id ) : undefined;
			}
			return findArgumentKey;
		},
		/**
		 * Returns the input label row of an input or
		 * argument declaration, given the rowId of the
		 * argument/Z17 object and the language Zid.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getZArgumentLabelForLanguage: function ( _state, getters ) {
			/**
			 * @param {string} rowId
			 * @param {string} lang
			 * @return {Object|undefined}
			 */
			function findArgumentLabel( rowId, lang ) {
				const argLabelsRow = getters.getRowByKeyPath( [
					Constants.Z_ARGUMENT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				], rowId );
				const argLabels = argLabelsRow ?
					getters.getChildrenByParentRowId( argLabelsRow.id ).slice( 1 ) :
					[];
				const argLabel = argLabels.find( ( monolingual ) => {
					const langZid = getters.getZMonolingualLangValue( monolingual.id );
					return langZid === lang;
				} );
				return argLabel;
			}
			return findArgumentLabel;
		},
		/**
		 * Returns ZIDs for testers connected to the root function.
		 * Note that this returns an array of only items, without the type from index 0.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getConnectedTests: function ( state, getters ) {
			/**
			 * @param {number} rowId
			 * @return {Array}
			 */
			return function ( rowId = 0 ) {
				const testsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_TESTERS
				], rowId );
 
				if ( !testsRow ) {
					return [];
				}
				const childRows = getters.getChildrenByParentRowId( testsRow.id ).slice( 1 );
				return childRows.map( ( row ) => getters.getZReferenceTerminalValue( row.id ) );
			};
		},
		/**
		 * Returns ZIDs for implementations connected to the root function.
		 * Note that this returns an array of only items, without the type from index 0.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {Function}
		 */
		getConnectedImplementations: function ( state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {Array}
			 */
			return function ( rowId = 0 ) {
				const implementationsRow = getters.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_IMPLEMENTATIONS
				], rowId );
 
				if ( !implementationsRow ) {
					return [];
				}
				const childRows = getters.getChildrenByParentRowId( implementationsRow.id ).slice( 1 );
				return childRows.map( ( row ) => getters.getZReferenceTerminalValue( row.id ) );
			};
		},
		/**
		 * Returns the array of input-related field ids that are invalid.
		 * Ignores those inputs that have no label and fully empty type
		 * because it will be deleted before submission.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Array}
		 */
		getInvalidInputFields: function ( _state, getters ) {
			const inputs = getters.getZFunctionInputs();
			let invalidRowIds = [];
			for ( const inputRow of inputs ) {
				// Get the validity state of all the type fields
				const inputTypeRow = getters.getRowByKeyPath( [ Constants.Z_ARGUMENT_TYPE ], inputRow.id );
				const inputTypeFields = getters.validateGenericType( inputTypeRow.id );
 
				// Get the values of the input labels
				const inputLabelsRow = getters.getRowByKeyPath( [
					Constants.Z_ARGUMENT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				], inputRow.id );
				const inputLabelRows = getters.getChildrenByParentRowId( inputLabelsRow.id ).slice( 1 );
				const inputLabelValues = inputLabelRows
					.map( ( row ) => getters.getZMonolingualTextValue( row.id ) )
					.filter( ( text ) => !!text );
 
				// If type value is empty and fields are empty, ignore this input:
				// because it's totally empty, the input will be erased before submission.
				const inputTypeIsEmpty = ( inputTypeFields.filter( ( e ) => e.isValid ).length === 0 );
				if ( inputTypeIsEmpty && inputLabelValues.length === 0 ) {
					continue;
				}
 
				// Return errors to report
				const invalidInputRowIds = inputTypeFields.filter( ( e ) => !e.isValid ).map( ( e ) => e.rowId );
				invalidRowIds = invalidRowIds.concat( invalidInputRowIds );
			}
			return invalidRowIds;
		},
		/**
		 * Returns the array of output-related field ids that are invalid.
		 *
		 * @param {Object} _state
		 * @param {Object} getters
		 * @return {Array}
		 */
		getInvalidOutputFields: function ( _state, getters ) {
			const outputTypeRow = getters.getZFunctionOutput();
			const outputTypeFields = getters.validateGenericType( outputTypeRow.id );
			return outputTypeFields.filter( ( e ) => !e.isValid ).map( ( e ) => e.rowId );
		}
	},
	actions: {
 
		/**
		 * Adds the given tests to the current function's list of
		 * of connected tests and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the tests to connect
		 * @return {Promise}
		 */
		connectTests: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_TESTERS
			], payload.rowId );
 
			return context
				.dispatch( 'pushValuesToList', { rowId: listRow.id, values: payload.zids } )
				.then( () => context.dispatch(
					'submitZObject', {
						summary: createConnectedItemsChangesSummaryMessage( 'wikilambda-updated-testers-approved-summary', payload.zids )
					} ).catch( ( e ) => {
					// Reset old ZObject if something failed
					context.commit( 'setZObject', zobjectCopy );
					throw e;
				} ) );
		},
 
		/**
		 * Adds the given implementations to the current function's list of
		 * of connected implementations and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the implementations to connect
		 * @return {Promise}
		 */
		connectImplementations: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_IMPLEMENTATIONS
			], payload.rowId );
 
			return context
				.dispatch( 'pushValuesToList', { rowId: listRow.id, values: payload.zids } )
				.then( () => context
					.dispatch( 'submitZObject', {
						summary: createConnectedItemsChangesSummaryMessage( 'wikilambda-updated-implementations-approved-summary', payload.zids )
					} )
					.then( () => context.dispatch( 'updateStoredObject' ) )
					.catch( ( e ) => {
						// Reset old ZObject if something failed
						context.commit( 'setZObject', zobjectCopy );
						throw e;
					} ) );
		},
		/**
		 * Removes the given tests from the the current function's list of
		 * of connected tests and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the tests to disconnect
		 * @return {Promise}
		 */
		disconnectTests: function ( context, payload ) {
			// Save a copy of the pre-submission ZObject in case the submission returns an error
			const zobjectCopy = context.getters.getZObjectCopy;
 
			// Find the item rows to delete from the list
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_TESTERS
			], payload.rowId );
			const listItems = context.getters.getChildrenByParentRowId( listRow.id ).slice( 1 );
			const deleteRows = listItems.filter( ( row ) => {
				const reference = context.getters.getZReferenceTerminalValue( row.id );
				return payload.zids.includes( reference );
			} );
 
			// Delete items from the list and recalculate the list keys
			for ( const row of deleteRows ) {
				context.dispatch( 'removeRowChildren', { rowId: row.id, removeParent: true } );
			}
			context.dispatch( 'recalculateTypedListKeys', listRow.id );
 
			return context.dispatch(
				'submitZObject', {
					summary: createConnectedItemsChangesSummaryMessage( 'wikilambda-updated-testers-deactivated-summary', payload.zids )
				} ).catch( ( e ) => {
				// Reset old ZObject if something failed
				context.commit( 'setZObject', zobjectCopy );
				throw e;
			} );
		},
		/**
		 * Removes the given implementations from the the current function's list of
		 * of connected implementations and persists the change.
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {Object} payload.rowId - the rowId of the function
		 * @param {Array} payload.zids - the Zids of the implementations to disconnect
		 * @return {Promise}
		 */
		disconnectImplementations: function ( context, payload ) {
			const zobjectCopy = context.getters.getZObjectCopy;
 
			// Find the item rows to delete from the list
			const listRow = context.getters.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_IMPLEMENTATIONS
			], payload.rowId );
			const listItems = context.getters.getChildrenByParentRowId( listRow.id ).slice( 1 );
			const deleteRows = listItems.filter( ( row ) => {
				const reference = context.getters.getZReferenceTerminalValue( row.id );
				return payload.zids.includes( reference );
			} );
 
			// Delete items from the list and recalculate the list keys
			for ( const row of deleteRows ) {
				context.dispatch( 'removeRowChildren', { rowId: row.id, removeParent: true } );
			}
			context.dispatch( 'recalculateTypedListKeys', listRow.id );
 
			return context.dispatch( 'submitZObject',
				{
					summary: createConnectedItemsChangesSummaryMessage( 'wikilambda-updated-implementations-deactivated-summary', payload.zids )
				}
			)
				.then( () => context.dispatch( 'updateStoredObject' ) )
				.catch( ( e ) => {
					// Reset old ZObject if something failed
					context.commit( 'setZObject', zobjectCopy );
					throw e;
				} );
		},
		/**
		 * Triggers the fetch of all (connected and disconnected) tests for the i
		 * given function Zid. Also fetches other relevant object Zids.
		 *
		 * @param {Object} context
		 * @param {string} functionZid
		 * @return {Promise}
		 */
		fetchTests: function ( context, functionZid ) {
			return apiUtils.fetchFunctionObjects( {
				functionZid,
				type: Constants.Z_TESTER
			} ).then( ( items ) => {
				const zids = items.map( ( item ) => item.zid );
				context.dispatch( 'fetchZids', { zids } );
				return zids;
			} );
		},
		/**
		 * Triggers the fetch of all (connected and disconnected) implementations for the given
		 * functionZid. Also fetches other relevant object Zids.
		 *
		 * @param {Object} context
		 * @param {string} functionZid
		 * @return {Promise}
		 */
		fetchImplementations: function ( context, functionZid ) {
			return apiUtils.fetchFunctionObjects( {
				functionZid,
				type: Constants.Z_IMPLEMENTATION
			} ).then( ( items ) => {
				const zids = items.map( ( item ) => item.zid );
				context.dispatch( 'fetchZids', { zids } );
				return zids;
			} );
		}
	}
};