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

78.37% Statements 29/37
44.44% Branches 8/18
83.33% Functions 10/12
78.37% Lines 29/37

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    19x 19x 19x 19x 19x                                           7x           2x 1x   1x         2x                                 2x       19x   19x     19x 19x 19x     19x     19x     19x 19x       7x 7x 7x         4x                             1x       4x                   1x       4x             19x                                
<!--
	WikiLambda Vue component for viewing a function examples.
 
	@copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
	@license MIT
-->
<template>
	<div class="ext-wikilambda-pagination">
		<cdx-button class="ext-wikilambda-pagination__view-all" @click="resetView">
			{{ getButtonText }}
		</cdx-button>
		<div class="ext-wikilambda-pagination__page-selector">
			<div class="ext-wikilambda-pagination__page-selector__main">
				<input
					ref="pageInput"
					class="ext-wikilambda-pagination__page-selector__input"
					:max="totalPages"
					:value="currentPage"
					@input="resetPage"
				>
				<span class="ext-wikilambda-pagination__page-selector__total-pages">/ {{ totalPages }} </span>
			</div>
			<cdx-button
				class="ext-wikilambda-pagination__page-selector__action"
				:disabled="currentPage === 1"
				@click="updatePage( -1 )"
			>
				<cdx-icon :icon="icons.cdxIconPrevious" icon-label="Back"></cdx-icon>
			</cdx-button>
			<cdx-button
				class="ext-wikilambda-pagination__page-selector__action"
				:disabled="currentPage === totalPages "
				@click="updatePage( 1 )"
			>
				<cdx-icon :icon="icons.cdxIconNext" icon-label="Next"></cdx-icon>
			</cdx-button>
		</div>
	</div>
</template>
 
<script>
const { defineComponent } = require( 'vue' );
const CdxButton = require( '@wikimedia/codex' ).CdxButton,
	CdxIcon = require( '@wikimedia/codex' ).CdxIcon,
	icons = require( '../../../lib/icons.json' );

module.exports = exports = defineComponent( {
	name: 'wl-pagination-component',
	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', inputValue );
				}
			}
		},
		resetView: function () {
			this.$emit( 'reset-view' );
		}
	}
} );
</script>
 
<style lang="less">
@import '../../ext.wikilambda.edit.variables.less';
 
.ext-wikilambda-pagination {
	padding-top: @spacing-50;
	padding-bottom: @spacing-100;
	display: flex;
 
	&__view-all {
		flex: none;
		height: @size-200;
	}
 
	&__page-selector {
		display: flex;
		align-content: center;
		margin-left: auto;
 
		&__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;
		}
 
		&__total-pages {
			height: @size-200;
			text-align: center;
			padding: 0 6px;
		}
 
		&__action {
			padding-left: 0;
			padding-right: 0;
			margin-left: @spacing-50;
		}
	}
}
</style>