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

97.5% Statements 274/281
89.47% Branches 17/19
100% Functions 4/4
97.5% Lines 274/281

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 2821x 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 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 3x 3x 1x 1x 1x 2x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 46x 46x 46x 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 7x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x     2x 2x 2x 2x 2x 2x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x       2x 2x 6x 6x 6x 6x 6x 6x 6x 6x 2x 1x 1x 1x 1x 2x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 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 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-select">
		<!-- Main function selector -->
		<div class="ext-wikilambda-app-function-select__field">
			<cdx-search-input
				:model-value="searchTerm"
				:placeholder="searchPlaceholder"
				@update:model-value="updateSearchTerm"
				@focus="showSearchCancel = true"
			></cdx-search-input>
		</div>
		<div class="ext-wikilambda-app-function-select__results">
			<!-- Suggested items -->
			<template v-if="showSuggested">
				<div class="ext-wikilambda-app-function-select__title">
					{{ i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-suggested-functions-title' ) }}
				</div>
				<wl-function-select-item
					v-for="item in suggested"
					:key="item.zid"
					:label-data="item.labelData"
					:description="item.description"
					@click="selectFunction( item.zid )"
				></wl-function-select-item>
			</template>
			<!-- Lookup results -->
			<template v-else>
				<template v-if="lookupResults.length > 0">
					<div class="ext-wikilambda-app-function-select__title">
						{{ i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-search-results-title' ) }}
					</div>
					<wl-function-select-item
						v-for="( item, index ) in lookupResults"
						:key="item.zid"
						:label="item.label"
						:description="descriptions[index]"
						@click="selectFunction( item.zid )"
					></wl-function-select-item>
				</template>
				<div v-else class="ext-wikilambda-app-function-select__no-results">
					<div class="ext-wikilambda-app-function-select__no-results-msg">
						{{ i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-search-no-results' ) }}
					</div>
					<div
						v-for="( cta, index ) in callsToAction"
						:key="`cta-${ index }`"
						class="ext-wikilambda-app-function-select__no-results-cta"
					>
						<!-- eslint-disable-next-line vue/no-v-html -->
						<span v-html="cta.title"></span><br>
						<span>{{ cta.description }}</span>
					</div>
				</div>
			</template>
		</div>
	</div>
</template>
 
<script>
const { CdxSearchInput } = require( '../../../codex.js' );
const { computed, defineComponent, inject, ref } = require( 'vue' );
 
const useType = require( '../../composables/useType.js' );
const useMainStore = require( '../../store/index.js' );
const FunctionSelectItem = require( './FunctionSelectItem.vue' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-select',
	components: {
		'cdx-search-input': CdxSearchInput,
		'wl-function-select-item': FunctionSelectItem
	},
	emits: [ 'select' ],
	setup( _, { emit } ) {
		const i18n = inject( 'i18n' );
		const { isValidZidFormat } = useType();
		const store = useMainStore();
 
		// Constants
		const callsToAction = [
			{
				title: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-suggest-title' ).parse(),
				description: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-suggest-description' ).text()
			},
			{
				title: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-create-title' ).parse(),
				description: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-create-description' ).text()
			},
			{
				title: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-explore-title' ).parse(),
				description: i18n( 'wikilambda-visualeditor-wikifunctionscall-dialog-cta-explore-description' ).text()
			}
		];
 
		// Search data
		const showSearchCancel = ref( false );
 
		/**
		 * Returns the current search term
		 *
		 * @return {string}
		 */
		const searchTerm = computed( () => store.getSearchTerm );
 
		/**
		 * Returns the search placeholder text
		 *
		 * @return {string}
		 */
		const searchPlaceholder = computed( () => i18n(
			'wikilambda-visualeditor-wikifunctionscall-dialog-search-placeholder',
			// Note: This is currently a hard-coded value of 2000 Functions.
			mw.language.convertNumber( 2000 )
		).text() );
 
		/**
		 * Update the lookupResults when there's a new search
		 * term in the language search box.
		 *
		 * @param {string} value
		 */
		function updateSearchTerm( value ) {
			store.setSearchTerm( value );
			if ( !value ) {
				store.setLookupResults( [] );
				return;
			}
			fetchLookupResults( value );
			// Track the searching for a function
			store.submitVEInteraction( 'search-change-query' );
		}
 
		// Suggested functions
		/**
		 * Returns the information of the suggested function Zids
		 *
		 * @return {Array}
		 */
		const suggested = computed( () => store.getSuggestedFunctions.map( ( zid ) => ( {
			zid,
			labelData: store.getLabelData( zid ),
			description: store.getDescription( zid )
		} ) ) );
 
		/**
		 * Returns true when search term is empty and there are suggestions to show
		 *
		 * @return {boolean}
		 */
		const showSuggested = computed( () => !!suggested.value.length && !searchTerm.value.length );
 
		// Lookup results
		let lookupAbortController = null;
 
		/**
		 * Returns the current lookup results
		 *
		 * @return {Array}
		 */
		const lookupResults = computed( () => store.getLookupResults );
 
		/**
		 * Returns the array of the description objects from the lookup results
		 *
		 * @return {Array}
		 */
		const descriptions = computed( () => lookupResults.value
			.map( ( item ) => store.getDescription( item.zid ) )
		);
 
		/**
		 * Triggers a lookup API to search for matches for the
		 * given substring and formats the results to be shown
		 * in the dialog list.
		 *
		 * @param {string} substring
		 */
		function fetchLookupResults( substring ) {
			const allZids = [];
			// Cancel previous request if any
			if ( lookupAbortController ) {
				lookupAbortController.abort();
			}
			lookupAbortController = new AbortController();
			store.lookupFunctions( {
				search: substring,
				renderable: true,
				signal: lookupAbortController.signal
			} ).then( ( data ) => {
				const { objects } = data;
				// If the string searched has changed, do not show the search result
				if ( !searchTerm.value.includes( substring ) ) {
					return;
				}
				// Compile information for every search result
				const results = objects
					.map( ( result ) => {
						allZids.push( result.page_title );
						return {
							zid: result.page_title,
							label: result.label,
							language: result.language
						};
					} );
				store.setLookupResults( results );
				// Fetch the result zid information (labels)
				store.fetchZids( { zids: allZids } );
			} ).catch( ( error ) => {
				if ( error.code === 'abort' ) {
					return;
				}
			} );
		}
 
		// Selection actions
		/**
		 * If the selected value is a valid Zid, emit select event
		 *
		 * @param {string} value
		 */
		function selectFunction( value ) {
			if ( value && isValidZidFormat( value ) ) {
				emit( 'select', value );
				// Track the selecting a function
				store.submitVEInteraction( 'search-choose-function' );
			}
		}
 
		return {
			callsToAction,
			descriptions,
			lookupResults,
			searchPlaceholder,
			searchTerm,
			selectFunction,
			showSearchCancel,
			showSuggested,
			suggested,
			updateSearchTerm,
			i18n
		};
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-function-select {
	.ext-wikilambda-app-function-select__field {
		padding: 16px 16px 8px;
	}
 
	.ext-wikilambda-app-function-select__results {
		overflow: scroll;
	}
 
	.ext-wikilambda-app-function-select__title {
		padding: @spacing-50 @spacing-100;
		color: @color-base;
		font-weight: @font-weight-bold;
	}
 
	.ext-wikilambda-app-function-select__no-results-msg {
		padding: @spacing-50 @spacing-100;
		color: @color-subtle;
		font-weight: @font-weight-bold;
	}
 
	.ext-wikilambda-app-function-select__no-results-cta {
		padding: @spacing-50 @spacing-100;
	}
}
</style>