Author:   Lars Michelsen <[email protected]>
Date:     Sat Jul 23 13:15:16 2011 +0200
Committer:   Lars Michelsen <[email protected]>
Commit-Date: Sat Jul 23 13:15:16 2011 +0200

Frontend hides ajax/json errors now after first successfull action of that type

For example ajax error messages or json parse error messages are shown
until next transaction is successfull. This way it is not needed anymore
to reload the page manually after e.g. a network problem or similar.

---

 ChangeLog                                      |    2 +
 share/frontend/nagvis-js/js/ajax.js            |   59 +++++++++++++----------
 share/frontend/nagvis-js/js/frontendMessage.js |   34 ++++++++++++--
 3 files changed, 64 insertions(+), 31 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 6bdab90..d7daa3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,8 @@
 1.6b3
 
 Frontend
+  * Frontend hides ajax/json errors now after first successfull action of that
+  type
   * Bugfix: Fixed background image positioning with enabled sidebar and small 
screens
   * Bugfix: Fixed coloring of two part lines in IE
   * Bugfix: Fixed drawing of second part in two part lines in IE
diff --git a/share/frontend/nagvis-js/js/ajax.js 
b/share/frontend/nagvis-js/js/ajax.js
index f8be9ac..265d5b4 100644
--- a/share/frontend/nagvis-js/js/ajax.js
+++ b/share/frontend/nagvis-js/js/ajax.js
@@ -114,7 +114,7 @@ function ajaxError(e) {
     eventlog("ajax", "critical", "Problem while ajax transaction");
     eventlog("ajax", "debug", e.toString());
 
-    frontendMessage({'type': 'CRITICAL', 'title': 'Ajax transaction error', 
'message': 'Problem while ajax transaction. Is the NagVis host reachable?'});
+    frontendMessage({'type': 'CRITICAL', 'title': 'Ajax transaction error', 
'message': 'Problem while ajax transaction. Is the NagVis host reachable?'}, 0, 
'ajaxError');
 }
 
 function phpError(text) {
@@ -122,7 +122,7 @@ function phpError(text) {
 }
 
 function jsonError(text) {
-    frontendMessage({'type': 'CRITICAL', 'title': 'Syntax error', 'message': 
text});
+    frontendMessage({'type': 'CRITICAL', 'title': 'Syntax error', 'message': 
text}, 0, 'jsonError');
 }
 
 /**
@@ -163,6 +163,7 @@ function getAsyncRequest(sUrl, bCacheable, callback, 
callbackParams) {
         oRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 
00:00:00 GMT");
         oRequest.onreadystatechange = function() {
             if(oRequest.readyState == 4) {
+                frontendMessageRemove('ajaxError');
                 if(oRequest.responseText.replace(/\s+/g, '').length === 0) {
                     if(bCacheable)
                         updateQueryCache(sUrl, iNow, '');
@@ -175,16 +176,11 @@ function getAsyncRequest(sUrl, bCacheable, callback, 
callbackParams) {
                     } else if(responseText.match(/^NagVisError:/)) {
                         frontendMessage(eval('( 
'+responseText.replace(/^NagVisError:/, '')+')'));
                     } else {
-            var oResponse = null;
-                        try {
-                            oResponse = eval('( '+responseText+')')
-
-                            if(bCacheable)
-                                updateQueryCache(sUrl, iNow, responseText);
-                        } catch(e) {
-                            jsonError("Exception: " + e.description + "\nTime: 
" + iNow + "\nURL: " + sUrl + "\nResponse: " + responseText);
-                        }
-            if(oResponse)
+                        // Handle responses of json objects - including eval 
and wron response
+                        // error handling and clearing
+                        var oResponse = handleJsonResponse(sUrl, responseText)
+
+                        if(oResponse)
                             callback(oResponse, callbackParams);
                         oResponse = null;
                     }
@@ -241,7 +237,9 @@ function getSyncRequest(sUrl, bCacheable, bRetryable) {
 
         // Prevent using invalid code in cache
         if(responseText !== '') {
-            sResponse = eval('( '+responseText+')');
+            // Handle responses of json objects - including eval and wron 
response
+            // error handling and clearing
+            sResponse = handleJsonResponse(sUrl, responseText)
         } else {
             // Remove the invalid code from cache
             cleanupQueryCache(sUrl);
@@ -261,6 +259,7 @@ function getSyncRequest(sUrl, bCacheable, bRetryable) {
 
             try {
                 oRequest.send(null);
+                frontendMessageRemove('ajaxError');
             } catch(e) {
                 ajaxError(e);
                 bCacheable = false;
@@ -294,18 +293,9 @@ function getSyncRequest(sUrl, bCacheable, bRetryable) {
                     // Clear the response
                     sResponse = '';
                 } else {
-                    // Handle invalid response (No JSON format)
-                    try {
-                        sResponse = eval('( '+responseText+')');
-                    } catch(e) {
-                        jsonError("Invalid json response:\nTime:" + timestamp 
+ "\nURL: " + sUrl + "\nResponse: " + responseText);
-                        sResponse = '';
-                    }
-
-                    if(typeof(sResponse) !== 'object') {
-                        jsonError("Invalid json response:\nTime:" + timestamp 
+ "\nURL: " + sUrl + "\nResponse: " + responseText);
-                        sResponse = '';
-                    }
+                    // Handle responses of json objects - including eval and 
wron response
+                    // error handling and clearing
+                    sResponse = handleJsonResponse(sUrl, responseText)
 
                     if(sResponse !== null && bCacheable) {
                         // Cache that answer (only when no error/warning/...)
@@ -325,6 +315,22 @@ function getSyncRequest(sUrl, bCacheable, bRetryable) {
     return sResponse;
 }
 
+function handleJsonResponse(sUrl, responseText) {
+    try {
+        sResponse = eval('( '+responseText+')');
+        frontendMessageRemove('jsonError');
+        return sResponse;
+    } catch(e) {
+        jsonError("Invalid json response:\nTime:" + iNow + "\nURL: " + sUrl + 
"\nResponse: " + responseText);
+        return '';
+    }
+
+    if(typeof(sResponse) !== 'object') {
+        jsonError("Invalid json response:\nTime:" + iNow + "\nURL: " + sUrl + 
"\nResponse: " + responseText);
+        return '';
+    }
+}
+
 /**
  * This function simply loads a remote url and returns the responseText 1:1
  * without any special error handling
@@ -404,6 +410,7 @@ function postSyncRequest(sUrl, sParams) {
 
         try {
             oRequest.send(sParams);
+            frontendMessageRemove('ajaxError');
         } catch(e) {
             ajaxError(e);
         }
@@ -551,4 +558,4 @@ function getFormParams(formId) {
     oForm = null;
 
     return sReturn;
-}
\ No newline at end of file
+}
diff --git a/share/frontend/nagvis-js/js/frontendMessage.js 
b/share/frontend/nagvis-js/js/frontendMessage.js
index 7c5a1bd..b6806ca 100644
--- a/share/frontend/nagvis-js/js/frontendMessage.js
+++ b/share/frontend/nagvis-js/js/frontendMessage.js
@@ -25,6 +25,8 @@
  * @author     Lars Michelsen <[email protected]>
  */
 
+var frontendMessages = {};
+
 function frontendMessageActive() {
     if(document.getElementById('messageBoxDiv')) {
         return true;
@@ -39,7 +41,18 @@ function frontendMessageHide() {
     }
 }
 
-function frontendMessage(oMessage, iTimeout) {
+function frontendMessagePresent(key) {
+    return isset(frontendMessages[key]);
+}
+
+function frontendMessageRemove(key) {
+    if(frontendMessagePresent(key)) {
+        document.body.removeChild(frontendMessages[key]);
+        delete frontendMessages[key];
+    }
+}
+
+function frontendMessage(oMessage, iTimeout, key) {
     var oContainerDiv;
     var oTable;
     var oTbody;
@@ -50,18 +63,29 @@ function frontendMessage(oMessage, iTimeout) {
     var sBoxType = oMessage.type.toLowerCase();
     var sTitle = '';
 
-    if(typeof oMessage.title !== 'undefined') {
+    if(typeof oMessage.title !== 'undefined')
         sTitle = oMessage.title;
-    }
+
+    // Skip processing when message with same key already shown
+    if(isset(key) && frontendMessagePresent(key))
+        return;
 
     // Set a close timeout when called to do so
-    if(typeof iTimeout !== 'undefined') {
+    if(typeof iTimeout !== 'undefined' && iTimeout !== 0) {
         window.setTimeout(function() { frontendMessageHide(); }, 
iTimeout*1000);
     }
 
     oContainerDiv = document.createElement('div');
     oContainerDiv.setAttribute('id', 'messageBoxDiv');
 
+    // An optional key can be defined to create this message.
+    // This key can later be used to
+    // a) Remove this message again
+    // b) Don't open the same message twice
+    if(isset(key)) {
+        frontendMessages[key] = oContainerDiv;
+    }
+
     oTable = document.createElement('table');
     oTable.setAttribute('id', 'messageBox');
     oTable.setAttribute('class', sBoxType);
@@ -181,4 +205,4 @@ function frontendMessage(oMessage, iTimeout) {
         // Register reload/redirect
         window.setTimeout(function() { window.location.href = sUrl; }, 
oMessage.reloadTime*1000);
     }
-}
\ No newline at end of file
+}


------------------------------------------------------------------------------
Storage Efficiency Calculator
This modeling tool is based on patent-pending intellectual property that
has been used successfully in hundreds of IBM storage optimization engage-
ments, worldwide.  Store less, Store more with what you own, Move data to 
the right place. Try It Now! http://www.accelacomm.com/jaw/sfnl/114/51427378/
_______________________________________________
Nagvis-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/nagvis-checkins

Reply via email to