All files / ext.wikilambda.app/components/default-view-types ZObjectKeyValue.vue

94.32% Statements 815/864
89.17% Branches 140/157
82.75% Functions 24/29
94.32% Lines 815/864

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 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 8651x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 83x 83x 83x 83x 83x 83x 83x 83x 83x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3250x 3250x 1x 1x 3249x 3249x 3249x 3249x 29x 3250x 29x 29x 3220x 3220x 3220x 3220x 3220x 3250x 3x 3x 1x 3x 3x 3217x 3217x 3217x 3217x 3250x 231x 231x 2986x 2986x 2986x 2986x 2986x 2986x 2196x 2160x 3250x 1x 1x 1x 1x 1x 1x 1x 1x 3321x 1x 1x 1x 1x 1x 1x 1x 1x 2842x 1x 1x 1x 1x 1x 1x 1x 1x 2659x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 2020x 10x 10x 2017x 78x 78x 1932x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 219x 219x 9x 9x 219x     210x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3321x 3321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3321x 3321x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2611x 2611x 2611x     2611x 2611x 78x 78x 78x 2533x 2533x 2533x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1376x 1376x 1376x     1376x 1376x 1376x 78x 78x 1298x 1298x 1x 1x 1x 1x 1x 1x 1x 1x 1x 162x 162x 162x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1760x 1760x 1760x 1370x 1760x 1254x 1254x 506x 506x 506x 506x 506x 506x 506x 506x 506x 506x 506x 506x 1760x 452x 1760x 54x 54x 452x 452x 452x 452x 452x 452x 395x 1760x 57x 57x 395x 395x 395x 395x 395x 395x 101x 1760x 89x 89x 306x 306x 306x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1402x 1402x 1402x 1402x 10x 10x 1392x 1392x 1392x 1392x 1392x 1392x 1392x 1402x 11x 11x 1381x 1402x 13x 13x 1368x 1402x 5x 5x 1363x 1402x 17x 17x 1346x 1346x 1346x 1402x 1402x 162x 162x 1402x 9x 9x 1402x 12x 12x 1402x 1x 1x 1402x 114x 114x 1402x 860x 860x 1402x 98x 98x 90x 90x 90x 90x 90x 1402x 12x 12x 78x 78x 78x 78x 1x 1x 1x 1x 1x 1x 1x 1x 162x 162x 162x 162x 1x 1x 1x 1x 1x 1x 1x 1x 1x 84x 84x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18x 18x     18x 18x 18x 18x 18x 18x 1x 18x 1x 1x 1x 17x 17x 18x     17x 17x 18x 1x 1x 1x 1x 1x 18x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 16x 16x 18x 2x 1x 1x 1x 1x 2x 2x 14x 14x 14x 14x 14x 14x 18x 1x 1x 1x 1x 1x 1x 1x 13x 13x 13x 13x 18x               13x 13x 13x 13x 13x 18x 18x 18x 13x     18x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x           1x     1x     1x     1x   1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 80x 80x 13x 13x 13x 67x 1x 1x 1x 1x 1x 1x 1x 17x 15x 15x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for rendering a ZObjectKeyValue.
	This component handles all the complex logic behind figuring out
	what key-values can be editted, what are their bound types (if any)
	and what kind of view will be rendered, expanded or simple.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-app-object-key-value" data-testid="z-object-key-value">
		<wl-key-value-block
			field-overrides
			:has-pre-column="hasPreColumn"
			:has-expanded-mode="hasExpandedMode"
			:has-expanded-border="expanded"
			:expanded="expanded"
			:edit="edit"
			:disable-edit="disableEdit"
			:expanded-disabled="edit && disableEdit"
			:depth="depth"
			:no-indent="!hasPreColumn"
			@toggle-expanded="setExpanded( !expanded )">
			<!-- Main content: either only one row with value, or top row with key and then value -->
			<!-- Key and Mode: render only when key is shown -->
			<template v-if="showKeyLabel" #key>
				<wl-localized-label :label-data="keyLabel"></wl-localized-label>
				<!-- Mode: never rendered in view mode -->
				<wl-mode-selector
					v-if="edit"
					:row-id="rowId"
					:disabled="disableEdit"
					:parent-expected-type="expectedType"
					@set-type="setType"
					@delete-list-item="deleteListItem"
					@move-before="moveBefore"
					@move-after="moveAfter"
				></wl-mode-selector>
			</template>
			<!-- Value: will always be rendered -->
			<template #value>
				<component
					:is="zobjectComponent"
					:edit="edit"
					:disabled="disableEdit"
					:expanded="expanded"
					:depth="depth"
					:row-id="rowId"
					:type="type"
					:expected-type="expectedType"
					:parent-expected-type="parentExpectedType"
					:parent-id="parentRowId"
					:parent-disable-edit="disableEdit"
					@set-value="setValue"
					@set-type="setType"
					@add-list-item="addListItem"
					@change-event="changeEvent"
					@expand="setExpanded"
				></component>
			</template>
		</wl-key-value-block>
	</div>
</template>
 
<script>
const { CdxButton, CdxIcon, CdxMessage } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const Constants = require( '../../Constants.js' ),
	ExpandedToggle = require( '../base/ExpandedToggle.vue' ),
	LocalizedLabel = require( '../base/LocalizedLabel.vue' ),
	ModeSelector = require( '../base/ModeSelector.vue' ),
	ZArgumentReference = require( './ZArgumentReference.vue' ),
	ZMonolingualString = require( './ZMonolingualString.vue' ),
	ZObjectKeyValueSet = require( './ZObjectKeyValueSet.vue' ),
	ZObjectStringRenderer = require( './ZObjectStringRenderer.vue' ),
	ZString = require( './ZString.vue' ),
	ZCode = require( './ZCode.vue' ),
	ZReference = require( './ZReference.vue' ),
	ZBoolean = require( './ZBoolean.vue' ),
	ZFunctionCall = require( './ZFunctionCall.vue' ),
	ZImplementation = require( './ZImplementation.vue' ),
	KeyValueBlock = require( '../base/KeyValueBlock.vue' ),
	ZTester = require( './ZTester.vue' ),
	ZTypedList = require( './ZTypedList.vue' ),
	WikidataItem = require( './wikidata/Item.vue' ),
	WikidataLexeme = require( './wikidata/Lexeme.vue' ),
	WikidataLexemeForm = require( './wikidata/LexemeForm.vue' ),
	LabelData = require( '../../store/classes/LabelData.js' ),
	typeUtils = require( '../../mixins/typeUtils.js' ),
	errorUtils = require( '../../mixins/errorUtils.js' ),
	{ mapActions, mapGetters } = require( 'vuex' );
 
module.exports = exports = defineComponent( {
	name: 'wl-z-object-key-value',
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon,
		'cdx-message': CdxMessage,
		'wl-expanded-toggle': ExpandedToggle,
		'wl-key-value-block': KeyValueBlock,
		'wl-localized-label': LocalizedLabel,
		'wl-mode-selector': ModeSelector,
		'wl-wikidata-item': WikidataItem,
		'wl-wikidata-lexeme': WikidataLexeme,
		'wl-wikidata-lexeme-form': WikidataLexemeForm,
		'wl-z-argument-reference': ZArgumentReference,
		'wl-z-boolean': ZBoolean,
		'wl-z-code': ZCode,
		'wl-z-function-call': ZFunctionCall,
		'wl-z-implementation': ZImplementation,
		'wl-z-monolingual-string': ZMonolingualString,
		'wl-z-object-key-value-set': ZObjectKeyValueSet,
		'wl-z-object-string-renderer': ZObjectStringRenderer,
		'wl-z-reference': ZReference,
		'wl-z-string': ZString,
		'wl-z-tester': ZTester,
		'wl-z-typed-list': ZTypedList
	},
	mixins: [ typeUtils, errorUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		},
		listItemType: {
			type: [ String, Object ],
			default: null
		},
		skipKey: {
			type: Boolean,
			required: false,
			default: false
		},
		skipIndent: {
			type: Boolean,
			required: false,
			default: false
		},
		errorId: {
			type: Number,
			required: false,
			default: null
		},
		parentDisableEdit: {
			type: Boolean,
			required: false
		}
	},
	data: function () {
		return {
			/**
			 * Expanded is a property of the key-value and it
			 * passes down only into its direct child. Every
			 * ZObjectKeyValue component sets its data property
			 * 'expanded' independently.
			 */
			expanded: false
		};
	},
	computed: Object.assign(
		mapGetters( [
			'createObjectByType',
			'getCurrentZObjectId',
			'getDepthByRowId',
			'getErrors',
			'getExpectedTypeOfKey',
			'getLabelData',
			'getParentRowId',
			'getTypedListItemType',
			'getZFunctionCallFunctionId',
			'getZKeyIsIdentity',
			'getZKeyTypeRowId',
			'getZObjectKeyByRowId',
			'getZObjectTypeByRowId',
			'getZObjectValueByRowId',
			'hasRenderer',
			'hasParser',
			'isCreateNewPage',
			'isIdentityKey',
			'isMainObject',
			'isWikidataLiteral',
			'isWikidataFetch',
			'isWikidataReference'
		] ),
		{
			/**
			 * Returns whether there are any errors
			 *
			 * @return {boolean}
			 */
			hasErrors: function () {
				return this.errors.length > 0;
			},
 
			/**
			 * Returns the errors associated to the given errorId
			 *
			 * @return {Array}
			 */
			errors: function () {
				return this.errorId ? this.getErrors( this.errorId ) : [];
			},
 
			/**
			 * Returns whether we want to disable the edit mode for a given key-value.
			 * Note that this is not the same thing as bounding the type, or disabling
			 * expanded mode. This does not coerce the component to render in view mode,
			 * just disables the selectability of the component.
			 *
			 * @return {boolean}
			 */
			disableEdit: function () {
				// If parentDisableEdit, all children must disableEdit
				if ( this.parentDisableEdit ) {
					return true;
				}
 
				// If this is the identity key of the root object, disableEdit
				if (
					this.isIdentityKey( this.key ) &&
					( this.parentKey === Constants.Z_PERSISTENTOBJECT_VALUE )
				) {
					return true;
				}
 
				// If the key is that of a typed list type (zero):
				// 1. If parent expected type is Z1: return false (allow edit)
				// 2. If parent expected type is Z881(Z1): return false (allow edit)
				// 3. If parent expected type is Z881(Zn): return true (disable edit)
				if ( this.isKeyTypedListType( this.key ) ) {
					return !(
						( this.parentExpectedType === Constants.Z_OBJECT ) ||
						( this.parentExpectedType[ Constants.Z_TYPED_LIST_TYPE ] === Constants.Z_OBJECT )
					);
				}
 
				// If the key is that of a key type/Z3K1 and:
				// * the key is identity/Z3K4 is true
				// return true (disable edit)
				if ( this.key === Constants.Z_KEY_TYPE ) {
					return this.getZKeyIsIdentity( this.parentRowId );
				}
 
				// If the key is that of Object type/Z1K1 and:
				// * the parent type is bound, or
				// * the parent key is Z2K2 and we are editing this object
				// return true (disable edit)
				return ( ( this.key === Constants.Z_OBJECT_TYPE ) && (
					( this.parentExpectedType !== Constants.Z_OBJECT ) ||
					( ( this.parentKey === Constants.Z_PERSISTENTOBJECT_VALUE ) && !this.isCreateNewPage )
				) );
			},
 
			/**
			 * Returns the key of the key-value pair of this component.
			 *
			 * @return {string}
			 */
			key: function () {
				return this.getZObjectKeyByRowId( this.rowId );
			},
 
			/**
			 * Returns the rowId of the parent of this key-value.
			 *
			 * @return {string}
			 */
			parentRowId: function () {
				return this.getParentRowId( this.rowId );
			},
 
			/**
			 * Returns the key of the parent of this key-value.
			 *
			 * @return {string}
			 */
			parentKey: function () {
				return this.getZObjectKeyByRowId( this.parentRowId );
			},
 
			/**
			 * Returns the value of the ZObject key-value pair represented
			 * in this component.
			 *
			 * @return {string}
			 */
			value: function () {
				return this.getZObjectValueByRowId( this.rowId );
			},
 
			/**
			 * Returns whether to show the key label or not
			 *
			 * @return {boolean}
			 */
			showKeyLabel: function () {
				if ( this.skipKey ) {
					return false;
				}
				if ( this.isKeyTypedListItem( this.key ) ) {
					return this.edit || this.expanded;
				}
				return true;
			},
 
			/**
			 * Returns the label data object of the given key.
			 * * If the key is a numeral, return hardcoded typed list labels
			 * * If the key is not available, return the key id
			 *
			 * @return {LabelData}
			 */
			keyLabel: function () {
				// since the FE represents typed lists in canonical form, we need to hardcode typed list keys
				if ( this.isKeyTypedListItem( this.key ) ) {
					return LabelData.fromString( this.$i18n( 'wikilambda-list-item-label', this.key ).text() );
				}
				if ( this.isKeyTypedListType( this.key ) ) {
					return LabelData.fromString( this.$i18n( 'wikilambda-list-items-type-label' ).text() );
				}
				return this.getLabelData( this.key );
			},
 
			/**
			 * Returns the depth of this key value to dynamically calculate
			 * the style of the nesting line. It returns 0 for the first level,
			 * and the following levels looping from [1..n], where n is the
			 * maximum number of levels (colors) set in our Constants.
			 *
			 * @return {number}
			 */
			depth: function () {
				const depth = this.getDepthByRowId( this.rowId );
				return ( ( depth - 1 ) % Constants.COLOR_NESTING_LEVELS ) + 1;
			},
 
			/**
			 * Returns the simple string type of the value of the the ZObject represented
			 * in this component. When it's not set, it's undefined.
			 *
			 * @return {string}
			 */
			type: function () {
				const noArgs = true;
				return this.typeToString( this.getZObjectTypeByRowId( this.rowId ), noArgs );
			},
 
			/**
			 * Returns the expected type of the parent key. If the key is of
			 * a typed list item, it returns the list item expected type.
			 *
			 * @return {string|Object|undefined}
			 */
			parentExpectedType: function () {
				// If parent key is a numerical index, it's the key of a typed list type/item
				// 1. If index is zero, it should always expect a Z4/Type in any mode
				if ( this.isKeyTypedListType( this.parentKey ) ) {
					return Constants.Z_TYPE;
				}
				// 2. If item index (not zero), it should expect the typed list type
				if ( this.isKeyTypedListItem( this.parentKey ) ) {
					const typedListRowId = this.getParentRowId( this.parentRowId );
					return this.getTypedListItemType( typedListRowId );
				}
 
				// If ZnKn shaped key, get expected from the key definition
				return this.getExpectedTypeOfKey( this.parentKey );
			},
 
			/**
			 * Returns the expected (or bound) type for the value of
			 * the key-value pair represented in this component.
			 *
			 * @return {string|Object|undefined}
			 */
			expectedType: function () {
				// If key is a numerical index, this is the key of a typed list type/item
				// 1. If index is zero, it should always expect a Z4/Type
				if ( this.isKeyTypedListType( this.key ) ) {
					return Constants.Z_TYPE;
				}
				// 2. If index is > zero, the type of each item must be whatever
				// the type of the list is, or Z1 if listItemType is undefined
				if ( this.isKeyTypedListItem( this.key ) ) {
					return this.listItemType || Constants.Z_OBJECT;
				}
 
				return this.getExpectedTypeOfKey( this.key );
			},
 
			/**
			 * Returns the function call function Id if the object
			 * represented is a function call. Else returns undefined
			 *
			 * @return {string|undefined}
			 */
			functionCallFunctionId: function () {
				return this.type === Constants.Z_FUNCTION_CALL ?
					this.getZFunctionCallFunctionId( this.rowId ) :
					undefined;
			},
 
			/**
			 * Returns whether this key-value can be expanded into an expanded mode.
			 * This contains the logic for detecting terminal key-values and
			 * not enter infinite UX recursion.
			 *
			 * @return {boolean}
			 */
			hasExpandedMode: function () {
				// TERMINAL rule for string and reference
				if (
					( this.type === Constants.Z_REFERENCE ) ||
					( this.type === Constants.Z_STRING )
				) {
					return false;
				}
 
				// TERMINAL rules for implementation:
				// * no expansion allowed for implementation component
				// * no expansion allowed for target function reference
				// * no expansion allowed for code component
				//   * TODO (T296815): This is because the programming language selection
				//     is hardcoded and it must only set literal Z61 with very limited values.
				//     Once this is fixed, we will be able to edit this, and select reference
				//     to persisted languages. When that happens, we'll need to remove this
				//     restriction and allow for code component to be expanded.
				if (
					( this.type === Constants.Z_IMPLEMENTATION ) ||
					( this.key === Constants.Z_IMPLEMENTATION_CODE ) ||
					( this.key === Constants.Z_IMPLEMENTATION_FUNCTION )
				) {
					return false;
				}
 
				// TERMINAL rules for tester:
				// * no expansion allowed for tester component
				// * no expansion allowed for target function reference
				if (
					( this.type === Constants.Z_TESTER ) ||
					( this.key === Constants.Z_TESTER_FUNCTION )
				) {
					return false;
				}
 
				// If the type doesn't have any builting component, it must
				// be always shown in its expanded-mode representation--the set
				// of key values, so we won't show the expanded mode toggle.
				if (
					!Object.keys( Constants.BUILTIN_COMPONENTS ).includes( this.type ) &&
					!( this.hasRenderer( this.type ) && this.hasParser( this.type ) )
				) {
					return false;
				}
 
				// Fallback: anything else can be expanded
				return true;
			},
 
			/**
			 * Selects the component to render in place of the dynamic component.
			 * There are different strategies to choose the component. The first
			 * one, by key. If the key of this key-value is Z1K1, we will render
			 * the special ZObjectType component, which will handle type and mode
			 * selection. Else, the second strategy is by the type of the value.
			 * All custom built components should be listed here, in order from
			 * more to less complex.
			 *
			 * @return {string}
			 */
			zobjectComponent: function () {
				// BY KEY
				// Argument Reference Key/Z18K1 should be rendered with the same component
				// as the parent Argument Reference/Z18 object when this is expanded.
				if ( this.key === Constants.Z_ARGUMENT_REFERENCE_KEY ) {
					return 'wl-z-argument-reference';
				}
 
				// BY TYPE
				// The typed list is a component that should not be shown with
				// ZObjectKeyValueSet, so it handles the expanded mode internally.
				// ZTypedList component will be in charge of handling the type,
				// binding it if necessary (E.g. if the parentType is bound)
				// or allowing for its edition.
				if ( this.type === Constants.Z_TYPED_LIST ) {
					return 'wl-z-typed-list';
				}
				// Implementation doesn't have an expanded mode
				if ( this.type === Constants.Z_IMPLEMENTATION ) {
					return 'wl-z-implementation';
				}
				// Code doesn't have an expanded mode
				if ( this.type === Constants.Z_CODE ) {
					return 'wl-z-code';
				}
				// Tester doesn't have an expanded mode
				if ( this.type === Constants.Z_TESTER ) {
					return 'wl-z-tester';
				}
				// Wikidata Entities and References
				if ( (
					this.isWikidataFetch( this.rowId ) ||
					this.isWikidataReference( this.rowId ) ||
					this.isWikidataLiteral( this.rowId ) ) && !!this.wikidataComponent && !this.expanded ) {
					return this.wikidataComponent;
				}
				if ( ( this.type === Constants.Z_ARGUMENT_REFERENCE ) && !this.expanded ) {
					return 'wl-z-argument-reference';
				}
				if ( ( this.type === Constants.Z_FUNCTION_CALL ) && !this.expanded ) {
					return 'wl-z-function-call';
				}
				if ( ( this.type === Constants.Z_MONOLINGUALSTRING ) && !this.expanded ) {
					return 'wl-z-monolingual-string';
				}
				if ( ( this.type === Constants.Z_REFERENCE ) && !this.expanded ) {
					return 'wl-z-reference';
				}
				if ( ( this.type === Constants.Z_STRING ) && !this.expanded ) {
					return 'wl-z-string';
				}
				if ( ( this.type === Constants.Z_BOOLEAN ) && !this.expanded ) {
					return 'wl-z-boolean';
				}
 
				// If there's no built-in component, check if there's a string renderer.
				// TODO (T359669): Currently there are no type distinctions between renderers,
				// all are string renderers. Whenever we create more types of renderers,
				// we should consider checking the right type in here.
				if ( this.hasRenderer( this.type ) && this.hasParser( this.type ) ) {
					return 'wl-z-object-string-renderer';
				}
 
				// If there's no builtin component or renderer, always show expanded mode
				this.setExpanded( true );
				return 'wl-z-object-key-value-set';
			},
 
			/**
			 * Return the wikidata component
			 *
			 * @return {string|undefined}
			 */
			wikidataComponent: function () {
				const wikidataComponent = this.functionCallFunctionId ?
					Constants.WIKIDATA_BUILTIN_COMPONENTS[ this.functionCallFunctionId ] :
					Constants.WIKIDATA_BUILTIN_COMPONENTS[ this.type ];
				return wikidataComponent;
			},
 
			/**
			 * Whether the main block is preceded by the button and
			 * indentation column.
			 *
			 * @return {boolean}
			 */
			hasPreColumn: function () {
				return !this.skipIndent || this.hasExpandedMode;
			}
		}
	),
	methods: Object.assign( mapActions( [
		'changeType',
		'clearType',
		'setDirty',
		'setValueByRowIdAndPath',
		'setZFunctionCallArguments',
		'setZImplementationContentType',
		'removeItemFromTypedList',
		'moveItemInTypedList',
		'navigate'
	] ),
	{
		/**
		 * Adds an item of the given value to the list
		 *
		 * @param {Object} payload
		 */
		addListItem: function ( payload ) {
			this.changeType( {
				id: this.rowId,
				type: payload.value,
				append: true
			} );
		},
		/**
		 * Handles the modification of the ZObject when the changed key-value
		 * is a type. This needs to call the changeType action, which handles
		 * the clearing of the old content and the initialization of a new
		 * scaffolding object representing the new type.
		 *
		 * @param {Object} payload
		 * @param {Object} payload.keyPath sequence of keys till the value to edit
		 * @param {Object | Array | string} payload.value new value
		 */
		setType: function ( payload ) {
			// If setType with no payload, clear the current object of
			// all its keys, except the Object type/Z1K1, and exit
			if ( !payload ) {
				this.clearType( this.rowId );
				return;
			}
 
			// If payload.value is reference or string, set expanded to false;
			// else, set expanded to true by default.
			this.expanded = (
				( payload.value !== Constants.Z_REFERENCE ) &&
				( payload.value !== Constants.Z_STRING )
			);
 
			// Force literal object if it's root object or request comes from mode selector
			const literal = payload.literal || this.key === Constants.Z_PERSISTENTOBJECT_VALUE;
 
			// Set the type
			this.changeType( {
				id: this.rowId,
				type: payload.value,
				append: false,
				literal
			} );
 
			// If we are setting the type of a Z1K1 key, we are changing the mode,
			// which means that we need to propagate and change the parent type
			// to clear the keys: emit a setType event with no payload
			if ( this.key === Constants.Z_OBJECT_TYPE ) {
				this.$emit( 'set-type' );
			}
 
			// If we are setting Z2K2 type
			if ( this.key === Constants.Z_PERSISTENTOBJECT_VALUE ) {
				// check if we need to reset the page title
				this.setPageCreateTitle( payload.value );
				// if we set it to Function/Z8, redirect to function editor
				if ( payload.value === Constants.Z_FUNCTION ) {
					this.navigate( { to: Constants.VIEWS.FUNCTION_EDITOR } );
					return;
				}
			}

			// Else remain in default view page and set to dirty
			this.setDirtyIfMainObject();
		},
 
		/**
		 * Handles the modification of the state value for the key-value
		 * represented in this component. Depending on the whether this
		 * change involves further changes, it will emit the event further
		 * up.
		 *
		 * @param {Object} payload
		 * @param {Object} payload.keyPath sequence of keys till the value to edit
		 * @param {Object | Array | string} payload.value new value
		 */
		setValue: function ( payload ) {
			// If value is null or undefined, do nothing
			if ( payload.value === null || payload.value === undefined ) {
				return;
			}
 
			// If we are setting a Z1K1 as typed list, this means we need to
			// render the typed list component: we delegate change to the parent;
			if (
				( this.key === Constants.Z_FUNCTION_CALL_FUNCTION ) &&
				( this.parentKey === Constants.Z_OBJECT_TYPE ) &&
				( payload.value === Constants.Z_TYPED_LIST )
			) {
				this.$emit( 'set-value', { keyPath: [], value: [ Constants.Z_OBJECT ] } );
				return;
			}
 
			// If the type of a typed list changed, notify the parent to take action
			if ( this.isKeyTypedListType( this.parentKey ) || this.isKeyTypedListType( this.key ) ) {
				this.changeEvent( payload );
			}
 
			// If the key Z3K4/is identity changed, notify the parent to take action
			if ( ( this.key === Constants.Z_BOOLEAN_IDENTITY ) && ( this.parentKey === Constants.Z_KEY_IS_IDENTITY ) ) {
				// 1. If the change is done on expanded boolean, let the parent handle it completely
				payload.keyPath = [ this.key, ...payload.keyPath ];
				this.$emit( 'set-value', payload );
				return;
			}
			if ( ( this.key === Constants.Z_KEY_IS_IDENTITY ) && ( payload.value === Constants.Z_BOOLEAN_TRUE ) ) {
				// 2. If the change is done on collapsed mode, get the parent to set Z3K1/type
				const keyTypeRowId = this.getZKeyTypeRowId( this.parentRowId );
				const keyTypeType = this.getZObjectTypeByRowId( keyTypeRowId );
 
				let identityPayload;
				if ( keyTypeType === Constants.Z_REFERENCE ) {
					identityPayload = {
						keyPath: [ Constants.Z_KEY_TYPE, Constants.Z_REFERENCE_ID ],
						value: this.getCurrentZObjectId
					};
				} else {
					const refObject = this.createObjectByType( {
						type: Constants.Z_REFERENCE,
						value: this.getCurrentZObjectId
					} );
					identityPayload = {
						keyPath: [ Constants.Z_KEY_TYPE ],
						value: refObject
					};
				}
 
				this.$emit( 'set-value', identityPayload );
			}
 
			// If the value of Z1K1 changes, tell parent key to change its type
			if ( this.key === Constants.Z_OBJECT_TYPE ) {
				if ( Array.isArray( payload.value ) ) {
					this.$emit( 'set-value', payload );
				} else {
					this.$emit( 'set-type', payload );
				}
				return;
			}
 
			// CHANGES ARE RESPONSIBILITY OF THIS COMPONENT:
			this.setDirtyIfMainObject();
 
			// If we are changing an implementation type, we need to clear
			// the unselected key and fill the other one with a blank value.
			if ( this.type === Constants.Z_IMPLEMENTATION ) {
				const contentType = payload.keyPath[ 0 ];
				this.setZImplementationContentType( {
					parentId: this.rowId,
					key: contentType
				} );
				return;
			}
 
			// If the value of Z7K1 changes, we need to change all keys, which
			// probably means that we need to pass up the responsability the way we
			// have done it with Z1K1.
			if ( this.key === Constants.Z_FUNCTION_CALL_FUNCTION ) {
				// Set new function call arguments and remove old ones and
				// continue to have this key set by setValueByRowIdAndPath
				this.setZFunctionCallArguments( {
					parentId: this.parentRowId,
					functionZid: payload.value
				} );
			}
 
			// Simple changes:
			// They don't affect the rest of the ZObject, only this key-value
			this.setValueByRowIdAndPath( {
				rowId: this.rowId,
				keyPath: payload.keyPath ? payload.keyPath : [],
				value: payload.value
			} ).then( () => {
				if ( payload.callback ) {
					payload.callback();
				}
			} );
		},
 
		/**
		 * Sets the page title to a more specific string for types,
		 * functions, implementations and tests.
		 *
		 * @param {string} type
		 */
		setPageCreateTitle: function ( type ) {
			// If this is an edit existing object page, do nothing
			if ( !this.isCreateNewPage ) {
				return;
			}
			let pageTitle;
			switch ( type ) {
				case Constants.Z_TYPE:
					pageTitle = this.$i18n( 'wikilambda-special-create-type' ).text();
					break;
				case Constants.Z_FUNCTION:
					pageTitle = this.$i18n( 'wikilambda-special-create-function' ).text();
					break;
				case Constants.Z_IMPLEMENTATION:
					pageTitle = this.$i18n( 'wikilambda-special-create-implementation' ).text();
					break;
				case Constants.Z_TESTER:
					pageTitle = this.$i18n( 'wikilambda-special-create-test' ).text();
					break;
				default:
					pageTitle = this.$i18n( 'wikilambda-special-createobject' ).text();
			}
			document.getElementById( 'firstHeading' ).textContent = pageTitle;
		},
 
		/**
		 * Generic handler to bubble up change events. This can be utilized by any component
		 * that acts as a wrapper to ZObjectKeyValue and wants to recieve events from children.
		 *
		 * It is currently used by ZTypedListType, to be notified of a type change to a list
		 *
		 * @param {Object} payload
		 */
		changeEvent: function ( payload ) {
			this.$emit( 'change-event', payload );
		},
 
		/**
		 * Sets the expanded flag to a given value. If the type
		 * cannot be expanded (because it's terminal), it persistently
		 * sets the expanded flag to false.
		 *
		 * @param {boolean} value
		 */
		setExpanded: function ( value ) {
			// Never allow expansion of references and strings, as they are terminal
			if ( this.type === Constants.Z_REFERENCE || this.type === Constants.Z_STRING ) {
				this.expanded = false;
				return;
			}
			this.expanded = value;
		},
 
		/**
		 * Sets object isDirty flag as true only if the changes
		 * are made in the main page object.
		 */
		setDirtyIfMainObject: function () {
			if ( this.isMainObject( this.rowId ) ) {
				this.setDirty();
			}
		},
 
		/**
		 * Process delete item action
		 */
		deleteListItem: function () {
			// TODO (T331132): can we create a 'revert delete' workflow?
			this.setDirtyIfMainObject();
			this.removeItemFromTypedList( { rowId: this.rowId } );
		},
		/**
		 * Process move-before list item action
		 */
		moveBefore: function () {
			this.setDirtyIfMainObject();
			this.moveItemInTypedList( {
				parentRowId: this.parentRowId,
				key: this.key,
				offset: -1
			} );
		},
		/**
		 * Process move-after list item action
		 */
		moveAfter: function () {
			this.setDirtyIfMainObject();
			this.moveItemInTypedList( {
				parentRowId: this.parentRowId,
				key: this.key,
				offset: 1
			} );
		}
 
	} )
} );
</script>