| |
| |
| |
| |
| |
| |
|
|
| (function ($) { |
|
|
| var layoutColumnSelector = '#dashboardWidgetsArea > .col'; |
|
|
| |
| |
| |
| |
| var dashboardLayout = {}; |
| |
| |
| |
| |
| var dashboardId = 1; |
| |
| |
| |
| |
| var dashboardName = ''; |
| |
| |
| |
| |
| var dashboardElement = null; |
| |
| |
| |
| |
| var dashboardChanged = false; |
|
|
| |
| |
| |
| |
| var methods = { |
|
|
| |
| |
| |
| |
| |
| init: function (options) { |
|
|
| dashboardElement = this; |
|
|
| if (options.idDashboard) { |
| dashboardId = options.idDashboard; |
| } |
|
|
| if (options.name) { |
| dashboardName = options.name; |
| } |
|
|
| if (options.layout) { |
| generateLayout(options.layout); |
| } |
|
|
| window.CoreHome.Matomo.postEvent('Dashboard.Dashboard.mounted', { element: this }); |
|
|
| return this; |
| }, |
|
|
| |
| |
| |
| |
| |
| destroy: function () { |
| if (dashboardElement && dashboardElement.length) { |
| window.CoreHome.Matomo.postEvent('Dashboard.Dashboard.unmounted', {element: dashboardElement[0]}); |
| } |
|
|
| $(dashboardElement).remove(); |
| dashboardElement = null; |
| destroyWidgets(); |
| }, |
|
|
| destroyWidgets: destroyWidgets, |
|
|
| |
| |
| |
| |
| |
| loadDashboard: function (dashboardIdToLoad, forceReload) { |
|
|
| $(dashboardElement).empty(); |
| dashboardName = ''; |
| dashboardLayout = null; |
| dashboardId = dashboardIdToLoad; |
|
|
| if (!forceReload && piwikHelper.isReportingPage()) { |
| var MatomoUrl = window.CoreHome.MatomoUrl; |
| MatomoUrl.updateHash(Object.assign({}, MatomoUrl.hashParsed.value, { |
| subcategory: dashboardIdToLoad, |
| })); |
| } else { |
| piwik.postEvent('Dashboard.loadDashboard', dashboardIdToLoad); |
| } |
|
|
| return this; |
| }, |
|
|
| |
| |
| |
| |
| |
| setColumnLayout: function (newLayout) { |
| adjustDashboardColumns(newLayout); |
| }, |
|
|
| |
| |
| |
| |
| |
| getColumnLayout: function () { |
| return dashboardLayout.config.layout; |
| }, |
|
|
| |
| |
| |
| |
| |
| getDashboardName: function () { |
| return dashboardName; |
| }, |
|
|
| |
| |
| |
| |
| |
| getDashboardId: function () { |
| return dashboardId; |
| }, |
|
|
| |
| |
| |
| |
| |
| setDashboardName: function (newName) { |
| dashboardName = newName; |
| dashboardChanged = true; |
| saveLayout(); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| addWidget: function (uniqueId, columnNumber, widgetParameters, addWidgetOnTop, isHidden) { |
| addWidgetTemplate(uniqueId, columnNumber, widgetParameters, addWidgetOnTop, isHidden); |
| saveLayout(); |
| }, |
|
|
| |
| |
| |
| resetLayout: function () { |
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams({ |
| module: 'Dashboard', |
| action: 'resetLayout', |
| idDashboard: dashboardId |
| }, 'get'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setCallback( |
| function () { |
| methods.loadDashboard.apply(this, [dashboardId, true]) |
| } |
| ); |
| ajaxRequest.setLoadingElement(); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| rebuildMenu: rebuildMenu, |
| |
| |
| |
| removeDashboard: function () { |
| if (dashboardId == 1) { |
| return; |
| } |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.setLoadingElement(); |
| ajaxRequest.addParams({ |
| module: 'API', |
| method: 'Dashboard.removeDashboard', |
| idDashboard: dashboardId, |
| login: piwik.userLogin, |
| format: 'json' |
| }, 'get'); |
| ajaxRequest.setCallback( |
| function () { |
| Promise.resolve(rebuildMenu()).then(function () { |
| methods.loadDashboard.apply(this, [1]); |
| }); |
| } |
| ); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| }, |
|
|
| |
| |
| |
| saveLayoutAsDefaultWidgetLayout: function () { |
| saveLayout('saveLayoutAsDefault'); |
| }, |
|
|
| |
| |
| |
| isDefaultDashboard: function () { |
| return (dashboardId == 1); |
| } |
| }; |
|
|
| function destroyWidgets() |
| { |
| var widgets = $('[widgetId]'); |
| for (var i = 0; i < widgets.length; i++) { |
| $(widgets[i]).dashboardWidget('destroy'); |
| } |
| } |
|
|
| function removeNonExistingWidgets(availableWidgets, layout) |
| { |
| var existingModuleAction = {}; |
| $.each(availableWidgets, function (category, widgets) { |
| $.each(widgets, function (index, widget) { |
| existingModuleAction[widget.module + '.' + widget.action] = true; |
| }); |
| }); |
|
|
| var columns = []; |
| $.each(layout.columns, function (i, column) { |
| var widgets = []; |
|
|
| $.each(column, function (j, widget) { |
| if (!widget.parameters || !widget.parameters.module) { |
| return; |
| } |
|
|
| var method = widget.parameters.module + '.' + widget.parameters.action |
| if (existingModuleAction[method]) { |
| widgets.push(widget); |
| } |
|
|
| }); |
|
|
| columns[i] = widgets; |
| }); |
|
|
| layout.columns = columns; |
|
|
| return layout; |
| } |
|
|
| |
| |
| |
| |
| |
| function generateLayout(layout) { |
|
|
| dashboardLayout = parseLayout(layout); |
|
|
| widgetsHelper.getAvailableWidgets(function (availableWidgets) { |
| dashboardLayout = removeNonExistingWidgets(availableWidgets, dashboardLayout); |
|
|
| piwikHelper.hideAjaxLoading(); |
| adjustDashboardColumns(dashboardLayout.config.layout); |
|
|
| var dashboardContainsWidgets = false; |
| for (var column = 0; column < dashboardLayout.columns.length; column++) { |
| for (var i in dashboardLayout.columns[column]) { |
| if (typeof dashboardLayout.columns[column][i] != 'object') { |
| |
| |
| continue; |
| } |
| var widget = dashboardLayout.columns[column][i]; |
| dashboardContainsWidgets = true; |
| addWidgetTemplate(widget.uniqueId, column + 1, widget.parameters, false, widget.isHidden) |
| } |
| } |
|
|
| if (!dashboardContainsWidgets) { |
| $(dashboardElement).trigger('dashboardempty'); |
| } |
|
|
| makeWidgetsSortable(); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function adjustDashboardColumns(layout) { |
| var columnWidth = layout.split('-'); |
| var columnCount = columnWidth.length; |
|
|
| var currentCount = $('> .col', dashboardElement).length; |
|
|
| if (currentCount < columnCount) { |
| $('.menuClear', dashboardElement).remove(); |
| for (var i = currentCount; i < columnCount; i++) { |
| if (dashboardLayout.columns.length < i) { |
| dashboardLayout.columns.push({}); |
| } |
| $(dashboardElement).append('<div class="col"> </div>'); |
| } |
| $(dashboardElement).append('<div class="menuClear"> </div>'); |
| } else if (currentCount > columnCount) { |
| for (var i = columnCount; i < currentCount; i++) { |
| if (dashboardLayout.columns.length >= i) { |
| dashboardLayout.columns.pop(); |
| } |
| |
| $('[widgetId]', $(layoutColumnSelector + ':last')).each(function (id, elem) { |
| var cols = $(layoutColumnSelector).slice(0, columnCount); |
| var smallestColumn = $(cols[0]); |
| var smallestColumnHeight = null; |
| cols.each(function (colId, col) { |
| if (smallestColumnHeight == null || smallestColumnHeight > $(col).height()) { |
| smallestColumnHeight = $(col).height(); |
| smallestColumn = $(col); |
| } |
| }); |
|
|
| $(elem).appendTo(smallestColumn); |
| }); |
|
|
| $(layoutColumnSelector + ':last').remove(); |
| } |
| } |
|
|
| var $dashboardElement = $(' > .col', dashboardElement); |
|
|
| if (!$dashboardElement.length) { |
| return; |
| } |
|
|
| switch (layout) { |
| case '100': |
| $dashboardElement.removeClass().addClass('col s12'); |
| break; |
| case '50-50': |
| $dashboardElement.removeClass().addClass('col s12 m6'); |
| break; |
| case '67-33': |
| $dashboardElement[0].className = 'col s12 m8'; |
| $dashboardElement[1].className = 'col s12 m4'; |
| break; |
| case '33-67': |
| $dashboardElement[0].className = 'col s12 m4'; |
| $dashboardElement[1].className = 'col s12 m8'; |
| break; |
| case '33-33-33': |
| $dashboardElement[0].className = 'col s12 m4'; |
| $dashboardElement[1].className = 'col s12 m4'; |
| $dashboardElement[2].className = 'col s12 m4'; |
| break; |
| case '40-30-30': |
| $dashboardElement[0].className = 'col s12 m6'; |
| $dashboardElement[1].className = 'col s12 m3'; |
| $dashboardElement[2].className = 'col s12 m3'; |
| break; |
| case '30-40-30': |
| $dashboardElement[0].className = 'col s12 m3'; |
| $dashboardElement[1].className = 'col s12 m6'; |
| $dashboardElement[2].className = 'col s12 m3'; |
| break; |
| case '30-30-40': |
| $dashboardElement[0].className = 'col s12 m3'; |
| $dashboardElement[1].className = 'col s12 m3'; |
| $dashboardElement[2].className = 'col s12 m6'; |
| break; |
| case '25-25-25-25': |
| $dashboardElement[0].className = 'col s12 m3'; |
| $dashboardElement[1].className = 'col s12 m3'; |
| $dashboardElement[2].className = 'col s12 m3'; |
| $dashboardElement[3].className = 'col s12 m3'; |
| break; |
| } |
|
|
| makeWidgetsSortable(); |
|
|
| |
| if (currentCount > 0 && dashboardLayout.config.layout != layout) { |
| dashboardChanged = true; |
| dashboardLayout.config.layout = layout; |
| saveLayout(); |
| } |
|
|
| |
| $('.widgetContent').each(function () { |
| $(this).trigger('widget:resize'); |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function parseLayout(layout) { |
|
|
| |
| |
| if ($.isArray(layout)) { |
| layout = { |
| config: {layout: '33-33-33'}, |
| columns: layout |
| }; |
| } |
|
|
| if (!layout.config.layout) { |
| layout.config.layout = '33-33-33'; |
| } |
|
|
| return layout; |
| } |
|
|
| |
| |
| |
| |
| |
| function reloadWidget($widget) { |
| if (typeof $widget === 'string') { |
| $widget = $('[widgetid="' + $widget + '"]', dashboardElement); |
| } |
|
|
| $widget.dashboardWidget('reload', false, true); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function addWidgetTemplate(uniqueId, columnNumber, widgetParameters, addWidgetOnTop, isHidden) { |
| if (!columnNumber) { |
| columnNumber = 1; |
| } |
|
|
| |
| if (columnNumber > $('> .col', dashboardElement).length) { |
| return; |
| } |
|
|
| var $widgetContent = $('<div class="sortable"></div>').attr('widgetId', uniqueId); |
|
|
| if (addWidgetOnTop) { |
| $('> .col:nth-child(' + columnNumber + ')', dashboardElement).prepend($widgetContent); |
| } else { |
| $('> .col:nth-child(' + columnNumber + ')', dashboardElement).append($widgetContent); |
| } |
|
|
| return $widgetContent.dashboardWidget({ |
| uniqueId: uniqueId, |
| widgetParameters: widgetParameters, |
| onChange: function () { |
| saveLayout(); |
| }, |
| isHidden: isHidden |
| }); |
| } |
|
|
| |
| |
| |
| function makeWidgetsSortable() { |
| function onStart(event, ui) { |
| if (!jQuery.support.noCloneEvent) { |
| $('object', this).hide(); |
| } |
| } |
|
|
| function onStop(event, ui) { |
| $('object', this).show(); |
| $('.widgetHover', this).removeClass('widgetHover'); |
| $('.widgetTopHover', this).removeClass('widgetTopHover'); |
| if ($('.widget:has(".piwik-graph")', ui.item).length) { |
| reloadWidget($('.widget', ui.item).attr('id')); |
| } |
| saveLayout(); |
| } |
|
|
| |
| $( layoutColumnSelector + ":data('ui-sortable')", dashboardElement ).sortable('destroy'); |
|
|
| $('> .col', dashboardElement) |
| .sortable({ |
| items: 'div.sortable', |
| opacity: 0.6, |
| forceHelperSize: true, |
| forcePlaceholderSize: true, |
| placeholder: 'hover', |
| handle: '.widgetTop', |
| helper: 'clone', |
| start: onStart, |
| stop: onStop, |
| connectWith: layoutColumnSelector |
| }); |
| } |
|
|
| |
| |
| |
| function rebuildMenu() { |
|
|
| if (piwikHelper.isReportingPage()) { |
| |
| return window.CoreHome.ReportingMenuStore.reloadMenuItems(); |
| } |
|
|
| var _self = this; |
|
|
| |
| var success = function (dashboards) { |
| var dashboardMenuList = $('#Dashboard_embeddedIndex_1').closest('ul'); |
| var dashboardMenuListItems = dashboardMenuList.find('>li'); |
|
|
| dashboardMenuListItems.filter(function () { |
| return $(this).attr('id').indexOf('Dashboard_embeddedIndex') == 0; |
| }).remove(); |
|
|
| if (dashboards.length === 0) { |
| dashboards = [{iddashboard: 1, name: _pk_translate('Dashboard_Dashboard')}]; |
| } |
|
|
| if (dashboards.length > 1 |
| || dashboardMenuListItems.length >= 1 |
| ) { |
| var items = []; |
| for (var i = 0; i < dashboards.length; i++) { |
| var $link = $('<a/>').attr('data-iddashboard', dashboards[i].iddashboard).text(dashboards[i].name).addClass('item'); |
| var $li = $('<li/>').attr('id', 'Dashboard_embeddedIndex_' + dashboards[i].iddashboard).addClass('dashboardMenuItem').attr('role', 'menuitem').append($link); |
|
|
| items.push($li); |
|
|
| if (dashboards[i].iddashboard == dashboardId) { |
| dashboardName = dashboards[i].name; |
| $li.addClass('active'); |
| } |
| } |
| dashboardMenuList.prepend(items); |
| } |
|
|
| dashboardMenuList.find('a[data-iddashboard]').click(function (e) { |
| e.preventDefault(); |
|
|
| var idDashboard = $(this).attr('data-iddashboard'); |
|
|
| $('#Dashboard ul li').removeClass('active'); |
|
|
| methods.loadDashboard.apply(_self, [idDashboard]); |
|
|
| $(this).closest('li').addClass('active'); |
| }); |
| }; |
|
|
| var ajaxRequest = new ajaxHelper(); |
| ajaxRequest.addParams({ |
| module: 'Dashboard', |
| action: 'getAllDashboards', |
| filter_limit: '-1' |
| }, 'get'); |
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setCallback(success); |
| return ajaxRequest.send(); |
| } |
|
|
| |
| |
| |
| |
| function saveLayout(action) { |
| var columns = []; |
|
|
| var columnNumber = 0; |
|
|
| $(layoutColumnSelector).each(function () { |
| columns[columnNumber] = []; |
| var items = $('[widgetId]', this); |
| for (var j = 0; j < items.length; j++) { |
| columns[columnNumber][j] = $(items[j]).dashboardWidget('getWidgetObject'); |
|
|
| |
| delete columns[columnNumber][j].parameters.segment; |
|
|
| } |
| columnNumber++; |
| }); |
|
|
| if (JSON.stringify(dashboardLayout.columns) != JSON.stringify(columns) || dashboardChanged || action) { |
|
|
| dashboardLayout.columns = JSON.parse(JSON.stringify(columns)); |
| columns = null; |
|
|
| if (!action) { |
| action = 'saveLayout'; |
| } |
|
|
| 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; |
| rebuildMenu(); |
| } |
| } |
| ); |
|
|
| ajaxRequest.withTokenInUrl(); |
| ajaxRequest.setFormat('html'); |
| ajaxRequest.send(); |
| } |
| } |
|
|
| |
| |
| |
| $.fn.dashboard = function (method) { |
| if (methods[method]) { |
| return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); |
| } else if (typeof method === 'object' || !method) { |
| return methods.init.apply(this, arguments); |
| } else { |
| $.error('Method ' + method + ' does not exist on jQuery.dashboard'); |
| } |
| } |
|
|
| })(jQuery); |
|
|