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

96.96% Statements 64/66
95.12% Branches 39/41
94.11% Functions 16/17
96.92% Lines 63/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 285 286    25x 25x 25x 25x 25x 25x 25x 25x 25x                                                 41x                       315x                 315x               315x               315x                                 315x                     315x 23x                           315x   315x 188x 188x             315x 47x 47x                 315x 315x 972x       315x 116x 116x 116x                           315x                 116x 116x 116x 116x                   315x                     7x 1x 1x   6x 1x 1x   5x 1x 1x   4x 4x 3x               25x   25x     25x 25x 25x         322x 322x 322x 322x           3x             50x                 2x       7x             25x                                            
<template>
	<div class="ext-wikilambda-mode-selector" data-testid="mode-selector">
		<cdx-button
			data-testid="mode-selector-button"
			:aria-label="$i18n( 'wikilambda-mode-selector-button-label' ).text()"
			weight="quiet"
			:disabled="disabled"
			@click="expanded = true"
			@blur="expanded = false"
		>
			<cdx-icon :icon="icon"></cdx-icon>
		</cdx-button>
		<cdx-menu
			v-if="!disabled"
			v-model:expanded="expanded"
			v-model:selected="selected"
			class="ext-wikilambda-mode-selector-menu"
			data-testid="mode-selector-menu"
			:menu-items="menuItems"
			:footer="footerAction"
			@update:selected="selectMode"
		></cdx-menu>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	CdxMenu = require( '@wikimedia/codex' ).CdxMenu,
	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-button': CdxButton,
		'cdx-icon': CdxIcon,
		'cdx-menu': CdxMenu
	},
	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 {
			expanded: false,
			icon: icons.cdxIconEllipsis
		};
	},
	computed: Object.assign( mapGetters( [
		'getChildrenByParentRowId',
		'getLabelData',
		'getParentRowId',
		'getZObjectTypeByRowId',
		'getZObjectKeyByRowId',
		'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 ) ) {
				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(),
					vEalue: typeString,
					type: this.type,
					icon: icons.cdxIconLiteral
				} );
			}

			// Return literals and resolvers, sorted by label
			const options = [ ...literals, ...resolvers ];
			options.sort( ( a, b ) => {
				return ( 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'
				} ] );
			}
			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;
		},
		/**
		 * Returns "Delete" footer item if the key belongs to a typed list item
		 *
		 * @return {Object|null}
		 */
		footerAction: function () {
			return this.isKeyTypedListItem( this.key ) ?
				{
					label: this.$i18n( 'wikilambda-delete-list-item' ).text(),
					value: Constants.LIST_MENU_OPTIONS.DELETE_ITEM,
					icon: icons.cdxIconTrash,
					class: 'ext-wikilambda-mode-selector-delete'
				} : null;
		}
	} ),
	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
				} );
			}
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-mode-selector {
	position: relative;
 
	.cdx-button {
		color: @color-subtle;
	}
 
	.ext-wikilambda-mode-selector-menu {
		max-width: @wl-field-label-width;
		width: @wl-field-label-width;
 
		.ext-wikilambda-mode-selector-move-before {
			border-top: 1px solid @border-color-subtle;
		}
 
		.ext-wikilambda-mode-selector-delete {
			.cdx-menu-item__content {
				color: @color-destructive;
			}
		}
	}
}
</style>