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

77.35% Statements 123/159
69.76% Branches 60/86
85.96% Functions 49/57
77.7% Lines 122/157

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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794    18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x                                                                       31x                                             16x               16x               16x                 6x                   6x 12x                 16x               16x               16x               2x               16x               108x                               108x               108x               108x                 75x                     34x                 75x                         31x                     23x                 21x                         5x     5x 5x 1x   4x 3x   4x   5x                   3x 3x 3x       3x                 5x 5x                                                                                                         16x     16x     16x 16x                 8x 8x             16x 2x 4x     16x                 5x 5x 5x 5x       5x             5x         5x                                         5x 3x         3x                   2x                                                                                                     47x 47x 47x 47x           31x 31x 31x 31x 31x                 8x 8x     8x       31x     18x   18x     18x 18x 18x     18x     18x     18x     18x     18x     18x     18x     18x     18x     18x       75x 75x 75x 75x 75x 75x 75x 75x 75x                       126x           160x                           126x         126x     126x     126x     3x                         126x     126x     126x                                       8x           8x     8x                               18x                                                                                                                                                                                                      
<!--
	WikiLambda Vue component for the AboutEditMetadata Dialog.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div>
		<cdx-dialog
			:open="open"
			class="ext-wikilambda-about-edit-metadata"
			:title="$i18n( 'wikilambda-about-widget-edit-metadata-accessible-title' ).text()"
			:primary-action="primaryAction"
			:default-action="defaultAction"
			data-testid="edit-label-dialog"
			@default="closeDialog"
			@primary="saveChanges"
			@update:open="closeDialog"
		>
			<!-- Dialog Header -->
			<template #header>
				<div class="cdx-dialog__header--default">
					<div class="cdx-dialog__header__title-group">
						<h2 class="cdx-dialog__header__title">
							{{ $i18n( 'wikilambda-about-widget-edit-metadata-title' ).text() }}
						</h2>
					</div>
					<cdx-button
						weight="quiet"
						class="cdx-dialog__header__close-button"
						@click="closeDialog"
					>
						<cdx-icon :icon="iconClose"></cdx-icon>
					</cdx-button>
				</div>
				<!-- Language Selector block -->
				<div class="ext-wikilambda-about-edit-metadata-language">
					<div class="ext-wikilambda-about-edit-metadata-title">
						{{ languageLabel }}
					</div>
					<div class="ext-wikilambda-about-edit-metadata-field">
						<wl-z-object-selector
							:edit="true"
							:disabled="freezeLanguage"
							:selected-zid="forLanguage"
							:type="languageType"
							@input="selectLanguage"
						></wl-z-object-selector>
					</div>
				</div>
			</template>
			<!-- Dialog Body -->
			<div class="ext-wikilambda-about-edit-metadata-fields">
				<cdx-field
					class="ext-wikilambda-about-edit-metadata-name
					ext-wikilambda-about-edit-metadata-field"
					:optional-flag="optionalText"
				>
					<cdx-text-input
						v-model="name"
						:disabled="!canEdit"
						:placeholder="namePlaceholder"
						data-testid="text-input"
					></cdx-text-input>
					<template #label>
						{{ nameLabel }}
					</template>
					<template #help-text>
						{{ labelCharsLeft }}
					</template>
				</cdx-field>
				<!-- Description field -->
				<cdx-field
					:optional-flag="optionalText"
					class="ext-wikilambda-about-edit-metadata-field
					ext-wikilambda-about-edit-metadata-description"
				>
					<cdx-text-area
						v-model="description"
						:disabled="!canEdit"
						:placeholder="descriptionPlaceholder"
						data-testid="text-area"
					></cdx-text-area>
					<template #label>
						{{ descriptionLabel }}
					</template>
					<template #help-text>
						{{ descriptionCharsLeft }}
					</template>
				</cdx-field>
				<!-- Aliases field -->
				<div class="ext-wikilambda-about-edit-metadata-alias">
					<div class="ext-wikilambda-about-edit-metadata-title">
						{{ aliasesLabel }}
						<span>{{ $i18n( 'parentheses', [ $i18n( 'wikilambda-optional' ).text() ] ).text() }}</span>
					</div>
					<div class="ext-wikilambda-about-edit-metadata-field">
						<wl-chip-container
							:chips="aliases"
							:disabled="!canEdit"
							:input-placeholder="aliasesPlaceholder"
							@add-chip="addAlias"
							@remove-chip="removeAlias"
						></wl-chip-container>
						<span class="ext-wikilambda-about-edit-metadata-field-caption">
							{{ $i18n( 'wikilambda-about-widget-aliases-caption' ).text() }}
						</span>
					</div>
				</div>
				<!-- Function input fields -->
				<template v-if="isFunction">
					<cdx-field
						v-for="( input, index ) in inputs"
						:key="'input-' + index"
						:optional-flag="optionalText"
						class="ext-wikilambda-about-edit-metadata-field
						ext-wikilambda-about-edit-metadata-input"
					>
						<cdx-text-input
							v-model="inputs[ index ]"
							:disabled="!canEdit"
							:placeholder="inputPlaceholder"
							data-testid="text-input"
						></cdx-text-input>
						<template #label>
							{{ $i18n( 'wikilambda-about-widget-input-label', index + 1 ).text() }}
						</template>
					</cdx-field>
				</template>
			</div>
		</cdx-dialog>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	ChipContainer = require( '../base/ChipContainer.vue' ),
	ZObjectSelector = require( '../base/ZObjectSelector.vue' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxDialog = require( '@wikimedia/codex' ).CdxDialog,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	CdxField = require( '@wikimedia/codex' ).CdxField,
	CdxTextArea = require( '@wikimedia/codex' ).CdxTextArea,
	CdxTextInput = require( '@wikimedia/codex' ).CdxTextInput,
	icons = require( '../../../lib/icons.json' ),
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
module.exports = exports = defineComponent( {
	name: 'wl-about-edit-metadata-dialog',
	components: {
		'cdx-button': CdxButton,
		'cdx-dialog': CdxDialog,
		'cdx-icon': CdxIcon,
		'cdx-text-area': CdxTextArea,
		'cdx-text-input': CdxTextInput,
		'cdx-field': CdxField,
		'wl-chip-container': ChipContainer,
		'wl-z-object-selector': ZObjectSelector
	},
	props: {
		edit: {
			type: Boolean,
			required: true
		},
		canEdit: {
			type: Boolean,
			required: true
		},
		forLanguage: {
			type: String,
			required: true
		},
		open: {
			type: Boolean,
			required: true,
			default: false
		},
		isFunction: {
			type: Boolean,
			required: true
		}
	},
	data: function () {
		return {
			iconClose: icons.cdxIconClose,
			languageType: Constants.Z_NATURAL_LANGUAGE,
			maxLabelChars: Constants.LABEL_CHARS_MAX,
			maxDescriptionChars: Constants.DESCRIPTION_CHARS_MAX,
			name: '',
			description: '',
			aliases: [],
			inputs: [],
			fakeAliasId: 0,
			initialName: '',
			initialDescription: '',
			initialAliases: '[]',
			initialInputs: '[]'
		};
	},
	computed: Object.assign( mapGetters( [
		'getLabel',
		'getMetadataLanguages',
		'getRowByKeyPath',
		'getZArgumentLabelForLanguage',
		'getZFunctionInputs',
		'getZMonolingualTextValue',
		'getZMonolingualStringsetValues',
		'getZPersistentAlias',
		'getZPersistentDescription',
		'getZPersistentName',
		'getUserLangZid'
	] ), {
		/**
		 * Returns the Name/Label (Z2K3) row for the selected language.
		 *
		 * @return {Object|undefined}
		 */
		currentNameObject: function () {
			return this.forLanguage ? this.getZPersistentName( this.forLanguage ) : undefined;
		},
		/**
		 * Returns the Description (Z2K5) row for the selected language.
		 *
		 * @return {Object|undefined}
		 */
		currentDescriptionObject: function () {
			return this.forLanguage ? this.getZPersistentDescription( this.forLanguage ) : undefined;
		},
		/**
		 * Returns the Alias (Z2K4) row for the selected language.
		 *
		 * @return {Object|undefined}
		 */
		currentAliasObject: function () {
			return this.forLanguage ? this.getZPersistentAlias( this.forLanguage ) : undefined;
		},
		/**
		 * Returns the parent rows for all the function inputs or empty
		 * array if there are none (or the object is not a function)
		 *
		 * @return {Array}
		 */
		functionInputs: function () {
			return this.getZFunctionInputs();
		},
		/**
		 * Returns the input label object in the selected language
		 * for all the function inputs or empty array if there are none
		 * (or the object is not a function)
		 *
		 * @return {Array}
		 */
		currentInputObjects: function () {
			return this.isFunction ?
				this.functionInputs.map( ( row ) => {
					return this.forLanguage ?
						this.getZArgumentLabelForLanguage( row.id, this.forLanguage ) :
						undefined;
				} ) :
				[];
		},
		/**
		 * Returns the i18n message for the name field placeholder
		 *
		 * @Ireturn {string}
		 */
		namePlaceholder: function () {
			return this.$i18n( 'wikilambda-about-widget-name-placeholder' ).text();
		},
		/**
		 * Returns the i18n message for the description field placeholder
		 *
		 * @return {string}
		 */
		descriptionPlaceholder: function () {
			return this.$i18n( 'wikilambda-about-widget-description-placeholder' ).text();
		},
		/**
		 * Returns the i18n message for the aliases field placeholder
		 *
		 * @return {string}
		 */
		aliasesPlaceholder: function () {
			return this.$i18n( 'wikilambda-about-widget-aliases-placeholder' ).text();
		},
		/**
		 * Returns the i18n message for an input field placeholder
		 *
		 * @Ireturn {string}
		 */
		inputPlaceholder: function () {
			return this.$i18n( 'wikilambda-function-definition-inputs-item-input-placeholder' ).text();
		},
		/**
		 * Returns the i18n message for wikilambda-optional text
		 *
		 * @return {string}
		 */
		optionalText: function () {
			return this.$i18n( 'parentheses', [ this.$i18n( 'wikilambda-optional' ).text() ] ).text();
		},
		/**
		 * Returns the label for the selected language
		 *
		 * @return {string}
		 */
		languageLabel: function () {
			return this.getLabel( Constants.Z_MONOLINGUALSTRING_LANGUAGE );
		},
		/**
		 * Returns the label for the selected language
		 *
		 * @return {string}
		 */
		selectedLanguageLabel: function () {
			return this.getLabel( this.forLanguage );
		},
		/**
		 * Returns the label for the name input field (Z2K3)
		 *
		 * @return {string}
		 */
		nameLabel: function () {
			return this.getLabel( Constants.Z_PERSISTENTOBJECT_LABEL );
		},
		/**
		 * Returns the label for the description input field (Z2K5)
		 *
		 * @return {string}
		 */
		descriptionLabel: function () {
			return this.getLabel( Constants.Z_PERSISTENTOBJECT_DESCRIPTION );
		},
		/**
		 * Returns the label for the aliases input field (Z2K4)
		 *
		 * @return {string}
		 */
		aliasesLabel: function () {
			return this.getLabel( Constants.Z_PERSISTENTOBJECT_ALIASES );
		},
 
		/**
		 * Returns whether any of the fields has suffered any
		 * change from its initial value
		 *
		 * @return {boolean}
		 */
		hasChanges: function () {
			return (
				( this.name !== this.initialName ) ||
				( this.description !== this.initialDescription ) ||
				( JSON.stringify( this.aliases ) !== this.initialAliases ) ||
				( JSON.stringify( this.inputs ) !== this.initialInputs )
			);
		},
 
		/**
		 * Returns whether the language field should be disabled.
		 * This happens only when it has a selected language and the
		 * content of the fields has been edited. This is to prevent
		 * loss of unsaved edits.
		 *
		 * @return {boolean}
		 */
		freezeLanguage: function () {
			return ( this.hasChanges && !!this.forLanguage );
		},
 
		/**
		 * Returns an object of type PrimaryDialogAction that describes
		 * the action of the primary (save or publish) dialog button.
		 *
		 * @return {Object}
		 */
		primaryAction: function () {
			return {
				actionType: 'progressive',
				label: this.edit ?
					this.$i18n( 'wikilambda-about-widget-done-button' ).text() :
					this.$i18n( 'wikilambda-about-widget-publish-button' ).text(),
				disabled: !this.hasChanges || !this.canEdit || !this.forLanguage
			};
		},
 
		/**
		 * Returns an object of type DialogAction that describes
		 * the action of the secondary (cancel) button.
		 *
		 * @return {Object}
		 */
		defaultAction: function () {
			return {
				laEbel: this.$i18n( 'wikilambda-cancel' ).text()
			};
		},
 
		/**
		 * Returns the number of characters left to reach the
		 * label field maximum allowed.
		 *I
		 * @return {number}
		 */
		labelCharsLeft: function () {
			return this.maxLabelChars - ( this.name && this.name.length || 0 );
		},
 
		/**I
		 * Returns the number of characters left to reach the
		 * description field maximum allowed.
		 *
		 * @return {number}
		 */I
		descriptionCharsLeft: function () {
			return this.maxDescriptionChars - this.description.length;
		}
	} ),
	methods: Object.assign( mapActions( [
		'changeType',
		'removeItemFromTypedList',
		'setValueByRowIdAndPath',
		'setDirty'
	] ), {
		/**
		 * Persist the changes in the store and finish the dialog
		 * flow, which differs between view and edit pages:
		 * * If we are on an edit page, we simply persist the changes
		 *   in the store and close the dialog.
		 * * If we are on a view page, we persist the changes in
		 *   the store and show the publish dialog to save the changes.
		 */
		saveChanges: function () {
			if ( !this.forLanguage ) {
				return;
			}I

			this.persistState();
			if ( !this.edit ) {
				this.$emit( 'publish' );
			} else {
				if ( this.forLanguage === this.getUserLangZid ) {
					this.setPageTitle( this.name );
				}
				this.setDirty( true );
			}
			this.closeDialog();
		},
 
		/**
		 * If this is the main language represented in the page,
		 * sets the page title to the new Function Name in regular bold-filled font.
		 * If the title is set back to an empty string, add the 'untitled' properties back to the class.
		 *
		 * @param {string} name
		 */
		setPageTitle: function ( name ) {
			const pageTitleSelector = '#firstHeading .ext-wikilambda-editpage-header-title--function-name';
			$( pageTitleSelector ).first().text( name );
 
			if ( name === '' ) {
				$( pageTitleSelector ).addClass( 'ext-wikilambda-editpage-header--title-untitled' );
				$( pageTitleSelector ).first().text( this.$i18n( 'wikilambda-editor-default-name' ).text() );
			} else {
				$( pageTitleSelector ).removeClass( 'ext-wikilambda-editpage-header--title-untitled' );
			}
		},

		/**
		 * Close the dialog and make sure that the selected language
		 * and unsaved changes are cleared.
		 */
		closeDialog: function () {
			// Emit unselected language to discard unsaved changes
			this.$emit( 'change-selected-language', '' );
			this.$emit( 'close' );
		},
 
		/**
		 * Adds a new alias to the temporary list of aliases.
		 *
		 * @param {string} alias
		 */
		addAlias: function ( alias ) {
			// The alias object needs its prop ID to be unique, that's why
			// we are assigning it a temporary fake id that cannot clash
			// with the existing alias IDs.
			// This is because the chip container and chip component are
			// too coupled with the backend store system and removing an
			// item expects to be sending up an event with the rowId.
			// TODO (T336347): We should probably decouple these components
			// from the internal data structure.
			this.aliases.push( {
				id: this.fakeAliasId,
				value: alias
			} );
			this.fakeAliasId++;
		},
 
		/**
		 * Remove an alias from the temporary list of aliases.
		 *
		 * @param {number} aliasId
		 */
		removeAlias: function ( aliasId ) {
			for ( const i in this.aliases ) {
				if ( this.aliases[ i ].id === aliasId ) {
					this.aliases.splice( i, 1 );
					return;
				}
			}
		},
 
		/**
		 * Throw the event changeSelectedLanguage when the language
		 * field has a new valid content. This will trigger the
		 * reloading of metadata for that language.
		 *
		 * @param {string} value
		 */
		selectLanguage: function ( value ) {
			if ( value ) {
				this.$emit( 'change-selected-language', value );
			}
		},I

		/**
		 * Intializes the metadata fields name, description and aliases
		 * for a given language.
		 */
		initialize: function () {
			// Initialize Name field for given language
			this.name = this.currentNameObject ?
				this.getZMonolingualTextValue( this.currentNameObject.rowId ) :
				'';
 
			// Initialize Description field for given language
			this.description = this.currentDescriptionObject ?
				this.getZMonolingualTextValue( this.currentDescriptionObject.rowId ) :
				'';
 
			// Initialize Aliases field for given language
			this.fakeAliasId = 0;
			this.aliases = this.currentAliasObject ?
				this.getZMonolingualStringsetValues( this.currentAliasObject.rowId )
					.map( ( value ) => {
						// We add the alias rowIds to calculate a fakeAliasId
						// integer to start assigning new ids for aliases.
						// This is the easiert way to assure the ids don't
						// clash with existing alias ones. The assigned
						// IDs to new alias are temporary and will not be
						// persisted.
						// TODO (T336347): fakeAliasId will not be necessary
						// after Chip component is decouples from zobject data
						this.fakeAliasId += value.rowId;
						return {
							id: value.rowId,
							value: value.value
						};
					} ) :
				[];
 
			// If function, initialize Input fields for given language
			if ( this.isFunction ) {
				this.inputs = this.currentInputObjects.map( ( row ) => {
					return row !== undefined ?
						this.getZMonolingualTextValue( row.id ) :
						'';
				} );
			}
 
			this.saveCopies();
		},
 
		/**
		 * Persists the new changes in the data store.
		 */
		persistState: function () {
			let currentRowId, parentRowId;
 
			// If name has changed, persist
			if ( this.name !== this.initialName ) {
				currentRowId = this.currentNameObject ? this.currentNameObject.rowId : null;
				parentRowId = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_LABEL,
					Constants.Z_MULTILINGUALSTRING_VALUE
				] ).id;
				this.persistZMonolingualString( parentRowId, currentRowId, this.name );
			}
 
			// If description has changed, persist
			if ( this.description !== this.initialDescription ) {
				currentRowId = this.currentDescriptionObject ? this.currentDescriptionObject.rowId : null;
				parentRowId = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_DESCRIPTION,
					Constants.Z_MULTILINGUALSTRING_VALUE
				] ).id;
				this.persistZMonolingualString( parentRowId, currentRowId, this.description );
			}
 
			// If aliases have changed, persist
			if ( JSON.stringify( this.aliases ) !== this.initialAliases ) {
				this.persistZMonolingualStringSet(
					this.currentAliasObject,
					this.aliases.map( ( a ) => a.value )
				);
			}
 
			// If inputs have changed, persist
			if ( JSON.stringify( this.inputs ) !== this.initialInputs ) {
				const initial = JSON.parse( this.initialInputs );
				for ( const index in this.inputs ) {
					if ( this.inputs[ index ] !== initial[ index ] ) {
						const inputRowId = this.functionInputs[ index ].id;
						parentRowId = this.getRowByKeyPath( [
							Constants.Z_ARGUMENT_LABEL,
							Constants.Z_MULTILINGUALSTRING_VALUE
						], inputRowId ).id;
						currentRowId = this.currentInputObjects[ index ] ?
							this.currentInputObjects[ index ].id :
							null;
						this.persistZMonolingualString( parentRowId, currentRowId, this.inputs[ index ] );
					}
				}
			}
		},
		/**
		 * Persists the ZMonolingualString type changes in the data store.
		 * These correspond to the Name/Z2K3 and Description/Z2K5 fields.
		 *
		 * @param {number} parentRowId identifies the parent multilingual value row Id
		 * @param {number|null} currentRowId identifies the current monolingual value row Id
		 * @param {string} value
		 */
		persistZMonolingualString: function ( parentRowId, currentRowId, value ) {
			if ( currentRowId !== null ) {
				if ( value === '' ) {
					this.removeItemFromTypedList( { rowId: currentRowId } );
				} else {
					this.setValueByRowIdAndPath( {
						rowId: currentRowId,
						keyPath: [
							Constants.Z_MONOLINGUALSTRING_VALUE,
							Constants.Z_STRING_VALUE
						],
						value
					} );
				}
			} else {
				// If currentRowId is null, 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.
				this.changeType( {
					id: parentRowId,
					type: Constants.Z_MONOLINGUALSTRING,
					lang: this.forLanguage,
					value,
					append: true
				} );
			}
		},
 
		/**
		 * Persists the ZMonolingualStringset type changes in the data store.
		 * These correspond to the Aliases/Z2K4 field.
		 *
		 * @param {Object} currentObject object with the row id to persist the changes
		 * @param {string} values list of alias to persist
		 */
		persistZMonolingualStringSet: function ( currentObject, values ) {
			if ( currentObject ) {
				if ( values.length === 0 ) {
					this.removeItemFromTypedList( { rowId: currentObject.rowId } );
				} else {
					this.setValueByRowIdAndPath( {
						rowId: currentObject.rowId,
						keyPath: [ Constants.Z_MONOLINGUALSTRINGSET_VALUE ],
						value: [ Constants.Z_STRING, ...values ]
					} );
				}
			} else {
				// If currentObject is undefined, there's no monolingual stringset
				// for the given language, so we create a new monolingual stringset
				// with the new value and append to the parent list.
				const parentRow = this.getRowByKeyPath( [
					Constants.Z_PERSISTENTOBJECT_ALIASES,
					Constants.Z_MULTILINGUALSTRINGSET_VALUE
				] );
				if ( !parentRow ) {
					// This should never happen because all Z2Kn's are initialized
					return;
				}
				this.changeType( {
					id: parentRow.id,
					type: Constants.Z_MONOLINGUALSTRINGSET,
					lang: this.forLanguage,
					value: values,
					append: true
				} );
			}
		},
		/**
		 * Saves copies of initial values for name, description, aliases and inputs
		 */
		saveCopies: function () {
			this.initialName = this.name;
			this.initialDescription = this.description;
			this.initialAliases = JSON.stringify( this.aliases );
			this.initialInputs = JSON.stringify( this.inputs );
		},
		/**
		 * Saves copies of initial values for name, description, aliases and inputs
		 */
		initializeBlankValues: function () {
			this.name = '';
			this.description = '';
			this.aliases = [];
			this.inputs = this.isFunction ? this.currentInputObjects.map( () => '' ) : [];
			this.saveCopies();
		}
	} ),
	watch: {
		forLanguage: function ( newLang, oldLang ) {
			// Skip initialization if:
			// * oldLang is empty and newLang is set
			// * newLang doesn't have any labels yet
			// * form has changes
			const hasLabels = this.getMetadataLanguages().includes( newLang );
			if ( !oldLang && !!newLang && !hasLabels && this.hasChanges ) {
				return;
			}
			this.initialize();
		}
	},
	mounted: function () {
		this.initializeBlankValues();
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-about-edit-metadata {
	.cdx-dialog__body {
		padding: @spacing-50 0;
	}
 
	.ext-wikilambda-about-edit-metadata-title {
		font-weight: @font-weight-bold;
		margin: 0 0 @spacing-25;
 
		span {
			font-weight: @font-weight-normal;
			color: @color-subtle;
			margin-left: @spacing-25;
		}
	}
 
	.ext-wikilambda-about-edit-metadata-char-counter {
		text-align: right;
	}
 
	.ext-wikilambda-about-edit-metadata-fields {
		padding: 0 @spacing-150;
 
		.ext-wikilambda-about-edit-metadata-field {
			margin: 0 0 @spacing-100;
 
			.cdx-field__help-text {
				float: right;
			}
 
			.cdx-text-input {
				flex: 1;
			}
 
			.ext-wikilambda-about-edit-metadata-field-caption {
				display: block;
				color: @color-subtle;
				margin-top: @spacing-25;
			}
		}
	}
 
	.ext-wikilambda-about-edit-metadata-language {
		.ext-wikilambda-about-edit-metadata-title {
			margin-top: @spacing-100;
		}
	}
}
</style>