PageSourceSearch

https://speleo.qc.ca/js/pagination.js

js speleo.qc.ca collected 2026-09-25 00:26:21 UTC 4,551 bytes, 145 lines download raw bytes

1 
2
3// Returns an array of maxLength (or less) page numbers
4// where a 0 in the returned array denotes a gap in the series.
5// Parameters:
6//   totalPages:     total number of pages
7//   page:           current page
8//   maxLength:      maximum size of returned array
9function getPageList(totalPages, page, maxLength) {
10  if (maxLength < 5) throw "maxLength must be at least 5";
11
12  function range(start, end) {
13    return Array.from(Array(end - start + 1), (_, i) => i + start);
14  }
15
16  var sideWidth = maxLength < 9 ? 1 : 2;
17  var leftWidth = (maxLength - sideWidth * 2 - 3) >> 1;
18  var rightWidth = (maxLength - sideWidth * 2 - 2) >> 1;
19  if (totalPages <= maxLength) {
20    // no breaks in list
21    return range(1, totalPages);
22  }
23  if (page <= maxLength - sideWidth - 1 - rightWidth) {
24    // no break on left of page
25    return range(1, maxLength - sideWidth - 1)
26      .concat([0])
27      .concat(range(totalPages - sideWidth + 1, totalPages));
28  }
29  if (page >= totalPages - sideWidth - 1 - rightWidth) {
30    // no break on right of page
31    return range(1, sideWidth)
32      .concat([0])
33      .concat(
34        range(totalPages - sideWidth - 1 - rightWidth - leftWidth, totalPages)
35      );
36  }
37  // Breaks on both sides
38  return range(1, sideWidth)
39    .concat([0])
40    .concat(range(page - leftWidth, page + rightWidth))
41    .concat([0])
42    .concat(range(totalPages - sideWidth + 1, totalPages));
43}
44
45$(function() {
46  // Number of items and limits the number of items per page
47  var numberOfItems = $("#jar .content").length;
48  var limitPerPage = 9;
49  // Total pages rounded upwards
50  var totalPages = Math.ceil(numberOfItems / limitPerPage);
51  // Number of buttons at the top, not counting prev/next,
52  // but including the dotted buttons.
53  // Must be at least 5:
54  var paginationSize = 5;
55  var currentPage;
56
57  function showPage(whichPage) {
58    if (whichPage < 1 || whichPage > totalPages) return false;
59    currentPage = whichPage;
60    $("#jar .content")
61      .hide()
62      .slice((currentPage - 1) * limitPerPage, currentPage * limitPerPage)
63      .show();
64    // Replace the navigation items (not prev/next):
65    $(".pagination li").slice(1, -1).remove();
66    getPageList(totalPages, currentPage, paginationSize).forEach(item => {
67      $("<li>")
68        .addClass(
69          "page-item " +
70            (item ? "current-page " : "") +
71            (item === currentPage ? "active " : "")
72        )
73        .append(
74          $("<a>")
75            .addClass("page-link")
76            .attr({
77              href: "javascript:void(0)"
78            })
79            .text(item || "...")
80        )
81        .insertBefore("#next-page");
82    });
83    return true;
84  }
85
86  // Include the prev/next buttons:
87  $(".pagination").append(
88    $("<li>").addClass("page-item").attr({ id: "previous-page" }).append(
89      $("<a>")
90        .addClass("page-link")
91        .attr({
92          href: "javascript:void(0)"
93        })
94        .text("Précédent")
95    ),
96    $("<li>").addClass("page-item").attr({ id: "next-page" }).append(
97      $("<a>")
98        .addClass("page-link")
99        .attr({
100          href: "javascript:void(0)"
101        })
102        .text("Suivant")
103    )
104  );
105  // Show the page links
106  $("#jar").show();
107  showPage(1);
108
109  // Use event delegation, as these items are recreated later
110  $(
111    document
112  ).on("click", ".pagination li.current-page:not(.active)", function() {
113    return showPage(+$(this).text());
114  });
115  $("#next-page").on("click", function() {
116    return showPage(currentPage + 1);
117  });
118
119  $("#previous-page").on("click", function() {
120    return showPage(currentPage - 1);
121  });
122  $(".pagination").on("click", function() {
123    $("html,body").animate({ scrollTop: 0 }, 0);
124  });
125});
126 function printDiv(div) {    
127    // Create and insert new print section
128    //var btnElement=btn;
129    //alert(btnElement);
130    var elem = document.getElementById(div);
131    var domClone = elem.cloneNode(true);
132    var $printSection = document.createElement("div");
133    $printSection.id = "printSection";
134    $printSection.appendChild(domClone);
135    document.body.insertBefore($printSection, document.body.firstChild);
136
137    window.print(); 
138
139    // Clean up print section for future use
140    var oldElem = document.getElementById("printSection");
141    if (oldElem != null) { oldElem.parentNode.removeChild(oldElem); } 
142                          //oldElem.remove() not supported by IE
143
144    return true;
145}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.