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

69.76% Statements 30/43
50% Branches 12/24
54.54% Functions 6/11
69.76% Lines 30/43

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              2x 2x 2x   2x                 2x                                                                                   2x               1x                       1x   1x     1x   1x       1x           1x     1x       1x         1x   1x 2x     2x   2x 2x             2x         2x       2x 2x     2x 2x   2x       2x                     1x                  
/*!
 * WikiLambda Vue editor: zKeys Vuex module
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
var Vue = require( 'vue' ),
	Constants = require( '../../Constants.js' ),
	canonicalize = require( '../../mixins/schemata.js' ).methods.canonicalizeZObject;
 
module.exports = exports = {
	state: {
		/**
		 * Collection of arguments
		 */
		zArguments: {}
	},
	getters: {
		getZarguments: function ( state ) {
			return state.zArguments;
		},
		// TODO(T299031): cleanup this code to be more performant
		/**
		 * Returns an array of objects with key and type of the available arguments.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 * @param {Object} rootState
		 * @param {Object} rootGetters
		 * @return {Array}
		 */
		getZargumentsArray: function ( state, getters, rootState, rootGetters ) {
			/**
			 * @param {boolean} zlang
			 * @return {Array}
			 */
			return function ( zlang ) {
				var lang = zlang || rootGetters.getCurrentZLanguage;
				return Object.keys( getters.getZarguments )
					.map( function ( key ) {
						return {
							zid: getters.getZarguments[ key ].zid,
							type: getters.getZarguments[ key ].type,
							label: ( getters.getZarguments[ key ].labels.filter( function ( label ) {
								return label.lang === lang;
							} )[ 0 ] || getters.getZarguments[ key ].labels[ 0 ] ).label
						};
					} );
			};
		}
		// TODO(T299031): cleanup this code to be more performant
	},
	mutations: {
		/**
		 * Add a specific argument to the zArgument object.
		 *
		 * @param {Object} state
		 * @param {Object} payload
		 * @param {string} payload.zid
		 */
		addZArgumentInfo: function ( state, payload ) {
			Vue.set( state.zArguments, payload.zid, payload );
		},
		/**
		 * Reset the zArguments object in the state
		 *
		 * @param {Object} state
		 */
		resetZArgumentInfo: function ( state ) {
			state.zArguments = {};
		}
	},
	actions: {
		/**
		 * reset and repopulate the zArguments in the store. This method also
		 * fetches any missing types.
		 *
		 * @param {Object} context
		 * @param {Object} zFunctionId
		 */
		setAvailableZArguments: function ( context, zFunctionId ) {
			context.commit( 'resetZArgumentInfo' );
 
			if ( context.getters.getCurrentZObjectId === zFunctionId ||
					context.getters.getZkeys[ zFunctionId ] ) {
				var zobject,
					missingTypes = [];
 
				Iif ( !context.getters.getZObjectAsJson ) {
					return;
				}
 
				Iif ( context.getters.getCurrentZObjectId === zFunctionId ) {
					zobject = canonicalize(
						JSON.parse( JSON.stringify( context.getters.getZObjectAsJson ) )
					);
				} else {
					// "JSON.parse( JSON.stringify() )" is to prevent zobject from updating Zkeys state when reassigned.
					zobject = JSON.parse( JSON.stringify( context.getters.getZkeys[ zFunctionId ] ) );
				}
 
				Iif ( !zobject[ Constants.Z_PERSISTENTOBJECT_VALUE ] ) {
					return;
				}
 
				var zArguments = zobject[
					Constants.Z_PERSISTENTOBJECT_VALUE ][
					Constants.Z_FUNCTION_ARGUMENTS ];
 
				// Remove argument type
				zArguments.shift();
 
				zArguments.forEach( function ( argument ) {
					var argumentLabels = argument[ Constants.Z_ARGUMENT_LABEL ][
						Constants.Z_MULTILINGUALSTRING_VALUE ];
					// Remove argument label type
					argumentLabels.shift();
 
					var labels = argumentLabels.map( function ( label ) {
							return {
								lang: label[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ][ Constants.Z_STRING_VALUE ] ||
									label[ Constants.Z_MONOLINGUALSTRING_LANGUAGE ],
								label: label[ Constants.Z_MONOLINGUALSTRING_VALUE ],
								key: label[ Constants.Z_MONOLINGUALSTRING_VALUE ] + ': '
							};
						} ),
						zid = argument[ Constants.Z_ARGUMENT_KEY ],
						typeZid,
						typeLabel;
 
					// We can either return an object or a straight string.
					Iif ( typeof argument[ Constants.Z_ARGUMENT_TYPE ] === 'object' ) {
						typeZid = argument[ Constants.Z_ARGUMENT_TYPE ][ Constants.Z_OBJECT_TYPE ];
						typeLabel = context.getters.getZkeyLabels[ typeZid ];
					} else {
						typeZid = argument[ Constants.Z_ARGUMENT_TYPE ];
						typeLabel = context.getters.getZkeyLabels[ typeZid ];
					}
 
					if ( typeof zid === 'object' ) {
						zid = zid[ Constants.Z_STRING_VALUE ];
					}
					Iif ( !typeLabel ) {
						missingTypes.push( argument[ Constants.Z_ARGUMENT_TYPE ] );
					}
 
					context.commit( 'addZArgumentInfo', {
						zid: zid,
						type: {
							label: typeLabel,
							zid: typeZid
						},
						labels: labels
					} );
				} );
 
				// If any argument types are not available, fetch them and rerun the function
				Iif ( missingTypes.filter( Boolean ).length ) {
					context.dispatch( 'fetchZKeys', { zids: missingTypes } ).then( function () {
						context.dispatch( 'setAvailableZArguments', zFunctionId );
					} );
				}
			}
		}
	}
};