|
| 1 | +function getUrlParameter(name) { |
| 2 | + const urlParams = new URLSearchParams(window.location.search); |
| 3 | + return urlParams.get(name); |
| 4 | +} |
| 5 | + |
| 6 | +const elemsPerPage = 20; |
| 7 | + |
| 8 | +function fillNav(currPage, numOfPages) |
| 9 | +{ |
| 10 | + const curr_url = window.location.origin + window.location.pathname; |
| 11 | + |
| 12 | + if(currPage == 1) |
| 13 | + $('.nav').append($(`<span class="page-number">←</span>`)); |
| 14 | + else |
| 15 | + $('.nav').append($(`<a class="page-number" href="${curr_url}?page=${currPage-1}" rel="prev">←</a>`)); |
| 16 | + |
| 17 | + for (let i = 1; i <= numOfPages; i++) { |
| 18 | + if(i != currPage) |
| 19 | + $('.nav').append($(`<a class="page-number" href="${curr_url}?page=${i}">${i}</a>`)); |
| 20 | + else |
| 21 | + $('.nav').append($(`<strong class="page-number">${i}</strong>`)); |
| 22 | + } |
| 23 | + |
| 24 | + if(currPage == numOfPages) |
| 25 | + $('.nav').append($(`<span class="page-number">→</span>`)); |
| 26 | + else |
| 27 | + $('.nav').append($(`<a class="page-number" href="${curr_url}?page=${currPage - (-1)}" rel="next">→</a>`)); |
| 28 | +} |
| 29 | + |
| 30 | +function addToDisplay(url, title, commentsCount, timestamp) |
| 31 | +{ |
| 32 | + const template = $("#recentItemTpl")[0]; |
| 33 | + |
| 34 | + const c = $( document.importNode(template.content, true) ); |
| 35 | + |
| 36 | + c.find(".page-link").attr("href", url); |
| 37 | + c.find(".page-link").text(title); |
| 38 | + c.find(".subscription-link .msgs-cnt").text(commentsCount); |
| 39 | + c.find(".subscription-link .timestamp").text(timestamp); |
| 40 | + |
| 41 | + $('#recentList').append(c); |
| 42 | +} |
| 43 | + |
| 44 | +document.addEventListener("DOMContentLoaded", async () => { |
| 45 | + let currPage = getUrlParameter('page'); |
| 46 | + if(currPage === null) |
| 47 | + currPage = 1; |
| 48 | + |
| 49 | + const r = await request({ |
| 50 | + action: 'getRecentCommentedList', |
| 51 | + "offset": currPage * elemsPerPage - elemsPerPage, |
| 52 | + "limit": elemsPerPage |
| 53 | + }); |
| 54 | + |
| 55 | + const numOfPages = Math.ceil(r.total / elemsPerPage); |
| 56 | + |
| 57 | + fillNav(currPage, numOfPages); |
| 58 | + |
| 59 | + r.recently_commented_list.forEach((e) => addToDisplay(e.url, e.title, e.commentsCount, e.new_added)); |
| 60 | +}); |
| 61 | + |
| 62 | +async function request(args, post_args ={}) |
| 63 | +{ |
| 64 | + /* args["url"] = app.getURL(); */ |
| 65 | + |
| 66 | + let data; |
| 67 | + |
| 68 | + try |
| 69 | + { |
| 70 | + data = await requester.request(args, post_args); |
| 71 | + |
| 72 | + if(data?.error) |
| 73 | + throw "data error"; |
| 74 | + |
| 75 | + return data; |
| 76 | + } |
| 77 | + catch(err) |
| 78 | + { |
| 79 | + $('#cwc-error').show(); |
| 80 | + |
| 81 | + console.log("Нет связи с сервером?", err); |
| 82 | + } |
| 83 | +} |
0 commit comments