ponymail-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject incubator-ponymail git commit: confusion of storageAvailable and localStorage in ponymail.js
Date Sat, 17 Dec 2016 00:52:59 GMT
Repository: incubator-ponymail
Updated Branches:
  refs/heads/master 6a7bc6275 -> 70eb2633f


confusion of storageAvailable and localStorage in ponymail.js

This fixes #194

unnecessary double-checking of window.sessionStorage in ponymail.js
This fixes #193

Project: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/commit/70eb2633
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/tree/70eb2633
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ponymail/diff/70eb2633

Branch: refs/heads/master
Commit: 70eb2633fdeee90e091d7eb408a67ffd51e59055
Parents: 6a7bc62
Author: Sebb <sebb@apache.org>
Authored: Sat Dec 17 00:52:35 2016 +0000
Committer: Sebb <sebb@apache.org>
Committed: Sat Dec 17 00:52:35 2016 +0000

----------------------------------------------------------------------
 CHANGELOG.md                              |   2 +
 site/js/dev/ponymail_assign_vars.js       |  21 +-
 site/js/dev/ponymail_composer.js          |  33 ++-
 site/js/dev/ponymail_dom_helpers.js       |   4 +-
 site/js/dev/ponymail_email_displays.js    | 121 +++++------
 site/js/dev/ponymail_helperfuncs.js       |  26 +--
 site/js/dev/ponymail_listview_flat.js     |   8 +-
 site/js/dev/ponymail_listview_threaded.js |  18 +-
 site/js/dev/ponymail_listview_tree.js     |  38 ++--
 site/js/dev/ponymail_user_preferences.js  |  12 +-
 site/js/ponymail.js                       | 281 +++++++++++--------------
 11 files changed, 252 insertions(+), 312 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/CHANGELOG.md
----------------------------------------------------------------------
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 253ef50..42432d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -70,6 +70,8 @@
 - pminfo.lua does some unnecessary work (#220)
 - stats.lua uses inconsistent email canonicalisation code (#300)
 - stats.lua - inconsistent output between slow_count = true/false (#301)
+- confusion of storageAvailable and localStorage in ponymail.js (#194)
+- unnecessary double-checking of window.sessionStorage in ponymail.js (#193)
 
 ## CHANGES in 0.9b:
 

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_assign_vars.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_assign_vars.js b/site/js/dev/ponymail_assign_vars.js
index 764abb3..d858d9b 100644
--- a/site/js/dev/ponymail_assign_vars.js
+++ b/site/js/dev/ponymail_assign_vars.js
@@ -59,16 +59,23 @@ var pending_urls = {} // URL list for GetAsync's support functions (such as the
 var pb_refresh = 0
 var treeview_guard = {}
 var mbox_month = null
-var storageAvailable = false
 
-try {
-    if (typeof(window.sessionStorage) !== "undefined") {
-        window.sessionStorage.setItem("pm_test", "1")
-        storageAvailable = true
+function isStorageAvailable(type) {
+    try {
+        var storage = window[type],
+            x = 'pm_test';
+        storage.setItem(x, x);
+        storage.removeItem(x);
+        return true;
+    }
+    catch(e) {
+        return false;
     }
-} catch(e) {
-    storageAvailable = false
 }
+
+var localStorageAvailable   = isStorageAvailable('localStorage')
+var sessionStorageAvailable = isStorageAvailable('sessionStorage')
+
 // Links from viewmode to the function that handles them
 var viewModes = {
     threaded: {

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_composer.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_composer.js b/site/js/dev/ponymail_composer.js
index 6b98b82..7eb9b3a 100644
--- a/site/js/dev/ponymail_composer.js
+++ b/site/js/dev/ponymail_composer.js
@@ -20,22 +20,18 @@ function saveDraft() {
     // If the user was composing a new thread, let's save the contents (if any)
     // for next time
     if (document.getElementById('reply_body')) {
-        if (storageAvailable) {
+        if (sessionStorageAvailable) {
             if (composeType == "new") {
-                if (typeof(window.sessionStorage) !== "undefined") {
-                    window.sessionStorage.setItem("reply_body_" + xlist, document.getElementById('reply_body').value)
-                    window.sessionStorage.setItem("reply_subject_" + xlist, document.getElementById('reply_subject').value)
-                    window.sessionStorage.setItem("reply_list", xlist)
-                }
+                window.sessionStorage.setItem("reply_body_" + xlist, document.getElementById('reply_body').value)
+                window.sessionStorage.setItem("reply_subject_" + xlist, document.getElementById('reply_subject').value)
+                window.sessionStorage.setItem("reply_list", xlist)
                 composeType = ""
             // Likewise, if composing a reply, save it in case the user wants to revisit
             // the draft
             } else if (composeType == "reply" && current_reply_eid) {
-                if (typeof(window.sessionStorage) !== "undefined") {
-                    window.sessionStorage.setItem("reply_body_eid_" + current_reply_eid, document.getElementById('reply_body').value)
-                    window.sessionStorage.setItem("reply_subject_eid_" + current_reply_eid, document.getElementById('reply_subject').value)
-                    window.sessionStorage.setItem("reply_list_eid_", current_reply_eid)
-                }
+                window.sessionStorage.setItem("reply_body_eid_" + current_reply_eid, document.getElementById('reply_body').value)
+                window.sessionStorage.setItem("reply_subject_eid_" + current_reply_eid, document.getElementById('reply_subject').value)
+                window.sessionStorage.setItem("reply_list_eid_", current_reply_eid)
                 composeType = ""
             }
         }
@@ -78,13 +74,13 @@ function sendEmail(form) {
     request.send(of.join("&")) // send email as a POST string
     
     // Clear the draft stuff
-    if (storageAvailable) {
-        if (typeof(window.sessionStorage) !== "undefined" && compose_headers.eid && compose_headers.eid.length > 0) {
+    if (sessionStorageAvailable) {
+        if (compose_headers.eid && compose_headers.eid.length > 0) {
             window.sessionStorage.removeItem("reply_subject_eid_" + compose_headers.eid)
             window.sessionStorage.removeItem("reply_body_eid_" + compose_headers.eid)
         }
         // Clear new draft too if need be
-        if (typeof(window.sessionStorage) !== "undefined" && composeType == "new") {
+        if (composeType == "new") {
             window.sessionStorage.removeItem("reply_subject_" + xlist)
             window.sessionStorage.removeItem("reply_body_" + xlist)
         }
@@ -200,14 +196,11 @@ function compose(eid, lid, type) {
             obj.appendChild(area)
             
             // Do we need to fetch cache here?
-            if (storageAvailable) {
-                    
-                if (composeType == "new" && typeof(window.sessionStorage) !== "undefined" &&
-                    window.sessionStorage.getItem("reply_subject_" + xlist)) {
+            if (sessionStorageAvailable) {
+                if (composeType == "new" && window.sessionStorage.getItem("reply_subject_" + xlist)) {
                     area.innerHTML = window.sessionStorage.getItem("reply_body_" + xlist)
                     txt.value = window.sessionStorage.getItem("reply_subject_" + xlist)
-                } else if (composeType == "reply" && typeof(window.sessionStorage) !== "undefined" &&
-                    window.sessionStorage.getItem("reply_subject_eid_" + eid)) {
+                } else if (composeType == "reply" && window.sessionStorage.getItem("reply_subject_eid_" + eid)) {
                     area.innerHTML = window.sessionStorage.getItem("reply_body_eid_" + eid)
                     txt.value = window.sessionStorage.getItem("reply_subject_eid_" + eid)
                 }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_dom_helpers.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_dom_helpers.js b/site/js/dev/ponymail_dom_helpers.js
index 44b995b..5947859 100644
--- a/site/js/dev/ponymail_dom_helpers.js
+++ b/site/js/dev/ponymail_dom_helpers.js
@@ -218,7 +218,7 @@ function findEpoch(epoch) {
 
 // popup reminder shutoff mechanism
 function setPopup(pid, close) {
-    if (typeof(window.localStorage) !== "undefined") {
+    if (localStorageAvailable) {
         window.localStorage.setItem("popup_reminder_" + pid, close)
     }
 }
@@ -229,7 +229,7 @@ function setPopup(pid, close) {
 function popup(title, body, timeout, pid, wloc) {
     var obj = document.getElementById('popupper')
     if (pid) {
-        if (typeof(window.localStorage) !== "undefined") {
+        if (localStorageAvailable) {
             var popre = window.localStorage.getItem("popup_reminder_" + pid)
             if (popre) {
                 return

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_email_displays.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_email_displays.js b/site/js/dev/ponymail_email_displays.js
index 0348215..b128775 100644
--- a/site/js/dev/ponymail_email_displays.js
+++ b/site/js/dev/ponymail_email_displays.js
@@ -40,25 +40,22 @@ function displayEmail(json, id, level) {
     last_opened_email = json.mid
     
     // color based on view before or not??
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            if (! window.localStorage.getItem("viewed_" + json.mid) ){
-                //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+    if (localStorageAvailable) {
+        if (! window.localStorage.getItem("viewed_" + json.mid) ){
+            //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+
+            try {
+                window.localStorage.setItem("viewed_" + json.mid, json.epoch)
+            } catch(e) {
                 
-                try {
-                    window.localStorage.setItem("viewed_" + json.mid, json.epoch)
-                } catch(e) {
-                    
-                }
             }
-            if (window.localStorage.getItem("viewed_" + json.mid) && window.localStorage.getItem("viewed_" + json.mid).search("!") == 10){
-                //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
-                var epoch = parseInt(window.localStorage.getItem("viewed_" + json.mid))
-                try {
-                    window.localStorage.setItem("viewed_" + json.mid, epoch + ":")
-                } catch(e) {
-                    
-                }
+        }
+        if (window.localStorage.getItem("viewed_" + json.mid) && window.localStorage.getItem("viewed_" + json.mid).search("!") == 10){
+            //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+            var epoch = parseInt(window.localStorage.getItem("viewed_" + json.mid))
+            try {
+                window.localStorage.setItem("viewed_" + json.mid, epoch + ":")
+            } catch(e) {
                 
             }
         }
@@ -97,12 +94,10 @@ function displayEmail(json, id, level) {
         ebody = ebody.replace(re_weburl, "<a href='$1'>$1</a>")
         
         // Get theme (social, default etc) if set locally in browser
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                var th = window.localStorage.getItem("pm_theme")
-                if (th) {
-                    prefs.theme = th
-                }
+        if (localStorageAvailable) {
+            var th = window.localStorage.getItem("pm_theme")
+            if (th) {
+                prefs.theme = th
             }
         }
         
@@ -227,15 +222,13 @@ function displaySingleEmail(json, id) {
 
     var thread = document.getElementById('email')
     if (thread) {
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + json.id) ){
-                    estyle = "background: background: linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%);"
-                    try {
-                        window.localStorage.setItem("viewed_" + json.id, latestEmailInThread + "!")
-                    } catch(e) {
-                        
-                    }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + json.id) ){
+                estyle = "background: background: linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%);"
+                try {
+                    window.localStorage.setItem("viewed_" + json.id, latestEmailInThread + "!")
+                } catch(e) {
+                    
                 }
             }
         }
@@ -353,23 +346,21 @@ function toggleEmails_threaded(id, close, toverride, threadobj) {
     var thread = threadobj ? threadobj : document.getElementById('thread_' + id.toString().replace(/@<.+>/, ""))
     if (thread) {
         current_thread = id
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                var epoch = latestEmailInThread + "!"
-                if (current_thread_json[id]) {
-                    var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
-                    if (xx) {
-                        var yy = parseInt(xx)
-                        if (yy >= parseInt(latestEmailInThread)) {
-                            epoch = yy
-                        }
-                    }
-                    try {
-                        window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
-                    } catch(e) {
-                        
+        if (localStorageAvailable) {
+            var epoch = latestEmailInThread + "!"
+            if (current_thread_json[id]) {
+                var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
+                if (xx) {
+                    var yy = parseInt(xx)
+                    if (yy >= parseInt(latestEmailInThread)) {
+                        epoch = yy
                     }
                 }
+                try {
+                    window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
+                } catch(e) {
+                    
+                }
             }
         }
         
@@ -451,27 +442,25 @@ function toggleEmails_threaded(id, close, toverride, threadobj) {
 // actually viewed before.
 function highlightNewEmails(id) {
     // This currently requires localStorage to store the view data
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            kiddos = []
-            var t = document.getElementById("thread_" + id)
-            if (t) {
-                traverseThread(t, 'thread') // find all child elements called 'thread*'
-                // For each email in this thread, check (or set) when it was first viewed
-                for (var i in kiddos) {
-                    var mid = kiddos[i].getAttribute("id")
-                    var epoch = window.localStorage.getItem("first_view_" + mid)
-                    if (epoch && epoch != pb_refresh) { // did we view this before the last page build?
-                        kiddos[i].style.color = "#AAA"
-                    } else { // never seen it before, have it at normal color and set the first-view-date
+    if (localStorageAvailable) {
+        kiddos = []
+        var t = document.getElementById("thread_" + id)
+        if (t) {
+            traverseThread(t, 'thread') // find all child elements called 'thread*'
+            // For each email in this thread, check (or set) when it was first viewed
+            for (var i in kiddos) {
+                var mid = kiddos[i].getAttribute("id")
+                var epoch = window.localStorage.getItem("first_view_" + mid)
+                if (epoch && epoch != pb_refresh) { // did we view this before the last page build?
+                    kiddos[i].style.color = "#AAA"
+                } else { // never seen it before, have it at normal color and set the first-view-date
+                    
+                    try {
+                        window.localStorage.setItem("first_view_" + mid, pb_refresh)
+                    } catch(e) {
                         
-                        try {
-                            window.localStorage.setItem("first_view_" + mid, pb_refresh)
-                        } catch(e) {
-                            
-                        }
-                        kiddos[i].style.color = "#000"
                     }
+                    kiddos[i].style.color = "#000"
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_helperfuncs.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_helperfuncs.js b/site/js/dev/ponymail_helperfuncs.js
index 05d6b52..5a6024d 100644
--- a/site/js/dev/ponymail_helperfuncs.js
+++ b/site/js/dev/ponymail_helperfuncs.js
@@ -153,27 +153,23 @@ function showSpinner(show) {
 // Saving prefs as a json string
 function saveEphemeral() {
     // This only works if the browser supports localStorage
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
     }
 }
 
 // load ephemeral prefs, replace what we have
 function loadEphemeral() {
     // This only works if the browser supports localStorage
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var str = window.localStorage.getItem("ponymail_config_ephemeral")
-            if (str) {
-                var eprefs = JSON.parse(str)
-                // for each original setting in config.js,
-                // check if we have a different one stored
-                for (i in prefs) {
-                    if (eprefs[i]) {
-                        prefs[i] = eprefs[i] // override
-                    }
+    if (localStorageAvailable) {
+        var str = window.localStorage.getItem("ponymail_config_ephemeral")
+        if (str) {
+            var eprefs = JSON.parse(str)
+            // for each original setting in config.js,
+            // check if we have a different one stored
+            for (i in prefs) {
+                if (eprefs[i]) {
+                    prefs[i] = eprefs[i] // override
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_listview_flat.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_listview_flat.js b/site/js/dev/ponymail_listview_flat.js
index 59c47f4..9a93f81 100644
--- a/site/js/dev/ponymail_listview_flat.js
+++ b/site/js/dev/ponymail_listview_flat.js
@@ -117,11 +117,9 @@ function loadList_flat(mjson, limit, start, deep) {
         
         // style based on view before or not??
         var estyle = ""
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) ){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) ){
+                estyle = "font-weight: bold;"
             }
         }
         var at = ""

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_listview_threaded.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_listview_threaded.js b/site/js/dev/ponymail_listview_threaded.js
index 8b14919..9d0de49 100644
--- a/site/js/dev/ponymail_listview_threaded.js
+++ b/site/js/dev/ponymail_listview_threaded.js
@@ -18,12 +18,10 @@
 
 // loadList_threaded: Same as above, but threaded display
 function loadList_threaded(mjson, limit, start, deep) {
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var th = window.localStorage.getItem("pm_theme")
-            if (th) {
-                prefs.theme = th
-            }
+    if (localStorageAvailable) {
+        var th = window.localStorage.getItem("pm_theme")
+        if (th) {
+            prefs.theme = th
         }
     }
     
@@ -140,11 +138,9 @@ function loadList_threaded(mjson, limit, start, deep) {
         var pds = people > 1 ? "visible" : "hidden"
         
         // style based on view before or not??
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
+                estyle = "font-weight: bold;"
             }
         }
         

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_listview_tree.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_listview_tree.js b/site/js/dev/ponymail_listview_tree.js
index c5a8026..1a463bb 100644
--- a/site/js/dev/ponymail_listview_tree.js
+++ b/site/js/dev/ponymail_listview_tree.js
@@ -18,12 +18,10 @@
 
 // loadList_treeview: Load a list as a treeview object, grouped by threads
 function loadList_treeview(mjson, limit, start, deep) {
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var th = window.localStorage.getItem("pm_theme")
-            if (th) {
-                prefs.theme = th
-            }
+    if (localStorageAvailable) {
+        var th = window.localStorage.getItem("pm_theme")
+        if (th) {
+            prefs.theme = th
         }
     }
     // Set displayed posts per page to 10 if social/compact theme, or auto-scale
@@ -113,11 +111,9 @@ function loadList_treeview(mjson, limit, start, deep) {
         var pds = people > 1 ? "visible" : "hidden"
         
         // style based on view before or not??
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
+                estyle = "font-weight: bold;"
             }
         }
         var people_label = "<label style='visibility:" + pds + "; float: right; margin-right: 8px; ' id='people_"+i+"' class='listview_label label label-" + lp + "'> <span class='glyphicon glyphicon-user'> </span> " + people + " <span class='hidden-xs hidden-sm'>people</span></label>"
@@ -345,7 +341,7 @@ function buildTreeview(nesting, list, obj, pbigger) {
         
         // style based on view before or not??
         var estyle = ""
-        if (typeof(window.localStorage) !== "undefined") {
+        if (localStorageAvailable) {
             if (! window.localStorage.getItem("viewed_" + eml.id) ){
                 estyle = "font-weight: bold;"
             }
@@ -393,15 +389,13 @@ function toggleEmails_treeview(id, close, toverride) {
             return
         }
         var epoch = null
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                epoch = latestEmailInThread + "!"
-                var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
-                if (xx) {
-                    var yy = parseInt(xx)
-                    if (yy >= parseInt(latestEmailInThread)) {
-                        epoch = yy
-                    }
+        if (localStorageAvailable) {
+            epoch = latestEmailInThread + "!"
+            var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
+            if (xx) {
+                var yy = parseInt(xx)
+                if (yy >= parseInt(latestEmailInThread)) {
+                    epoch = yy
                 }
             }
         }
@@ -456,7 +450,7 @@ function toggleEmails_treeview(id, close, toverride) {
         var html = buildTreeview(nesting, [current_thread_json[id]], thread, [true])
         current_thread = current_thread_json[id].tid
         
-        if (epoch !== null) { // only non-null if localstorage works
+        if (epoch !== null && localStorageAvailable) { // only non-null if localstorage works, but check anyway for consistency
             try {
                 window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
             } catch(e) {

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/dev/ponymail_user_preferences.js
----------------------------------------------------------------------
diff --git a/site/js/dev/ponymail_user_preferences.js b/site/js/dev/ponymail_user_preferences.js
index 246541f..252ee62 100644
--- a/site/js/dev/ponymail_user_preferences.js
+++ b/site/js/dev/ponymail_user_preferences.js
@@ -46,10 +46,8 @@ function savePreferences() {
     GetAsync("/api/preferences.lua?save=true&" + prefarr.join("&"), null, hideComposer)
     
     // Save ephemeral settings
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
     }
 }
 
@@ -262,10 +260,8 @@ function setupUser() {
 // set theme, both in prefs and localstorage (for non-logged-in-users)
 function setTheme(theme) {
     prefs.theme = theme
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("pm_theme", theme)
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("pm_theme", theme)
         if (document.getElementById('emails')) {
             buildPage()
         }

http://git-wip-us.apache.org/repos/asf/incubator-ponymail/blob/70eb2633/site/js/ponymail.js
----------------------------------------------------------------------
diff --git a/site/js/ponymail.js b/site/js/ponymail.js
index 1d08e94..8f52fba 100644
--- a/site/js/ponymail.js
+++ b/site/js/ponymail.js
@@ -67,16 +67,23 @@ var pending_urls = {} // URL list for GetAsync's support functions (such as the
 var pb_refresh = 0
 var treeview_guard = {}
 var mbox_month = null
-var storageAvailable = false
 
-try {
-    if (typeof(window.sessionStorage) !== "undefined") {
-        window.sessionStorage.setItem("pm_test", "1")
-        storageAvailable = true
+function isStorageAvailable(type) {
+    try {
+        var storage = window[type],
+            x = 'pm_test';
+        storage.setItem(x, x);
+        storage.removeItem(x);
+        return true;
+    }
+    catch(e) {
+        return false;
     }
-} catch(e) {
-    storageAvailable = false
 }
+
+var localStorageAvailable   = isStorageAvailable('localStorage')
+var sessionStorageAvailable = isStorageAvailable('sessionStorage')
+
 // Links from viewmode to the function that handles them
 var viewModes = {
     threaded: {
@@ -107,22 +114,18 @@ function saveDraft() {
     // If the user was composing a new thread, let's save the contents (if any)
     // for next time
     if (document.getElementById('reply_body')) {
-        if (storageAvailable) {
+        if (sessionStorageAvailable) {
             if (composeType == "new") {
-                if (typeof(window.sessionStorage) !== "undefined") {
-                    window.sessionStorage.setItem("reply_body_" + xlist, document.getElementById('reply_body').value)
-                    window.sessionStorage.setItem("reply_subject_" + xlist, document.getElementById('reply_subject').value)
-                    window.sessionStorage.setItem("reply_list", xlist)
-                }
+                window.sessionStorage.setItem("reply_body_" + xlist, document.getElementById('reply_body').value)
+                window.sessionStorage.setItem("reply_subject_" + xlist, document.getElementById('reply_subject').value)
+                window.sessionStorage.setItem("reply_list", xlist)
                 composeType = ""
             // Likewise, if composing a reply, save it in case the user wants to revisit
             // the draft
             } else if (composeType == "reply" && current_reply_eid) {
-                if (typeof(window.sessionStorage) !== "undefined") {
-                    window.sessionStorage.setItem("reply_body_eid_" + current_reply_eid, document.getElementById('reply_body').value)
-                    window.sessionStorage.setItem("reply_subject_eid_" + current_reply_eid, document.getElementById('reply_subject').value)
-                    window.sessionStorage.setItem("reply_list_eid_", current_reply_eid)
-                }
+                window.sessionStorage.setItem("reply_body_eid_" + current_reply_eid, document.getElementById('reply_body').value)
+                window.sessionStorage.setItem("reply_subject_eid_" + current_reply_eid, document.getElementById('reply_subject').value)
+                window.sessionStorage.setItem("reply_list_eid_", current_reply_eid)
                 composeType = ""
             }
         }
@@ -165,13 +168,13 @@ function sendEmail(form) {
     request.send(of.join("&")) // send email as a POST string
     
     // Clear the draft stuff
-    if (storageAvailable) {
-        if (typeof(window.sessionStorage) !== "undefined" && compose_headers.eid && compose_headers.eid.length > 0) {
+    if (sessionStorageAvailable) {
+        if (compose_headers.eid && compose_headers.eid.length > 0) {
             window.sessionStorage.removeItem("reply_subject_eid_" + compose_headers.eid)
             window.sessionStorage.removeItem("reply_body_eid_" + compose_headers.eid)
         }
         // Clear new draft too if need be
-        if (typeof(window.sessionStorage) !== "undefined" && composeType == "new") {
+        if (composeType == "new") {
             window.sessionStorage.removeItem("reply_subject_" + xlist)
             window.sessionStorage.removeItem("reply_body_" + xlist)
         }
@@ -287,14 +290,11 @@ function compose(eid, lid, type) {
             obj.appendChild(area)
             
             // Do we need to fetch cache here?
-            if (storageAvailable) {
-                    
-                if (composeType == "new" && typeof(window.sessionStorage) !== "undefined" &&
-                    window.sessionStorage.getItem("reply_subject_" + xlist)) {
+            if (sessionStorageAvailable) {
+                if (composeType == "new" && window.sessionStorage.getItem("reply_subject_" + xlist)) {
                     area.innerHTML = window.sessionStorage.getItem("reply_body_" + xlist)
                     txt.value = window.sessionStorage.getItem("reply_subject_" + xlist)
-                } else if (composeType == "reply" && typeof(window.sessionStorage) !== "undefined" &&
-                    window.sessionStorage.getItem("reply_subject_eid_" + eid)) {
+                } else if (composeType == "reply" && window.sessionStorage.getItem("reply_subject_eid_" + eid)) {
                     area.innerHTML = window.sessionStorage.getItem("reply_body_eid_" + eid)
                     txt.value = window.sessionStorage.getItem("reply_subject_eid_" + eid)
                 }
@@ -1314,7 +1314,7 @@ function findEpoch(epoch) {
 
 // popup reminder shutoff mechanism
 function setPopup(pid, close) {
-    if (typeof(window.localStorage) !== "undefined") {
+    if (localStorageAvailable) {
         window.localStorage.setItem("popup_reminder_" + pid, close)
     }
 }
@@ -1325,7 +1325,7 @@ function setPopup(pid, close) {
 function popup(title, body, timeout, pid, wloc) {
     var obj = document.getElementById('popupper')
     if (pid) {
-        if (typeof(window.localStorage) !== "undefined") {
+        if (localStorageAvailable) {
             var popre = window.localStorage.getItem("popup_reminder_" + pid)
             if (popre) {
                 return
@@ -1398,25 +1398,22 @@ function displayEmail(json, id, level) {
     last_opened_email = json.mid
     
     // color based on view before or not??
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            if (! window.localStorage.getItem("viewed_" + json.mid) ){
-                //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+    if (localStorageAvailable) {
+        if (! window.localStorage.getItem("viewed_" + json.mid) ){
+            //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+
+            try {
+                window.localStorage.setItem("viewed_" + json.mid, json.epoch)
+            } catch(e) {
                 
-                try {
-                    window.localStorage.setItem("viewed_" + json.mid, json.epoch)
-                } catch(e) {
-                    
-                }
             }
-            if (window.localStorage.getItem("viewed_" + json.mid) && window.localStorage.getItem("viewed_" + json.mid).search("!") == 10){
-                //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
-                var epoch = parseInt(window.localStorage.getItem("viewed_" + json.mid))
-                try {
-                    window.localStorage.setItem("viewed_" + json.mid, epoch + ":")
-                } catch(e) {
-                    
-                }
+        }
+        if (window.localStorage.getItem("viewed_" + json.mid) && window.localStorage.getItem("viewed_" + json.mid).search("!") == 10){
+            //estyle = "linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%)"
+            var epoch = parseInt(window.localStorage.getItem("viewed_" + json.mid))
+            try {
+                window.localStorage.setItem("viewed_" + json.mid, epoch + ":")
+            } catch(e) {
                 
             }
         }
@@ -1455,12 +1452,10 @@ function displayEmail(json, id, level) {
         ebody = ebody.replace(re_weburl, "<a href='$1'>$1</a>")
         
         // Get theme (social, default etc) if set locally in browser
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                var th = window.localStorage.getItem("pm_theme")
-                if (th) {
-                    prefs.theme = th
-                }
+        if (localStorageAvailable) {
+            var th = window.localStorage.getItem("pm_theme")
+            if (th) {
+                prefs.theme = th
             }
         }
         
@@ -1585,15 +1580,13 @@ function displaySingleEmail(json, id) {
 
     var thread = document.getElementById('email')
     if (thread) {
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + json.id) ){
-                    estyle = "background: background: linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%);"
-                    try {
-                        window.localStorage.setItem("viewed_" + json.id, latestEmailInThread + "!")
-                    } catch(e) {
-                        
-                    }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + json.id) ){
+                estyle = "background: background: linear-gradient(to bottom, rgba(252,255,244,1) 0%,rgba(233,233,206,1) 100%);"
+                try {
+                    window.localStorage.setItem("viewed_" + json.id, latestEmailInThread + "!")
+                } catch(e) {
+                    
                 }
             }
         }
@@ -1711,23 +1704,21 @@ function toggleEmails_threaded(id, close, toverride, threadobj) {
     var thread = threadobj ? threadobj : document.getElementById('thread_' + id.toString().replace(/@<.+>/, ""))
     if (thread) {
         current_thread = id
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                var epoch = latestEmailInThread + "!"
-                if (current_thread_json[id]) {
-                    var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
-                    if (xx) {
-                        var yy = parseInt(xx)
-                        if (yy >= parseInt(latestEmailInThread)) {
-                            epoch = yy
-                        }
-                    }
-                    try {
-                        window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
-                    } catch(e) {
-                        
+        if (localStorageAvailable) {
+            var epoch = latestEmailInThread + "!"
+            if (current_thread_json[id]) {
+                var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
+                if (xx) {
+                    var yy = parseInt(xx)
+                    if (yy >= parseInt(latestEmailInThread)) {
+                        epoch = yy
                     }
                 }
+                try {
+                    window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
+                } catch(e) {
+                    
+                }
             }
         }
         
@@ -1809,27 +1800,25 @@ function toggleEmails_threaded(id, close, toverride, threadobj) {
 // actually viewed before.
 function highlightNewEmails(id) {
     // This currently requires localStorage to store the view data
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            kiddos = []
-            var t = document.getElementById("thread_" + id)
-            if (t) {
-                traverseThread(t, 'thread') // find all child elements called 'thread*'
-                // For each email in this thread, check (or set) when it was first viewed
-                for (var i in kiddos) {
-                    var mid = kiddos[i].getAttribute("id")
-                    var epoch = window.localStorage.getItem("first_view_" + mid)
-                    if (epoch && epoch != pb_refresh) { // did we view this before the last page build?
-                        kiddos[i].style.color = "#AAA"
-                    } else { // never seen it before, have it at normal color and set the first-view-date
+    if (localStorageAvailable) {
+        kiddos = []
+        var t = document.getElementById("thread_" + id)
+        if (t) {
+            traverseThread(t, 'thread') // find all child elements called 'thread*'
+            // For each email in this thread, check (or set) when it was first viewed
+            for (var i in kiddos) {
+                var mid = kiddos[i].getAttribute("id")
+                var epoch = window.localStorage.getItem("first_view_" + mid)
+                if (epoch && epoch != pb_refresh) { // did we view this before the last page build?
+                    kiddos[i].style.color = "#AAA"
+                } else { // never seen it before, have it at normal color and set the first-view-date
+                    
+                    try {
+                        window.localStorage.setItem("first_view_" + mid, pb_refresh)
+                    } catch(e) {
                         
-                        try {
-                            window.localStorage.setItem("first_view_" + mid, pb_refresh)
-                        } catch(e) {
-                            
-                        }
-                        kiddos[i].style.color = "#000"
                     }
+                    kiddos[i].style.color = "#000"
                 }
             }
         }
@@ -2320,27 +2309,23 @@ function showSpinner(show) {
 // Saving prefs as a json string
 function saveEphemeral() {
     // This only works if the browser supports localStorage
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
     }
 }
 
 // load ephemeral prefs, replace what we have
 function loadEphemeral() {
     // This only works if the browser supports localStorage
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var str = window.localStorage.getItem("ponymail_config_ephemeral")
-            if (str) {
-                var eprefs = JSON.parse(str)
-                // for each original setting in config.js,
-                // check if we have a different one stored
-                for (i in prefs) {
-                    if (eprefs[i]) {
-                        prefs[i] = eprefs[i] // override
-                    }
+    if (localStorageAvailable) {
+        var str = window.localStorage.getItem("ponymail_config_ephemeral")
+        if (str) {
+            var eprefs = JSON.parse(str)
+            // for each original setting in config.js,
+            // check if we have a different one stored
+            for (i in prefs) {
+                if (eprefs[i]) {
+                    prefs[i] = eprefs[i] // override
                 }
             }
         }
@@ -2463,11 +2448,9 @@ function loadList_flat(mjson, limit, start, deep) {
         
         // style based on view before or not??
         var estyle = ""
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) ){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) ){
+                estyle = "font-weight: bold;"
             }
         }
         var at = ""
@@ -2621,12 +2604,10 @@ function loadEmails_flat(id, close, treeview) {
 
 // loadList_threaded: Same as above, but threaded display
 function loadList_threaded(mjson, limit, start, deep) {
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var th = window.localStorage.getItem("pm_theme")
-            if (th) {
-                prefs.theme = th
-            }
+    if (localStorageAvailable) {
+        var th = window.localStorage.getItem("pm_theme")
+        if (th) {
+            prefs.theme = th
         }
     }
     
@@ -2743,11 +2724,9 @@ function loadList_threaded(mjson, limit, start, deep) {
         var pds = people > 1 ? "visible" : "hidden"
         
         // style based on view before or not??
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
+                estyle = "font-weight: bold;"
             }
         }
         
@@ -2908,12 +2887,10 @@ function loadEmails_threaded(json, state) {
 
 // loadList_treeview: Load a list as a treeview object, grouped by threads
 function loadList_treeview(mjson, limit, start, deep) {
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            var th = window.localStorage.getItem("pm_theme")
-            if (th) {
-                prefs.theme = th
-            }
+    if (localStorageAvailable) {
+        var th = window.localStorage.getItem("pm_theme")
+        if (th) {
+            prefs.theme = th
         }
     }
     // Set displayed posts per page to 10 if social/compact theme, or auto-scale
@@ -3003,11 +2980,9 @@ function loadList_treeview(mjson, limit, start, deep) {
         var pds = people > 1 ? "visible" : "hidden"
         
         // style based on view before or not??
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
-                    estyle = "font-weight: bold;"
-                }
+        if (localStorageAvailable) {
+            if (! window.localStorage.getItem("viewed_" + eml.id) || (subs > 0 && parseInt(window.localStorage.getItem("viewed_" + eml.id)) < latest )){
+                estyle = "font-weight: bold;"
             }
         }
         var people_label = "<label style='visibility:" + pds + "; float: right; margin-right: 8px; ' id='people_"+i+"' class='listview_label label label-" + lp + "'> <span class='glyphicon glyphicon-user'> </span> " + people + " <span class='hidden-xs hidden-sm'>people</span></label>"
@@ -3235,7 +3210,7 @@ function buildTreeview(nesting, list, obj, pbigger) {
         
         // style based on view before or not??
         var estyle = ""
-        if (typeof(window.localStorage) !== "undefined") {
+        if (localStorageAvailable) {
             if (! window.localStorage.getItem("viewed_" + eml.id) ){
                 estyle = "font-weight: bold;"
             }
@@ -3283,15 +3258,13 @@ function toggleEmails_treeview(id, close, toverride) {
             return
         }
         var epoch = null
-        if (storageAvailable) {
-            if (typeof(window.localStorage) !== "undefined") {
-                epoch = latestEmailInThread + "!"
-                var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
-                if (xx) {
-                    var yy = parseInt(xx)
-                    if (yy >= parseInt(latestEmailInThread)) {
-                        epoch = yy
-                    }
+        if (localStorageAvailable) {
+            epoch = latestEmailInThread + "!"
+            var xx = window.localStorage.getItem("viewed_" + current_thread_json[id].tid)
+            if (xx) {
+                var yy = parseInt(xx)
+                if (yy >= parseInt(latestEmailInThread)) {
+                    epoch = yy
                 }
             }
         }
@@ -3346,7 +3319,7 @@ function toggleEmails_treeview(id, close, toverride) {
         var html = buildTreeview(nesting, [current_thread_json[id]], thread, [true])
         current_thread = current_thread_json[id].tid
         
-        if (epoch !== null) { // only non-null if localstorage works
+        if (epoch !== null && localStorageAvailable) { // only non-null if localstorage works, but check anyway for consistency
             try {
                 window.localStorage.setItem("viewed_" + current_thread_json[id].tid, epoch)
             } catch(e) {
@@ -5120,10 +5093,8 @@ function savePreferences() {
     GetAsync("/api/preferences.lua?save=true&" + prefarr.join("&"), null, hideComposer)
     
     // Save ephemeral settings
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("ponymail_config_ephemeral", JSON.stringify(prefs))
     }
 }
 
@@ -5336,10 +5307,8 @@ function setupUser() {
 // set theme, both in prefs and localstorage (for non-logged-in-users)
 function setTheme(theme) {
     prefs.theme = theme
-    if (storageAvailable) {
-        if (typeof(window.localStorage) !== "undefined") {
-            window.localStorage.setItem("pm_theme", theme)
-        }
+    if (localStorageAvailable) {
+        window.localStorage.setItem("pm_theme", theme)
         if (document.getElementById('emails')) {
             buildPage()
         }


Mime
View raw message