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

100% Statements 23/23
87.5% Branches 14/16
100% Functions 9/9
100% Lines 23/23

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 16225x 25x 25x 25x     25x                                                               83x                 29x               29x                   17x                 54x               24x                     54x                     4x 4x             25x   25x     25x 25x 25x     25x   83x 83x                             25x                                          
<!--
	WikiLambda Vue component for Z9/Reference objects.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-reference">
		<template v-if="!edit">
			<a
				class="ext-wikilambda-edit-link"
				:href="valueUrl">{{ valueLabel }}</a>
		</template>
		<template v-else>
			<wl-z-object-selector
				:edit="edit"
				:disabled="disabled"
				:row-id="rowId"
				:selected-zid="value"
				:type="selectType"
				:exclude-zids="excludeZids"
				data-testid="z-reference-selector"
				@input="setValue"
			></wl-z-object-selector>
		</template>
	</div>
</template>
 
<script>
const Constants = require( '../../Constants.js' ),
	ZObjectSelector = require( './../base/ZObjectSelector.vue' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-reference',
	components: {
		'wl-z-object-selector': ZObjectSelector
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		},
		expectedType: {
			type: [ String, Object ],
			required: true
		},
		disabled: {
			type: Boolean,
			default: false
		}
	},
	computed: $.extend(
		mapGetters( [
			'getLabel',
			'getParentRowId',
			'getZObjectKeyByRowId',
			'getZReferenceTerminalValue'
		] ),
		{
			/**
			 * Returns the value of the selected reference.
			 *
			 * @return {string}
			 */
			value: function () {
				return this.getZReferenceTerminalValue( this.rowId );
			},
 
			/**
			 * Returns the string value of the label for the selected reference.
			 * If no label is found, returns undefined.
			 *
			 * @return {string|undefined}
			 */
			valueLabel: function () {
				return this.value ? this.getLabel( this.value ) : undefined;
			},
 
			/**
			 * Returns the link to the page of the selected reference.
			 *
			 * @return {string}
			 */
			valueUrl: function () {
				return '/view/' + ( mw.language.getFallbackLanguageChain()[ 0 ] || 'en' ) + '/' + this.value;
			},
 
			/**
			 * Returns the bound type to configure the ZObjectSelector
			 * if any, else returns an empty string. The type must be
			 * converted to string with no args in case it's a generic type.
			 *
			 * @return {string}
			 */
			selectType: function () {
				return !this.expectedType || ( this.expectedType === Constants.Z_OBJECT ) ?
					'' :
					this.typeToString( this.expectedType, true );
			},
 
			/**
			 * Returns the key that contains the reference value
			 * represented in this component.
			 *
			 * @return {string}
			 */
			key: function () {
				return this.getZObjectKeyByRowId( this.rowId );
			},
 
			/**
			 * Returns the key of the parent
			 *
			 * @return {string}
			 */
			parentKey: function () {
				return this.getZObjectKeyByRowId( this.getParentRowId( this.rowId ) );
			},
 
			/**
			 * Returns the list of excluded Zids to restrict in the lookup
			 * component depending on the current Key. Currently only for the
			 * content type of Z2K2, the keys from EXCLUDE_FROM_PERSISTENT_CONTENT
			 * are intentionally excluded.
			 *
			 * @return {Array}
			 */
			excludeZids: function () {
				return (
					( this.key === Constants.Z_OBJECT_TYPE ) &&
					( this.parentKey === Constants.Z_PERSISTENTOBJECT_VALUE )
				) ? Constants.EXCLUDE_FROM_PERSISTENT_CONTENT : [];
			}
		}
	),
	methods: {
		/**
		 * Emits the event setValue so that ZObjectKey can update
		 * the terminal value in the ZObject data table.
		 *
		 * @param {string} newValue
		 */
		setValue: function ( value ) {
			const keyPath = ( this.key !== Constants.Z_REFERENCE_ID ) ?
				[ Constants.Z_REFERENCE_ID ] :
				[];
			this.$emit( 'set-value', { keyPath, value } );
		}
	}
};
 
</script>