From 0e296bc0aa4f99e5b7193ee7ed8a816acd6d4a24 Mon Sep 17 00:00:00 2001 From: sgiehl Date: Mon, 29 Oct 2012 20:08:09 +0000 Subject: fixes #3487, refs #3359 refactoring ajax requests to use a global method to query for module actions git-svn-id: http://dev.piwik.org/svn/trunk@7322 59fd770c-687e-43c8-a1e3-f5a4ff64c105 --- plugins/CoreHome/templates/datatable_rowactions.js | 2 +- plugins/CoreHome/templates/header.tpl | 2 +- plugins/CoreHome/templates/styles.css | 7 +- plugins/Dashboard/templates/dashboardObject.js | 174 +++++++++------------ plugins/Insight/templates/insight.js | 2 +- plugins/Transitions/templates/transitions.js | 78 ++++----- themes/default/common.js | 75 +++++---- 7 files changed, 164 insertions(+), 176 deletions(-) diff --git a/plugins/CoreHome/templates/datatable_rowactions.js b/plugins/CoreHome/templates/datatable_rowactions.js index 151b8ac22b..9acb517da2 100644 --- a/plugins/CoreHome/templates/datatable_rowactions.js +++ b/plugins/CoreHome/templates/datatable_rowactions.js @@ -360,5 +360,5 @@ DataTable_RowActions_RowEvolution.prototype.showRowEvolution = function(apiMetho self.openPopover(apiMethod, metric, label); return true; }); - }, alert, 'html'); + }, 'html'); }; \ No newline at end of file diff --git a/plugins/CoreHome/templates/header.tpl b/plugins/CoreHome/templates/header.tpl index ee60b81581..da4c206e6b 100644 --- a/plugins/CoreHome/templates/header.tpl +++ b/plugins/CoreHome/templates/header.tpl @@ -26,7 +26,7 @@

- +
{* untrusted host warning *} diff --git a/plugins/CoreHome/templates/styles.css b/plugins/CoreHome/templates/styles.css index 3d9b1c0a43..73b364c0f2 100644 --- a/plugins/CoreHome/templates/styles.css +++ b/plugins/CoreHome/templates/styles.css @@ -210,4 +210,9 @@ a.Piwik_Popover_Error_Back { margin: 20px 0 0 0; color: #1D3256; font-size: 14px; -} \ No newline at end of file +} + +#alert.ui-confirm input { + display: block; + margin: 10px auto 5px!important; +} \ No newline at end of file diff --git a/plugins/Dashboard/templates/dashboardObject.js b/plugins/Dashboard/templates/dashboardObject.js index b38c12f813..8f7b06d087 100644 --- a/plugins/Dashboard/templates/dashboardObject.js +++ b/plugins/Dashboard/templates/dashboardObject.js @@ -154,22 +154,17 @@ */ resetLayout: function() { - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=resetLayout', - dataType: 'html', - async: false, - error: piwikHelper.ajaxHandleError, - success: function() { methods.loadDashboard.apply(this, [dashboardId])}, - data: { - token_auth: piwik.token_auth, - idDashboard: dashboardId, - idSite: piwik.idSite - } - }; piwikHelper.showAjaxLoading(); - $.ajax(ajaxRequest); + piwikHelper.ajaxCall( + 'Dashboard', + 'resetLayout', + {idDashboard: dashboardId}, + function() { + methods.loadDashboard.apply(this, [dashboardId]) + }, + 'html', + false + ); }, /** @@ -230,21 +225,13 @@ function fetchLayout(callback) { piwikHelper.abortQueueAjax(); - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=getDashboardLayout', - dataType: 'json', - async: true, - error: piwikHelper.ajaxHandleError, - success: callback, - data: { - idDashboard: dashboardId, - token_auth: piwik.token_auth, - idSite: piwik.idSite - } - }; - piwikHelper.queueAjaxRequest($.ajax(ajaxRequest)); + piwikHelper.ajaxCall( + 'Dashboard', + 'getDashboardLayout', + {idDashboard: dashboardId}, + callback, + 'json' + ); } /** @@ -410,48 +397,45 @@ * Builds the menu for choosing between available dashboards */ function buildMenu() { - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=getAllDashboards', - data: { - token_auth: piwik.token_auth - }, - dataType: 'json', - async: true, - success: function(dashboards) { - var dashboardMenuList = $('#Dashboard > ul'); - dashboardMenuList.empty(); - if (dashboards.length > 1) { - dashboardMenuList.show(); - for (var i=0; i'+ piwikHelper.htmlEntities( dashboards[i].name ) +''); - if(dashboards[i].iddashboard == dashboardId) { - dashboardName = dashboards[i].name; - } + + var success = function(dashboards) { + var dashboardMenuList = $('#Dashboard > ul'); + dashboardMenuList.empty(); + if (dashboards.length > 1) { + dashboardMenuList.show(); + for (var i=0; i'+ piwikHelper.htmlEntities( dashboards[i].name ) +''); + if(dashboards[i].iddashboard == dashboardId) { + dashboardName = dashboards[i].name; } - $('li a', dashboardMenuList).each(function(){$(this).css({width:$(this).width()+30, paddingLeft:0, paddingRight:0});}); - $('#Dashboard_embeddedIndex_'+dashboardId).addClass('sfHover'); - } else { - dashboardMenuList.hide(); } + $('li a', dashboardMenuList).each(function(){$(this).css({width:$(this).width()+30, paddingLeft:0, paddingRight:0});}); + $('#Dashboard_embeddedIndex_'+dashboardId).addClass('sfHover'); + } else { + dashboardMenuList.hide(); + } - $('.dashboardMenuItem').on('click', function() { - if (typeof piwikMenu != 'undefined') { - piwikMenu.activateMenu('Dashboard', 'embeddedIndex'); - } - $('.dashboardMenuItem').removeClass('sfHover'); - if ($(dashboardElement).length) { - $(dashboardElement).dashboard('loadDashboard', $('a', this).attr('dashboardId')); - } else { - broadcast.propagateAjax('module=Dashboard&action=embeddedIndex&idDashboard='+$('a', this).attr('dashboardId')); - } - $(this).addClass('sfHover'); - }); - }, - error: piwikHelper.ajaxHandleError + $('.dashboardMenuItem').on('click', function() { + if (typeof piwikMenu != 'undefined') { + piwikMenu.activateMenu('Dashboard', 'embeddedIndex'); + } + $('.dashboardMenuItem').removeClass('sfHover'); + if ($(dashboardElement).length) { + $(dashboardElement).dashboard('loadDashboard', $('a', this).attr('dashboardId')); + } else { + broadcast.propagateAjax('module=Dashboard&action=embeddedIndex&idDashboard='+$('a', this).attr('dashboardId')); + } + $(this).addClass('sfHover'); + }); }; - piwikHelper.queueAjaxRequest( $.ajax(ajaxRequest) ); + + piwikHelper.ajaxCall( + 'Dashboard', + 'getAllDashboards', + {}, + success, + 'json' + ); } /** @@ -485,27 +469,22 @@ action = 'saveLayout'; } - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action='+action, - dataType: 'html', - async: true, - success: function() { + piwikHelper.ajaxCall( + 'Dashboard', + action, + { + layout: JSON.stringify(dashboardLayout), + name: dashboardName, + idDashboard: dashboardId + }, + function() { if(dashboardChanged) { dashboardChanged = false; buildMenu(); } }, - error: piwikHelper.ajaxHandleError, - data: { - token_auth: piwik.token_auth, - layout: JSON.stringify(dashboardLayout), - name: dashboardName, - idDashboard: dashboardId - } - }; - $.ajax(ajaxRequest); + 'html' + ); } } @@ -516,23 +495,20 @@ if (dashboardId == 1) { return; // dashboard with id 1 should never be deleted, as it is the default } - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=removeDashboard', - dataType: 'html', - async: false, - success: function() { + + piwikHelper.showAjaxLoading(); + piwikHelper.ajaxCall( + 'Dashboard', + 'removeDashboard', + { + idDashboard: dashboardId + }, + function() { methods.loadDashboard.apply(this, [1]); }, - error: piwikHelper.ajaxHandleError, - data: { - idDashboard: dashboardId, - token_auth: piwik.token_auth - } - }; - piwikHelper.showAjaxLoading(); - $.ajax(ajaxRequest); + 'html', + false + ); } /** diff --git a/plugins/Insight/templates/insight.js b/plugins/Insight/templates/insight.js index 30547e5ff6..ff3caa4031 100644 --- a/plugins/Insight/templates/insight.js +++ b/plugins/Insight/templates/insight.js @@ -24,7 +24,7 @@ var Piwik_Insight = (function() { handleFullScreen(); return false; }); - }, false, 'html'); + }, 'html'); } /** Adjust the height of the iframe */ diff --git a/plugins/Transitions/templates/transitions.js b/plugins/Transitions/templates/transitions.js index f37788ec3c..bf2f3bed90 100644 --- a/plugins/Transitions/templates/transitions.js +++ b/plugins/Transitions/templates/transitions.js @@ -1391,41 +1391,49 @@ Piwik_Transitions_Ajax.prototype.callApi = function(method, params, callback) { params.format = 'JSON'; - piwikHelper.ajaxCallApi(method, params, callback, function(errorName) { - var showError = function() { - var errorTitle, errorMessage, errorBack; - if (typeof Piwik_Transitions_Translations[errorName] == 'undefined') { - errorTitle = 'Exception'; - errorMessage = errorName; - errorBack = '<<<'; - } else { - errorTitle = Piwik_Transitions_Translations[errorName]; - errorMessage = Piwik_Transitions_Translations[errorName + 'Details']; - errorBack = Piwik_Transitions_Translations[errorName + 'Back']; - } - - if (typeof params.actionName != 'undefined') { - var url = params.actionName; - url = piwikHelper.addBreakpointsToUrl(url); - errorTitle = errorTitle.replace(/%s/, '' + url + ''); - } - - errorMessage = errorMessage.replace(/%s/g, '
'); - Piwik_Popover.showError(errorTitle, errorMessage, errorBack); - }; - - if (typeof Piwik_Transitions_Translations == 'undefined') { - self.callApi('Transitions.getTranslations', {}, function(response) { - if (typeof response[0] == 'object') { - Piwik_Transitions_Translations = response[0]; - } else { - Piwik_Transitions_Translations = {}; - } - showError(); - }); - } else { - showError(); - } + piwikHelper.ajaxCallApi(method, params, function(result) { + if (typeof result.result != 'undefined' && result.result == 'error') + { + var errorName = result.message; + var showError = function() { + var errorTitle, errorMessage, errorBack; + if (typeof Piwik_Transitions_Translations[errorName] == 'undefined') { + errorTitle = 'Exception'; + errorMessage = errorName; + errorBack = '<<<'; + } else { + errorTitle = Piwik_Transitions_Translations[errorName]; + errorMessage = Piwik_Transitions_Translations[errorName + 'Details']; + errorBack = Piwik_Transitions_Translations[errorName + 'Back']; + } + + if (typeof params.actionName != 'undefined') { + var url = params.actionName; + url = piwikHelper.addBreakpointsToUrl(url); + errorTitle = errorTitle.replace(/%s/, '' + url + ''); + } + + errorMessage = errorMessage.replace(/%s/g, '
'); + Piwik_Popover.showError(errorTitle, errorMessage, errorBack); + }; + + if (typeof Piwik_Transitions_Translations == 'undefined') { + self.callApi('Transitions.getTranslations', {}, function(response) { + if (typeof response[0] == 'object') { + Piwik_Transitions_Translations = response[0]; + } else { + Piwik_Transitions_Translations = {}; + } + showError(); + }); + } else { + showError(); + } + } + else + { + callback(result); + } }); }; diff --git a/themes/default/common.js b/themes/default/common.js index 0575ea133b..f9c7e0543f 100644 --- a/themes/default/common.js +++ b/themes/default/common.js @@ -110,15 +110,15 @@ var piwikHelper = { * * @param method API method name, i.e. Plugin.Method * @param params parameters for the request - * @param callback regular callback - * @param exceptionCallback callback for when the API returns an error, i.e. PHP Exception - * + * @param callback regular callback + * @param async defines if the request should be asnyc (default: true) + * * @return {void} */ - ajaxCallApi: function(method, params, callback, exceptionCallback) + ajaxCallApi: function(method, params, callback, async) { params.method = method; - piwikHelper.ajaxCall('API', false, params, callback, exceptionCallback); + piwikHelper.ajaxCall('API', false, params, callback, async); }, /** @@ -128,50 +128,49 @@ var piwikHelper = { * @param action method name * @param params parameters for the request * @param callback regular callback - * @param exceptionCallback callback for when the API returns an error, i.e. PHP Exception * @param format response format, default json + * @param async defines if the request should be asnyc (default: true) * * @return {void} */ - ajaxCall: function(module, action, params, callback, exceptionCallback, format) { - params.module = module; - if (action) { - params.action = action; + ajaxCall: function(module, action, params, callback, format, async) { + + var urlParams = {}; + urlParams.module = module; + if (action) + { + urlParams.action = action; } - params.date = piwik.currentDateString; - params.idSite = piwik.idSite; - params.period = piwik.period; - params.token_auth = piwik.token_auth; - if (params.period == 'range') + urlParams.date = params.date ? params.date : piwik.currentDateString; + urlParams.idSite = params.idSite ? params.idSite : piwik.idSite; + urlParams.period = params.period ? params.period : piwik.period; + if (urlParams.period == 'range') { - params.date = piwik.startDateString + ',' + params.date; + urlParams.date = piwik.startDateString + ',' + urlParams.date; } - - var segment = broadcast.getValueFromHash('segment', window.location.href); + + // send token_auth always as post parameter + params.token_auth = piwik.token_auth; + + var segment = params.segment ? params.segment : broadcast.getValueFromHash('segment', window.location.href); if (segment) { - params.segment = segment; + urlParams.segment = segment; } - - piwikHelper.queueAjaxRequest($.get('index.php', params, function(result) - { - if (typeof result.result != 'undefined' && result.result == 'error') - { - if (typeof exceptionCallback == 'function') - { - exceptionCallback(result.message); - } - else - { - alert(result.message); - } - } - else - { - callback(result); - } - }, format || 'json')); + + var ajaxRequest = + { + type: 'POST', + async: async !== false, + url: 'index.php?' + $.param(urlParams), + dataType: format || 'json', + error: piwikHelper.ajaxHandleError, + success: callback, + data: params + }; + + piwikHelper.queueAjaxRequest($.ajax(ajaxRequest)); }, /** -- cgit v1.2.3