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

100% Statements 34/34
87.5% Branches 7/8
100% Functions 11/11
100% Lines 34/34

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 18424x 24x 24x 24x 24x 24x 24x     24x                                                       14x                       25x 50x   50x                   5x                 3x         1x       14x     24x   24x     24x 24x 24x     24x       24x     24x     24x         25x 25x 25x 25x 25x 25x                   50x                         4x           24x                                                                                  
<!--
	WikiLambda Vue component for a Typed List set of items.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-ztyped-list-items ext-wikilambda-key-value">
		<!-- if expanded, show toggle button -->
		<div
			v-if="expanded"
			class="ext-wikilambda-key-value-pre"
		>
			<wl-expanded-toggle
				class="ext-wikilambda-key-value-pre-button"
				:has-expanded-mode="false"
				:expanded="true"
			></wl-expanded-toggle>
		</div>
 
		<div class="ext-wikilambda-key-value-main">
			<!-- if expanded, show key label -->
			<div
				v-if="expanded"
				:class="listItemsEditClass"
				class="ext-wikilambda-ztyped-list-items-label ext-wikilambda-key-block"
			>
				<wl-localized-label :label-data="itemsLabel"></wl-localized-label>
			</div>
			<!-- else, simply show list of items -->
			<div class="ext-wikilambda-ztyped-list-items-block ext-wikilambda-value-block">
				<wl-z-object-key-value
					v-for="item in listItemsRowIds"
					:key="'list-item-' + item"
					:row-id="item"
					:edit="edit"
					:list-item-type="listItemType"
				></wl-z-object-key-value>
			</div>
 
			<!-- Button to add a new item -->
			<div
				v-if="edit"
				class="ext-wikilambda-ztyped-list-add-button"
			>
				<cdx-button
					data-testid="typed-list-add-item"
					:title="$i18n( 'wikilambda-editor-zlist-additem-tooltip' ).text()"
					:aria-label="$i18n( 'wikilambda-editor-zlist-additem-tooltip' ).text()"
					@click="addListItem"
				>
					<cdx-icon :icon="icons.cdxIconAdd"></cdx-icon>
				</cdx-button>
			</div>
		</div>
	</div>
</template>
 
<script>
const ExpandedToggle = require( '../base/ExpandedToggle.vue' ),
	LocalizedLabel = require( '../base/LocalizedLabel.vue' ),
	CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	LabelData = require( '../../store/classes/LabelData.js' ),
	icons = require( '../../../lib/icons.json' ),
	mapGetters = require( 'vuex' ).mapGetters;
 
// @vue/component
module.exports = exports = {
	name: 'wl-z-typed-list-items',
	components: {
		'wl-expanded-toggle': ExpandedToggle,
		'wl-localized-label': LocalizedLabel,
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon
	},
	props: {
		rowId: {
			type: Number,
			required: false,
			default: 0
		},
		edit: {
			type: Boolean,
			required: true
		},
		expanded: {
			type: Boolean,
			required: true
		},
		listItemType: {
			type: String,
			required: true
		}
	},
	data: function () {
		return {
			icons: icons
		};
	},
	computed: $.extend(
		mapGetters( [
			'getUserLangZid',
			'getChildrenByParentRowId'
		] ),
		{
			/**
			 * Returns the list of item row Ids (without the type item)
			 * sorted by their key
			 *
			 * @return {Array}
			 */
			listItemsRowIds: function () {
				return this.getChildrenByParentRowId( this.rowId )
					.sort( ( a, b ) => {
						return parseInt( a.key ) - parseInt( b.key );
					} )
					.slice( 1 )
					.map( ( row ) => {
						return row.id;
					} );
			},
 
			/**
			 * Returns the key label for the list of items.
			 * Since the FE represents typed lists as benjamin arrays, this must be hardcoded
			 *
			 * @return {string}
			 */
			itemsLabel: function () {
				return new LabelData(
					null,
					this.$i18n( 'wikilambda-list-items-label' ).text(),
					this.getUserLangZid
				);
			},
 
			/**
			 * Returns all the conditional class names for the
			 * the list items label
			 *
			 * @return {string}
			 */
			listItemsEditClass: function () {
				return this.edit ?
					'ext-wikilambda-key-block-edit' :
					'ext-wikilambda-key-block-view';
			}
		}
	),
	methods: {
		addListItem: function () {
			this.$emit( 'add-list-item' );
		}
	},
	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-items {
	margin-bottom: 0;
 
	.ext-wikilambda-ztyped-list-add-button {
		margin-left: -@spacing-50;
		margin-top: @spacing-75;
	}
 
	.ext-wikilambda-ztyped-list-items-block {
		margin-left: -@spacing-25;
	}
 
	.ext-wikilambda-ztyped-list-items-label {
		label {
			line-height: @spacing-200;
		}
	}
}
</style>