aprog 1.4.0 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -11,3 +11,4 @@ export { encrypt, decrypt } from './src/shifrator.js';
11
11
  export { gemini } from './src/OpenAI.js';
12
12
  export { mathInput } from './src/math.js';
13
13
  export { cookie, storage } from './src/storage.js';
14
+ export { Paginate, parseData } from './src/Paginate.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aprog",
3
- "version": "1.4.0",
3
+ "version": "1.4.2",
4
4
  "description": "Допоміжна бібліотека js",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,86 @@
1
+ import {storage} from "./storage.js";
2
+ import {button} from "./aprog.js";
3
+
4
+ export const Paginate = ({count = 0, currentPage = 1, onPageChange = () => {}}) => {
5
+ const generatePageButtons = () => {
6
+ const buttons = [];
7
+ if (count <= 5) {
8
+ for (let i = 1; i <= count; i++) {
9
+ buttons.push(createPageButton(i));
10
+ }
11
+ } else {
12
+ buttons.push(createPageButton(1));
13
+ if (currentPage > 3) {
14
+ buttons.push(createDotsButton());
15
+ }
16
+ for (
17
+ let i = Math.max(2, currentPage - 1);
18
+ i <= Math.min(count - 1, currentPage + 1);
19
+ i++
20
+ ) {
21
+ buttons.push(createPageButton(i));
22
+ }
23
+ if (currentPage < count - 2) {
24
+ buttons.push(createDotsButton());
25
+ }
26
+ buttons.push(createPageButton(count));
27
+ }
28
+ return buttons;
29
+ }
30
+
31
+ const createPageButton = (page) => button({
32
+ class: `paginate-btn ${page === currentPage ? "active" : ""}`,
33
+ value: `${page}`,
34
+ onClick: () => onPageChange(page),
35
+ });
36
+
37
+ const createDotsButton = () => button({
38
+ class: "paginate-dots",
39
+ disabled: true,
40
+ value: "...",
41
+ });
42
+
43
+ // const navigateTo = (path) => {
44
+ // window.history.pushState("", "", path);
45
+ // }
46
+ //
47
+ // navigateTo(`${window.location.pathname}?page=${currentPage || 1}${!isEmpty(_get("filter", null))
48
+ // ? `&filter=${_get("filter")}`
49
+ // : ""
50
+ // }${!isEmpty(_get("sort", null))
51
+ // ? `&sort=${_get('sort')}`
52
+ // : ""
53
+ // }`
54
+ // );
55
+
56
+ return div({
57
+ class: "paginate-block",
58
+ value: [
59
+ button({
60
+ class: "paginate-prev",
61
+ value: "<<<",
62
+ onClick: () => onPageChange(currentPage > 1 ? currentPage - 1 : 1),
63
+ disabled: currentPage === 1 || currentPage === 0,
64
+ }),
65
+ ...generatePageButtons(),
66
+ button({
67
+ class: "paginate-next",
68
+ value: ">>>",
69
+ onClick: () => onPageChange(currentPage < count ? currentPage + 1 : count),
70
+ disabled: currentPage === count,
71
+ }),
72
+ ],
73
+ });
74
+ };
75
+
76
+ export const parseData = (data, page) => {
77
+ const pageSize = storage.get("pageSize");
78
+ const totalData = data.length;
79
+ const pages = Math.ceil(totalData / pageSize);
80
+ if (page > pages) {
81
+ page = pages;
82
+ }
83
+ const cutData = data.slice((page - 1) * pageSize, page * pageSize);
84
+
85
+ return [pages, cutData, page];
86
+ };