From 57932ff78c94cf2e6145ae6636914ba251d9c58f Mon Sep 17 00:00:00 2001 From: Dale Price Date: Thu, 6 Jun 2013 00:23:30 -0500 Subject: [PATCH] Check for localStorage with Modernizr before attempting to use it. Solves foosel/OctoPrint#91 --- octoprint/static/js/ui.js | 47 ++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/octoprint/static/js/ui.js b/octoprint/static/js/ui.js index d2d0882..970aaa9 100644 --- a/octoprint/static/js/ui.js +++ b/octoprint/static/js/ui.js @@ -1655,43 +1655,48 @@ function ItemListHelper(listType, supportedSorting, supportedFilters, defaultSor //~~ local storage self._saveCurrentSortingToLocalStorage = function() { - self._initializeLocalStorage(); - - var currentSorting = self.currentSorting(); - if (currentSorting !== undefined) - localStorage[self.listType + "." + "currentSorting"] = currentSorting; - else - localStorage[self.listType + "." + "currentSorting"] = undefined; + if ( self._initializeLocalStorage() ) { + var currentSorting = self.currentSorting(); + if (currentSorting !== undefined) + localStorage[self.listType + "." + "currentSorting"] = currentSorting; + else + localStorage[self.listType + "." + "currentSorting"] = undefined; + } } self._loadCurrentSortingFromLocalStorage = function() { - self._initializeLocalStorage(); - - if (_.contains(_.keys(supportedSorting), localStorage[self.listType + "." + "currentSorting"])) - self.currentSorting(localStorage[self.listType + "." + "currentSorting"]); - else - self.currentSorting(defaultSorting); + if ( self._initializeLocalStorage() ) { + if (_.contains(_.keys(supportedSorting), localStorage[self.listType + "." + "currentSorting"])) + self.currentSorting(localStorage[self.listType + "." + "currentSorting"]); + else + self.currentSorting(defaultSorting); + } } self._saveCurrentFiltersToLocalStorage = function() { - self._initializeLocalStorage(); - - var filters = _.intersection(_.keys(self.supportedFilters), self.currentFilters()); - localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(filters); + if ( self._initializeLocalStorage() ) { + var filters = _.intersection(_.keys(self.supportedFilters), self.currentFilters()); + localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(filters); + } } self._loadCurrentFiltersFromLocalStorage = function() { - self._initializeLocalStorage(); - - self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.listType + "." + "currentFilters"]))); + if ( self._initializeLocalStorage() ) { + self.currentFilters(_.intersection(_.keys(self.supportedFilters), JSON.parse(localStorage[self.listType + "." + "currentFilters"]))); + } } self._initializeLocalStorage = function() { + if (!Modernizr.localstorage) + return false; + if (localStorage[self.listType + "." + "currentSorting"] !== undefined && localStorage[self.listType + "." + "currentFilters"] !== undefined && JSON.parse(localStorage[self.listType + "." + "currentFilters"]) instanceof Array) - return; + return true; localStorage[self.listType + "." + "currentSorting"] = self.defaultSorting; localStorage[self.listType + "." + "currentFilters"] = JSON.stringify(self.defaultFilters); + + return true; } self._loadCurrentFiltersFromLocalStorage();