All files / ext.wikilambda.edit/components/base ModeSelector.vue

99.22% Statements 255/257
93.33% Branches 28/30
100% Functions 8/8
99.22% Lines 255/257

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 2581x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 317x 1x 1x 1x 1x 1x 1x 1x 1x 317x 1x 1x 1x 1x 1x 1x 1x 225x 1x 1x 1x 1x 1x 1x 1x 225x 225x 225x 225x 225x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 26x 26x 26x 26x 26x 26x 26x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 317x 184x 317x 184x 184x 184x 184x 184x 184x 184x 184x 317x 47x 47x 47x 47x 47x 47x 47x 47x 317x 317x 317x 317x 317x 317x 317x 317x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 317x 1x 1x 1x 1x 1x 1x 1x 1x 116x 116x 116x 116x 116x     1x 1x 1x 7x 7x 1x 1x 1x 7x 1x 1x 1x 7x 1x 1x 1x 4x 7x 3x 3x 3x 3x 3x 3x 3x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<template>
	<div class="ext-wikilambda-mode-selector" data-testid="mode-selector">
		<cdx-menu-button
			data-testid="mode-selector-button"
			class="ext-wikilambda-mode-selector-menu-button"
			:selected="selected"
			:menu-items="menuItems"
			:aria-label="$i18n( 'wikilambda-mode-selector-button-label' ).text()"
			:disabled="disabled"
			@update:selected="selectMode"
		>
			<cdx-icon :icon="icon"></cdx-icon>
		</cdx-menu-button>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	CdxMenuButton = require( '@wikimedia/codex' ).CdxMenuButton,
	Constants = require( '../../Constants.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	icons = require( '../../../lib/icons.json' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-mode-selector',
	components: {
		'cdx-icon': CdxIcon,
		'cdx-menu-button': CdxMenuButton
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		parentExpectedType: {
			type: [ String, Object ],
			required: false,
			default: Constants.Z_OBJECT
		},
		disabled: {
			type: Boolean,
			default: false
		}
	},
	data: function () {
		return {
			icon: icons.cdxIconEllipsis
		};
	},
	computed: Object.assign( mapGetters( [
		'getChildrenByParentRowId',
		'getLabelData',
		'getParentRowId',
		'getZObjectTypeByRowId',
		'getZObjectKeyByRowId',
		'isCustomEnum',
		'isInsideComposition'
	] ), {
		/**
		 * Returns the key of the key-value pair of this component.
		 *
		 * @return {string}
		 */
		key: function () {
			return this.getZObjectKeyByRowId( this.rowId );
		},
		/**
		 * Returns the type of the value of the the ZObject represented
		 * in this component. When it's not set, it's undefined.
		 *
		 * @return {string | Object | undefined}
		 */
		type: function () {
			return this.getZObjectTypeByRowId( this.rowId );
		},
		/**
		 * Value of the selected option or Z1/Object if unselected
		 *
		 * @return {string}
		 */
		selected: function () {
			return this.type ? this.typeToString( this.type, true ) : Constants.Z_OBJECT;
		},
		/**
		 * Whether the selected mode is a resolver or a literal type
		 *
		 * @return {boolean}
		 */
		isResolver: function () {
			return [
				Constants.Z_FUNCTION_CALL,
				Constants.Z_ARGUMENT_REFERENCE,
				Constants.Z_REFERENCE
			].includes( this.selected );
		},
		/**
		 * Returns the available options for the type mode selector.
		 * The available options are all the available resolver types
		 * (Z9/Reference, Z7/Function call and Z18/Argument reference
		 * if we are inside an implementation composition) and the
		 * literal type.
		 * The literal type is specific if selected or bound by key,
		 * else it will allow the selection for any literal type (Object)
		 *
		 * @return {Array} Array of codex MenuItemData objects
		 */
		menuItems: function () {
			// Resolver types:
			// * Reference and function are always available
			// * Argument reference only if we are inside a composition
			const resolvers = [
				{
					label: this.getLabelData( Constants.Z_REFERENCE ).label,
					value: Constants.Z_REFERENCE,
					type: Constants.Z_REFERENCE,
					icon: icons.cdxIconInstance
				},
				{
					label: this.getLabelData( Constants.Z_FUNCTION_CALL ).label,
					value: Constants.Z_FUNCTION_CALL,
					type: Constants.Z_FUNCTION_CALL,
					icon: icons.cdxIconFunction
				}
			];
			if ( this.isInsideComposition( this.rowId ) ) {
				resolvers.push( {
					label: this.getLabelData( Constants.Z_ARGUMENT_REFERENCE ).label,
					value: Constants.Z_ARGUMENT_REFERENCE,
					type: Constants.Z_ARGUMENT_REFERENCE,
					icon: icons.cdxIconFunctionArgument
				} );
			}
 
			// Literal types:
			// * if parent expects a given type and parent is not Z1K1:
			//   * Add "Literal <Expected Type>"
			// * If parent expects any type:
			//   * Add "Literal Object"
			//   * If type is defined, add "Literal <Selected Type>"
			const literals = [];
			let typeString;
			if ( this.key !== Constants.Z_OBJECT_TYPE &&
				!this.isKeyTypedListType( this.key ) &&
				!this.isCustomEnum( this.parentExpectedType )
			) {
				typeString = this.typeToString( this.parentExpectedType, true );
				literals.push( {
					label: this.$i18n( 'wikilambda-literal-type', this.getLabelData( typeString ).label ).text(),
					value: typeString,
					type: this.parentExpectedType,
					icon: icons.cdxIconLiteral
				} );
			}
			if ( !!this.type && !this.isResolver && ( this.parentExpectedType === Constants.Z_OBJECT ) ) {
				typeString = this.typeToString( this.type, true );
				literals.push( {
					label: this.$i18n( 'wikilambda-literal-type', this.getLabelData( typeString ).label ).text(),
					value: typeString,
					type: this.type,
					icon: icons.cdxIconLiteral
				} );
			}
 
			// Return literals and resolvers, sorted by label
			const options = [ ...literals, ...resolvers ];
			options.sort( ( a, b ) => ( a.label < b.label ) ? -1 :
				( a.label > b.label ) ? 1 : 0 );
 
			// If it's a list item, add "Move before" and "Move after" items
			if ( this.isKeyTypedListItem( this.key ) ) {
				const isFirst = this.key === '1';
				const isLast = this.key === String( this.listCount );
				options.push( ...[ {
					label: this.$i18n( 'wikilambda-move-before-list-item' ).text(),
					value: Constants.LIST_MENU_OPTIONS.MOVE_BEFORE,
					icon: icons.cdxIconTableMoveRowBefore,
					disabled: isFirst,
					class: 'ext-wikilambda-mode-selector-move-before'
				}, {
					label: this.$i18n( 'wikilambda-move-after-list-item' ).text(),
					value: Constants.LIST_MENU_OPTIONS.MOVE_AFTER,
					icon: icons.cdxIconTableMoveRowAfter,
					disabled: isLast,
					class: 'ext-wikilambda-mode-selector-move-after'
				}, {
					label: this.$i18n( 'wikilambda-delete-list-item' ).text(),
					value: Constants.LIST_MENU_OPTIONS.DELETE_ITEM,
					icon: icons.cdxIconTrash,
					action: 'destructive',
					class: 'ext-wikilambda-mode-selector-delete'
				} ] );
			}
			return options;
		},
		/**
		 * If the key belongs to a typed list item, it returns the
		 * list item count (not including the type element)
		 *
		 * @return {number}
		 */
		listCount: function () {
			if ( this.isKeyTypedListItem( this.key ) ) {
				const parentRowId = this.getParentRowId( this.rowId );
				const children = this.getChildrenByParentRowId( parentRowId );
				return children.length - 1;
			}
			return 0;
		}
	} ),
	methods: {
		selectMode: function ( value ) {
			// List actions:
			if ( value === Constants.LIST_MENU_OPTIONS.DELETE_ITEM ) {
				this.$emit( 'delete-list-item' );
				return;
			}
			if ( value === Constants.LIST_MENU_OPTIONS.MOVE_BEFORE ) {
				this.$emit( 'move-before' );
				return;
			}
			if ( value === Constants.LIST_MENU_OPTIONS.MOVE_AFTER ) {
				this.$emit( 'move-after' );
				return;
			}
 
			if ( value !== this.selected ) {
				const newType = this.menuItems.find( ( menu ) => menu.value === value );
				this.$emit( 'set-type', {
					keypath: [],
					value: newType.type,
					literal: true
				} );
			}
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-mode-selector {
	position: relative;
 
	.ext-wikilambda-mode-selector-move-before {
		box-shadow: 0 -1px 0 0 @border-color-subtle;
	}
 
	.ext-wikilambda-mode-selector-delete {
		box-shadow: 0 -1px 0 0 @border-color-subtle;
	}
}
</style>