All files / ext.wikilambda.app/components/function/editor FunctionEditorInputsItem.vue

99.39% Statements 329/331
100% Branches 19/19
92.3% Functions 12/13
99.39% Lines 329/331

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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 3321x 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 34x 34x 34x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 34x 1x 1x 1x 1x 1x 1x 1x 1x 129x 1x 1x 1x 1x 1x 1x 1x 1x 88x 88x 88x 1x 1x 1x 1x 1x 1x 1x 30x 1x 1x 1x 1x 1x 1x 1x 32x 1x 1x 1x 1x 1x 1x 1x 1x 89x 1x 1x 1x 1x 1x 1x 1x 15x 15x 15x 1x 1x 1x 1x 1x 1x 1x 15x 15x 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 9x 9x 3x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 9x 9x 1x 1x 34x 34x 34x 34x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for an individual input to be set for a ZFunction in the Function editor.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-app-function-editor-inputs-item" data-testid="input-list-item">
		<!-- Per-input label -->
		<div class="ext-wikilambda-app-function-editor-inputs-item__header">
			<span class="ext-wikilambda-app-function-editor-inputs-item__label">
				{{ inputFieldLabel }}
			</span>
			<cdx-button
				v-if="canEditType"
				weight="quiet"
				class="ext-wikilambda-app-function-editor-inputs-item__action-delete"
				:aria-label="$i18n( 'wikilambda-function-definition-inputs-item-remove' ).text()"
				data-testid="remove-input"
				@click="removeInput"
			>
				<cdx-icon
					:icon="icons.cdxIconTrash"
					class="ext-wikilambda-app-function-editor-inputs-item__icon-trash"></cdx-icon>
			</cdx-button>
		</div>
 
		<!-- Input fields -->
		<div class="ext-wikilambda-app-function-editor-inputs-item__body">
			<!-- Label field: always active -->
			<cdx-field
				class="ext-wikilambda-app-function-editor-inputs-item__field"
				data-testid="function-editor-input-item-label"
			>
				<template #label>
					{{ inputLabelTitle }}
				</template>
				<cdx-text-input
					:model-value="inputLabel"
					:placeholder="inputLabelFieldPlaceholder"
					:aria-label="inputLabelFieldPlaceholder"
					:maxlength="maxInputLabelChars"
					@input="updateRemainingChars"
					@change="persistInputLabel"
				></cdx-text-input>
				<div class="ext-wikilambda-app-function-editor-inputs-item__counter">
					{{ remainingChars }}
				</div>
			</cdx-field>
			<!-- Type field: only first block -->
			<wl-type-selector
				v-if="isMainLanguageBlock"
				class="ext-wikilambda-app-function-editor-inputs-item__field"
				data-testid="function-editor-input-item-type"
				:row-id="inputTypeRowId"
				:type="typeZid"
				:label-data="inputTypeLabel"
				:disabled="!canEditType"
				:placeholder="inputTypeFieldPlaceholder"
			></wl-type-selector>
		</div>
	</div>
</template>
 
<script>
const { CdxButton, CdxField, CdxIcon, CdxTextInput } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	LabelData = require( '../../../store/classes/LabelData.js' ),
	TypeSelector = require( '../../base/TypeSelector.vue' ),
	{ mapActions, mapGetters } = require( 'vuex' ),
	icons = require( './../../../../lib/icons.json' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-inputs-item',
	components: {
		'wl-type-selector': TypeSelector,
		'cdx-button': CdxButton,
		'cdx-field': CdxField,
		'cdx-icon': CdxIcon,
		'cdx-text-input': CdxTextInput
	},
	props: {
		rowId: {
			type: Number,
			required: true
		},
		/**
		 * Index for this input in the list of inputs (zero-lead)
		 */
		index: {
			type: Number,
			required: true
		},
		/**
		 * If this input is in the main language block
		 */
		isMainLanguageBlock: {
			type: Boolean,
			required: true
		},
		/**
		 * if user has permissions to edit the input type
		 */
		canEditType: {
			type: Boolean,
			required: true
		},
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			required: true
		}
	},
	data: function () {
		return {
			typeZid: Constants.Z_TYPE,
			maxInputLabelChars: Constants.INPUT_CHARS_MAX,
			remainingChars: Constants.INPUT_CHARS_MAX,
			icons: icons
		};
	},
	computed: Object.assign( mapGetters( [
		'getRowByKeyPath',
		'getZArgumentLabelForLanguage',
		'getZArgumentTypeRowId',
		'getZMonolingualTextValue'
	] ), {
		/**
		 * Returns the label and index for the current input.
		 * If not active, returns label, index and selected type.
		 *
		 * @return {string}
		 */
		inputFieldLabel: function () {
			const inputNumber = this.index + 1;
			return this.$i18n( 'wikilambda-function-viewer-details-input-number', inputNumber ).text();
		},
		/**
		 * Returns the row of the input label in the given language
		 * or undefined if not found.
		 *
		 * @return {Object|undefined}
		 */
		inputLabelRow: function () {
			return this.getZArgumentLabelForLanguage( this.rowId, this.zLanguage );
		},
		/**
		 * Returns the string value of the input label in the given
		 * language or empty string if undefined.
		 *
		 * @return {string}
		 */
		inputLabel: function () {
			return this.inputLabelRow ?
				this.getZMonolingualTextValue( this.inputLabelRow.id ) :
				'';
		},
		/**
		 * Returns the title for the input label field
		 *
		 * @return {string}
		 */
		inputLabelTitle: function () {
			return this.$i18n( 'wikilambda-function-definition-input-item-label' ).text();
		},
		/**
		 * Returns the placeholder for the input label field
		 *
		 * @return {string}
		 */
		inputLabelFieldPlaceholder: function () {
			return this.$i18n( 'wikilambda-function-definition-inputs-item-input-placeholder' ).text();
		},
		/**
		 * Returns the row Id of the current input type
		 * or undefined if not found.
		 *
		 * @return {number|undefined}
		 */
		inputTypeRowId: function () {
			return this.getZArgumentTypeRowId( this.rowId );
		},
		/**
		 * Returns the title for the input type field
		 *
		 * @return {string}
		 */
		inputTypeLabel: function () {
			return LabelData.fromString(
				this.$i18n( 'wikilambda-function-definition-input-item-type' ).text()
			);
		},
		/**
		 * Returns the placeholder for the input type field
		 *
		 * @return {string}
		 */
		inputTypeFieldPlaceholder: function () {
			return this.$i18n( 'wikilambda-function-definition-inputs-item-selector-placeholder' ).text();
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'setValueByRowIdAndPath',
		'removeItemFromTypedList'
	] ), {
		/**
		 * Updates the remainingChars data property as the user types into the Z2K2 field
		 *
		 * @param {Event} event - the event object that is automatically passed in on input
		 */
		updateRemainingChars: function ( event ) {
			const { length } = event.target.value;
			this.remainingChars = this.maxInputLabelChars - length;
		},
		/**
		 * Removes the input given by this rowId
		 */
		removeInput: function () {
			this.$emit( 'remove', this.rowId );
		},
		/**
		 * Persist the new input label in the globally stored object
		 *
		 * @param {Object} event
		 */
		persistInputLabel: function ( event ) {
			const value = event.target.value;
			if ( this.inputLabelRow ) {
				if ( value === '' ) {
					this.removeItemFromTypedList( { rowId: this.inputLabelRow.id } );
				} else {
					this.setValueByRowIdAndPath( {
						rowId: this.inputLabelRow.id,
						keyPath: [
							Constants.Z_MONOLINGUALSTRING_VALUE,
							Constants.Z_STRING_VALUE
						],
						value
					} );
				}
			} else {
				// If this.inputLabelRow is undefined, there's no monolingual string
				// for the given language, so we create a new monolingual string
				// with the new value and append to the parent list.
				const parentRow = this.getRowByKeyPath( [
					Constants.Z_ARGUMENT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				], this.rowId );
				this.changeType( {
					id: parentRow.id,
					type: Constants.Z_MONOLINGUALSTRING,
					lang: this.zLanguage,
					value,
					append: true
				} );
			}
			this.$emit( 'update-argument-label' );
		}
	} ),
	mounted: function () {
		this.$nextTick( function () {
			this.remainingChars = this.maxInputLabelChars - this.inputLabel.length;
		} );
	}
} );
</script>
 
<style lang="less">
@import '../../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-function-editor-inputs-item {
	border-radius: @border-radius-base;
	border: @border-subtle;
	padding: @spacing-35 @spacing-75 @spacing-75;
	margin-bottom: @spacing-100;
 
	.ext-wikilambda-app-function-editor-inputs-item__counter {
		color: @color-subtle;
		margin-left: @spacing-50;
		display: flex;
		justify-content: flex-end;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__header {
		display: flex;
		flex-direction: row;
		margin-bottom: @spacing-100;
		margin-right: -@spacing-35;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__label {
		flex-grow: 1;
		font-weight: @font-weight-bold;
		display: inline-block;
		line-height: @size-200;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__action-delete {
		flex-grow: 0;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__icon-trash.cdx-icon {
		width: @size-100;
		height: @size-100;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__body {
		width: 100%;
		box-sizing: border-box;
		padding: 0;
	}
 
	.ext-wikilambda-app-function-editor-inputs-item__field {
		margin-bottom: @spacing-100;
 
		&:last-child {
			margin-bottom: 0;
		}
 
		.cdx-label__label__text {
			font-weight: @font-weight-normal;
		}
	}
}
</style>