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

97.5% Statements 39/40
92.85% Branches 13/14
93.75% Functions 15/16
97.5% Lines 39/40

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 29624x 24x 24x 24x     24x                                               175x               171x                           175x                 1x 1x                         130x               171x               131x               175x                 131x                 28x                     17x     24x   24x     24x 24x 24x       24x       24x     24x     24x     24x     24x       24x         24x       24x         175x 175x 175x 175x                                 64x       1x             28x                         24x                                                                                                                                                      
<!--
	WikiLambda Vue component for Z14/Implementation objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div
		class="ext-wikilambda-implementation"
		data-testid="implementation"
	>
		<!-- Function selection block -->
		<div
			class="ext-wikilambda-implementation-function"
			data-testid="implementation-function"
		>
			<div class="ext-wikilambda-key-block">
				<label>{{ functionLabel }}</label>
			</div>
			<wl-z-object-key-value
				:key="functionRowId"
				:row-id="functionRowId"
				:skip-key="true"
				:skip-indent="true"
				:edit="edit && !isTypeBuiltin"
			></wl-z-object-key-value>
		</div>
		<!-- Implementation type block -->
		<div class="ext-wikilambda-implementation-type">
			<div class="ext-wikilambda-key-block">
				<label>{{ implementationLabel }}</label>
			</div>
			<div class="ext-wikilambda-value-block">
				<!-- Show warning message for builtins -->
				<cdx-message
					v-if="isTypeBuiltin"
					:inline="true">
					{{ $i18n( 'wikilambda-implementation-selector-none' ).text() }}
				</cdx-message>
				<!-- Show radio button for code or composition -->
				<template v-else>
					<span
						v-if="!edit"
						class="ext-wikilambda-value-text"
					>{{ implementationTypeLabel }}</span>
					<div
						v-else
						class="ext-wikilambda-value-input"
						data-testid="implementation-radio"
					>
						<cdx-radio
							v-for="radio in radioChoices"
							:key="'radio-' + radio.value"
							v-model="implementationType"
							:input-value="radio.value"
							:name="'implementation-radios-' + rowId"
							:inline="true"
						>
							{{ radio.label }}
						</cdx-radio>
					</div>
				</teEmplate>
			</div>
		</div>
		<!-- Implementation content block -->
		<div
			v-if="!isTypeBuiltin"
			class="ext-wikilambda-implementation-content"
		>
			<div
				v-if="!isTypeCode"
				class="ext-wikilambda-key-block"
			>
				<label>{{ implementationTypeLabel }}</label>
			</div>
			<wl-z-object-key-value
				:key="implementationContentRowId"
				:skip-key="true"
				:skip-indent="isTypeCode"
				:row-id="implementationContentRowId"
				:error-id="implementationContentRowId"
				:edit="edit"
				data-testid="implementation-content-block"
			></wl-z-object-key-value>
		</div>
	</div>
</template>
 
<script>
const CdxRadio = require( '@wikimedia/codex' ).CdxRadio,
	CdxMessage = require( '@wikimedia/codex' ).CdxMessage,
	Constants = require( '../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-implementation',
	components: {
		'cdx-message': CdxMessage,
		'cdx-radio': CdxRadio
	},
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		}
	},
 
	computed: $.extend(
		mapGetters( [
			'getZImplementationFunctionRowId',
			'getZImplementationContentType',
			'getZImplementationContentRowId',
			'getLabel'
		] ),
		{
			/**
			 * Returns the row Id of the target function key: Z14K1
			 *
			 * @return {number|undefined}
			 */
			functionRowId: function () {
				return this.getZImplementationFunctionRowId( this.rowId );
			},
 
			/**
			 * Returns the human readable label for "Function"
			 *
			 * @return {string}
			 */
			functionLabel: function () {
				return this.getLabel( Constants.Z_IMPLEMENTATION_FUNCTION );
			},
 
			/**
			 * Active implementation key, that which contains the implementation
			 * content. The values are Z14K2 for composition, Z14K3 for code or
			 * Z14K4 for built-in.
			 */
			implementationType: {
				/**
				 * Returns the implementation type or key which is selected
				 *
				 * @return {string}
				 */
				get: function () {
					return this.getZImplementationContentType( this.rowId );
				},
				/**
				 * Sets the implementation type and initializes the value
				 * to a blank scaffolding depending on what key is selected
				 *
				 * @param {string} value
				 */
				set: function ( value ) {
					if ( this.edit ) {
						this.$emit( 'set-value', {
							keyPath: [ value ],
							value: ''
						} );
					}
				}
			},
 
			/**
			 * Returns the human readable label for the implementation type
			 *
			 * @return {string}
			 */
			implementationTypeLabel: function () {
				return this.getLabel( this.implementationType );
			},
 
			/**
			 * Returns the human readable label for "Implementation"
			 *
			 * @return {string}
			 */
			implementationLabel: function () {
				return this.getLabel( Constants.Z_IMPLEMENTATION );
			},
 
			/**
			 * Whether the implementation content is of type code (Z14K3)
			 *
			 * @return {boolean}
			 */
			isTypeCode: function () {
				return ( this.implementationType === Constants.Z_IMPLEMENTATION_CODE );
			},
 
			/**
			 * Whether the implementation content is of type builtin (Z14K4)
			 *
			 * @return {boolean}
			 */
			isTypeBuiltin: function () {
				return ( this.implementationType === Constants.Z_IMPLEMENTATION_BUILT_IN );
			},
 
			/**
			 * Returns the ID of the row where the implementation content
			 * starts, depending on which implementation type is selected.
			 *
			 * @return {number|undefined}
			 */
			implementationContentRowId: function () {
				return this.getZImplementationContentRowId(
					this.rowId,
					this.implementationType
				);
			},
 
			/**
			 * Returns the formatted choices for the Codex Radio component
			 * to select the type of implementation (code vs composition)
			 *
			 * @return {Array}
			 */
			radioChoices: function () {
				return [
					{
						label: this.getLabel( Constants.Z_IMPLEMENTATION_CODE ),
						value: Constants.Z_IMPLEMENTATION_CODE
					},
					{
						label: this.getLabel( Constants.Z_IMPLEMENTATION_COMPOSITION ),
						value: Constants.Z_IMPLEMENTATION_COMPOSITION
					}
				];
			}
		}
	),
	beforeCreate: function () {
		// Need to delay require of ZObjectKeyValue to avoid loop
		this.$options.components[ 'wl-z-object-key-value' ] = require( './ZObjectKeyValue.vue' );
	}
};
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-implementation {
	.ext-wikilambda-implementation-function {
		.ext-wikilambda-key-block {
			margin-bottom: 0;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
	}
 
	.ext-wikilambda-implementation-type {
		padding-top: @spacing-75;
 
		& > .ext-wikilambda-key-block {
			margin-bottom: 0;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
 
		& > .ext-wikilambda-value-block {
			margin-bottom: @spacing-75;
 
			.ext-wikilambda-value-input {
				margin-top: @spacing-25;
			}
		}
	}
 
	.ext-wikilambda-implementation-content {
		padding-top: @spacing-75;
 
		& > .ext-wikilambda-key-block {
			margin-bottom: 0;
 
			label {
				font-weight: bold;
				color: @color-base;
			}
		}
	}
}
</style>