All files / ext.wikilambda.edit/store/modules/zobject currentZObject.js

100% Statements 67/67
90.62% Branches 29/32
100% Functions 25/25
100% Lines 62/62

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            21x 21x   21x                           16x                     24x                     7x                                     26x 4x           4x   26x                 1x   1x 1x 1x           1x 1x 1x           1x 1x 1x                                 73x                   28x                                   62x   62x 1x     61x 20x   41x   18x                   2x                       15x                 147x                     37x                         15x                                         60x 60x       60x         60x 16x 16x 17x     60x   46x                       13x 13x 13x   24x 24x     24x       24x 24x 15x 15x       28x 24x 3x       25x 21x   13x                   14x 14x 24x        
/*!
 * WikiLambda Vuex code to manipulate the current ZObject.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
const Constants = require( '../../../Constants.js' );
const eventLogUtils = require( '../../../mixins/eventLogUtils.js' );
 
module.exports = exports = {
	state: {
		currentZid: Constants.NEW_ZID_PLACEHOLDER,
		dirty: false,
		multilingualDataCopy: null
	},
	mutations: {
		/**
		 * Set the value of the current Zid
		 *
		 * @param {Object} state
		 * @param {string} currentZid
		 */
		setCurrentZid: function ( state, currentZid ) {
			state.currentZid = currentZid;
		},
		/**
		 * Sets the value of the isDirty flag,
		 * which is true if there's been any changes
		 * in the page that will need saving.
		 *
		 * @param {Object} state
		 * @param {boolean} value
		 */
		setDirty: function ( state, value ) {
			state.dirty = value;
		},
		/**
		 * Save initial multilingual data values
		 * so that About widget knows how to reset to original
		 * state in the case of a publish cancelation action.
		 *
		 * @param {Object} state
		 * @param {Object} zobject
		 */
		saveMultilingualDataCopy: function ( state, zobject ) {
			state.multilingualDataCopy = {
				names: zobject[ Constants.Z_PERSISTENTOBJECT_LABEL ],
				descriptions: zobject[ Constants.Z_PERSISTENTOBJECT_DESCRIPTION ],
				aliases: zobject[ Constants.Z_PERSISTENTOBJECT_ALIASES ]
			};
		}
	},
	actions: {
		/**
		 * Sets the value of the isDirty flag,
		 * which is true if there's been any changes
		 * in the page that will need saving.
		 *
		 * @param {Object} context
		 * @param {boolean} value
		 */
		setDirty: function ( context, value = true ) {
			// T350497 Update Wikilambda instrument to use core interaction events
			// sending the 'change' event for the first change
			if ( value === true && !context.getters.isDirty ) {
				const interactionData = {
					zobjectid: context.getters.getCurrentZObjectId,
					zobjecttype: context.getters.getCurrentZObjectType || null,
					implementationtype: context.getters.getCurrentZImplementationType || null,
					zlang: context.getters.getUserLangZid || null
				};
				eventLogUtils.methods.submitInteraction( 'change', interactionData );
			}
			context.commit( 'setDirty', value );
		},
		/**
		 * Resets the initial state of the multilingual
		 * data of the current object.
		 *
		 * @param {Object} context
		 */
		resetMultilingualData: function ( context ) {
			const initialData = context.getters.getMultilingualDataCopy;
 
			const nameRow = context.getters.getRowByKeyPath( [ Constants.Z_PERSISTENTOBJECT_LABEL ], 0 );
			Eif ( nameRow ) {
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: nameRow.id,
					value: initialData.names
				} );
			}
 
			const descriptionRow = context.getters.getRowByKeyPath( [ Constants.Z_PERSISTENTOBJECT_DESCRIPTION ], 0 );
			Eif ( descriptionRow ) {
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: descriptionRow.id,
					value: initialData.descriptions
				} );
			}
 
			const aliasesRow = context.getters.getRowByKeyPath( [ Constants.Z_PERSISTENTOBJECT_ALIASES ], 0 );
			Eif ( aliasesRow ) {
				context.dispatch( 'injectZObjectFromRowId', {
					rowId: aliasesRow.id,
					value: initialData.aliases
				} );
			}
		}
	},
	getters: {
		/**
		 * Return whether the page is creating a new ZObject
		 * or is instead editing an already persisted one.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {boolean} is new ZObject page
		 */
		isNewZObject: function ( state, getters ) {
			return getters.getCurrentZObjectId === Constants.NEW_ZID_PLACEHOLDER;
		},
		/**
		 * Returns whether the page has had any
		 * changes that need saving.
		 *
		 * @param {Object} state
		 * @return {boolean}
		 */
		isDirty: function ( state ) {
			return state.dirty;
		},
		/**
		 * Whether the given rowId is part of the main
		 * page object or is a detached object. If the
		 * oldest ancestor is row Id 0, then this is the
		 * main object.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {boolean}
		 */
		isMainObject: function ( state, getters ) {
			/**
			 * @param {string} rowId
			 * @return {boolean}
			 */
			function findOldestAncestor( rowId ) {
				const row = getters.getRowById( rowId );
				// Row doesn't exist, return false
				if ( row === undefined ) {
					return false;
				}
				// Row is oldest ancestor, return true if id is 0
				if ( row.parent === undefined ) {
					return ( row.id === 0 );
				}
				return findOldestAncestor( row.parent );
			}
			return findOldestAncestor;
		},
		/**
		 * Returns the multilingual data initial copy
		 * saved on initialization
		 *
		 * @param {Object} state
		 * @return {Object}
		 */
		getMultilingualDataCopy: function ( state ) {
			return state.multilingualDataCopy;
		},
		/**
		 * Return the complete zObject as a JSON
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @param {Object} rootState
		 * @param {Object} rootGetters
		 * @return {Array} zObjectJson
		 */
		getZObjectAsJson: function ( state, getters, rootState, rootGetters ) {
			return rootGetters.getZObjectAsJsonById( 0, rootState.zobjectModule.zobject[ 0 ].isArray() );
		},
		/**
		 * Return the root ZObjectId, equivalend to the Z_REFERENCE_ID of Z_PERSISTENTOBJECT_ID
		 *
		 * @param {Object} state
		 * @return {string} currentZObjectId
		 */
		getCurrentZObjectId: function ( state ) {
			return state.currentZid || Constants.NEW_ZID_PLACEHOLDER;
		},
		/**
		 * Return the type of the root ZObject or undefined
		 * if it's still not set
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {string | Object | undefined} current ZObject Type
		 */
		getCurrentZObjectType: function ( state, getters ) {
			return getters.getZObjectTypeByRowId(
				getters.getZPersistentContentRowId()
			);
		},
		/**
		 * Return the key indicating the content type of the current implementation:
		 * 'Z14K2' (composition), 'Z14K3' (code) or 'Z14K4' (builtin).
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {string | undefined} currentZImplementationContentType
		 */
		getCurrentZImplementationType: function ( state, getters ) {
			return getters.getZImplementationContentType(
				getters.getZPersistentContentRowId()
			);
		},
		/**
		 * Recursively waks a nested generic type and returns
		 * the field IDs and whether they are valid or not.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @return {Function}
		 */
		validateGenericType: function ( state, getters ) {
			/**
			 * @param {number} rowId
			 * @param {Object} fields
			 * @return {Object} fields
			 */
			function validate( rowId, fields = [] ) {
				// There's no need to convert to string as the
				// possible options will be either Z7 or Z9
				const mode = getters.getZObjectTypeByRowId( rowId );
				const value = ( mode === Constants.Z_REFERENCE ) ?
					getters.getZReferenceTerminalValue( rowId ) :
					getters.getZFunctionCallFunctionId( rowId );
 
				fields.push( {
					rowId,
					isValid: !!value
				} );
 
				if ( mode === Constants.Z_FUNCTION_CALL ) {
					const args = getters.getZFunctionCallArguments( rowId );
					for ( const arg of args ) {
						getters.validateGenericType( arg.id, fields );
					}
				}
				return fields;
			}
			return validate;
		},
		/**
		 * 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}
		 */
		currentZFunctionInvalidInputs: 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}
		 */
		currentZFunctionInvalidOutput: function ( _state, getters ) {
			const outputTypeRow = getters.getZFunctionOutput();
			const outputTypeFields = getters.validateGenericType( outputTypeRow.id );
			return outputTypeFields.filter( ( e ) => !e.isValid ).map( ( e ) => e.rowId );
		}
	}
};