@nethserver/ns8-ui-lib 2.0.1 → 2.1.0

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.
@@ -18069,7 +18069,31 @@ var LottieService = {
18069
18069
  }
18070
18070
  };
18071
18071
 
18072
+ /*
18073
+ * Copyright (C) 2023 Nethesis S.r.l.
18074
+ * SPDX-License-Identifier: GPL-3.0-or-later
18075
+ */
18076
+
18077
+ var StorageService = {
18078
+ name: "StorageService",
18079
+ methods: {
18080
+ getFromStorage(prop) {
18081
+ return JSON.parse(localStorage.getItem(prop));
18082
+ },
18083
+ saveToStorage(prop, object) {
18084
+ localStorage.setItem(prop, JSON.stringify(object));
18085
+ },
18086
+ deleteFromStorage(prop) {
18087
+ localStorage.removeItem(prop);
18088
+ }
18089
+ }
18090
+ };
18091
+
18072
18092
  //
18093
+
18094
+ // localStorage entry holding the page sizes chosen by the user, grouped by
18095
+ // page sizes set (e.g. {"10,25,50,100": 50}) or by pageSizeStorageKey prop
18096
+ const PAGE_SIZES_STORAGE_KEY = "nsDataTablePageSizes";
18073
18097
  var script$a = {
18074
18098
  name: "NsDataTable",
18075
18099
  extends: CvDataTable,
@@ -18085,7 +18109,7 @@ var script$a = {
18085
18109
  ChevronRight16,
18086
18110
  NsPagination
18087
18111
  },
18088
- mixins: [UtilService, LottieService],
18112
+ mixins: [UtilService, LottieService, StorageService],
18089
18113
  props: {
18090
18114
  actionBarAriaLabel: {
18091
18115
  type: String,
@@ -18117,6 +18141,17 @@ var script$a = {
18117
18141
  type: Array,
18118
18142
  default: () => [10, 25, 50, 100]
18119
18143
  },
18144
+ // remember the page size chosen by the user in localStorage
18145
+ persistPageSize: {
18146
+ type: Boolean,
18147
+ default: true
18148
+ },
18149
+ // key used to store the page size; tables with the same key share the same page size.
18150
+ // If empty, tables are grouped by their page sizes set
18151
+ pageSizeStorageKey: {
18152
+ type: String,
18153
+ default: ""
18154
+ },
18120
18155
  rowSize: {
18121
18156
  type: String,
18122
18157
  default: "standard",
@@ -18234,7 +18269,8 @@ var script$a = {
18234
18269
  searchFilter: "",
18235
18270
  pageStart: 0,
18236
18271
  pageNumber: 0,
18237
- pageLength: 0
18272
+ pageLength: 0,
18273
+ selectedPageSize: null
18238
18274
  };
18239
18275
  },
18240
18276
  watch: {
@@ -18243,9 +18279,13 @@ var script$a = {
18243
18279
  },
18244
18280
  filteredRows: function () {
18245
18281
  this.$emit("updatePage", this.tablePage);
18282
+ },
18283
+ pageSizeGroup: function () {
18284
+ this.selectedPageSize = this.getStoredPageSize();
18246
18285
  }
18247
18286
  },
18248
18287
  created() {
18288
+ this.selectedPageSize = this.getStoredPageSize();
18249
18289
  this.filterRows();
18250
18290
  },
18251
18291
  computed: {
@@ -18253,12 +18293,31 @@ var script$a = {
18253
18293
  if (this.pageSizes.length) {
18254
18294
  return {
18255
18295
  numberOfItems: this.filteredRows.length,
18256
- pageSizes: this.pageSizes
18296
+ pageSizes: this.paginationPageSizes
18257
18297
  };
18258
18298
  } else {
18259
18299
  return false;
18260
18300
  }
18261
18301
  },
18302
+ isPageSizePersisted() {
18303
+ return this.persistPageSize && this.pageSizes.length > 1 && this.pageSizes.every(size => typeof size === "number");
18304
+ },
18305
+ pageSizeGroup() {
18306
+ if (!this.isPageSizePersisted) {
18307
+ return "";
18308
+ }
18309
+ return this.pageSizeStorageKey || this.pageSizes.join(",");
18310
+ },
18311
+ paginationPageSizes() {
18312
+ if (this.selectedPageSize === null || !this.pageSizes.includes(this.selectedPageSize)) {
18313
+ return this.pageSizes;
18314
+ }
18315
+ // preselect page size in NsPagination
18316
+ return this.pageSizes.map(size => ({
18317
+ value: size,
18318
+ selected: size === this.selectedPageSize
18319
+ }));
18320
+ },
18262
18321
  tablePage() {
18263
18322
  if (this.pageSizes.length) {
18264
18323
  return this.filteredRows.slice(this.pageStart, this.pageStart + this.pageLength);
@@ -18362,7 +18421,45 @@ var script$a = {
18362
18421
  return searchResults;
18363
18422
  }
18364
18423
  },
18424
+ readStoredPageSizes() {
18425
+ try {
18426
+ const storedPageSizes = this.getFromStorage(PAGE_SIZES_STORAGE_KEY);
18427
+ if (storedPageSizes && typeof storedPageSizes === "object" && !Array.isArray(storedPageSizes)) {
18428
+ return storedPageSizes;
18429
+ }
18430
+ } catch (e) {
18431
+ // corrupted value or storage not available
18432
+ }
18433
+ return {};
18434
+ },
18435
+ getStoredPageSize() {
18436
+ if (!this.pageSizeGroup) {
18437
+ return null;
18438
+ }
18439
+ const pageSize = this.readStoredPageSizes()[this.pageSizeGroup];
18440
+ if (this.pageSizes.includes(pageSize)) {
18441
+ return pageSize;
18442
+ }
18443
+ return null;
18444
+ },
18445
+ storePageSize(pageSize) {
18446
+ if (!this.pageSizeGroup) {
18447
+ return;
18448
+ }
18449
+ const storedPageSizes = this.readStoredPageSizes();
18450
+ storedPageSizes[this.pageSizeGroup] = pageSize;
18451
+ try {
18452
+ this.saveToStorage(PAGE_SIZES_STORAGE_KEY, storedPageSizes);
18453
+ } catch (e) {
18454
+ // storage not available or full
18455
+ }
18456
+ },
18365
18457
  paginateTable(ev) {
18458
+ if (this.isPageSizePersisted && this.pageLength !== 0 && ev.length !== this.pageLength) {
18459
+ // page size changed by the user
18460
+ this.selectedPageSize = ev.length;
18461
+ this.storePageSize(ev.length);
18462
+ }
18366
18463
  this.pageStart = ev.start - 1;
18367
18464
  this.pageNumber = ev.page;
18368
18465
  this.pageLength = ev.length;
@@ -26410,26 +26507,6 @@ var queryParam = {
26410
26507
  }
26411
26508
  };
26412
26509
 
26413
- /*
26414
- * Copyright (C) 2023 Nethesis S.r.l.
26415
- * SPDX-License-Identifier: GPL-3.0-or-later
26416
- */
26417
-
26418
- var StorageService = {
26419
- name: "StorageService",
26420
- methods: {
26421
- getFromStorage(prop) {
26422
- return JSON.parse(localStorage.getItem(prop));
26423
- },
26424
- saveToStorage(prop, object) {
26425
- localStorage.setItem(prop, JSON.stringify(object));
26426
- },
26427
- deleteFromStorage(prop) {
26428
- localStorage.removeItem(prop);
26429
- }
26430
- }
26431
- };
26432
-
26433
26510
  /*
26434
26511
  * Copyright (C) 2023 Nethesis S.r.l.
26435
26512
  * SPDX-License-Identifier: GPL-3.0-or-later