All files / ext.wikilambda.app/store/stores listItems.js

100% Statements 119/119
100% Branches 14/14
100% Functions 6/6
100% Lines 119/119

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 120103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 18x 103x 103x 103x 103x 103x 103x 103x 103x 103x 51x 51x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 5x 103x 103x 103x 103x 103x 103x 103x 103x 3x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 103x 8x 8x 3x 3x 5x 5x 5x 5x 5x 8x 1x 1x 1x 1x 4x 4x 4x 4x 4x 12x 12x 10x 10x 4x 4x 4x 4x 4x 4x 8x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 8x 103x 103x  
/*!
 * WikiLambda Vue editor: Handle item actions on the existing typed lists. (Pinia)
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' ); // Import Constants
const { getZObjectType } = require( '../../utils/zobjectUtils.js' );
const { typeToString } = require( '../../utils/typeUtils.js' );
 
module.exports = {
	state: {
		invalidListItems: {}
	},
 
	getters: {
		/**
		 * Get the invalid list items
		 *
		 * @param {Object} state
		 * @return {Object}
		 */
		getInvalidListItems: function ( state ) {
			return state.invalidListItems;
		},
 
		/**
		 * Check if there are any invalid list items
		 *
		 * @param {Object} state
		 * @return {boolean}
		 */
		hasInvalidListItems: function ( state ) {
			return Object.keys( state.invalidListItems ).length > 0;
		}
	},
 
	actions: {
		/**
		 * Set invalid list items for a particular list ID
		 *
		 * @param {Object} payload
		 * @param {string} payload.listId
		 * @param {Array<string>} payload.listItems
		 */
		setInvalidListItems: function ( payload ) {
			this.invalidListItems[ payload.listId ] = payload.listItems;
		},
 
		/**
		 * Clear invalid list items for a particular list ID
		 *
		 * @param {string} listId
		 */
		clearInvalidListItems: function ( listId ) {
			this.invalidListItems[ listId ] = [];
		},
 
		/**
		 * Handles the logic for when the type of a list changes.
		 * Warns the user that this will delete list items that are incompatible with the new type.
		 * - If the new type is Z1/Object, clears all errors and invalid items.
		 * - If the new type is different and there are existing list items:
		 *   - Sets a warning error to notify the user about the type change.
		 *   - Marks list items with incompatible types as invalid.
		 *
		 * @param {Object} payload
		 * @param {string} payload.keyPath
		 * @param {Array} payload.objectValue
		 * @param {Object|string} payload.newType
		 */
		handleListTypeChange: function ( { keyPath, objectValue, newType } ) {
			// objectValue must be an array with items (other than the benjamin)
			if ( !objectValue || !Array.isArray( objectValue ) || objectValue.length <= 1 ) {
				return;
			}
 
			const anyType = newType === Constants.Z_OBJECT;
			const itemIndexes = Object.keys( objectValue ).slice( 1 ).map( Number );
 
			// If the type was changed to Object/Z1, clear errors and invalid items for the listId (its keyPath)
			if ( anyType ) {
				this.clearErrors( keyPath );
				this.clearInvalidListItems( keyPath );
				return;
			}
 
			// Set invalid list items
			const newTypeString = typeToString( newType );
			const listItems = [];
			itemIndexes.forEach( ( index ) => {
				const itemTypeString = typeToString( getZObjectType( objectValue[ index ] ) );
				if ( newTypeString !== itemTypeString ) {
					listItems.push( index );
				}
			} );
 
			// If the type was changed to a different type and there are list items, show a warning
			if (
				!this.hasErrorByKey( keyPath, 'wikilambda-list-type-change-warning' ) &&
				listItems.length > 0
			) {
				// If the typed list type changed error has not been set, set it
				this.setError( {
					errorId: Constants.STORED_OBJECTS.MAIN,
					errorMessageKey: 'wikilambda-list-type-change-warning',
					errorType: Constants.ERROR_TYPES.WARNING
				} );
			}
 
			this.setInvalidListItems( {
				listId: keyPath,
				listItems
			} );
		}
	}
};