From 72041ecafb2d7a68471e4c7dbb7d355d39a83d3a Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Tue, 7 May 2019 10:02:53 -0600 Subject: [PATCH] Remove pagination (keep the code) --- src/js/pagination.js | 13 +++++++++++++ src/js/render.js | 29 ++++++++++++----------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/js/pagination.js b/src/js/pagination.js index 0dac4ef..c8cbabc 100644 --- a/src/js/pagination.js +++ b/src/js/pagination.js @@ -1,5 +1,18 @@ +import { DEFAULT_PAGE_SIZE } from '../constants'; import { renderWords } from "./render"; +export function getPaginationData(words) { + const numWords = words.length; + const pageSize = window.localStorage.getItem('pageSize') ? parseInt(window.localStorage.getItem('pageSize')) : DEFAULT_PAGE_SIZE; + const pages = Math.floor(numWords / pageSize); + const currentPage = window.hasOwnProperty('currentPage') ? window.currentPage : 0; + const pageStart = currentPage * pageSize; + const pageEnd = typeof words[pageStart + pageSize] !== 'undefined' + ? pageStart + pageSize : words.length - 1; + + return { numWords, pageSize, pages, currentPage, pageStart, pageEnd, }; +} + export function goToPage(page) { if (typeof page.target !== 'undefined') { page = page.target.value; diff --git a/src/js/render.js b/src/js/render.js index c30136a..f1217ef 100644 --- a/src/js/render.js +++ b/src/js/render.js @@ -4,7 +4,7 @@ import { getWordsStats, wordExists } from './utilities'; import { getMatchingSearchWords, highlightSearchTerm, getSearchFilters, getSearchTerm } from './search'; import { showSection } from './displayToggles'; import { setupSearchFilters, setupWordOptionButtons, setupPagination, setupWordOptionSelections, setupEditFormButtons } from './setupListeners'; -import { DEFAULT_PAGE_SIZE } from '../constants'; +import { getPaginationData } from './pagination'; export function renderAll() { renderDictionaryDetails(); @@ -108,12 +108,10 @@ export function renderWords() { const words = getMatchingSearchWords(); let wordsHTML = ''; - const pageSize = window.localStorage.getItem('pageSize') ? parseInt(window.localStorage.getItem('pageSize')) : DEFAULT_PAGE_SIZE; - const currentPage = window.hasOwnProperty('currentPage') ? window.currentPage : 0; - const start = currentPage * pageSize; - const end = typeof words[start + pageSize] !== 'undefined' ? start + pageSize : words.length - 1; + // const { pageStart, pageEnd } = getPaginationData(words); - words.slice(start, end).forEach(originalWord => { + // words.slice(pageStart, pageEnd).forEach(originalWord => { + words.forEach(originalWord => { let detailsMarkdown = removeTags(originalWord.longDefinition); const references = detailsMarkdown.match(/\{\{.+?\}\}/g); if (references && Array.isArray(references)) { @@ -165,23 +163,20 @@ export function renderWords() { resultsText += !filters.allPartsOfSpeechChecked ? ' (Filtered)' : ''; document.getElementById('searchResults').innerHTML = resultsText; - renderPagination(); + // renderPagination(words); } -export function renderPagination() { - const numWords = window.currentDictionary.words.length; - const pageSize = window.localStorage.getItem('pageSize') ? parseInt(window.localStorage.getItem('pageSize')) : DEFAULT_PAGE_SIZE; - const pages = Math.floor(numWords / pageSize); - const currentPage = window.hasOwnProperty('currentPage') ? window.currentPage : 0; +export function renderPagination(filteredWords) { + const paginationData = getPaginationData(filteredWords); - if (pages > 0) { - let paginationHTML = (currentPage > 0 ? '« Previous' : '') + if (paginationData.pages > 0) { + let paginationHTML = (paginationData.currentPage > 0 ? '« Previous' : '') + '' - + (currentPage < pages - 1 ? 'Next »' : ''); + + (paginationData.currentPage < paginationData.pages - 1 ? 'Next »' : ''); Array.from(document.getElementsByClassName('pagination')).forEach(pagination => { pagination.innerHTML = paginationHTML;