1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function () {
angular.module('piwikApp').controller('ReportingMenuController', ReportingMenuController);
ReportingMenuController.$inject = ['$scope', 'piwik', '$location', '$timeout', 'reportingMenuModel', '$rootScope'];
function ReportingMenuController($scope, piwik, $location, $timeout, menuModel, $rootScope) {
var idSite = getUrlParam('idSite');
var period = getUrlParam('period');
var date = getUrlParam('date');
var segment = getUrlParam('segment');
function markAllCategoriesAsInactive()
{
angular.forEach(menuModel.menu, function (cat) {
cat.active = false;
});
}
function markAllCategoriesAndChildrenInactive()
{
angular.forEach(menuModel.menu, function (cat) {
cat.active = false;
angular.forEach(cat.subcategories, function (subcat) {
subcat.active = false;
if (subcat.isGroup && subcat.subcategories) {
angular.forEach(subcat.subcategories, function (sub) {
sub.active = false;
});
}
});
});
}
function getUrlParam(param)
{
var value = piwik.broadcast.getValueFromHash(param);
if (!value) {
value = piwik.broadcast.getValueFromUrl(param);
}
return value;
}
$scope.menuModel = menuModel;
// highlight the currently hovered subcategory (and category)
function enterSubcategory(category, subcategory, subsubcategory) {
if (!category || !subcategory) {
return;
}
markAllCategoriesAndChildrenInactive();
category.active = true;
subcategory.active = true;
if (subsubcategory) {
subcategory.name = subsubcategory.name;
subsubcategory.active = true;
}
};
$scope.makeUrl = function (category, subcategory) {
var url = 'idSite=' + encodeURIComponent(idSite);
url += '&period=' + encodeURIComponent(period);
url += '&date=' + encodeURIComponent(date);
url += '&category=' + encodeURIComponent(category.id);
url += '&subcategory=' + encodeURIComponent(subcategory.id);
if (segment) {
url+= '&segment='+ segment;
}
return url;
}
$scope.loadCategory = function (category) {
if (category.active) {
category.active = false;
} else {
markAllCategoriesAsInactive();
category.active = true;
}
if (category.active && category.subcategories && category.subcategories.length === 1) {
var subcategory = category.subcategories[0];
if (subcategory.active) {
// we need to manually trigger change as URL would not change and therefore page would not be
// reloaded
$scope.loadSubcategory(category, subcategory);
} else {
var url = $scope.makeUrl(category, subcategory);
$location.search(url);
}
}
};
$scope.loadSubcategory = function (category, subcategory) {
if (subcategory && subcategory.active) {
// this menu item is already active, a location change success would not be triggered,
// instead trigger an event
$rootScope.$emit('loadPage', category.id, subcategory.id);
}
};
menuModel.fetchMenuItems().then(function (menu) {
if (!$location.search().subcategory) {
// load first, initial page if no subcategory is present
enterSubcategory(menu[0], menu[0].subcategories[0]);
$location.search($scope.makeUrl(menu[0], menu[0].subcategories[0]));
}
});
$rootScope.$on('updateReportingMenu', function () {
menuModel.reloadMenuItems().then(function (menu) {
var $search = $location.search();
var category = $search.category;
var subcategory = $search.subcategory;
// we need to make sure to select same categories again
if (category && subcategory) {
var found = menuModel.findSubcategory(category, subcategory);
if (found) {
enterSubcategory(found.category, found.subcategory, found.subsubcategory);
}
}
});
if ('object' === typeof widgetsHelper && widgetsHelper.availableWidgets) {
// lets also update widgetslist so will be easier to update list of available widgets in dashboard selector
// immediately
delete widgetsHelper.availableWidgets;
widgetsHelper.getAvailableWidgets();
}
});
$rootScope.$on('$locationChangeSuccess', function () {
var $search = $location.search();
var category = $search.category;
var subcategory = $search.subcategory;
period = getUrlParam('period');
date = getUrlParam('date');
segment = getUrlParam('segment');
if (!category || !subcategory) {
return;
}
var found = menuModel.findSubcategory(category, subcategory);
enterSubcategory(found.category, found.subcategory, found.subsubcategory);
});
}
})();
|