From 7988e7487ff36308c7709a51a01fd0ba6260c213 Mon Sep 17 00:00:00 2001 From: malnvenshorn Date: Tue, 1 May 2018 12:18:28 +0200 Subject: [PATCH 1/3] DRY principle for storage IDs --- src/octoprint/static/js/app/helpers.js | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index 98a1e5741..4628be011 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -20,6 +20,11 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self.currentFilters = ko.observableArray(self.defaultFilters); self.selectedItem = ko.observable(undefined); + self.storageIds = { + "currentSorting": self.listType + "." + "currentSorting", + "currentFilters": self.listType + "." + "currentFilters", + }; + //~~ item handling self.refresh = function() { @@ -281,16 +286,16 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor if ( self._initializeLocalStorage() ) { var currentSorting = self.currentSorting(); if (currentSorting !== undefined) - localStorage[self.listType + "." + "currentSorting"] = currentSorting; + localStorage[self.storageIds.currentSorting] = currentSorting; else - localStorage[self.listType + "." + "currentSorting"] = undefined; + localStorage[self.storageIds.currentSorting] = undefined; } }; self._loadCurrentSortingFromLocalStorage = function() { if ( self._initializeLocalStorage() ) { - if (_.contains(_.keys(supportedSorting), localStorage[self.listType + "." + "currentSorting"])) - self.currentSorting(localStorage[self.listType + "." + "currentSorting"]); + if (_.contains(_.keys(supportedSorting), localStorage[self.storageIds.currentSorting])) + self.currentSorting(localStorage[self.storageIds.currentSorting]); else self.currentSorting(defaultSorting); } @@ -299,13 +304,13 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self._saveCurrentFiltersToLocalStorage = function() { if ( self._initializeLocalStorage() ) { var filters = _.intersection(_.keys(self.supportedFilters), self.currentFilters()); - localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(filters); + localStorage[self.storageIds.currentFilters] = JSON.stringify(filters); } }; self._loadCurrentFiltersFromLocalStorage = function() { if ( self._initializeLocalStorage() ) { - self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.listType + "." + "currentFilters"]))); + self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.storageIds.currentFilters]))); } }; @@ -313,11 +318,11 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor if (!Modernizr.localstorage) return false; - if (localStorage[self.listType + "." + "currentSorting"] !== undefined && localStorage[self.listType + "." + "currentFilters"] !== undefined && JSON.parse(localStorage[self.listType + "." + "currentFilters"]) instanceof Array) + if (localStorage[self.storageIds.currentSorting] !== undefined && localStorage[self.storageIds.currentFilters] !== undefined && JSON.parse(localStorage[self.storageIds.currentFilters]) instanceof Array) return true; - localStorage[self.listType + "." + "currentSorting"] = self.defaultSorting; - localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(self.defaultFilters); + localStorage[self.storageIds.currentSorting] = self.defaultSorting; + localStorage[self.storageIds.currentFilters] = JSON.stringify(self.defaultFilters); return true; }; From 9d2ee2b55bec9441ee348d5f979da984d2632436 Mon Sep 17 00:00:00 2001 From: malnvenshorn Date: Tue, 1 May 2018 12:32:55 +0200 Subject: [PATCH 2/3] Save page size to local storage --- src/octoprint/static/js/app/helpers.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index 4628be011..8fe45085a 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -1,4 +1,4 @@ -function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSorting, defaultFilters, exclusiveFilters, filesPerPage) { +function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSorting, defaultFilters, exclusiveFilters, defaultPageSize) { var self = this; self.listType = listType; @@ -7,6 +7,7 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self.defaultSorting = defaultSorting; self.defaultFilters = defaultFilters; self.exclusiveFilters = exclusiveFilters; + self.defaultPageSize = defaultPageSize; self.searchFunction = undefined; @@ -14,7 +15,7 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self.allSize = ko.observable(0); self.items = ko.observableArray([]); - self.pageSize = ko.observable(filesPerPage); + self.pageSize = ko.observable(self.defaultPageSize); self.currentPage = ko.observable(0); self.currentSorting = ko.observable(self.defaultSorting); self.currentFilters = ko.observableArray(self.defaultFilters); @@ -23,6 +24,7 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self.storageIds = { "currentSorting": self.listType + "." + "currentSorting", "currentFilters": self.listType + "." + "currentFilters", + "pageSize": self.listType + "." + "pageSize", }; //~~ item handling @@ -314,21 +316,37 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor } }; + self._savePageSizeToLocalStorage = function(pageSize) { + if (self._initializeLocalStorage()) { + localStorage[self.storageIds.pageSize] = pageSize; + } + } + + self.pageSize.subscribe(self._savePageSizeToLocalStorage); + + self._loadPageSizeFromLocalStorage = function() { + if (self._initializeLocalStorage) { + self.pageSize(localStorage[self.storageIds.pageSize]); + } + } + self._initializeLocalStorage = function() { if (!Modernizr.localstorage) return false; - if (localStorage[self.storageIds.currentSorting] !== undefined && localStorage[self.storageIds.currentFilters] !== undefined && JSON.parse(localStorage[self.storageIds.currentFilters]) instanceof Array) + if (localStorage[self.storageIds.currentSorting] !== undefined && localStorage[self.storageIds.currentFilters] !== undefined && JSON.parse(localStorage[self.storageIds.currentFilters]) instanceof Array && localStorage[self.storageIds.pageSize] !== undefined) return true; localStorage[self.storageIds.currentSorting] = self.defaultSorting; localStorage[self.storageIds.currentFilters] = JSON.stringify(self.defaultFilters); + localStorage[self.storageIds.pageSize] = self.defaultPageSize; return true; }; self._loadCurrentFiltersFromLocalStorage(); self._loadCurrentSortingFromLocalStorage(); + self._loadPageSizeFromLocalStorage(); } function formatSize(bytes) { From 735e2c6c94d27d4d4383151803cc79ac050ba59d Mon Sep 17 00:00:00 2001 From: malnvenshorn Date: Tue, 1 May 2018 15:50:27 +0200 Subject: [PATCH 3/3] Convert page size to integer when reading from local storage --- src/octoprint/static/js/app/helpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/static/js/app/helpers.js b/src/octoprint/static/js/app/helpers.js index 8fe45085a..af233a5f6 100644 --- a/src/octoprint/static/js/app/helpers.js +++ b/src/octoprint/static/js/app/helpers.js @@ -326,7 +326,7 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor self._loadPageSizeFromLocalStorage = function() { if (self._initializeLocalStorage) { - self.pageSize(localStorage[self.storageIds.pageSize]); + self.pageSize(parseInt(localStorage[self.storageIds.pageSize])); } }