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

100% Statements 34/34
100% Branches 17/17
100% Functions 12/12
100% Lines 34/34

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              3x 3x   3x                       14x 14x       14x   84x 14x         70x 10x       60x 12x       48x     16x   8x     24x                       2x 2x 2x 12x 1x     2x                           2x 2x   2x 12x 1x       2x                     2x 2x 2x 12x 1x       2x          
/*!
 * WikiLambda Vue editor: Getters for handling ZObject modes in the UI
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
 
var Constants = require( '../../Constants.js' ),
	modes = Constants.Z_MODE_SELECTOR_MODES;
 
module.exports = exports = {
	getters: {
		/**
		 * Getters that retrieve possible display mode for a zObject.
		 * This is used by the mode selector.
		 *
		 * @param {Object} state
		 * @param {Object} getters
		 *
		 * @return {Array} modes
		 */
		getAllModes: function ( state, getters ) {
			return function ( payload ) {
				var typeIsItsOwnIdentity = payload.parentType === payload.literalType;
				// If literal and parents are the same, it means it is its own identity,
				// or if the type is Reference or FunctionCall
				// mode selector it does not have a generic or literal view
				return modes.filter( function ( mode ) {
					// Do not display the argument reference mode if not allowed
					if ( mode.key === Constants.Z_KEY_MODES.ARGUMENT_REF ) {
						return payload.allowZArgumentRefMode;
					}
 
					// If the parent and literal types are the same, do not display
					// reference or generic
					if ( typeIsItsOwnIdentity ) {
						return mode.type !== null;
					}
 
					// Do not display the JSON mode if not in expert mode
					if ( mode.key === Constants.Z_KEY_MODES.JSON ) {
						return getters.isExpertMode;
					}
 
					// Filter out specific modes depending on type
					switch ( payload.literalType ) {
						case Constants.Z_REFERENCE:
						case Constants.Z_FUNCTION_CALL:
							return mode.key !== Constants.Z_KEY_MODES.LITERAL;
						case Constants.Z_STRING:
							return mode.key !== Constants.Z_KEY_MODES.GENERIC_LITERAL;
					}
 
					return true;
				} );
			};
		},
		getModeIsValid: function () {
			/**
			 * Boolean determining if the current mode selected is valid
			 *
			 * @param {Object} selectedMode
			 *
			 * @return {boolean}
			 */
			return function ( selectedMode ) {
				var modeIdValid = false;
				modes.forEach( function ( mode ) {
					if ( mode.key === selectedMode ) {
						modeIdValid = true;
					}
				} );
				return modeIdValid;
			};
		},
		getTypeByMode: function () {
			/**
			 * Gets the type of the zObject by its mode. This is required when reloading
			 * a page with a zObject that may have a selected mode different to the default one.
			 *
			 * @param {Object} payload
			 * @param {string} payload.literalType
			 * @param {string} payload.selectedMode
			 *
			 * @return {string} type
			 */
			return function ( payload ) {
				var type = payload.literalType;
 
				modes.forEach( function ( mode ) {
					if ( mode.key === payload.selectedMode && mode.type ) {
						type = mode.type;
					}
				} );
 
				return type;
			};
		},
		getModeByType: function () {
			/**
			 * Gets the default mode for a type.
			 *
			 * @param {string} currentType
			 *
			 * @return {string} mode
			 */
			return function ( currentType ) {
				var selectedMode = Constants.Z_KEY_MODES.LITERAL;
				modes.forEach( function ( mode ) {
					if ( mode.type === currentType ) {
						selectedMode = mode.key;
					}
				} );
 
				return selectedMode;
			};
		}
	}
};