All files / ext.wikilambda.edit/components/function/editor FunctionEditorName.vue

97.43% Statements 38/39
91.66% Branches 11/12
100% Functions 11/11
97.43% Lines 38/39

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    18x 18x 18x 18x 18x 18x                                         25x                       106x                 106x                 108x                   25x               25x               25x               25x                   11x 11x 6x 1x       5x                   5x 5x       5x               11x 7x   11x                 7x 7x       18x   18x     18x 18x 18x     18x     18x 18x       108x 108x                     18x                            
<!--
	WikiLambda Vue component for setting the name of a ZFunction in the Function editor.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-function-definition-name">
		<div class="ext-wikilambda-function-block__label">
			<label :for="nameFieldId">
				{{ nameLabel }}
				<span>{{ nameOptional }}</span>
			</label>
		</div>
		<div class="ext-wikilambda-function-block__body">
			<cdx-text-input
				:id="nameFieldId"
				:model-value="name"
				class="ext-wikilambda-function-definition-name__input"
				:aria-label="nameLabel"
				:placeholder="nameFieldPlaceholder"
				:max-chars="maxLabelChars"
				@change="persistName"
			></cdx-text-input>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions,
	CdxTextInput = require( '@wikimedia/codex' ).CdxTextInput;
 
module.exports = exports = defineComponent( {
	name: 'wl-function-editor-name',
	components: {
		'cdx-text-input': CdxTextInput
	},
	props: {
		/**
		 * zID of item label language
		 *
		 * @example Z1014
		 */
		zLanguage: {
			type: String,
			required: true
		},
		isMainLanguageBlock: {
			type: Boolean,
			default: false
		}
	},
	data: function () {
		return {
			maxLabelChars: Constants.LABEL_CHARS_MAX
		};
	},
	computed: Object.assign( mapGetters( [
		'getZPersistentName',
		'getZMonolingualTextValue',
		'getRowByKeyPath'
	] ), {
		/**
		 * Returns the Name (Z2K3) row for the given language.
		 * If the language is not set, returns undefined
		 *
		 * @return {Object|undefined}
		 */
		nameObject: function () {
			return this.zLanguage ? this.getZPersistentName( this.zLanguage ) : undefined;
		},
		/**
		 * Returns whether this function has a name object
		 * for the given language.
		 *
		 * @return {boolean}
		 */
		hasName: function () {
			return !!this.nameObject;
		},
		/**
		 * Returns the Name value for the given language.
		 * If value is not available, returns empty string
		 *
		 * @return {string}
		 */
		name: function () {
			return this.hasName ?
				this.getZMonolingualTextValue( this.nameObject.rowId ) :
				'';
		},
		/**
		 * Returns the label for the name field
		 *
		 * @return {string}
		 */
		nameLabel: function () {
			// TODO (T335583): Replace i18n message with key label
			// return this.getLabel( Constants.Z_PERSISTENTOBJECT_LABEL );
			return this.$i18n( 'wikilambda-function-definition-name-label' ).text();
		},
		/**
		 * Returns the i18n message for the name field placeholder
		 *
		 * @return {string}
		 */
		nameFieldPlaceholder: function () {
			return this.$i18n( 'wikilambda-function-definition-name-placeholder' ).text();
		},
		/**
		 * Returns the "optional" caption for the name field
		 *
		 * @return {string}
		 */
		nameOptional: function () {
			return this.$i18n( 'parentheses', [ this.$i18n( 'wikilambda-optional' ).text() ] ).text();
		},
		/**I
		 * Returns the id for the input field
		 *
		 * @return {string}
		 */
		nameFieldId: function () {
			return `ext-wikilambda-function-definition-name__input${ this.zLanguage }`;
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'removeItemFromTypedList',
		'setValueByRowIdAndPath'
	] ), {
		/**
		 * Persist the new name value in the globally stored object
		 *
		 * @param {Object} event
		 */
		persistName: function ( event ) {
			const value = event.target.value;
			if ( this.hasName ) {
				if ( value === '' ) {
					this.removeItemFromTypedList( { rowId: this.nameObject.rowId } );
				} else {
					this.setValueByRowIdAndPath( {
						rowId: this.nameObject.rowId,
						keyPath: [
							Constants.Z_MONOLINGUALSTRING_VALUE,
							Constants.Z_STRING_VALUE
						],
						value
					} );
				}
			} else {
				// If this.nameObject is undefined, there's no monolingual string
				// for the given language, so we create a new monolingual string
				// with the new value and append to the parent list.
				const parentRow = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				] );
				if ( !parentRow ) {
					// This should never happen because all Z2Kn's are initialized
					return;
				}
				this.changeType( {
					id: parentRow.id,
					type: Constants.Z_MONOLINGUALSTRING,
					lang: this.zLanguage,
					value,
					append: true
				} );
			}
			if ( this.isMainLanguageBlock ) {
				this.setPageTitle( value );
			}
			this.$emit( 'updated-name' );
		},
 
		/**
		 * If this is the main language represented in the page,
		 * sets the page title to the new Function Name
		 *
		 * @param {string} name
		 */
		setPageTitle: function ( name ) {
			const pageTitleSelector = '#firstHeading .ext-wikilambda-editpage-header-title--function-name';
			$( pageTitleSelector ).first().text( name );
		}
	} )
} );
</script>