All files / ext.wikilambda.edit/components/default-view-types ZMonolingualString.vue

100% Statements 31/31
87.5% Branches 7/8
100% Functions 14/14
100% Lines 31/31

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 23725x 25x 25x 25x     25x                                   6x                                               21x                 1x                           21x                 21x               4x                   21x         12x 10x   2x           6x             6x 6x     25x   25x     25x 25x 25x     25x         23x 23x 23x           8x                             1x           1x       25x                                                                                                                                  
<!--
	WikiLambda Vue component for Z11/Monolingual String objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-monolingual-string">
		<!-- Monolingual string on view mode -->
		<div
			v-if="!edit"
			class="ext-wikilambda-monolingual-string__view-mode"
		>
			<cdx-info-chip
				class="ext-wikilambda-lang-chip"
				:class="{ 'ext-wikilambda-lang-chip__empty': hasEmptyLang }"
			>
				{{ langIso.toUpperCase() }}
			</cdx-info-chip>
			{{ text }}
		</div>
		<!-- Monolingual string on edit mode -->
		<div
			v-else
			class="ext-wikilambda-monolingual-string__edit-mode"
			:style="inputCssVariablesStyle"
		>
			<cdx-info-chip
				ref="chipComponent"
				class="ext-wikilambda-lang-chip"
				:class="{ 'ext-wikilambda-lang-chip__empty': hasEmptyLang }"
			>
				{{ langIso.toUpperCase() }}
			</cdx-info-chip>
			<cdx-text-input
				v-model="text"
				placeholder="Enter text"
			>
			</cdx-text-input>
		</div>
	</div>
</template>
 
<script>
const CdxInfoChip = require( '@wikimedia/codex' ).CdxInfoChip,
	CdxTextInput = require( '@wikimedia/codex' ).CdxTextInput,
	Constants = require( '../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-monolingual-string',
	components: {
		'cdx-text-input': CdxTextInput,
		'cdx-info-chip': CdxInfoChip
	},
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		}
	},
	data: function () {
		return {
			chipComponent: null,
			chipWidth: 72
		};
	},
	computed: $.extend(
		mapGetters( [
			'getLanguageIsoCodeOfZLang',
			'getZMonolingualTextValue',
			'getZMonolingualLangValue'
		] ),
		{
			/**
			 * Computed value:
			 * 1. Getter gets the value from the state.
			 * 2. Setter informs the ZObjectKeyValue of the change.
			 * Only the ZObjectKeyValue responds to the 'setValue' emitted event
			 * so only the ZObjectKeyValue is doing operations to transform
			 * the state data. This is so that we don't duplicate state mutation
			 * logic all over the components, and builtin components are just
			 * visual representations and have zero logic.
			 */
			text: {
				/**
				 * Returns the terminal value of the string represented
				 * in this component.
				 *
				 * @return {string}
				 */
				get: function () {
					return this.getZMonolingualTextValue( this.rowId );
				},
				/**
				 * Emits a setValue event with the new value for the string
				 * and the key path information depending on the object key.
				 *
				 * @param {string} value
				 */
				set: function ( value ) {
					this.$emit( 'set-value', {
						keyPath: [
							Constants.Z_MONOLINGUALSTRING_VALUE,
							Constants.Z_STRING_VALUE
						],
						value
					} );
				}
			},
 
			/**
			 * Returns the language Zid of the Monolingual string
			 * object represented in this component, or the language code
			 * if lang is a literal.
			 *
			 * @return {string}
			 */
			lang: function () {
				return this.getZMonolingualLangValue( this.rowId );
			},
 
			/**
			 * Return the text that identifies the language in which
			 * this Monolingual String is written.
			 *
			 * @return {string}
			 */
			langIso: function () {
				return this.getLanguageIsoCodeOfZLang( this.lang ) || '';
			},
			/**
			 * Returns the dynamically calculated width of the inner language chip
			 *
			 * @return {string}
			 */
			inputCssVariablesStyle: function () {
				return {
					'--chipWidthPx': `${ this.chipWidth }px`
				};
			},
			/**
			 * Whether the language is still not defined, so langIso is an empty string
			 *
			 * @return {boolean}
			 */
			hasEmptyLang: function () {
				return ( this.langIso === '' );
			}
		}
	),
	methods: {
		getAndStoreChipWidth() {
			if ( !this.chipComponent ) {
				return;
			}
			this.chipWidth = this.chipComponent.$el.offsetWidth;
		}
	},
	watch: {
		langIso: {
			handler: function () {
				this.getAndStoreChipWidth();
			},
			immediate: true,
			flush: 'post'
		}
	},
	mounted() {
		this.chipComponent = this.$refs.chipComponent;
		this.getAndStoreChipWidth();
	}
};
 
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-monolingual-string {
	.ext-wikilambda-lang-chip {
		.cdx-info-chip--text {
			font-size: ~'14px';
		}
 
		&__empty {
			border: 1px dashed @border-color-base;
 
			.cdx-info-chip--text {
				height: 22px;
				min-width: 22px;
			}
		}
	}
 
	&__view-mode {
		margin: 0;
		color: @color-base;
		display: flex;
		flex-direction: row;
		align-items: flex-start;
 
		.ext-wikilambda-lang-chip {
			margin-right: @spacing-50;
		}
	}
 
	&__edit-mode {
		min-height: @min-size-interactive-pointer;
		display: flex;
		flex-direction: row;
		align-items: center;
		position: relative;
		z-index: 3;
		min-width: calc( 36px - 16px );
 
		.cdx-text-input__input {
			--spacing-50: @spacing-50;
			padding-left: ~'calc( var(--spacing-50) + var(--chipWidthPx) + var(--spacing-50) )';
		}
 
		.ext-wikilambda-lang-chip {
			position: absolute;
			z-index: 3;
			min-width: calc( 36px - 16px );
			left: @spacing-50;
		}
	}
}
</style>