From 72340f2504c275a5686f8502e5fe6101c1d64a33 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Mon, 21 Jul 2014 14:39:46 +0200
Subject: [PATCH] refs #5406 hide loading indicator if any error occurs and
 make sure to display an error message. Not sure yet about any side effects

---
 plugins/CoreHome/CoreHome.php                    | 1 +
 plugins/CoreHome/javascripts/broadcast.js        | 6 +++---
 plugins/CoreHome/javascripts/corehome.js         | 2 ++
 plugins/CoreHome/javascripts/dataTable.js        | 6 ++++++
 plugins/Dashboard/javascripts/dashboardWidget.js | 6 +++++-
 plugins/Dashboard/javascripts/widgetMenu.js      | 5 ++++-
 plugins/Dashboard/stylesheets/dashboard.less     | 2 +-
 7 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/plugins/CoreHome/CoreHome.php b/plugins/CoreHome/CoreHome.php
index 18ad9f68c2..172ce8d5b8 100644
--- a/plugins/CoreHome/CoreHome.php
+++ b/plugins/CoreHome/CoreHome.php
@@ -208,5 +208,6 @@ class CoreHome extends \Piwik\Plugin
         $translationKeys[] = 'General_Delete';
         $translationKeys[] = 'General_Default';
         $translationKeys[] = 'General_LoadingData';
+        $translationKeys[] = 'General_ErrorRequest';
     }
 }
diff --git a/plugins/CoreHome/javascripts/broadcast.js b/plugins/CoreHome/javascripts/broadcast.js
index 32284d71a1..544cd8d70a 100644
--- a/plugins/CoreHome/javascripts/broadcast.js
+++ b/plugins/CoreHome/javascripts/broadcast.js
@@ -454,14 +454,14 @@ var broadcast = {
     customAjaxHandleError: function (deferred, status) {
         broadcast.lastUrlRequested = null;
 
+        piwikHelper.hideAjaxLoading();
+
         // do not display error message if request was aborted
         if(status == 'abort') {
             return;
         }
+
         $('#loadingError').show();
-        setTimeout( function(){
-            $('#loadingError').fadeOut('slow');
-        }, 2000);
     },
 
     /**
diff --git a/plugins/CoreHome/javascripts/corehome.js b/plugins/CoreHome/javascripts/corehome.js
index 0dbfd48cb1..de7232721b 100755
--- a/plugins/CoreHome/javascripts/corehome.js
+++ b/plugins/CoreHome/javascripts/corehome.js
@@ -153,6 +153,8 @@
 
                 // scroll to report
                 piwikHelper.lazyScrollTo(report, 400);
+            }, function () {
+                loading.hide();
             });
         });
     });
diff --git a/plugins/CoreHome/javascripts/dataTable.js b/plugins/CoreHome/javascripts/dataTable.js
index 6fe1eba805..6b63820fca 100644
--- a/plugins/CoreHome/javascripts/dataTable.js
+++ b/plugins/CoreHome/javascripts/dataTable.js
@@ -207,6 +207,8 @@ $.extend(DataTable.prototype, UIControl.prototype, {
             $('#' + self.workingDivId + ' .loadingPiwik').last().css('display', 'block');
         }
 
+        $('#loadingError').hide();
+
         // when switching to display graphs, reset limit
         if (self.param.viewDataTable && self.param.viewDataTable.indexOf('graph') === 0) {
             delete self.param.filter_offset;
@@ -233,6 +235,10 @@ $.extend(DataTable.prototype, UIControl.prototype, {
                 callbackSuccess(response);
             }
         );
+        ajaxRequest.setErrorCallback(function () {
+            $('#' + self.workingDivId + ' .loadingPiwik').last().css('display', 'none');
+            $('#loadingError').show();
+        });
         ajaxRequest.setFormat('html');
 
         ajaxRequest.send(false);
diff --git a/plugins/Dashboard/javascripts/dashboardWidget.js b/plugins/Dashboard/javascripts/dashboardWidget.js
index 6d8859e8db..64b7c8042d 100755
--- a/plugins/Dashboard/javascripts/dashboardWidget.js
+++ b/plugins/Dashboard/javascripts/dashboardWidget.js
@@ -134,7 +134,11 @@
             }
 
             var params = $.extend(this.widgetParameters, overrideParams || {});
-            widgetsHelper.loadWidgetAjax(this.uniqueId, params, onWidgetLoadedReplaceElementWithContent);
+            widgetsHelper.loadWidgetAjax(this.uniqueId, params, onWidgetLoadedReplaceElementWithContent, function () {
+                $('.widgetContent', currentWidget).removeClass('loading');
+                var errorMessage = _pk_translate('General_ErrorRequest');
+                $('.widgetContent', currentWidget).html('<div class="widgetLoadingError">' + errorMessage + '</div>');
+            });
 
             return this;
         },
diff --git a/plugins/Dashboard/javascripts/widgetMenu.js b/plugins/Dashboard/javascripts/widgetMenu.js
index 65bf58cf74..6d58415cb9 100644
--- a/plugins/Dashboard/javascripts/widgetMenu.js
+++ b/plugins/Dashboard/javascripts/widgetMenu.js
@@ -72,7 +72,7 @@ widgetsHelper.getWidgetNameFromUniqueId = function (uniqueId) {
  * @param {function} onWidgetLoadedCallback   callback to be executed after widget is loaded
  * @return {object}
  */
-widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWidgetLoadedCallback) {
+widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWidgetLoadedCallback, onWidgetErrorCallback) {
     var disableLink = broadcast.getValueFromUrl('disableLink');
     if (disableLink.length) {
         widgetParameters['disableLink'] = disableLink;
@@ -83,6 +83,9 @@ widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWid
     var ajaxRequest = new ajaxHelper();
     ajaxRequest.addParams(widgetParameters, 'get');
     ajaxRequest.setCallback(onWidgetLoadedCallback);
+    if (onWidgetErrorCallback) {
+        ajaxRequest.setErrorCallback(onWidgetErrorCallback);
+    }
     ajaxRequest.setFormat('html');
     ajaxRequest.send(false);
     return ajaxRequest;
diff --git a/plugins/Dashboard/stylesheets/dashboard.less b/plugins/Dashboard/stylesheets/dashboard.less
index f12b8afa6d..fb59494026 100644
--- a/plugins/Dashboard/stylesheets/dashboard.less
+++ b/plugins/Dashboard/stylesheets/dashboard.less
@@ -204,7 +204,7 @@
   display: none;
 }
 
-.widgetLoading {
+.widgetLoading, .widgetLoadingError {
   cursor: wait;
   padding: 10px;
   text-align: center;
-- 
GitLab