All files / ext.wikilambda.edit/components ZObjectKeyList.vue

31.81% Statements 7/22
0% Branches 0/3
0% Functions 0/8
31.81% Lines 7/22

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                                                          23x 23x 23x 23x 23x 23x     23x                                                                                                                                                                                        
<template>
	<!--
		WikiLambda Vue interface module for generic ZObject manipulation.
 
		@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
		@license MIT
	-->
	<ul class="ext-wikilambda-zobject-key-list">
		<li v-for="( value, key ) in zObjectWithoutPersistent" :key="key">
			<cdx-button
				v-if="!( viewmode || readonly )"
				:title="tooltipRemoveZObjectKey"
				:destructive="true"
				@click="removeKey( value.id )"
			>
				{{ $i18n( 'wikilambda-editor-removeitem' ).text() }}
			</cdx-button>
			<wl-z-object-key
				:z-key="value.key"
				:zobject-id="value.id"
				:parent-type="zObjectType"
				:readonly="readonly"
			>
			</wl-z-object-key>
		</li>
	</ul>
</template>
 
<script>
var Constants = require( '../Constants.js' ),
	typeUtils = require( '../mixins/typeUtils.js' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	mapMutations = require( 'vuex' ).mapMutations,
	mapActions = require( 'vuex' ).mapActions,
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-object-key-list',
	components: {
		'cdx-button': CdxButton
	},
	mixins: [ typeUtils ],
	inject: {
		viewmode: { default: false }
	},
	props: {
		zobjectId: {
			type: Number,
			required: true
		},
		readonly: {
			type: Boolean,
			default: false
		}
	},
	computed: $.extend( {},
		mapGetters( [
			'getZObjectChildrenById',
			'getZObjectTypeById'
		] ),
		{
			tooltipRemoveZObjectKey: function () {
				return this.$i18n( 'wikilambda-editor-zobject-removekey-tooltip' ).text();
			},
			zObject: function () {
				return this.getZObjectChildrenById( this.zobjectId );
			},
			zObjectWithoutPersistent: function () {
				return this.zObject.filter( function ( item ) {
					var isObjectType = item.key === Constants.Z_OBJECT_TYPE,
						isPersistentObjectId = item.key === Constants.Z_PERSISTENTOBJECT_ID;
 
					return !isObjectType && !isPersistentObjectId;
				} );
			},
			zObjectType: function () {
				return this.getZObjectTypeById( this.zobjectId );
			}
		}
	),
	methods: $.extend( {},
		mapActions( [
			'addZObject',
			'removeZObjectChildren',
			'setIsZObjectDirty'
		] ),
		mapMutations( [ 'addAllZKeyLabels' ] ),
		{
			/**
			 * Adds a new key field to the list.
			 *
			 * @param {string} key
			 */
			addKey: function ( key ) {
				var payload = {
						key: key,
						parent: this.zobjectId
					},
					keyExist = false;
 
				// Check if this key is already set in the zobject
				keyExist = this.findKeyInArray( key, this.zObject ) !== false;
 
				Iif ( !keyExist ) {
					this.addZObject( payload );
				}
			},
 
			/**
			 * Removes one key from the ZObject key list.
			 * Fires the event `delete` so that ZObject can update
			 * its data.
			 *
			 * @param {number} objectId
			 */
			removeKey: function ( objectId ) {
				this.removeZObjectChildren( objectId );
				this.setIsZObjectDirty( true );
			}
		}
	),
 
	beforeCreate: function () {
		// Need to delay require of ZObjectKey to avoid loop
		this.$options.components[ 'wl-z-object-key' ] = require( './ZObjectKey.vue' );
	}
};
</script>