All files / ext.wikilambda.languageselector/components LanguageSelector.vue

98.03% Statements 300/306
93.75% Branches 30/32
94.11% Functions 16/17
98.03% Lines 300/306

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 3071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 10x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 1x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 3x 3x 4x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 20x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 6x 6x 6x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x     3x 3x 3x 3x 3x 1x 1x 2x 2x 2x 1x 1x 1x 1x 1x 1x 7x 7x 7x 1x 1x 1x 1x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 11x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue interface module for selecting a page language.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		ref="languageSelector"
		class="ext-wikilambda-language-selector"
	>
		<cdx-button
			weight="quiet"
			aria-label="Toggle"
			class="ext-wikilambda-language-selector__trigger"
			@click="openLanguageSelector"
		>
			<cdx-icon :icon="icons.cdxIconLanguage"></cdx-icon>
			{{ selectedLanguageLabel }}
		</cdx-button>
		<div
			ref="languageSelectorDropdown"
			class="ext-wikilambda-language-selector__dropdown"
		>
			<cdx-lookup
				ref="languageSelectorLookup"
				v-model:input-value="inputValue"
				class="ext-wikilambda-language-selector__lookup"
				:selected="selectedLanguage"
				:menu-items="lookupResults"
				:start-icon="icons.cdxIconSearch"
				:placeholder="selectLanguagePlaceholder"
				@update:input-value="onInput"
				@update:selected="onSelect"
			></cdx-lookup>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const { CdxButton, CdxIcon, CdxLookup } = require( '../../codex.js' );
const icons = require( '../../lib/icons.json' );
 
module.exports = exports = defineComponent( {
	name: 'wl-language-selector',
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon,
		'cdx-lookup': CdxLookup
	},
	data: function () {
		return {
			icons: icons,
			inputValue: '',
			lookupDelayTimer: null,
			lookupDelayMs: 300,
			lookupResults: [],
			maxItems: 10,
			selectedLanguage: null
		};
	},
	computed: {
		/**
		 * Returns the language iso code for the current selected language.
		 *
		 * @return {string}
		 */
		selectedLanguageCode: function () {
			return mw.config.get( 'wgUserLanguage' );
		},
 
		/**
		 * Returns the language name for the current selected language.
		 *
		 * @return {string}
		 */
		selectedLanguageLabel: function () {
			return mw.config.get( 'wgUserLanguageName' );
		},
 
		/**
		 * Returns the placeholder text for the global language
		 * selector
		 *
		 * @return {string}
		 */
		selectLanguagePlaceholder: function () {
			return this.$i18n( 'wikilambda-about-widget-search-language-placeholder' ).text();
		},
 
		/**
		 * Returns the current Uri path
		 *
		 * @return {string}
		 */
		currentPath: function () {
			return mw.Uri().path;
		},
 
		/**
		 * Returns whether the current path is a wikilambda
		 * /view/<lang>/<zid> content path
		 *
		 * @return {boolean}
		 */
		isViewPath: function () {
			return /^\/view\/.*\/.*$/.test( this.currentPath );
		}
	},
	methods: {
		/**
		 * Triggers the language lookup and redirects to the
		 * new language page for the given language code
		 *
		 * @param {string} input
		 */
		onInput: function ( input ) {
			// If empty input, clear and exit
			if ( !input ) {
				this.lookupResults = [];
				this.inputValue = '';
				return;
			}
 
			// Just search if more than one characters
			if ( input.length < 2 ) {
				return;
			}
 
			// Search after 300 ms
			clearTimeout( this.lookupDelayTimer );
			this.lookupDelayTimer = setTimeout( () => {
				this.fetchLookupResults( input );
			}, this.lookupDelayMs );
		},
 
		/**
		 * Performs the languageinfo fetch and filters
		 * the returned results for the lookup component
		 */
		fetchLookupResults: function () {
			const api = new mw.Api();
			api.get( {
				action: 'query',
				format: 'json',
				uselang: this.selectedLanguageCode,
				meta: 'languageinfo',
				formatversion: '2',
				liprop: 'code|name|autonym'
			} ).then( ( data ) => {
				if ( ( 'query' in data ) && ( 'languageinfo' in data.query ) ) {
					// Filter items that match the input substring
					const matchedLangs = Object.keys( data.query.languageinfo )
						.map( ( key ) => data.query.languageinfo[ key ] )
						.filter( ( result ) => result.name.toLowerCase().includes( this.inputValue.toLowerCase() ) ||
								result.autonym.toLowerCase().includes( this.inputValue.toLowerCase() ) ||
								result.code.toLowerCase().includes( this.inputValue.toLowerCase() ) );
					// Limit lookup reults to maxItems
					this.setLookupResults( matchedLangs.slice( 0, this.maxItems ) );
				}
			} );
		},
 
		/**
		 * Sets the menu items for the lookup component
		 * with the filtered results
		 *
		 * @param {Array} results
		 */
		setLookupResults: function ( results ) {
			this.lookupResults = [];
 
			// Update lookupResults list
			if ( results && results.length > 0 ) {
				results.forEach( ( result ) => {
					const value = result.code;
					const label = result.name;
					this.lookupResults.push( { value, label } );
				} );
			}
		},
 
		/**
		 * Returns the new Uri path given a selected language code
		 *
		 * @param {string} languageCode
		 * @return {string}
		 */
		getNewLanguagePath: function ( languageCode ) {
			// Either we are in /view/<lang>/zid and replace the lang url section...
			if ( this.isViewPath ) {
				const pathParts = this.currentPath.split( '/' );
				pathParts[ 2 ] = languageCode;
				return pathParts.join( '/' );
			}
			// ... or we have to use mw.Uri.extend to set or replace uselang=lang
			const uri = mw.Uri().extend( { uselang: languageCode } );
			return uri.toString();
		},
 
		/**
		 * Redirects to the current page in the specified language
		 *
		 * @param {string} languageCode
		 */
		redirectToLanguagePage: function ( languageCode ) {
			const targetUrl = this.getNewLanguagePath( languageCode );
			window.location.href = targetUrl;
		},
 
		/**
		 * On language selection, we find out the language Code that coresponds
		 * to the selected Zid, we fetch it if it's still not available in the
		 * store, and once we have it, we navigate to the new page
		 *
		 * @param {string} languageCode
		 */
		onSelect: function ( languageCode ) {
			// T374246: update:selected events are emitted with null value
			// whenever input changes, so we need to exit early whenever
			// selected value is null, instead of setting the value to empty
			// for now. When Codex fixes this issue, we'll be able to remove
			// the following lines and restore the clear behavior.
			if ( languageCode === null ) {
				return;
			}
 
			this.closeLanguageSelector();
 
			// If the language Zid is empty or the same as the current one, we pass
			if ( !languageCode || ( this.selectedLanguageCode === languageCode ) ) {
				return;
			}
 
			// We make sure that we know the corresponding language code before navigating out
			this.redirectToLanguagePage( languageCode );
		},
 
		/**
		 * Closes the language selector dropdown
		 */
		closeLanguageSelector: function () {
			// Hide dropdown
			const dropdown = this.$refs.languageSelectorDropdown;
			$( dropdown ).removeClass( 'ext-wikilambda-language-selector__dropdown--visible' );
		},
 
		/**
		 * Opens the language selector dropdown
		 */
		openLanguageSelector: function () {
			// Display dropdown
			const dropdown = this.$refs.languageSelectorDropdown;
			$( dropdown ).addClass( 'ext-wikilambda-language-selector__dropdown--visible' );
			// Focus selector
			// eslint-disable-next-line no-jquery/variable-pattern
			const lookup = this.$refs.languageSelectorLookup.$el;
			try {
				// Get input element from cdx-lookup->cdx-text-input->input
				// Wrap in try catch to avoid throwing errors in case of codex changes
				const input = lookup.firstChild.firstChild;
				input.focus();
			} catch ( e ) {
				return;
			}
		},
 
		/**
		 * Event handler to close the language selector
		 * dropdown if user clicks outside of the language
		 * selector section
		 *
		 * @param {Object} e
		 */
		handleClick: function ( e ) {
			const parent = this.$refs.languageSelector;
			if ( e.target !== parent && !parent.contains( e.target ) ) {
				this.closeLanguageSelector();
			}
		}
	},
	mounted: function () {
		window.addEventListener( 'click', this.handleClick );
	},
	beforeUnmount: function () {
		window.removeEventListener( 'click', this.handleClick );
	}
} );
</script>
 
<style lang="less">
.ext-wikilambda-language-selector {
	position: relative;
 
	&__dropdown {
		position: absolute;
		right: 0;
		display: none;
 
		&--visible {
			display: block;
		}
	}
}
</style>