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

92.2% Statements 71/77
92.85% Branches 26/28
82.75% Functions 24/29
92.2% Lines 71/77

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    16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x                                                     27x                               3x     3x 3x     3x 6x 6x 6x 6x 6x                 3x     3x     3x                     6x 6x                 2x     9x     1x     1x     1x 1x         11x 1x   10x       27x   27x 25x             16x   16x     16x 16x 16x     16x     16x     16x 16x     16x     16x     16x     16x     16x 16x     16x     16x     16x       28x 28x 28x 28x 28x 28x       29x     29x                       40x               29x                                           32x                       1x                                 20x             38x             16x                                                                                                                                                                        
<!--
	WikiLambda Vue component widget for exploring function details.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<wl-widget-base class="ext-wikilambda-function-explorer">
		<template #header>
			{{ $i18n( 'wikilambda-function-explorer-title' ).text() }}
		</template>
		<template #header-action>
			<cdx-button
				v-if="edit"
				weight="quiet"
				:class="{ 'ext-wikilambda-function-explorer-disabled': resetButtonDisabled }"
				:aria-label="$i18n( 'wikilambda-function-explorer-accessible-label' ).text()"
				:disabled="resetButtonDisabled"
				data-testid="function-explorer-reset-button"
				@click="resetFunction"
			>
				<cdx-icon :icon="resetIcon"></cdx-icon>
			</cdx-button>
		</template>
 
		<template #main>
			<!-- Function name & lookup -->
			<section>
				<div
					class="ext-wikilambda-function-explorer-flex ext-wikilambda-function-explorer-space-between
					ext-wikilambda-function-explorer-function-name-wrapper">
					<h5 class="ext-wikilambda-function-explorer-heading-no-spacing">
						{{ $i18n( 'wikilambda-function-explorer-name-title' ) }}
					</h5>
					<span
						v-if="implementation === Constants.Z_IMPLEMENTATION_CODE"
						class="ext-wikilambda-function-explorer-copyable"
						:class="{ 'ext-wikilambda-function-explorer-untitled': functionisUntitled }"
						data-testid="function-zid"
						@click.stop="copyToClipboard( functionZid )"
					>{{ showValueOrCopiedMessage( functionZid ) }}</span>
				</div>
				<!-- Edit mode -->
				<template v-if="edit">
					<wl-z-object-selector
						:type="Constants.Z_FUNCTION"
						:placeholder="$i18n( 'wikilambda-function-typeselector-label' ).text()"
						:selected-zid="currentFunctionZid"
						data-testid="function-selector"
						@input="updateSelectedFunction"
					>
					</wl-z-object-selector>
				</template>
				<!-- Read mode -->
				<div
					v-else
					class="ext-wikilambda-function-explorer-flex
						ext-wikilambda-function-explorer-space-between
						ext-wikilambda-function-explorer-dark-links"
				>I
					<a
						:href="getWikiUrl( currentFunctionZid )"
						data-testid="function-name"
					>
						{{ functionName }}
					</a>
				</div>
			</section>
 
			<!-- Function details (inputs and outputs) -->
			<section v-if="functionExists && functionArguments.length">
				<div class="ext-wikilambda-function-explorer-function-inputs-title-wrapper">
					<h5 class="ext-wikilambda-function-explorer-heading-no-spacing">
						{{ $i18n( 'wikilambda-function-inputs-title' ).text() }}
					</h5>
				</div>
 
				<!-- TODO: Consider using v-memo to optimize -->
				<!-- Function inputs/arguments -->
				<div
					v-for="arg in functionArguments"
					:key="arg.label"
					class="ext-wikilambda-function-explorer-function-inputs-wrapper"
 
				>
					<div class="ext-wikilambda-function-explorer-flex ext-wikilambda-function-explorer-space-between">
						<span class="ext-wikilambda-function-explorer-type ext-wikilambda-function-explorer-dark-links">
							<wl-type-to-string
								data-testid="function-input-type"
								:type="arg.type"
							></wl-type-to-string>
						</span>
						<span
							v-if="implementation === Constants.Z_IMPLEMENTATION_CODE"
							class="ext-wikilambda-function-explorer-copyable"
							data-testid="function-input-zkey"
							data-title="Click to copy"
							@click.stop="copyToClipboard( arg.keyZid )"
						>
							{{ showValueOrCopiedMessage( arg.keyZid ) }}
						</span>
					</div>
					<span
						:class="{ 'ext-wikilambda-function-explorer-untitled': arg.isUntitled }"
						data-testid="function-input-name"
					>
						{{ arg.label }}
					</span>
				</div>
 
				<div class="ext-wikilambda-function-explorer-function-outputs-title-wrapper">
					<h5 class="ext-wikilambda-function-explorer-heading-no-spacing">
						{{ $i18n( 'wikilambda-function-definition-output-label' ).text() }}
					</h5>
					<span class="ext-wikilambda-function-explorer-type ext-wikilambda-function-explorer-dark-links">
						<wl-type-to-string
							data-testid="function-output"
							:type="outputType"
						></wl-type-to-string>
					</span>
				</div>
			</section>
		</template>
		<template v-if="edit && functionExists" #footer>
			<div class="ext-wikilambda-function-explorer-footer-wrapper">
				<cdx-button
					class="ext-wikilambda-function-explorer-button-view-function"
					action="progressive"
					data-testid="button-view-function"
					@click="navigateToFunction">
					{{ $i18n( 'wikilambda-function-view-function-button-text' ).text() }}
				</cdx-button>
			</div>
		</template>
	</wl-widget-base>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const
	Constants = require( '../../Constants.js' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	WidgetBase = require( '../base/WidgetBase.vue' ),
	TypeToString = require( '../base/TypeToString.vue' ),
	ZObjectSelector = require( '../base/ZObjectSelector.vue' ),
	mapGetters = require( 'vuex' ).mapGetters,
	icons = require( '../../../lib/icons.json' ),
	typeUtils = require( '../../mixins/typeUtils.js' );
 
module.exports = exports = defineComponent( {
	name: 'wl-function-explorer',
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon,
		'wl-type-to-string': TypeToString,
		'wl-widget-base': WidgetBase,
		'wl-z-object-selector': ZObjectSelector
	},
	mixins: [ typeUtils ],
	props: {
		edit: {
			type: Boolean,
			default: false
		},
		/** The default function object selected for this component */
		functionZid: {
			type: String,
			required: false,
			default: ''
		},
		implementation: {
			type: String,
			default: null
		}
	},
	data() {
		return {
			currentFunctionZid: this.functionZid,
			initialFunctionObject: null,
			Constants: Constants,
			resetIcon: icons.cdxIconHistory,
			itemsCopied: []
		};
	},
	computed: Object.assign(
		mapGetters( [
			'getUserLangCode',
			'getStoredObject',
			'getInputsOfFunctionZid',
			'getLabelData'
		] ),
		{
			functionIsUntitled: function () {
				return this.getLabelOrUntitledObject( this.currentFunctionZid ).isUntitled;
			},
			functionName: function () {
				return this.getLabelOrUntitledObject( this.currentFunctionZid ).text;
			},
			functionObject: function () {
				return this.getStoredObject( this.currentFunctionZid );
			},
			functionArguments: function () {
				const args = this.getInputsOfFunctionZid( this.currentFunctionZid );
 
				if ( !args ) {
					return [];
				}
 
				return args.map( ( arg ) => {
					const keyZid = arg[ Constants.Z_ARGUMENT_KEY ];
 
					const argLabelObject = this.getLabelOrUntitledObject( keyZid );
					const label = argLabelObject.text;
					const isUntitled = argLabelObject.isUntitled;
 
					return {
						label,
						isUntitled,
						keyZid,
						type: arg[ Constants.Z_ARGUMENT_TYPE ]
					};
				} );
			},
			functionExists: function () {
				return Boolean( this.functionObject );
			},
			outputType: function () {
				return this.functionObject[ Constants.Z_PERSISTENTOBJECT_VALUE ][
					Constants.Z_FUNCTION_RETURN_TYPE ];
			},
			resetButtonDisabled: function () {
				return this.currentFunctionZid === this.functionZid;
			}
		}
	),
	methods: {
		/**
		 * Gets and returns a label for a given zid if found. Otherwise returns the equivalent of 'Untitled'
		 *
		 * @param {string} zid
		 * @return {string}
		 */
		getLabelOrUntitledObject( zid ) {
			const labelData = this.getLabelData( zid );
 
			return {
				isUntitled: !labelData,
				text: labelData ? labelData.label : this.$i18n( 'wikilambda-editor-default-name' ).text()
			};
		},
		updateSelectedFunction( zIdSelected ) {
			this.currentFunctionZid = zIdSelected;
		},
		resetFunction() {
			this.currentFunctionZid = this.functionZid;
		},
		getWikiUrl( zid ) {
			return '/view/' + this.getUserLangCode + '/' + zid;
		},
		navigateToFunction() {
			window.open( this.getWikiUrl( this.currentFunctionZid ), '_blank' );
		},
		copyToClipboard( value ) {
			navigator.clipboard.writeText( value );
 
			// This will allow us to display a "copied" message for a short time
			this.itemsCopied.push( value );
 
			setTimeout( () => {
				this.itemsCopied.shift();
			}, 2000 );
		},
		showValueOrCopiedMessage( value ) {
			if ( this.itemsCopied.includes( value ) ) {
				return this.$i18n( 'wikilambda-function-explorer-copied-text' ).text();
			}
 
			return value;
		}
 
	},
	created: function () {
		this.$watch(
			'functionObject',
			( newFunctionObject ) => {
				// Only update the initial function if a functionZid was provided
				if ( this.functionZid ) {
					this.initialFunctionObject = newFunctionObject || this.initialFunctionObject;
				}
			},
			{ immediate: true }
		);
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-function-explorer {
	.cdx-lookup {
		min-width: fit-content;
	}
 
	.cdx-text-input {
		.cdx-text-input__input {
			min-width: fit-content;
		}
	}
 
	.ext-wikilambda-function-explorer-dark-links {
		a,
		a:visited,
		a:hover,
		a:active {
			color: @color-base;
		}
	}
 
	.ext-wikilambda-function-explorer-heading-no-spacing {
		margin-top: 0;
		padding-top: 0;
	}
 
	.ext-wikilambda-function-explorer-function-name-wrapper {
		margin-bottom: @spacing-25;
	}
 
	.ext-wikilambda-function-explorer-function-inputs-wrapper:not( :last-child ) {
		margin-bottom: @spacing-50;
	}
 
	.ext-wikilambda-function-explorer-untitled {
		color: @color-placeholder;
	}
 
	.ext-wikilambda-function-explorer-function-inputs-title-wrapper,
	.ext-wikilambda-function-explorer-function-outputs-title-wrapper {
		margin-top: @spacing-100;
	}
 
	.ext-wikilambda-function-explorer-flex {
		display: flex;
		align-items: center;
	}
 
	.ext-wikilambda-function-explorer-space-between {
		justify-content: space-between;
	}
 
	.ext-wikilambda-function-explorer-copyable {
		font-family: @font-family-monospace;
		cursor: pointer;
	}
 
	.ext-wikilambda-function-explorer-button-view-function {
		font-size: @wl-font-size-base;
	}
 
	.ext-wikilambda-function-explorer-disabled {
		cursor: not-allowed;
	}
}
</style>