All files / ext.wikilambda.edit/components/function/editor FunctionEditorInputs.vue

97.67% Statements 42/43
75% Branches 9/12
94.44% Functions 17/18
97.67% Lines 42/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 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    18x 18x 18x 18x 18x 18x 18x 18x 18x 18x                                                                                                 24x                     108x               5x                   24x               24x               24x               24x               51x                   24x               91x               93x               5x                         1x               8x       18x   18x     18x 18x 18x       18x     18x 18x     18x 18x   108x 108x 108x 108x 108x                                         157x                                 182x           18x
<!--
	WikiLambda Vue component for setting the list of inputs of a ZFunction in the Function editor.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-function-definition-inputs" data-testid="function-definition-inputs">
		<div class="ext-wikilambda-function-block__label">
			<label :id="inputsFieldId">
				{{ inputsLabel }}
				<span>{{ inputsOptional }}</span>
			</label>
			<wl-tooltip
				v-if="tooltipMessage && !canEdit"
				:content="tooltipMessage"
			>
				<cdx-icon
					v-if="tooltipIcon"
					class="ext-wikilambda-function-block__label__tooltip-icon"
					:icon="tooltipIcon">
				</cdx-icon>
			</wl-tooltip>
			<span class="ext-wikilambda-function-block__label__description">
				{{ inputsFieldDescription }}
				<a :href="listObjectsUrl" target="_blank">{{ listObjectsLink }}</a>
			</span>
		</div>
		<!-- List of input fields -->
		<div
			:aria-labelledby="inputsFieldId"
			class="ext-wikilambda-function-block__body"
		>
			<wl-function-editor-inputs-item
				v-for="( input, index ) in inputs"
				:key="'input-' + input.id + '-lang-' + zLanguage"
				data-testid="function-editor-input-item"
				:row-id="input.id"
				:index="index"
				:z-language="zLanguage"
				:can-edit-type="canEdit"
				:is-main-language-block="isMainLanguageBlock"
				@remove="removeItem"
				@update-argument-label="updateArgumentLabel"
			></wl-function-editor-inputs-item>
			<cdx-button
				v-if="canEdit"
				:class="addInputButtonClass"
				@click="addNewItem"
			>
				<cdx-icon :icon="icons.cdxIconAdd"></cdx-icon>
				{{ addNewItemText }}
			</cdx-button>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	FunctionEditorInputsItem = require( './FunctionEditorInputsItem.vue' ),
	Tooltip = require( '../../base/Tooltip.vue' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	icons = require( './../../../../lib/icons.json' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions;
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-inputs',
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon,
		'wl-function-editor-inputs-item': FunctionEditorInputsItem,
		'wl-tooltip': Tooltip
	},
	props: {
		rowId: {
			type: Number,
			default: 0
		},
		isMainLanguageBlock: {
			type: Boolean,
			required: true
		},
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			default: ''
		},
		/**
		 * icon that will display a tooltip
		 */
		tooltipIcon: {
			type: [ String, Object ],
			default: null
		},
		/**
		 * if a user has permission to edit a function
		 */
		canEdit: {
			type: Boolean,
			default: false
		},
		/**
		 * message the tooltip displays
		 */
		tooltipMessage: {
			type: String,
			default: null
		}
	},
	data: function () {
		return {
			icons: icons
		};
	},
	computed: Object.assign( mapGetters( [
		'getZFunctionInputs',
		'getRowByKeyPath',
		'getUserLangCode'
	] ), {
		/**
		 * List of inputs
		 *
		 * @return {Array}
		 */
		inputs: function () {
			return this.getZFunctionInputs();
		},
		/**
		 * Returns the rowId of the inputs list
		 *
		 * @return {number}
		 */
		inputsListRowId: function () {
			return this.getRowByKeyPath( [
				Constants.Z_PERSISTENTOBJECT_VALUE,
				Constants.Z_FUNCTION_ARGUMENTS
			], this.rowId ).id;
		},
		/**
		 * Returns the label for the inputs field
		 *
		 * @return {string}
		 */
		inputsLabel: function () {
			// TODO (T335583): Replace i18n message with key label
			// return this.getLabel( Constants.Z_FUNCTION_ARGUMENTS );
			return this.$i18n( 'wikilambda-function-definition-inputs-label' ).text();
		},
		/**
		 * Returns the "optional" caption for the inputs field
		 *
		 * @return {string}
		 */
		inputsOptional: function () {
			return this.$i18n( 'parentheses', [ this.$i18n( 'wikilambda-optional' ).text() ] ).text();
		},
		/**
		 * Returns the id for the input field
		 *
		 * @return {string}
		 */
		inputsFieldId: function () {
			return `ext-wikilambda-function-definition-inputs__label_${ this.zLanguage }`;
		},
		/**
		 * Returns the description for the inputs field
		 *
		 * @return {string}
		 */
		inputsFieldDescription: function () {
			return this.$i18n( 'wikilambda-function-definition-inputs-description' ).text();
		},
		/**
		 * Returns the URL to the Special page List Object by Type
		 *
		 * @return {string}
		 */
		listObjectsUrl: function () {
			return new mw.Title( Constants.PATHS.LIST_OBJECTS_BY_TYPE_TYPE )
				.getUrl( { uselang: this.getUserLangCode } );
		},
		/**
		 * Returns the text for the link to the Special page List Object by Type
		 *
		 * @return {string}
		 */
		listObjectsLink: function () {
			return this.$i18n( 'wikilambda-function-definition-input-types' ).text();
		},
		/**
		 * Returns the text of the button to add a new input
		 *
		 * @return {string}
		 */
		addNewItemText: function () {
			return this.inputs.length === 0 ?
				this.$i18n( 'wikilambda-function-definition-inputs-item-add-first-input-button' ).text() :
				this.$i18n( 'wikilambda-function-definition-inputs-item-add-input-button' ).text();
		},
		/**
		 * Returns the class name of the button to add a new input
		 *
		 * @return {string}
		 */
		addInputButtonClass: function () {
			return this.inputs.length === 0 ?
				'ext-wikilambda-function-definition-inputs__add-input-button' :
				'ext-wikilambda-function-definition-inputs__add-another-input-button';
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'removeItemFromTypedList'
	] ), {
		/**
		 * Add a new input item to the function inputs list
		 */
		addNewItem: function () {
			this.changeType( {
				type: Constants.Z_ARGUMENT,
				id: this.inputsListRowId,
				lang: this.zLanguage,
				append: true
			} );
		},
		/**
		 * Removes an item from the list of inputs
		 *
		 * @param {number} rowId
		 */
		removeItem: function ( rowId ) {
			this.removeItemFromTypedList( { rowId } );
		},
		/**
		 * Emits the event updated-argument-label
		 */
		updateArgumentLabel: function () {
			this.$emit( 'updated-argument-label' );
		}
	} )
} );
</script>