All files / ext.wikilambda.app/components/base Pagination.vue

100% Statements 148/148
100% Branches 9/9
100% Functions 5/5
100% Lines 148/148

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 1491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 10x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1x 6x 6x 6x 6x 6x 2x 6x 2x 2x 2x 2x 6x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 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 viewing a function examples.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-app-pagination" data-testid="pagination">
		<cdx-button class="ext-wikilambda-app-pagination__view-all" @click="resetView">
			{{ getButtonText }}
		</cdx-button>
		<div v-if="!showingAll" class="ext-wikilambda-app-pagination__container">
			<div class="ext-wikilambda-app-pagination__numbers">
				<input
					ref="pageInput"
					class="ext-wikilambda-app-pagination__input"
					:max="totalPages"
					:value="currentPage"
					@input="resetPage"
				>
				<span class="ext-wikilambda-app-pagination__total-pages">/ {{ totalPages }} </span>
			</div>
			<cdx-button
				aria-label="Back"
				class="ext-wikilambda-app-pagination__action"
				:disabled="currentPage === 1"
				@click="updatePage( -1 )"
			>
				<cdx-icon :icon="icons.cdxIconPrevious" icon-label="Back"></cdx-icon>
			</cdx-button>
			<cdx-button
				aria-label="Next"
				class="ext-wikilambda-app-pagination__action"
				:disabled="currentPage === totalPages "
				@click="updatePage( 1 )"
			>
				<cdx-icon :icon="icons.cdxIconNext" icon-label="Next"></cdx-icon>
			</cdx-button>
		</div>
	</div>
</template>
 
<script>
const { CdxButton, CdxIcon } = require( '@wikimedia/codex' );
const { defineComponent } = require( 'vue' );
const icons = require( '../../../lib/icons.json' );
 
module.exports = exports = defineComponent( {
	name: 'wl-pagination',
	components: {
		'cdx-button': CdxButton,
		'cdx-icon': CdxIcon
	},
	props: {
		totalPages: {
			type: Number,
			required: true
		},
		currentPage: {
			type: Number,
			required: false,
			default: 1
		},
		showingAll: {
			type: Boolean,
			default: false
		}
	},
	data: function () {
		return {
			icons: icons
		};
	},
	computed: {
		getButtonText: function () {
			if ( !this.showingAll ) {
				return this.$i18n( 'wikilambda-function-viewer-details-table-view-all' ).text();
			}
			return this.$i18n( 'wikilambda-function-viewer-details-table-view-less' ).text();
		}
	},
	methods: {
		updatePage: function ( direction ) {
			this.$emit( 'update-page', this.currentPage + direction );
		},
		resetPage: function () {
			const inputValue = this.$refs.pageInput.value;
			// if there is value in the input box and it is a number
			if ( inputValue && !isNaN( inputValue ) ) {
				// if input number is too low set to 1 and if it is too high set to last page
				if ( inputValue < 1 ) {
					this.$emit( 'update-page', 1 );
				} else if ( inputValue > this.totalPages ) {
					this.$emit( 'update-page', this.totalPages );
				} else {
					this.$emit( 'update-page', Number( inputValue ) );
				}
			}
		},
		resetView: function () {
			this.$emit( 'reset-view' );
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.app.variables.less';
 
.ext-wikilambda-app-pagination {
	padding-top: @spacing-50;
	padding-bottom: @spacing-100;
	display: flex;
 
	.ext-wikilambda-app-pagination__view-all {
		flex: none;
		height: @size-200;
	}
 
	.ext-wikilambda-app-pagination__container {
		display: flex;
		align-content: center;
		margin-left: auto;
	}
 
	.ext-wikilambda-app-pagination__input {
		width: @size-200;
		height: @size-200;
		margin: 0;
		text-align: center;
		border: 1px solid @border-color-base;
		border-radius: @border-radius-base;
		box-sizing: @box-sizing-base;
	}
 
	.ext-wikilambda-app-pagination__total-pages {
		height: @size-200;
		text-align: center;
		padding: 0 6px;
	}
 
	.ext-wikilambda-app-pagination__action {
		padding-left: 0;
		padding-right: 0;
		margin-left: @spacing-50;
	}
}
</style>