From 7988e7487ff36308c7709a51a01fd0ba6260c213 Mon Sep 17 00:00:00 2001 From: malnvenshorn Date: Tue, 1 May 2018 12:18:28 +0200 Subject: [PATCH] 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; };