diff options
Diffstat (limited to 'plugins/Dashboard/javascripts/widgetMenu.js')
-rw-r--r-- | plugins/Dashboard/javascripts/widgetMenu.js | 113 |
1 files changed, 59 insertions, 54 deletions
diff --git a/plugins/Dashboard/javascripts/widgetMenu.js b/plugins/Dashboard/javascripts/widgetMenu.js index 55eef4ab09..465e8001f5 100644 --- a/plugins/Dashboard/javascripts/widgetMenu.js +++ b/plugins/Dashboard/javascripts/widgetMenu.js @@ -89,47 +89,50 @@ widgetsHelper.getAvailableWidgets = function (callback) { } $('#loadingError').show(); }); - ajaxRequest.send(true); + ajaxRequest.send(); + return; } if (callback) { callback(widgetsHelper.availableWidgets); } - - return widgetsHelper.availableWidgets; }; /** - * Returns the complete widget object by its unique id + * Determines the complete widget object by its unique id and sends it to callback method * * @param {string} uniqueId - * @return {object} widget object + * @param {function} callback */ -widgetsHelper.getWidgetObjectFromUniqueId = function (uniqueId) { - var widgets = widgetsHelper.getAvailableWidgets(); - for (var widgetCategory in widgets) { - var widgetInCategory = widgets[widgetCategory]; - for (var i in widgetInCategory) { - if (widgetInCategory[i]["uniqueId"] == uniqueId) { - return widgetInCategory[i]; +widgetsHelper.getWidgetObjectFromUniqueId = function (uniqueId, callback) { + widgetsHelper.getAvailableWidgets(function(widgets){ + for (var widgetCategory in widgets) { + var widgetInCategory = widgets[widgetCategory]; + for (var i in widgetInCategory) { + if (widgetInCategory[i]["uniqueId"] == uniqueId) { + callback(widgetInCategory[i]); + return; + } } } - } - return false; + callback(false); + }); }; /** - * Returns the name of a widget by its unique id + * Determines the name of a widget by its unique id and sends it to the callback * * @param {string} uniqueId unique id of the widget + * @param {function} callback * @return {string} */ -widgetsHelper.getWidgetNameFromUniqueId = function (uniqueId) { - var widget = this.getWidgetObjectFromUniqueId(uniqueId); - if (widget == false) { - return false; - } - return widget["name"]; +widgetsHelper.getWidgetNameFromUniqueId = function (uniqueId, callback) { + this.getWidgetObjectFromUniqueId(uniqueId, function(widget) { + if (widget == false) { + callback(false); + } + callback(widget["name"]); + }); }; /** @@ -155,7 +158,7 @@ widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWid ajaxRequest.setErrorCallback(onWidgetErrorCallback); } ajaxRequest.setFormat('html'); - ajaxRequest.send(false); + ajaxRequest.send(); return ajaxRequest; }; @@ -389,6 +392,7 @@ widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWid * Show widget with the given uniqueId in preview * * @param {string} widgetUniqueId unique id of widget to display + * @param widgetPreview * @return {void} */ function showPreview(widgetUniqueId, widgetPreview) { @@ -399,39 +403,40 @@ widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWid var previewElement = createPreviewElement(widgetPreview); - var widget = widgetsHelper.getWidgetObjectFromUniqueId(widgetUniqueId); - var widgetParameters = widget['parameters']; - - var emptyWidgetHtml = require('piwik/UI/Dashboard').WidgetFactory.make( - widgetUniqueId, - $('<div/>') - .attr('title', _pk_translate("Dashboard_AddPreviewedWidget")) - .text(_pk_translate('Dashboard_WidgetPreview')) - ); - previewElement.html(emptyWidgetHtml); - - var onWidgetLoadedCallback = function (response) { - var widgetElement = $(document.getElementById(widgetUniqueId)); - // document.getElementById needed for widgets with uniqueid like widgetOpens+Contact+Form - $('.widgetContent', widgetElement).html($(response)); - $('.widgetContent', widgetElement).trigger('widget:create'); - settings.onPreviewLoaded(widgetUniqueId, widgetElement); - $('.' + settings.widgetpreviewClass + ' .widgetTop', widgetPreview).on('click', function () { - settings.onSelect(widgetUniqueId); - $(widgetPreview).closest('.dashboard-manager').removeClass('expanded'); - if (settings.resetOnSelect) { - resetWidgetPreview(widgetPreview); - } - return false; - }); - }; - - // abort previous sent request - if (widgetPreview.widgetAjaxRequest) { - widgetPreview.widgetAjaxRequest.abort(); - } + widgetsHelper.getWidgetObjectFromUniqueId(widgetUniqueId, function(widget) { + var widgetParameters = widget['parameters']; + + var emptyWidgetHtml = require('piwik/UI/Dashboard').WidgetFactory.make( + widgetUniqueId, + $('<div/>') + .attr('title', _pk_translate("Dashboard_AddPreviewedWidget")) + .text(_pk_translate('Dashboard_WidgetPreview')) + ); + previewElement.html(emptyWidgetHtml); + + var onWidgetLoadedCallback = function (response) { + var widgetElement = $(document.getElementById(widgetUniqueId)); + // document.getElementById needed for widgets with uniqueid like widgetOpens+Contact+Form + $('.widgetContent', widgetElement).html($(response)); + $('.widgetContent', widgetElement).trigger('widget:create'); + settings.onPreviewLoaded(widgetUniqueId, widgetElement); + $('.' + settings.widgetpreviewClass + ' .widgetTop', widgetPreview).on('click', function () { + settings.onSelect(widgetUniqueId); + $(widgetPreview).closest('.dashboard-manager').removeClass('expanded'); + if (settings.resetOnSelect) { + resetWidgetPreview(widgetPreview); + } + return false; + }); + }; + + // abort previous sent request + if (widgetPreview.widgetAjaxRequest) { + widgetPreview.widgetAjaxRequest.abort(); + } - widgetPreview.widgetAjaxRequest = widgetsHelper.loadWidgetAjax(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); + widgetPreview.widgetAjaxRequest = widgetsHelper.loadWidgetAjax(widgetUniqueId, widgetParameters, onWidgetLoadedCallback); + }); } /** |