diff --git a/plugins/CoreAdminHome/CoreAdminHome.php b/plugins/CoreAdminHome/CoreAdminHome.php index d2b000b447f6954cddae754797f661dd2ed4f6c9..f6dcc4ddeb9683f7d18c5859f352fcea6588fa43 100644 --- a/plugins/CoreAdminHome/CoreAdminHome.php +++ b/plugins/CoreAdminHome/CoreAdminHome.php @@ -84,6 +84,7 @@ class Piwik_CoreAdminHome extends Piwik_Plugin $jsFiles[] = "libs/jquery/jquery-ui.js"; $jsFiles[] = "libs/javascript/sprintf.js"; $jsFiles[] = "themes/default/common.js"; + $jsFiles[] = "themes/default/ajaxHelper.js"; $jsFiles[] = "libs/jquery/jquery.history.js"; $jsFiles[] = "plugins/CoreHome/templates/broadcast.js"; $jsFiles[] = "plugins/CoreAdminHome/templates/generalSettings.js"; diff --git a/plugins/CoreHome/CoreHome.php b/plugins/CoreHome/CoreHome.php index d6525bc7db6a278533c78339dc61fd49424078d0..318404654c01cdda6e096fbb1cfb8fa1b0bca0fd 100644 --- a/plugins/CoreHome/CoreHome.php +++ b/plugins/CoreHome/CoreHome.php @@ -67,6 +67,7 @@ class Piwik_CoreHome extends Piwik_Plugin $jsFiles[] = "libs/swfobject/swfobject.js"; $jsFiles[] = "libs/javascript/sprintf.js"; $jsFiles[] = "themes/default/common.js"; + $jsFiles[] = "themes/default/ajaxHelper.js"; $jsFiles[] = "plugins/CoreHome/templates/tooltip.js"; $jsFiles[] = "plugins/CoreHome/templates/datatable.js"; $jsFiles[] = "plugins/CoreHome/templates/datatable_rowactions.js"; diff --git a/plugins/CoreHome/templates/broadcast.js b/plugins/CoreHome/templates/broadcast.js index 6e153702e4719da76b426109fa1156336ad09b68..0cd5c7548971d1bf75da0a0516b4154dae821275 100644 --- a/plugins/CoreHome/templates/broadcast.js +++ b/plugins/CoreHome/templates/broadcast.js @@ -163,7 +163,7 @@ var broadcast = { broadcast.init(); // abort all existing ajax requests - piwikHelper.abortQueueAjax(); + globalAjaxQueue.abort(); // available in global scope var currentHashStr = broadcast.getHash(); @@ -230,7 +230,7 @@ var broadcast = { propagateNewPage: function (str, showAjaxLoading) { // abort all existing ajax requests - piwikHelper.abortQueueAjax(); + globalAjaxQueue.abort(); if (typeof showAjaxLoading === 'undefined' || showAjaxLoading) { @@ -391,7 +391,7 @@ var broadcast = { success: sectionLoaded, // Callback when the request succeeds data: new Object }; - piwikHelper.queueAjaxRequest( $.ajax(ajaxRequest) ); + globalAjaxQueue.push( $.ajax(ajaxRequest) ); return false; }, diff --git a/plugins/CoreHome/templates/datatable.js b/plugins/CoreHome/templates/datatable.js index c91cd5d52d626c5f37a4144944cddeb49d900dea..03c1684e22fda717be13e2e47273389ce9c43ca1 100644 --- a/plugins/CoreHome/templates/datatable.js +++ b/plugins/CoreHome/templates/datatable.js @@ -158,18 +158,17 @@ dataTable.prototype = params[key] = self.param[key]; } - piwikHelper.ajaxCall( - self.param.module, - self.param.action, - params, + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(params, 'get'); + ajaxRequest.setCallback( function (response) { container.trigger('piwikDestroyPlot'); container.off('piwikDestroyPlot'); callbackSuccess(response); - }, - 'html', - true + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); }, // Function called when the AJAX request is successful diff --git a/plugins/CoreHome/templates/datatable_rowactions.js b/plugins/CoreHome/templates/datatable_rowactions.js index c0105b196647a8f2da7e982c38029612535c413a..bd4b962e59f90c2dbbb3750a0235b3bd8adf069a 100644 --- a/plugins/CoreHome/templates/datatable_rowactions.js +++ b/plugins/CoreHome/templates/datatable_rowactions.js @@ -335,7 +335,7 @@ DataTable_RowActions_RowEvolution.prototype.showRowEvolution = function(apiMetho requestParams.column = multiRowEvolutionParam; } - piwikHelper.ajaxCall('CoreHome', action, requestParams, function(html) { + var callback = function(html) { Piwik_Popover.setContent(html); // use the popover title returned from the server @@ -371,5 +371,14 @@ DataTable_RowActions_RowEvolution.prototype.showRowEvolution = function(apiMetho self.openPopover(apiMethod, metric, label); return true; }); - }, 'html'); + }; + + requestParams.module = 'CoreHome'; + requestParams.action = action; + + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(requestParams, 'get'); + ajaxRequest.setCallback(callback); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); }; \ No newline at end of file diff --git a/plugins/CoreHome/templates/popover.js b/plugins/CoreHome/templates/popover.js index eb7f690f2e2f0a4db44b9fb307a28160c40e50b1..678e0088e2f472fd08ff1c1f59fe135ffcc0f583 100644 --- a/plugins/CoreHome/templates/popover.js +++ b/plugins/CoreHome/templates/popover.js @@ -36,7 +36,7 @@ var Piwik_Popover = (function() { container.find('div.jqplot-target').trigger('piwikDestroyPlot'); container[0].innerHTML = ''; // IE8 fix container.dialog('destroy').remove(); - piwikHelper.abortQueueAjax(); + globalAjaxQueue.abort(); $('.ui-widget-overlay').off('click.popover'); isOpen = false; broadcast.propagateNewPopoverParameter(false); diff --git a/plugins/DBStats/templates/index.tpl b/plugins/DBStats/templates/index.tpl index 10f10e686490376e28ed1caf4934df1eb949fa0f..18c559d1a1b1fc041e9391911cf3827ed1a9f810 100755 --- a/plugins/DBStats/templates/index.tpl +++ b/plugins/DBStats/templates/index.tpl @@ -125,17 +125,20 @@ var action = $(this).attr('action'); // build & execute AJAX request - piwikHelper.ajaxCall( - 'DBStats', - action, - {viewDataTable: 'table'}, - function(data) { - $('.loadingPiwik', self).remove(); - $(self).html(data); - }, - 'html', - true + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'DBStats', + action: action, + viewDataTable: 'table' + }, 'get'); + ajaxRequest.setCallback( + function (data) { + $('.loadingPiwik', self).remove(); + $(self).html(data); + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); }); }); })( jQuery ); diff --git a/plugins/Dashboard/templates/dashboard.js b/plugins/Dashboard/templates/dashboard.js index 20c6542112a954a61a35c82ea207414277c8ddbe..cde58c3b2f925d0bda0cd028372ea42e4c8421ae 100644 --- a/plugins/Dashboard/templates/dashboard.js +++ b/plugins/Dashboard/templates/dashboard.js @@ -86,25 +86,23 @@ function createDashboard() { piwikHelper.modalConfirm('#createDashboardConfirm', {yes: function(){ var dashboardName = $('#createDashboardName').attr('value'); var type = ($('#dashboard_type_empty:checked').length > 0) ? 'empty' : 'default'; - piwikHelper.showAjaxLoading(); - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=createNewDashboard', - dataType: 'json', - async: true, - error: piwikHelper.ajaxHandleError, - success: function(id) { + + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'createNewDashboard' + }, 'get'); + ajaxRequest.addParams({ + name: encodeURIComponent(dashboardName), + type: type + }, 'post'); + ajaxRequest.setCallback( + function (id) { $('#dashboardWidgetsArea').dashboard('loadDashboard', id); - }, - data: { - token_auth: piwik.token_auth, - idSite: piwik.idSite, - name: encodeURIComponent(dashboardName), - type: type } - }; - $.ajax(ajaxRequest); + ); + ajaxRequest.send(true); }}); } @@ -146,29 +144,27 @@ function setAsDefaultWidgets() { function copyDashboardToUser() { $('#copyDashboardName').attr('value', $('#dashboardWidgetsArea').dashboard('getDashboardName')); piwikHelper.modalConfirm('#copyDashboardToUserConfirm', { - yes: function(){ - var copyDashboardName = $('#copyDashboardName').attr('value'); - var copyDashboardUser = $('#copyDashboardUser').attr('value'); - var ajaxRequest = - { - type: 'POST', - url: 'index.php?module=Dashboard&action=copyDashboardToUser', - dataType: 'json', - async: true, - error: piwikHelper.ajaxHandleError, - success: function(id) { - $('#alert h2').text(_pk_translate('Dashboard_DashboardCopied_js')); - piwikHelper.modalConfirm('#alert', {}); - }, - data: { - token_auth: piwik.token_auth, - idSite: piwik.idSite, - name: encodeURIComponent(copyDashboardName), - dashboardId: $('#dashboardWidgetsArea').dashboard('getDashboardId'), - user: encodeURIComponent(copyDashboardUser) - } - }; - $.ajax(ajaxRequest); - } + yes: function() { + var copyDashboardName = $('#copyDashboardName').attr('value'); + var copyDashboardUser = $('#copyDashboardUser').attr('value'); + + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'copyDashboardToUser' + }, 'get'); + ajaxRequest.addParams({ + name: encodeURIComponent(copyDashboardName), + dashboardId: $('#dashboardWidgetsArea').dashboard('getDashboardId'), + user: encodeURIComponent(copyDashboardUser) + }, 'post'); + ajaxRequest.setCallback( + function (id) { + $('#alert h2').text(_pk_translate('Dashboard_DashboardCopied_js')); + piwikHelper.modalConfirm('#alert', {}); + } + ); + ajaxRequest.send(true); + } }); } diff --git a/plugins/Dashboard/templates/dashboardObject.js b/plugins/Dashboard/templates/dashboardObject.js index 79647758967f402f34cb953520f63b193a13e733..37004269a376bcb4c1dcd1b7501e6b736c915eea 100644 --- a/plugins/Dashboard/templates/dashboardObject.js +++ b/plugins/Dashboard/templates/dashboardObject.js @@ -163,17 +163,20 @@ */ resetLayout: function() { - piwikHelper.showAjaxLoading(); - piwikHelper.ajaxCall( - 'Dashboard', - 'resetLayout', - {idDashboard: dashboardId}, - function() { + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'resetLayout', + idDashboard: dashboardId + }, 'get'); + ajaxRequest.setCallback( + function () { methods.loadDashboard.apply(this, [dashboardId]) - }, - 'html', - false + } ); + ajaxRequest.setLoadingElement(); + ajaxRequest.setFormat('html'); + ajaxRequest.send(true); }, /** @@ -233,14 +236,15 @@ */ function fetchLayout(callback) { - piwikHelper.abortQueueAjax(); - piwikHelper.ajaxCall( - 'Dashboard', - 'getDashboardLayout', - {idDashboard: dashboardId}, - callback, - 'json' - ); + globalAjaxQueue.abort(); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'getDashboardLayout', + idDashboard: dashboardId + }, 'get'); + ajaxRequest.setCallback(callback); + ajaxRequest.send(false); } /** @@ -367,7 +371,8 @@ /** * Make all widgets on the dashboard sortable */ - function makeWidgetsSortable() { + function makeWidgetsSortable() + { function onStart(event, ui) { if (!jQuery.support.noCloneEvent) { $('object', this).hide(); @@ -438,13 +443,13 @@ }); }; - piwikHelper.ajaxCall( - 'Dashboard', - 'getAllDashboards', - {}, - success, - 'json' - ); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'getAllDashboards' + }, 'get'); + ajaxRequest.setCallback(success); + ajaxRequest.send(false); } /** @@ -478,22 +483,26 @@ action = 'saveLayout'; } - piwikHelper.ajaxCall( - 'Dashboard', - action, - { - layout: JSON.stringify(dashboardLayout), - name: dashboardName, - idDashboard: dashboardId - }, - function() { + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: action, + idDashboard: dashboardId + }, 'get'); + ajaxRequest.addParams({ + layout: JSON.stringify(dashboardLayout), + name: dashboardName + }, 'post'); + ajaxRequest.setCallback( + function () { if(dashboardChanged) { dashboardChanged = false; buildMenu(); } - }, - 'html' + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); } } @@ -505,19 +514,20 @@ return; // dashboard with id 1 should never be deleted, as it is the default } - piwikHelper.showAjaxLoading(); - piwikHelper.ajaxCall( - 'Dashboard', - 'removeDashboard', - { - idDashboard: dashboardId - }, - function() { + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'removeDashboard', + idDashboard: dashboardId + }, 'get'); + ajaxRequest.setCallback( + function () { methods.loadDashboard.apply(this, [1]); - }, - 'html', - false + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(true); } /** diff --git a/plugins/Dashboard/templates/dashboardWidget.js b/plugins/Dashboard/templates/dashboardWidget.js index 6e1a1e82576eb010de58acf51608c1167aee9940..b91ff269562a7483b8364b10fd2d0a61dc93fedb 100755 --- a/plugins/Dashboard/templates/dashboardWidget.js +++ b/plugins/Dashboard/templates/dashboardWidget.js @@ -150,7 +150,7 @@ $('.widgetContent', currentWidget).addClass('loading'); } - piwikHelper.queueAjaxRequest( $.ajax(widgetsHelper.getLoadWidgetAjaxRequest(this.uniqueId, this.widgetParameters, onWidgetLoadedReplaceElementWithContent)) ); + widgetsHelper.loadWidgetAjax(this.uniqueId, this.widgetParameters, onWidgetLoadedReplaceElementWithContent); return this; }, diff --git a/plugins/Dashboard/templates/widgetMenu.js b/plugins/Dashboard/templates/widgetMenu.js index fb9615d59a8ba4919309e15ae8242ae4f71750a6..008c58b05c741bd8fa9f5660c582f0c91fbe4d0e 100644 --- a/plugins/Dashboard/templates/widgetMenu.js +++ b/plugins/Dashboard/templates/widgetMenu.js @@ -17,16 +17,17 @@ function widgetsHelper() widgetsHelper.getAvailableWidgets = function () { if(!widgetsHelper.availableWidgets) { - piwikHelper.ajaxCall( - 'Dashboard', - 'getAvailableWidgets', - {}, - function(data) { + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Dashboard', + action: 'getAvailableWidgets' + }, 'get'); + ajaxRequest.setCallback( + function (data) { widgetsHelper.availableWidgets = data; - }, - 'json', - false + } ); + ajaxRequest.send(true); } return widgetsHelper.availableWidgets; @@ -74,10 +75,10 @@ widgetsHelper.getWidgetNameFromUniqueId = function (uniqueId) * @param {object} widgetParameters parameters to be used for loading the widget * @param {function} onWidgetLoadedCallback callback to be executed after widget is loaded * @return {object} + * @deprecated since 1.9.3 - will be removed in next major release. use widgetsHelper.loadWidgetAjax */ widgetsHelper.getLoadWidgetAjaxRequest = function (widgetUniqueId, widgetParameters, onWidgetLoadedCallback) { - var token_auth = broadcast.getValueFromUrl('token_auth'); if(token_auth.length && token_auth != 'anonymous') { @@ -101,6 +102,38 @@ widgetsHelper.getLoadWidgetAjaxRequest = function (widgetUniqueId, widgetParamet }; }; +/** + * Sends and ajax request to query for the widgets html + * + * @param {string} widgetUniqueId unique id of the widget + * @param {object} widgetParameters parameters to be used for loading the widget + * @param {function} onWidgetLoadedCallback callback to be executed after widget is loaded + * @return {object} + */ +widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWidgetLoadedCallback) +{ + + var token_auth = broadcast.getValueFromUrl('token_auth'); + if(token_auth.length && token_auth != 'anonymous') + { + widgetParameters['token_auth'] = token_auth; + } + var disableLink = broadcast.getValueFromUrl('disableLink'); + if(disableLink.length) + { + widgetParameters['disableLink'] = disableLink; + } + + widgetParameters['widget'] = 1; + + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(widgetParameters, 'get'); + ajaxRequest.setCallback(onWidgetLoadedCallback); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); + return ajaxRequest; +}; + /** * Returns the base html use for displaying a widget * @@ -338,8 +371,7 @@ widgetsHelper.getEmptyWidgetHtml = function (uniqueId, widgetName) widgetAjaxRequest.abort(); } - var ajaxRequest = widgetsHelper.getLoadWidgetAjaxRequest(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); - widgetAjaxRequest = $.ajax(ajaxRequest); + widgetAjaxRequest = widgetsHelper.loadWidgetAjax(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); }; /** diff --git a/plugins/Goals/templates/GoalForm.js b/plugins/Goals/templates/GoalForm.js index df0124507d2344a8bbe7aabfe51c2f9c9c5ec9e4..78f447756680f8e461a93426aeed7ca420b585a4 100644 --- a/plugins/Goals/templates/GoalForm.js +++ b/plugins/Goals/templates/GoalForm.js @@ -103,27 +103,27 @@ function bindGoalForm() function ajaxDeleteGoal(idGoal) { piwikHelper.lazyScrollTo(".entityContainer", 400); - piwikHelper.showAjaxLoading('goalAjaxLoading'); var parameters = {}; parameters.format = 'json'; parameters.idGoal = idGoal; + parameters.module = 'API'; + parameters.method = 'Goals.deleteGoal'; - piwikHelper.ajaxCallApi( - 'Goals.deleteGoal', - parameters, + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(parameters, 'get'); + ajaxRequest.setLoadingElement('#goalAjaxLoading'); + ajaxRequest.setCallback( function (response) { piwikHelper.ajaxHandleResponse(response, 'goalAjaxLoading', parameters); - }, - 'json', - false + } ); + ajaxRequest.send(true); } function ajaxAddGoal() { piwikHelper.lazyScrollTo(".entityContainer", 400); - piwikHelper.showAjaxLoading('goalAjaxLoading'); var parameters = {}; parameters.name = encodeURIComponent( $('#goal_name').val() ); @@ -144,16 +144,18 @@ function ajaxAddGoal() parameters.idGoal = $('input[name=goalIdUpdate]').val(); parameters.format = 'json'; + parameters.module = 'API'; + parameters.method = $('input[name=methodGoalAPI]').val(); - piwikHelper.ajaxCallApi( - $('input[name=methodGoalAPI]').val(), - parameters, + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(parameters, 'get'); + ajaxRequest.setLoadingElement('#goalAjaxLoading'); + ajaxRequest.setCallback( function (response) { piwikHelper.ajaxHandleResponse(response, 'goalAjaxLoading', parameters); - }, - 'json', - false + } ); + ajaxRequest.send(true); } function bindListGoalEdit() diff --git a/plugins/Goals/templates/table_by_dimension.tpl b/plugins/Goals/templates/table_by_dimension.tpl index 941eb8cf511b2528a7773a87bdc4b66b7d14899f..6add478be00e91468b7a5592d7cae706fff59d7b 100644 --- a/plugins/Goals/templates/table_by_dimension.tpl +++ b/plugins/Goals/templates/table_by_dimension.tpl @@ -100,8 +100,7 @@ $(document).ready( function() { $('#tableGoalsByDimension').hide(); $('#tableGoalsLoading').show(); - ajaxRequest = widgetsHelper.getLoadWidgetAjaxRequest(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); - $.ajax(ajaxRequest); + widgetsHelper.loadWidgetAjax(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); }); $('.goalDimension').first().click(); }); diff --git a/plugins/Live/templates/scripts/live.js b/plugins/Live/templates/scripts/live.js index a274031c67433410a4d669300fffbd2c5fc51e8a..288f174ce5a36b8729d6b5bd81d188d44f84221a 100644 --- a/plugins/Live/templates/scripts/live.js +++ b/plugins/Live/templates/scripts/live.js @@ -43,7 +43,7 @@ updated = false; // fetch data - piwikHelper.queueAjaxRequest( $.get(settings.dataUrl, {}, function(r) { + globalAjaxQueue.push( $.get(settings.dataUrl, {}, function(r) { parseResponse(r); // add default interval to last interval if not updated or reset to default if so diff --git a/plugins/Overlay/templates/index.js b/plugins/Overlay/templates/index.js index 45ec759d427c4a2266e8721df620f1830cb5771d..3cdc985deb26894f0a4da0844087d3e6026182e2 100644 --- a/plugins/Overlay/templates/index.js +++ b/plugins/Overlay/templates/index.js @@ -31,39 +31,47 @@ var Piwik_Overlay = (function() { iframeCurrentPage = currentUrl; iframeDomain = currentUrl.match(/http(s)?:\/\/(www\.)?([^\/]*)/i)[3]; - piwikHelper.abortQueueAjax(); - piwikHelper.ajaxCall('Overlay', 'renderSidebar', { - currentUrl: currentUrl - }, function(response) { - hideLoading(); - - var $response = $(response); - - var $responseLocation = $response.find('.Overlay_Location'); - var $url = $responseLocation.find('span'); - iframeCurrentPageNormalized = $url.data('normalizedUrl'); - iframeCurrentActionLabel = $url.data('label'); - $url.html(piwikHelper.addBreakpointsToUrl($url.text())); - $location.html($responseLocation.html()).show(); - $responseLocation.remove(); - - $location.find('span').hover(function() { - if (iframeDomain) { - // use addBreakpointsToUrl because it also encoded html entities - Piwik_Tooltip.show('<b>' + Piwik_Overlay_Translations.domain + ':</b> ' + - piwikHelper.addBreakpointsToUrl(iframeDomain), 'Overlay_Tooltip'); - } - }, function() { - Piwik_Tooltip.hide(); - }); - - $sidebar.empty().append($response).show(); - - if ($sidebar.find('.Overlay_NoData').size() == 0) { - $rowEvolutionLink.show(); - $transitionsLink.show() - } - }, 'html'); + globalAjaxQueue.abort(); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Overlay', + action: 'renderSidebar', + currentUrl: currentUrl + }, 'get'); + ajaxRequest.setCallback( + function (response) { + hideLoading(); + + var $response = $(response); + + var $responseLocation = $response.find('.Overlay_Location'); + var $url = $responseLocation.find('span'); + iframeCurrentPageNormalized = $url.data('normalizedUrl'); + iframeCurrentActionLabel = $url.data('label'); + $url.html(piwikHelper.addBreakpointsToUrl($url.text())); + $location.html($responseLocation.html()).show(); + $responseLocation.remove(); + + $location.find('span').hover(function () { + if (iframeDomain) { + // use addBreakpointsToUrl because it also encoded html entities + Piwik_Tooltip.show('<b>' + Piwik_Overlay_Translations.domain + ':</b> ' + + piwikHelper.addBreakpointsToUrl(iframeDomain), 'Overlay_Tooltip'); + } + }, function () { + Piwik_Tooltip.hide(); + }); + + $sidebar.empty().append($response).show(); + + if ($sidebar.find('.Overlay_NoData').size() == 0) { + $rowEvolutionLink.show(); + $transitionsLink.show() + } + } + ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); } /** Adjust the dimensions of the iframe */ diff --git a/plugins/PrivacyManager/templates/privacySettings.js b/plugins/PrivacyManager/templates/privacySettings.js index 3e4afb299c348e266147e9ed7b3469ef7dcd1ff4..509ef8594158cf099a631d6b7a7fb616aff2b95a 100644 --- a/plugins/PrivacyManager/templates/privacySettings.js +++ b/plugins/PrivacyManager/templates/privacySettings.js @@ -39,8 +39,7 @@ $(document).ready(function() { } $('#deleteDataEstimate').hide(); - $('#deleteDataEstimateSect .loadingPiwik').show(); - + var data = $('#formDeleteSettings').serializeArray(); var formData = {}; for(var i=0; i<data.length; i++) { @@ -50,21 +49,24 @@ $(document).ready(function() { formData['forceEstimate'] = 1; } - currentRequest = piwikHelper.ajaxCall( - 'PrivacyManager', - 'getDatabaseSize', - formData, + currentRequest = new ajaxHelper(); + currentRequest.setLoadingElement('#deleteDataEstimateSect .loadingPiwik'); + currentRequest.addParams({ + module: 'PrivacyManager', + action: 'getDatabaseSize' + }, 'get'); + currentRequest.addParams(formData, 'post'); + currentRequest.setCallback( function (data) { currentRequest = undefined; - $('#deleteDataEstimateSect .loadingPiwik').hide(); $('#deleteDataEstimate').html(data).show(); // lock size of db size estimate $('#deleteDataEstimateSect').height($('#deleteDataEstimateSect').height()); - }, - 'html', - true + } ); + currentRequest.setFormat('html'); + currentRequest.send(false); } // make sure certain sections only display if their corresponding features are enabled @@ -164,16 +166,16 @@ $(document).ready(function() { piwikHelper.modalConfirm('#confirmPurgeNow', { yes: function() { $(link).hide(); - $('#deleteSchedulingSettings .loadingPiwik').show(); - + // execute a data purge - piwikHelper.ajaxCall( - 'PrivacyManager', - 'executeDataPurge', - {}, + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement('#deleteSchedulingSettings .loadingPiwik'); + ajaxRequest.addParams({ + module: 'PrivacyManager', + action: 'executeDataPurge' + }, 'get'); + ajaxRequest.setCallback( function () { - $('#deleteSchedulingSettings .loadingPiwik').hide(); - // force reload $('#deleteDataEstimate').html(''); reloadDbStats(); @@ -186,10 +188,10 @@ $(document).ready(function() { $(link).show(); }); }, 2000); - }, - 'html', - true + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); } }); }); diff --git a/plugins/SEO/templates/rank.js b/plugins/SEO/templates/rank.js index 8e8749aaf734ff04e2ba14dc93d05dd180e4c873..d7a3802fc9f0c8f6c5843c265f1fb2abb9d23b5e 100644 --- a/plugins/SEO/templates/rank.js +++ b/plugins/SEO/templates/rank.js @@ -5,27 +5,27 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ -$(document).ready(function() { - function getRank() - { - piwikHelper.showAjaxLoading('ajaxLoadingSEO'); - piwikHelper.ajaxCall( - 'SEO', - 'getRank', - {url:encodeURIComponent($('#seoUrl').val())}, +$(document).ready(function () { + function getRank() { + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement('#ajaxLoadingSEO'); + ajaxRequest.addParams({ + module: 'SEO', + action: 'getRank', + url: encodeURIComponent($('#seoUrl').val()) + }, 'get'); + ajaxRequest.setCallback( function (response) { - piwikHelper.hideAjaxLoading('ajaxLoadingSEO'); $('#SeoRanks').html(response); - }, - 'html', - true + } ); - } - - // click on Rank button - $('#rankbutton').on('click', function() { - getRank(); - return false ; - }); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); + } + // click on Rank button + $('#rankbutton').on('click', function () { + getRank(); + return false; + }); }); diff --git a/plugins/Transitions/templates/transitions.js b/plugins/Transitions/templates/transitions.js index dfc4d32114ffbbd8add12f6b44fd61357589aff7..c3a186249ab2ebba06eeea84978f2e3666d59994 100644 --- a/plugins/Transitions/templates/transitions.js +++ b/plugins/Transitions/templates/transitions.js @@ -1389,64 +1389,70 @@ Piwik_Transitions_Ajax.prototype.loadTotalNbPageviews = function(callback) { }; Piwik_Transitions_Ajax.prototype.callTransitionsController = function(action, callback) { - piwikHelper.queueAjaxRequest($.post('index.php', { - module: 'Transitions', - action: action, - date: piwik.currentDateString, - idSite: piwik.idSite, - period: piwik.period - }, callback)); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams({ + module: 'Transitions', + action: action + }, 'get'); + ajaxRequest.setCallback(callback); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); }; Piwik_Transitions_Ajax.prototype.callApi = function(method, params, callback) { var self = this; params.format = 'JSON'; - - 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/, '<span>' + url + '</span>'); - } - - errorMessage = errorMessage.replace(/%s/g, '<br />'); - 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]; + params.module = 'API'; + params.method = method; + + var ajaxRequest = new ajaxHelper(); + ajaxRequest.addParams(params, 'get'); + ajaxRequest.setCallback( + 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 { - Piwik_Transitions_Translations = {}; + 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/, '<span>' + url + '</span>'); + } + + errorMessage = errorMessage.replace(/%s/g, '<br />'); + 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 { - showError(); + } + } + else { + callback(result); } } - else - { - callback(result); - } - }); + ); + ajaxRequest.send(false); }; diff --git a/plugins/UserCountry/templates/admin.js b/plugins/UserCountry/templates/admin.js index efed733f06d9768d373eeed1e2d218981185df81..8d10ca6e5cff691764b78c5a4755c090587dac99 100755 --- a/plugins/UserCountry/templates/admin.js +++ b/plugins/UserCountry/templates/admin.js @@ -13,14 +13,16 @@ $(document).ready(function() { var parent = $(this).parent(), loading = $('.loadingPiwik', parent), ajaxSuccess = $('.ajaxSuccess', parent); - loading.show(); - piwikHelper.ajaxCall( - 'UserCountry', - 'setCurrentLocationProvider', - {id: $(this).val()}, - function() { - loading.hide(); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement(loading); + ajaxRequest.addParams({ + module: 'UserCountry', + action: 'setCurrentLocationProvider', + id: $(this).val() + }, 'get'); + ajaxRequest.setCallback( + function () { ajaxSuccess.fadeIn(1000, function() { setTimeout(function() { ajaxSuccess.fadeOut(1000); @@ -28,6 +30,7 @@ $(document).ready(function() { }); } ); + ajaxRequest.send(false); }); // handle 'refresh location' link click @@ -39,18 +42,21 @@ $(document).ready(function() { location = $('.location', cell); location.css('visibility', 'hidden'); - loading.show(); - piwikHelper.ajaxCall( - 'UserCountry', - 'getLocationUsingProvider', - {id: $(this).attr('data-impl-id')}, - function(response) { - loading.hide(); + var ajaxRequest = new ajaxHelper(); + ajaxRequest.setLoadingElement(loading); + ajaxRequest.addParams({ + module: 'UserCountry', + action: 'getLocationUsingProvider', + id: $(this).attr('data-impl-id') + }, 'get'); + ajaxRequest.setCallback( + function (response) { location.html('<strong><em>' + response + '</em></strong>').css('visibility', 'visible'); - }, - 'html' + } ); + ajaxRequest.setFormat('html'); + ajaxRequest.send(false); return false; }); diff --git a/themes/default/ajaxHelper.js b/themes/default/ajaxHelper.js new file mode 100644 index 0000000000000000000000000000000000000000..5356787415d2bc02e200d7b2a746067a68e47385 --- /dev/null +++ b/themes/default/ajaxHelper.js @@ -0,0 +1,284 @@ +/*! + * Piwik - Web Analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +/** + * global ajax queue + * + * @type {Array} array holding XhrRequests with automatic cleanup + */ +var globalAjaxQueue = new Array(); + +/** + * Extend Array.push with automatic cleanup for finished requests + * + * @return {Object} + */ +globalAjaxQueue.push = function () { + // cleanup ajax queue + for (var i = this.length; i--;) { + if (!this[i] || this[i].readyState == 4) { + this.splice(i, 1); + } + } + // call original array push + return Array.prototype.push.apply(this, arguments); +}; + +/** + * Extend with abort function to abort all queued requests + * + * @return {void} + */ +globalAjaxQueue.abort = function () { + // abort all queued requests + for (var i = this.length; i--;) { + this[i] && this[i].abort && this[i].abort(); // abort if possible + } + // remove all elements from array + this.splice(0, this.length); +}; + +/** + * Global ajax helper to handle requests within piwik + * + * @type {Object} + */ +function ajaxHelper() { + + /** + * Format of response + * @type {String} + */ + this.format = 'json'; + + /** + * Should ajax request be synchronous + * @type {Boolean} + */ + this.async = true; + + /** + * Callback function to be executed on success + */ + this.callback = function () {}; + + /** + * Callback function to be executed on error + */ + this.errorCallback = piwikHelper.ajaxHandleError; + + /** + * Params to be passed as GET params + * @type {Object} + * @see ajaxHelper._mixinDefaultGetParams + */ + this.getParams = {}; + + /** + * Params to be passed as GET params + * @type {Object} + * @see ajaxHelper._mixinDefaultPostParams + */ + this.postParams = {}; + + /** + * Element to be displayed while loading + * @type {String} + */ + this.loadingElement = null; + + /** + * Handle for current request + * @type {XMLHttpRequest} + */ + this.requestHandle = null; + + /** + * Adds params to the request. + * If params are given more then once, the latest given value is used for the request + * + * @param {object} params + * @param {string} type type of given parameters (POST or GET) + * @return {void} + */ + this.addParams = function (params, type) { + switch (type.toLowerCase()) { + + case 'get': + for (var key in params) { + this.getParams[key] = params[key]; + } + break; + case 'post': + for (var key in params) { + this.postParams[key] = params[key]; + } + break; + } + }; + + /** + * Sets the callback called after the request finishes + * + * @param {function} callback Callback function + * @return {void} + */ + + this.setCallback = function (callback) { + this.callback = callback; + }; + + /** + * Sets the callback called in case of an error within the request + * + * @param {function} callback Callback function + * @return {void} + */ + this.setErrorCallback = function (callback) { + this.errorCallback = callback; + }; + + /** + * Sets the response format for the request + * + * @param {string} format response format (e.g. json, html, ...) + * @return {void} + */ + this.setFormat = function (format) { + this.format = format; + }; + + /** + * Set the div element to show while request is loading + * + * @param {String} element selector for the loading element + */ + this.setLoadingElement = function (element) { + if (!element) { + element = '#ajaxLoading'; + } + this.loadingElement = element; + }; + + /** + * Send the request + * @param {Boolean} sync indicates if the request should be synchronous (defaults to false) + * @return {void} + */ + this.send = function (sync) { + if (sync === true) { + this.async = false; + } + + if (this.loadingElement) { + $(this.loadingElement).fadeIn(); + } + this.requestHandle = this._buildAjaxCall(); + globalAjaxQueue.push(this.requestHandle); + }; + + /** + * Aborts the current request if it is (still) running + * @return {void} + */ + this.abort = function () { + if (this.requestHandle && typeof this.requestHandle.abort == 'function') { + this.requestHandle.abort(); + this.requestHandle = null; + } + }; + + /** + * Builds and sends the ajax requests + * @return {XMLHttpRequest} + * @private + */ + this._buildAjaxCall = function () { + var that = this; + + var ajaxCall = { + type: 'POST', + async: this.async !== false, + url: 'index.php?' + $.param(this._mixinDefaultGetParams(this.getParams)), + dataType: this.format || 'json', + error: this.errorCallback, + success: function (response) { + if (that.loadingElement) { + $(that.loadingElement).hide(); + } + that.callback(response); + }, + data: this._mixinDefaultPostParams(this.postParams) + }; + + return $.ajax(ajaxCall); + }; + + /** + * Mixin the default parameters to send as POST + * + * @param {object} params parameter object + * @return {object} + * @private + */ + this._mixinDefaultPostParams = function (params) { + + var defaultParams = { + token_auth: piwik.token_auth + }; + + for (var index in defaultParams) { + + if (!params[index]) { + + params[index] = defaultParams[index]; + } + } + + return params; + }; + + /** + * Mixin the default parameters to send as GET + * + * @param {object} params parameter object + * @return {object} + * @private + */ + this._mixinDefaultGetParams = function (params) { + + var defaultParams = { + idSite: piwik.idSite || broadcast.getValueFromUrl('idSite'), + period: piwik.period || broadcast.getValueFromUrl('period'), + segment: broadcast.getValueFromHash('segment', window.location.href) + }; + + // never append token_auth to url + if (params.token_auth) { + params.token_auth = null; + delete params.token_auth; + } + + for (var key in defaultParams) { + if (!params[key] && defaultParams[key]) { + params[key] = defaultParams[key]; + } + } + + // handle default date & period if not already set + if (!params.date) { + params.date = piwik.currentDateString || broadcast.getValueFromUrl('date'); + if (params.period == 'range' && piwik.currentDateString) { + defaultParams.date = piwik.startDateString + ',' + params.date; + } + } + + return params; + }; + + return this; +} \ No newline at end of file diff --git a/themes/default/common.js b/themes/default/common.js index 8d1acc64ccb0f1d688226d87b2cd6474ce142783..9f6effd31853cbec320692cb094afd220e1f7a56 100644 --- a/themes/default/common.js +++ b/themes/default/common.js @@ -83,117 +83,6 @@ var piwikHelper = { }); }, - /** - * Array holding all running ajax requests - * @type {Array} - */ - globalAjaxQueue: [], - - /** - * Registers the given requests to the list of running requests - * @param {XMLHttpRequest} request Request to be registered - * @return {XMLHttpRequest} - */ - queueAjaxRequest: function( request ) - { - this.globalAjaxQueue.push(request); - // clean up finished requests - for(var i = this.globalAjaxQueue.length; i--; ) { - if(!this.globalAjaxQueue[i] || this.globalAjaxQueue[i].readyState == 4) { - this.globalAjaxQueue.splice(i, 1); - } - } - return request; - }, - - /** - * Call an API method - * - * @param method API method name, i.e. Plugin.Method - * @param params parameters for the request - * @param callback regular callback - * @param format response format, default json - * @param async defines if the request should be asnyc (default: true) - * - * @return {XMLHttpRequest} - */ - ajaxCallApi: function(method, params, callback, format, async) - { - params.method = method; - return piwikHelper.ajaxCall('API', false, params, callback, format, async); - }, - - /** - * Do an AJAX request - * - * @param module plugin name - * @param action method name - * @param params parameters for the request - * @param callback regular callback - * @param format response format, default json - * @param async defines if the request should be asnyc (default: true) - * - * @return {XMLHttpRequest} - */ - ajaxCall: function(module, action, params, callback, format, async) { - - params.module = module; - if (action) - { - params.action = action; - } - - params.idSite = params.idSite ? params.idSite : piwik.idSite || broadcast.getValueFromUrl('idSite'); - params.period = params.period ? params.period : piwik.period || broadcast.getValueFromUrl('period'); - - if (!params.date) - { - params.date = piwik.currentDateString || broadcast.getValueFromUrl('date'); - if (params.period == 'range' && piwik.currentDateString) - { - params.date = piwik.startDateString + ',' + params.date; - } - } - - // never append token_auth to url - if(params.token_auth) { - params.token_auth = null; - delete params.token_auth; - } - - var segment = params.segment ? params.segment : broadcast.getValueFromHash('segment', window.location.href); - if (segment) - { - params.segment = segment; - } - - var ajaxRequest = - { - type: 'POST', - async: async !== false, - url: 'index.php?' + $.param(params), - dataType: format || 'json', - error: piwikHelper.ajaxHandleError, - success: callback, - data: {token_auth: piwik.token_auth} - }; - - return piwikHelper.queueAjaxRequest($.ajax(ajaxRequest)); - }, - - /** - * Aborts all registered running ajax requests - * @return {Boolean} - */ - abortQueueAjax: function() - { - for(var request in this.globalAjaxQueue) { - this.globalAjaxQueue[request].abort(); - } - this.globalAjaxQueue = []; - return true; - }, - /** * Returns the current query string with the given parameters modified * @param {object} newparams parameters to be modified