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

100% Statements 214/214
100% Branches 7/7
100% Functions 4/4
100% Lines 214/214

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 2151x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 20x 20x 20x 20x 20x 20x 20x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 8x 8x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 1x 1x  
<!--
	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>
	<wl-function-editor-field
		class="ext-wikilambda-app-function-editor-inputs"
		:show-tooltip="tooltipMessage && !canEdit"
		:tooltip-message="tooltipMessage"
		:tooltip-icon="tooltipIcon">
		<template #label>
			<label :id="inputsFieldId">
				{{ i18n( 'wikilambda-function-definition-inputs-label' ).text() }}
				<span>{{ i18n( 'parentheses', [ i18n( 'wikilambda-optional' ).text() ] ).text() }}</span>
			</label>
		</template>
		<template #description>
			{{ i18n( 'wikilambda-function-definition-inputs-description' ).text() }}
			<a :href="listObjectsUrl" target="_blank">
				{{ i18n( 'wikilambda-function-definition-input-types' ).text() }}
			</a>
		</template>
		<template #body>
			<div :aria-labelledby="inputsFieldId">
				<wl-function-editor-inputs-item
					v-for="( input, index ) in inputs"
					:key="`input-${ input.key }-lang-${ zLanguage }`"
					data-testid="function-editor-input-item"
					:index="index"
					:input="input"
					:lang-label-data="langLabelData"
					:z-language="zLanguage"
					:can-edit-type="canEdit"
					:is-main-language-block="isMainLanguageBlock"
					@remove="removeItem"
					@argument-label-updated="updateArgumentLabel"
				></wl-function-editor-inputs-item>
				<cdx-button
					v-if="canEdit"
					:class="inputs.length === 0 ?
						'ext-wikilambda-app-function-editor-inputs__action-add' :
						'ext-wikilambda-app-function-editor-inputs__action-add-another'"
					@click="addNewItem"
				>
					<cdx-icon :icon="iconAdd"></cdx-icon>
					{{ inputs.length === 0 ?
						i18n( 'wikilambda-function-definition-inputs-item-add-first-input-button' ).text() :
						i18n( 'wikilambda-function-definition-inputs-item-add-input-button' ).text() }}
				</cdx-button>
			</div>
		</template>
	</wl-function-editor-field>
</template>
 
<script>
const { computed, defineComponent, inject } = require( 'vue' );
 
const Constants = require( '../../../Constants.js' );
const icons = require( './../../../../lib/icons.json' );
const LabelData = require( '../../../store/classes/LabelData.js' );
const useMainStore = require( '../../../store/index.js' );
const { canonicalToHybrid } = require( '../../../utils/schemata.js' );
 
// Function editor components
const FunctionEditorField = require( './FunctionEditorField.vue' );
const FunctionEditorInputsItem = require( './FunctionEditorInputsItem.vue' );
// Codex components
const { CdxButton, CdxIcon, CdxTooltip } = require( '../../../../codex.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-inputs',
	directives: {
		tooltip: CdxTooltip
	},
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon,
		'wl-function-editor-field': FunctionEditorField,
		'wl-function-editor-inputs-item': FunctionEditorInputsItem
	},
	props: {
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			default: ''
		},
		/**
		 * whether is the first language in the page
		 */
		isMainLanguageBlock: {
			type: Boolean,
			required: true
		},
		/**
		 * whether user has permission to edit the function
		 */
		canEdit: {
			type: Boolean,
			default: false
		},
		/**
		 * icon that will display a tooltip
		 */
		tooltipIcon: {
			type: [ String, Object ],
			default: null
		},
		/**
		 * message the tooltip displays
		 */
		tooltipMessage: {
			type: String,
			default: null
		},
		/**
		 * label data for the language
		 */
		langLabelData: {
			type: LabelData,
			default: null
		}
	},
	emits: [ 'argument-label-updated' ],
	setup( props, { emit } ) {
		const i18n = inject( 'i18n' );
		const store = useMainStore();
 
		// Constants
		const iconAdd = icons.cdxIconAdd;
 
		// Inputs data
		/**
		 * List of inputs, in hybrid format (without benjamin item)
		 *
		 * @return {Array}
		 */
		const inputs = computed( () => store.getZFunctionInputLabels( props.zLanguage ) );
 
		// Field display
		/**
		 * Returns the id for the input field
		 *
		 * @return {string}
		 */
		const inputsFieldId = computed( () => `ext-wikilambda-app-function-editor-inputs__label-${ props.zLanguage }` );
 
		/**
		 * Returns the URL to the Special page List Object by Type
		 *
		 * @return {string}
		 */
		const listObjectsUrl = computed( () => new mw.Title( Constants.PATHS.LIST_OBJECTS_BY_TYPE_TYPE )
			.getUrl( { uselang: store.getUserLangCode } )
		);
 
		// Actions
		/**
		 * Add a new input item to the function inputs list
		 */
		function addNewItem() {
			const value = canonicalToHybrid( store.createObjectByType( { type: Constants.Z_ARGUMENT } ) );
			store.pushItemsByKeyPath( {
				keyPath: [
					Constants.STORED_OBJECTS.MAIN,
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_ARGUMENTS
				],
				values: [ value ]
			} );
		}
 
		/**
		 * Removes an item from the list of inputs
		 *
		 * @param {number} index
		 */
		function removeItem( index ) {
			const itemIndex = String( ( Number( index ) + 1 ) );
			store.deleteListItemsByKeyPath( {
				keyPath: [
					Constants.STORED_OBJECTS.MAIN,
					Constants.Z_PERSISTENTOBJECT_VALUE,
					Constants.Z_FUNCTION_ARGUMENTS
				],
				indexes: [ itemIndex ]
			} );
		}
 
		/**
		 * Emits the event argument-label-updated
		 */
		function updateArgumentLabel() {
			emit( 'argument-label-updated' );
		}
 
		return {
			addNewItem,
			iconAdd,
			inputs,
			inputsFieldId,
			i18n,
			listObjectsUrl,
			removeItem,
			updateArgumentLabel
		};
	}
} );
</script>