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

33.84% Statements 22/65
10.34% Branches 3/29
33.33% Functions 4/12
33.84% Lines 22/65

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              2x 2x 2x                                                   1x     1x           1x 1x   1x       2x                                                                                                                                                             1x             1x                                                                                                                                                                                               6x 6x   6x       6x 5x     5x 5x       6x 6x 6x 6x                                                              
/*!
 * WikiLambda Vue editor: zTypedLists Vuex module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
var Constants = require( '../../Constants.js' ),
	zobjectTreeUtils = require( '../../mixins/zobjectTreeUtils.js' ).methods,
	performFunctionCall = require( '../../mixins/api.js' ).methods.performFunctionCall;
 
function getNewItemParentId( nestedChildren, rootId ) {
	var items = nestedChildren.filter( function ( child ) {
		return child.key === Constants.Z_TYPED_OBJECT_ELEMENT_2;
	} );
 
	if ( items.length === 0 ) {
		return rootId;
	} else {
		return items[ items.length - 1 ].id;
	}
}
 
/**
 * Set one or multiple types for the specificed typed Object
 *
 * @param {Object} context
 * @param {Object} payload
 * @param {number} payload.objectId
 * @param {Array} payload.types
 * @param {string} payload.types.value
 * @param {string} payload.types.argumentZObjectId
 */
function setTypeOfTypedObject( context, payload ) {
 
	payload.types.forEach( function ( type ) {
		var genericValue,
			genericObjectType = // the object can either be nested in a Z_OBJECT_TYPE or directly set.
			context.getters.getNestedZObjectById(
				payload.objectId,
				[ Constants.Z_OBJECT_TYPE, type.argumentZObjectId ]
			) ||
			context.getters.getNestedZObjectById( payload.objectId, [ type.argumentZObjectId ] );
 
		genericObjectType.value = type.value;
		genericValue = genericObjectType;
 
		context.dispatch( 'setZObjectValue', genericValue );
	} );
 
}
module.exports = exports = {
	state: {
	},
	getters: {
	},
	mutations: {
	},
	actions: {
		/**
		 * Add a new item in a typed list. This will create the following format:
		 * {
		 *   "K1": { … structure of the current list item type },
		 *   "K2": {
		 *     "Z1K1": {
		 *       "Z1K1": "Z7",
		 *       "Z7K1": "Z881",
		 *       "Z881K1": "current list item type"
		 *     }
		 *   }
		 * }
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.id
		 * @param {string | Object} payload.value
		 */
		addTypedListMapItem: function ( context, payload ) {
			var nestedChildren = context.getters.getZObjectChildrenByIdRecursively( payload.id ),
				newItemParentId = getNewItemParentId( nestedChildren, payload.id ),
				nextId = zobjectTreeUtils.getNextObjectId( context.rootState.zobjectModule.zobject );
 
			// Create a K1 with the correct type
			context.dispatch( 'addZObject', { key: Constants.Z_TYPED_OBJECT_ELEMENT_1, value: 'object', parent: newItemParentId } );
			if ( payload.value instanceof Object && payload.value.type && payload.value.values ) {
				context.dispatch( 'changeType', { id: nextId, type: payload.value.type, values: payload.value.values, isDeclaration: false } );
			} else {
				context.dispatch( 'changeType', { id: nextId, type: payload.value } );
			}
 
			// Create a K2 and set its type to the current list type
			nextId = zobjectTreeUtils.getNextObjectId( context.rootState.zobjectModule.zobject );
			context.dispatch( 'addZObject', { key: Constants.Z_TYPED_OBJECT_ELEMENT_2, value: 'object', parent: newItemParentId } );
			context.dispatch( 'addZTypedList', {
				id: nextId,
				value: payload.value
			} );
 
		},
		/**
		 * Add a new item in a typed pair. This will create the following format:
		 * {
		 * K1: { scaffolding for pair type 1 },
		 * K2: { scaffolding for pair type 2 }
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.objectId
		 */
		addTypedPairItem: function ( context, payload ) {
			var key1Id = context.getters.getNestedZObjectById(
				payload.objectId, [ Constants.Z_TYPED_OBJECT_ELEMENT_1 ]
			).id;
			context.dispatch( 'changeType', { id: key1Id, type: payload.types[ 0 ].value } );
 
			var key2Id = context.getters.getNestedZObjectById(
				payload.objectId, [ Constants.Z_TYPED_OBJECT_ELEMENT_2 ]
			).id;
			context.dispatch( 'changeType', { id: key2Id, type: payload.types[ 1 ].value } );
 
		},
		/**
		 * Select a type for a typed list
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.objectId
		 * @param {string} payload.type
		 */
		setTypeOfTypedList: function ( context, payload ) {
			var type = {
				objectId: payload.objectId,
				types: [ {
					argumentZObjectId: Constants.Z_TYPED_LIST_TYPE,
					value: payload.type
				} ]
			};
			setTypeOfTypedObject( context, type );
		},
		/**
		 * Select types for a typed pair
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.objectId
		 * @param {Array} payload.types
		 */
		setTypeOfTypedPair: function ( context, payload ) {
			if ( payload.types.length < 2 ) {
				return;
			}
 
			// We set the scaffolding to defien the pair type
			var types = {
				objectId: payload.objectId,
				types: [ {
					argumentZObjectId: Constants.Z_TYPED_PAIR_TYPE1,
					value: payload.types[ 0 ]
				}, {
					argumentZObjectId: Constants.Z_TYPED_PAIR_TYPE2,
					value: payload.types[ 1 ]
				} ]
			};
			setTypeOfTypedObject( context, types );
 
			// When both types are set, we initialize the newly created pair creating instance of its "types"
			if ( payload.types[ 0 ] && payload.types[ 1 ] ) {
				context.dispatch( 'addTypedPairItem', types );
			}
 
		},
		/**
		 * Select types for a typed map. This will complete the following action:
		 * - Set the types in the Map declaration
		 * - Create a Pair declaration with the following types
		 * - initialize a typed list with a type of pair
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {number} payload.objectId
		 * @param {Array} payload.types
		 */
		setTypeOfTypedMap: function ( context, payload ) {
			if ( payload.types.length < 2 ) {
				return;
			}
 
			// We assign the types to the Map declaration
			var mapObjectElementId = null,
				types = {
					objectId: payload.objectId,
					types: [ {
						argumentZObjectId: Constants.Z_TYPED_MAP_TYPE1,
						value: payload.types[ 0 ]
					}, {
						argumentZObjectId: Constants.Z_TYPED_MAP_TYPE2,
						value: payload.types[ 1 ]
					} ]
				};
			setTypeOfTypedObject( context, types );
 
			// We return unless both types are defined
			if ( !payload.types[ 0 ] || !payload.types[ 1 ] ) {
				return;
			}
			// We create a K1 (Z_TYPED_OBJECT_ELEMENT_1) in the MAP object
			mapObjectElementId = zobjectTreeUtils.getNextObjectId( context.rootState.zobjectModule.zobject );
			context.dispatch( 'addZObject', { key: Constants.Z_TYPED_OBJECT_ELEMENT_1, value: 'object', parent: payload.objectId } );
 
			// We create list of type pair
			var createTypedListPayload = {
				id: mapObjectElementId,
				type: Constants.Z_TYPED_LIST,
				value: {
					type: Constants.Z_TYPED_PAIR,
					values: payload.types
				}
			};
			context.dispatch( 'changeType', createTypedListPayload );
 
			// context.dispatch( 'addTypedListMapItem', createTypedListPayload );
 
		},
		/**
		 * Remove an item from the generic List. Due to the structure of the list.
		 * this required object to be shift accross.
		 *
		 * @param {Object} context
		 * @param {Object} item
		 * @param {number} item.id
		 * @param {number} item.parent
		 */
		removeTypedListItem: function ( context, item ) {
			var currentListItemParentId = context.getters.getZObjectById( item.parent ).id;
			var currentItemK2 = context.getters.getNestedZObjectById( item.parent,
				[ Constants.Z_TYPED_OBJECT_ELEMENT_2 ] );
			var currentItemNestedElement = context.getters.getNestedZObjectById( item.parent,
				[ Constants.Z_TYPED_OBJECT_ELEMENT_2, Constants.Z_TYPED_OBJECT_ELEMENT_1 ] );
 
			// If nested values are available, shift them one level up by changing its parent
			if ( currentItemNestedElement ) {
				var nestedItemNestedList = context.getters.getNestedZObjectById( item.parent,
					[ Constants.Z_TYPED_OBJECT_ELEMENT_2, Constants.Z_TYPED_OBJECT_ELEMENT_2 ] );
 
				context.dispatch( 'setZObjectParent', { id: currentItemNestedElement.id, parent: currentListItemParentId } );
				context.dispatch( 'setZObjectParent', { id: nestedItemNestedList.id, parent: currentListItemParentId } );
			}
 
			// remove the K1 and K2 values and its children
			context.dispatch( 'removeZObjectChildren', item.id );
			context.dispatch( 'removeZObject', item.id );
			context.dispatch( 'removeZObjectChildren', currentItemK2.id );
			context.dispatch( 'removeZObject', currentItemK2.id );
		},
		/**
		 * Perform a function call to retrieve the return typed of a "Function to Type"
		 *
		 * @param {Object} context
		 * @param {Object} payload
		 * @param {string} payload.objectId
		 * @param {string} payload.dynamicZKey
		 *
		 * @return {Promise}
		 */
		retriveTypeOfFunctionToType: function ( context, payload ) {
			var ZfunctionCallObject = context.getters.getZObjectAsJsonById( payload.objectId );
 
			if ( ZfunctionCallObject && ZfunctionCallObject[ Constants.Z_OBJECT_TYPE ] ) {
				return performFunctionCall( ZfunctionCallObject[ Constants.Z_OBJECT_TYPE ] ).then( function ( data ) {
 
					var canonicalZObject = data.response;
					if ( canonicalZObject[ Constants.Z_RESPONSEENVELOPE_VALUE ] ) {
						context.commit( 'addZKeyInfo', {
							zid: payload.dynamicZKey,
							info: canonicalZObject[ Constants.Z_RESPONSEENVELOPE_VALUE ]
						} );
						return true;
					}
				} );
			}
		}
	}
};