All files / ext.wikilambda.edit/components/widgets About.vue

91.2% Statements 83/91
80.43% Branches 37/46
87.87% Functions 29/33
91.2% Lines 83/91

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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458    17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x                                           25x                                 354x                 374x                 374x                 130x                 374x                 374x               111x               130x                   374x               374x               25x               34x     34x 34x                 34x     34x 34x 56x 56x 56x 56x 56x 56x                         1x               8x             5x 5x 5x             5x 5x                               17x   17x     17x 17x 17x     17x       17x     17x       17x       17x     17x       17x       17x     17x     17x 17x       17x     17x     17x     17x     17x       374x 374x 374x 374x 374x 374x 374x 374x         383x     383x               102x               383x         259x                         56x                                                       3x                             383x         167x                 17x                                                                                                                              
<!--
	WikiLambda Vue component - About Widget.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-widget-base class="ext-wikilambda-about" data-testid="about">
		<!-- Widget header -->
		<template #header>
			{{ $i18n( 'wikilambda-about-widget-title' ).text() }}
		</template>
		<template #header-action>
			<cdx-button
				weight="quiet"
				aria-label="Edit"
				data-testid="open-dialog-button"
				:disabled="!canEditObject"
				@click="openUserLanguageDialog"
			>
				<cdx-icon :icon="icons.cdxIconEdit"></cdx-icon>
			</cdx-button>
		</template>
 
		<!-- Widget main -->
		<template #main>
			<div class="ext-wikilambda-about-fields">
				<!-- No descriptions or aliases message -->
				<div v-if="!hasDescription && !hasAliases" class="ext-wikilambda-about-unavailable">
					{{ $i18n( 'wikilambda-about-widget-no-descriptions-or-aliases' ).text() }}
				</div>
				<template v-else>
					<!-- Description block -->
					<div class="ext-wikilambda-about-description">
						<span v-if="hasDescription" class="ext-wikilambda-about-value">
							{{ description }}
						</span>
						<span v-else class="ext-wikilambda-about-unavailable">
							{{ $i18n( 'wikilambda-about-widget-no-descriptions' ).text() }}
						</span>
					</div>
					<!-- Aliases block -->
					<div class="ext-wikilambda-about-aliases">
						<template v-if="hasAliases">
							<span
								v-for="( alias, index ) in visibleAliases"
								:key="'alias-' + index"
								class="ext-wikilambda-about-alias"
							>
								{{ alias.value }}
							</span>
							<a
								v-if="aliases.length > 3 && !seeAllAliases"
								class="ext-wikilambda-about-aliases-more"
								@click="seeAllAliases = true"
							>+{{ aliases.length - 3 }}</a>
						</template>
						<span v-else class="ext-wikilambda-about-unavailable">
							{{ $i18n( 'wikilambda-about-widget-no-aliases' ).text() }}
						</span>
					</div>
				</template>
			</div>
 
			<!-- Function specific fields -->
			<div v-if="isFunction" class="ext-wikilambda-about-function-fields">
				<div class="ext-wikilambda-about-function-input">
					<!-- Inputs -->
					<div class="ext-wikilambda-about-function-field-title">
						{{ $i18n( 'wikilambda-function-definition-inputs-label' ).text() }}
					</div>
					<template v-if="inputs.length > 0">
						<div
							v-for="input in inputs"
							:key="input.key"
							class="ext-wikilambda-about-function-field-value"
						>
							<span
								class="ext-wikilambda-about-function-input-label"
								:class="{ 'ext-wikilambda-about-unavailable': !input.hasLabel }"
							>{{ input.label }}<span>:</span></span>
							<wl-z-object-to-string :row-id="input.typeRowId"></wl-z-object-to-string>
						</div>
					</template>
					<div v-else class="ext-wikilambda-about-function-field-value">
						<span class="ext-wikilambda-about-unavailable">
							{{ $i18n( 'wikilambda-about-widget-no-inputs' ).text() }}
						</span>
					</div>
				</div>
				<!-- Output -->
				<div class="ext-wikilambda-about-function-output">
					<div class="ext-wikilambda-about-function-field-title">
						{{ $i18n( 'wikilambda-function-definition-output-label' ).text() }}
					</div>
					<div class="ext-wikilambda-about-function-field-value">
						<wl-z-object-to-string :row-id="outputTypeRowId"></wl-z-object-to-string>
					</div>
				</div>
			</div>
 
			<!-- Dialogs -->
			<wl-about-view-languages-dialog
				:can-edit="canEditObject"
				:open="showViewLanguagesDialog"
				@open-edit-language="openEditLanguageDialog"
				@change-selected-language="changeSelectedLanguage"
				@close="showViewLanguagesDialog = false"
			>
			</wl-about-view-languages-dialog>
			<wl-about-edit-metadata-dialog
				:can-edit="canEditObject"
				:edit="edit"
				:for-language="selectedLanguage"
				:open="showEditMetadataDialog"
				:is-function="isFunction"
				@change-selected-language="changeSelectedLanguage"
				@close="showEditMetadataDialog = false"
				@publish="showPublishDialog = true"
			>
			</wl-about-edit-metadata-dialog>
			<wl-publish-dialog
				v-if="!edit"
				:show-dialog="showPublishDialog"
				@close-dialog="cancelPublish"
			></wl-publish-dialog>
		</template>
 
		<!-- Widget footer -->
		<template v-if="languageCount > 0" #footer>
			<div class="ext-wikilambda-about-button">
				<cdx-button data-testid="language-buttons" @click="openViewLanguagesDialog">
					<cdx-icon :icon="icons.cdxIconLanguage"></cdx-icon>
					{{ $i18n( 'wikilambda-about-widget-language-count-button', languageCount ).text() }}
				</cdx-button>
			</div>
		</template>
	</wl-widget-base>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	AboutViewLanguagesDialog = require( './AboutViewLanguagesDialog.vue' ),
	AboutEditMetadataDialog = require( './AboutEditMetadataDialog.vue' ),
	PublishDialog = require( './PublishDialog.vue' ),
	ZObjectToString = require( '../default-view-types/ZObjectToString.vue' ),
	WidgetBase = require( '../base/WidgetBase.vue' ),
	CdxBuItton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	icons = require( '../../../lib/icons.json' ),
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-about-widget',
	components: {
		'cdx-icon': CdxIcon,
		'cdx-button': CdxButton,
		'wl-about-view-languages-dialog': AboutViewLanguagesDialog,
		'wl-about-edit-metadata-dialog': AboutEditMetadataDialog,
		'wl-Ipublish-dialog': PublishDialog,
		'wl-z-object-to-string': ZObjectToString,
		'wl-widget-base': WidgetBase
	},
	props: {
		edit: {
			type: Boolean,
			required: true
		},
		type: {
			type: String,
			required: true
		}
	},
	data: function () {
		return {
			icons: icons,
			showViewLanguagesDialog: false,
			showEditMetadataDialog: false,
			showPublishDialog: false,
			selectedLanguage: '',
			seeAllAliases: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getLabel',
		'getMetadataLanguages',
		'getUserLangZid',
		'getZArgumentTypeRowId',
		'getZArgumentKey',
		'getZFunctionOutput',
		'getZFunctionInputs',
		'getZMonolingualTextValue',
		'getZMonolingualStringsetValues',
		'getZPersistentAlias',
		'getZPersistentDescription',
		'isCreateNewPage',
		'isDirty',
		'isUserLoggedIn'
	] ), {
		/**
		 * Returns whether the user can edit the function
		 *
		 * @return {boolean}
		 */
		canEditObject: function () {
			// TODO (T301667): restrict to only certain user roles
			return this.isCreateNewPage ? true : this.isUserLoggedIn;
		},
		/**
		 * Returns the best Description (Z2K5) row depending
		 * on the user preferred language.
		 *
		 * @return {Object}
		 */
		selectedDescriptionObject: function () {
			return this.getZPersistentDescription();
		},
		/**
		 * Returns the best Alias (Z2K4) row depending
		 * on the user preferred language.
		 *
		 * @return {Object}
		 */
		selectedAliasesObject: function () {
			return this.getZPersistentAlias();
		},
		/**
		 * Returns the visible aliases depending on
		 * the seeAllAliases flag.
		 *
		 * @return {Array}
		 */
		visibleAliases: function () {
			return this.seeAllAliases ? this.aliases : this.aliases.slice( 0, 3 );
		},
		/**
		 * Returns whether the object has any available description
		 * (in any language)
		 *
		 * @return {boolean}
		 */
		hasDescription: function () {
			return this.selectedDescriptionObject !== undefined;
		},
		/**
		 * Returns whether the object has any available aliases
		 * (in any language)
		 *
		 * @return {boolean}
		 */
		hasAliases: function () {
			return this.selectedAliasesObject !== undefined;
		},
 
		/**
		 * Returns the string value for the selected description, if any
		 *
		 * @return {string}
		 */
		description: function () {
			return this.hasDescription ?
				this.getZMonolingualTextValue( this.selectedDescriptionObject.rowId ) :
				'';
		},
		/**
		 * Returns the array of strings for the selected aliases, if any
		 *
		 * @return {string}
		 */
		aliases: function () {
			return this.hasAliases ?
				this.getZMonolingualStringsetValues( this.selectedAliasesObject.rowId ) :
				[];
		},
		/**
		 * Returns a list of all the language Zids that are present
		 * in the metadata collection (must have at least a name, a
		 * description or a set of aliases).
		 *
		 * @return {Array}
		 */
		languages: function () {
			return this.getMetadataLanguages();
		},
		/**
		 * Returns the count of the list of unique metadata languages
		 *
		 * @return {number}
		 */
		languageCount: function () {
			return this.languages.length;
		},
		/**
		 * Returns whether the current object is a function
		 *
		 * @return {boolean}
		 */
		isFunction: function () {
			return this.type === Constants.Z_FUNCTION;
		},
		/**
		 * Returns the rowId of the output type
		 *
		 * @return {number|undefined}
		 */
		outputTypeRowId: function () {
			if ( !this.isFunction ) {
				return undefined;
			}
			const outputTypeRow = this.getZFunctionOutput();
			return outputTypeRow ? outputTypeRow.id : undefined;
		},
		/**
		 * Returns the array of input data: its label or 'Unlabelled',
		 * its typeRowId, and whether the input has a label or not.
		 *
		 * @return {Array}
		 */
		inputs: function () {
			if ( !this.isFunction ) {
				return [];
			}
			const inputs = this.getZFunctionInputs();
			return inputs.map( ( row ) => {
				const typeRowId = this.getZArgumentTypeRowId( row.id );
				const key = this.getZArgumentKey( row.id );
				const labelData = this.getLabel( key );
				const hasLabel = labelData !== key;
				const label = hasLabel ?
					labelData :
					this.$i18n( 'wikilambda-about-widget-unlabelled-input' ).text();
				return {
					label,
					typeRowId,
					hasLabel
				};
			} );
		}
	} ),
	methods: Object.assign( mapActions( [
		'resetMultilingualData'
	] ), {
		/**
		 * Opens the AboutEditMetadataDialog
		 */
		openViewLanguagesDialog: function () {
			this.showViewLanguagesDialog = true;
		},
		/**
		 * Changes the selected language to the given value
		 *
		 * @param {string} lang
		 */
		changeSelectedLanguage: function ( lang ) {
			this.selectedLanguage = lang;
		},
		/**
		 * Opens the AboutViewLanguagesDialog for the current
		 * selected language.
		 */
		openEditLanguageDialog: function () {
			this.showViewLanguagesDialog = false;
			this.showEditMetadataDialog = true;
			this.$emit( 'edit-metadata' );
		},
		/**
		 * Opens the AboutViewLanguagesDialog with the first
		 * language, which is the one with the selected Name.
		 */
		openUserLanguageDialog: function () {
			this.changeSelectedLanguage( this.getUserLangZid );
			this.openEditLanguageDialog();
		},
		/**
		 * Restores original view state and cancels publish
		 * action. This only happens when we are in a view page
		 * and after editing information, we proceed to publish
		 * and then we cancel.
		 */
		cancelPublish: function () {
			if ( this.isDirty ) {
				this.resetMultilingualData();
			}
			this.showPublishDialog = false;
		}
	} )
} );
 
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-about {
	.ext-wikilambda-about-unavailable {
		color: @color-placeholder;
 
		& > span {
			color: @color-base;
		}
	}
 
	.ext-wikilambda-about-fields {
		.ext-wikilambda-about-description,
		.ext-wikilambda-about-aliases {
			margin-top: @spacing-50;
		}
 
		.ext-wikilambda-about-aliases {
			padding-bottom: @spacing-25;
			display: flex;
			flex-wrap: wrap;
			gap: 8px;
 
			.ext-wikilambda-about-alias {
				display: inline-block;
				border-radius: @border-radius-pill;
				border: 1px solid @border-color-subtle;
				color: @color-subtle;
				padding: 0 @spacing-50;
			}
 
			.ext-wikilambda-about-aliases-more {
				color: @color-progressive;
			}
		}
	}
 
	.ext-wikilambda-about-function-fields {
		margin-top: @spacing-100;
		border-top: 1px solid @border-color-subtle;
 
		& > div {
			margin-top: @spacing-100;
		}
 
		.ext-wikilambda-about-function-field-title {
			font-weight: @font-weight-bold;
		}
 
		.ext-wikilambda-about-function-field-value {
			margin-top: @spacing-25;
			display: flex;
 
			& > span {
				margin-right: @spacing-25;
			}
		}
	}
 
	.ext-wikilambda-about-button {
		margin-top: @spacing-125;
	}
}
</style>