Реализован постраничный вывод элементов массива news - объектов с картинкой и заголовком (пагинация). Если длина заголовка слишком длинная (headlineArray.length > 29), он обрезается и добавляется троеточие (headlineArray = headlineArray.slice(0,28)+'...'). При каждом повторном нажатии на одну и ту же страницу заголовок обрезается ещё и ещё. Возможно ли вернуть первоначальное значение переменной headline при повторном выполнении функции обрезания заголовка (*закомментированный код - попытки это сделать)? Или нужно переместить функцию в другое место в коде? Извиняюсь за "кривую" реализацию, только только начала изучать JavaScript, и буду рада советам.
const news = [
{image: '<img src="./assets/images/image-for-news-1.jpg">', headline: 'Poligraphy 2023. The best style ever'},
{image: '<img src="./assets/images/image-for-news-2.jpg">', headline: 'History of Abstract: The Fashion in marketing'},
{image: '<img src="./assets/images/image-for-news-3.jpg">', headline: 'Student in 2023. How will a future student changes'},
{image: '<img src="./assets/images/image-for-news-4.jpg">', headline: 'New book about photography. Book of Isaiah'},
{image: '<img src="./assets/images/image-for-news-5.jpg">', headline: 'Travel to Mexico. Who will go and why'},
{image: '<img src="./assets/images/image-for-news-6.jpg">', headline: 'New disciplines'},
{image: '<img src="./assets/images/image-for-news-7.jpg">', headline: 'Octopus dances in films'},
{image: '<img src="./assets/images/image-for-news-8.jpg">', headline: 'Fugue of five contemporary abstract artists'},
{image: '<img src="./assets/images/image-for-news-9.jpg">', headline: 'Notre Dame Cathedral will welcome visitors again'},
{image: '<img src="./assets/images/image-for-news-10.jpg">', headline: 'Inside Gaudí: Retrospective in Orsay'},
{image: '<img src="./assets/images/image-for-news-11.jpg">', headline: 'French contemporary art star Sophie Kahl'},
{image: '<img src="./assets/images/image-for-news-12.jpg">', headline: 'Grand Palais Exhibition Palace reopens'},
{image: '<img src="./assets/images/image-for-news-13.jpg">', headline: 'The long-awaited retrospective of Georg Baselitz'},
{image: '<img src="./assets/images/image-for-news-14.jpg">', headline: 'Louvre attendance drops to 1986 levels'},
{image: '<img src="./assets/images/image-for-news-15.jpg">', headline: 'Carnavalet Museum reopens'},
{image: '<img src="./assets/images/image-for-news-16.jpg">', headline: 'Closing of the Pompidou Center for renovations will be postponed'},
]
let newsBlock = document.querySelector('.news-block__wrapper')
let pagination = document.querySelector('.news-block__pagination')
let articlesOnPage = 6
let countOfPages = Math.ceil(news.length / articlesOnPage)
let showPage = (function() {
let activePage
return function(item) {
if (activePage) {
activePage.classList.remove('news-block__li_active')
}
activePage = item
item.classList.add('news-block__li_active')
let pageNum = +item.innerHTML
let start = (pageNum - 1) * articlesOnPage
let end = start + articlesOnPage
let articles = news.slice(start, end)
newsBlock.innerHTML = ''
for (let article of articles) {
let articleElement = document.createElement('div')
newsBlock.appendChild(articleElement)
articleElement.classList.add('news-block__article')
//const headlineResetting = article.headline
//const headlineResetting = news.headline
let headline = article.headline
//headline = headlineResetting
let headlineArray = headline.split('')
if (headlineArray.length > 29) {
headlineArray = headlineArray.slice(0,28)+'...'
}
headline = headlineArray.toString().replace(/,/g, '').link("./index.html")
article.headline = headline
createDiv(article.image, articleElement)
createDiv(article.headline, articleElement)
}
}
}())
let pageItems = []
for (let i = 1; i <= countOfPages; i++) {
let pageElement = document.createElement('li')
pageElement.innerHTML = i
pagination.appendChild(pageElement)
pageItems.push(pageElement)
}
showPage(pageItems[0]);
for (let item of pageItems) {
item.addEventListener('click', function() {
showPage(this);
})
}
function createDiv(content, articleElement) {
let contentElement = document.createElement('div')
contentElement.innerHTML = content
articleElement.appendChild(contentElement)
contentElement.classList.add('news-block__content')
}