All files / ext.wikilambda.app/components/visualeditor FunctionCallSetup.vue

96.77% Statements 120/124
100% Branches 12/12
60% Functions 3/5
96.77% Lines 120/124

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 1251x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 22x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 6x 6x 6x 6x 6x 6x 1x 1x 6x 6x 6x 6x 6x     6x 6x 6x 6x 6x     6x 6x 6x 6x 6x 6x 6x 16x 16x 16x 16x 16x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x  
<!--
	WikiLambda Vue component for Visual Editor Wikifunctions function call
	insertion and edit plugin.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-app-function-call-setup">
		<wl-function-select
			v-if="!hasValidFunction"
			@select="selectFunction"
		></wl-function-select>
		<wl-function-input-setup
			v-else
			@update="updateFunctionInputs"
			@loading-start="startLoading"
			@loading-end="endLoading"
		></wl-function-input-setup>
	</div>
</template>
 
<script>
const { computed, defineComponent, inject, watch } = require( 'vue' );
 
const useMainStore = require( '../../store/index.js' );
const FunctionSelect = require( './FunctionSelect.vue' );
const FunctionInputSetup = require( './FunctionInputSetup.vue' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-call-setup',
	components: {
		'wl-function-select': FunctionSelect,
		'wl-function-input-setup': FunctionInputSetup
	},
	emits: [ 'function-inputs-updated', 'function-name-updated', 'loading-start', 'loading-end' ],
	setup( _, { emit } ) {
		const i18n = inject( 'i18n' );
		const store = useMainStore();
 
		// Function data
		/**
		 * Returns the function id as stored in wikitext,
		 * or null if function is not yet selected.
		 *
		 * @return {string|null}
		 */
		const functionZid = computed( () => store.getVEFunctionId );
 
		/**
		 * Returns if function in wikitext is selected and properly
		 * configured (valid)
		 *
		 * @return {boolean}
		 */
		const hasValidFunction = computed( () => store.validateVEFunctionId );
 
		/**
		 * Returns the LabelData object of the selected valid Function Id,
		 * or undefined if no valid Function is yet selected.
		 *
		 * @return {LabelData|undefined}
		 */
		const functionLabelData = computed( () => hasValidFunction.value ?
			store.getLabelData( functionZid.value ) :
			undefined );
 
		// Actions
		/**
		 * Set the wikitext function value with the new selected Function ID
		 * Set function params to blank state
		 *
		 * @param {string} value
		 */
		function selectFunction( value ) {
			store.setVEFunctionId( value );
			store.setVEFunctionParams();
		}
 
		/**
		 * When the function input values are updated,
		 * emit a 'function-inputs-updated' event to VisualEditor
		 */
		function updateFunctionInputs() {
			emit( 'function-inputs-updated' );
		}
 
		/**
		 * When we start loading, emit a 'loading-start' event to VisualEditor
		 */
		function startLoading() {
			emit( 'loading-start' );
		}
 
		/**
		 * When we want to end loading, emit a 'loading-end' event to VisualEditor
		 */
		function endLoading() {
			emit( 'loading-end' );
		}
 
		// Watch
		/**
		 * When function updates and we have the new name,
		 * emit a 'function-name-updated' event to VisualEditor
		 */
		watch( functionLabelData, ( labelData ) => {
			const newTitle = ( labelData && !labelData.isUntitled ) ? labelData.label :
				i18n( 'brackets', i18n( 'wikilambda-visualeditor-wikifunctionscall-no-name' ).text() ).text();
			if ( functionZid.value ) {
				emit( 'function-name-updated', newTitle );
			}
		} );
 
		return {
			endLoading,
			hasValidFunction,
			selectFunction,
			startLoading,
			updateFunctionInputs
		};
	}
} );
</script>