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

100% Statements 147/147
100% Branches 11/11
100% Functions 5/5
100% Lines 147/147

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 1481x 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 2x 2x 1x 1x 1x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 3x 3x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
<!--
	WikiLambda Vue component for a Typed List Type.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-ztyped-list-type">
		<wl-z-object-key-value
			:row-id="rowId"
			:edit="edit"
			:list-item-type="listItemType"
			@change-event="changeType"
		></wl-z-object-key-value>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const typeUtils = require( '../../mixins/typeUtils.js' ),
	Constants = require( '../../Constants.js' ),
	mapGetters = require( 'vuex' ).mapGetters,
	mapActions = require( 'vuex' ).mapActions;
 
module.exports = exports = defineComponent( {
	name: 'wl-z-typed-list-type',
	components: {
		//
	},
	mixins: [ typeUtils ],
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		},
		parentRowId: {
			type: Number,
			default: undefined
		},
		listItemType: {
			type: [ String, Object ],
			default: null
		},
		listItemsRowIds: {
			type: Array,
			default() {
				return [];
			}
		}
	},
	data: function () {
		return {
			hasError: false
		};
	},
	computed: Object.assign( mapGetters( [
		'getZObjectTypeByRowId'
	] ) ),
	methods: Object.assign( mapActions( [
		'setListItemsForRemoval',
		'clearListItemsForRemoval',
		'setError',
		'clearErrors'
	] ), {
 
		/**
		 * Retrieves a list of items to be removed.
		 *
		 * @param {string} newListItemType
		 * @return {Array} - The list of items to be removed.
		 */
		getListItemsForRemoval( newListItemType ) {
			return this.listItemsRowIds
				.filter( ( rowId ) => this.getZObjectTypeByRowId( rowId ) !== newListItemType );
		},
 
		/**
		 * Handles the change of the type of the list.
		 *
		 * @param {Object} payload
		 * @return {void}
		 */
		changeType: function ( payload ) {
			/**
			 * if the type of the list has changed:
			 * - warn the user this will delete list items (now the 'wrong' type)
			 * - except when the type was changed to Object/Z1: we can clear the errors
			 * - the items that need to be deleted are the ones that are not of the new type
			 */
			const newListItemType = payload.value;
			const isZObject = newListItemType === Constants.Z_OBJECT;
			const isDifferentType = newListItemType !== this.listItemType && newListItemType !== Constants.Z_OBJECT;
			const hasListItems = this.listItemsRowIds.length > 0;
 
			// If the type was changed to Object/Z1, we can clear the errors
			if ( this.hasError && isZObject ) {
				this.hasError = false;
				this.clearErrors( 0 );
				this.clearListItemsForRemoval();
				return;
			}
 
			// If the type was changed to a different type and there are list items, show a warning
			if ( isDifferentType && hasListItems ) {
 
				// Set the error only once
				if ( !this.hasError ) {
					this.hasError = true;
					this.setError( {
						rowId: 0,
						errorCode: Constants.errorCodes.TYPED_LIST_TYPE_CHANGED,
						errorType: Constants.errorTypes.WARNING
					} );
				}
 
				this.setListItemsForRemoval( {
					parentRowId: this.parentRowId,
					listItems: this.getListItemsForRemoval( newListItemType )
				} );
			}
		}
	} ),
	beforeCreate: function () {
		this.$options.components[ 'wl-z-object-key-value' ] = require( './ZObjectKeyValue.vue' );
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-ztyped-list-type {
	margin-bottom: @wl-key-value-set-margin-bottom;
 
	&__label {
		display: inline-block;
		color: @color-subtle;
		font-weight: @font-weight-normal;
		margin-bottom: @spacing-12;
	}
}
</style>