blob: 57bc0b580a43c1962db09ce0a93f8f90326d8830 (
plain)
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
|
/*!
* 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.service').service('piwik', piwikService);
piwikService.$inject = ['piwikPeriods'];
function piwikService(piwikPeriods) {
var originalTitle;
piwik.helper = piwikHelper;
piwik.broadcast = broadcast;
piwik.updatePeriodParamsFromUrl = updatePeriodParamsFromUrl;
piwik.updateDateInTitle = updateDateInTitle;
piwik.hasUserCapability = hasUserCapability;
return piwik;
function hasUserCapability(capability) {
return angular.isArray(piwik.userCapabilities) && piwik.userCapabilities.indexOf(capability) !== -1;
}
function updatePeriodParamsFromUrl() {
var date = piwik.broadcast.getValueFromHash('date') || piwik.broadcast.getValueFromUrl('date');
var period = piwik.broadcast.getValueFromHash('period') || piwik.broadcast.getValueFromUrl('period');
if (!isValidPeriod(period, date)) {
// invalid data in URL
return;
}
if (piwik.period === period && piwik.currentDateString === date) {
// this period / date is already loaded
return;
}
piwik.period = period;
var dateRange = piwikPeriods.parse(period, date).getDateRange();
piwik.startDateString = piwikPeriods.format(dateRange[0]);
piwik.endDateString = piwikPeriods.format(dateRange[1]);
updateDateInTitle(date, period);
// do not set anything to previousN/lastN, as it's more useful to plugins
// to have the dates than previousN/lastN.
if (piwik.period === 'range') {
date = piwik.startDateString + ',' + piwik.endDateString;
}
piwik.currentDateString = date;
}
function isValidPeriod(periodStr, dateStr) {
try {
piwikPeriods.get(periodStr).parse(dateStr);
return true;
} catch (e) {
return false;
}
}
function updateDateInTitle( date, period ) {
if (!$('.top_controls #periodString').length) {
return;
}
// Cache server-rendered page title
originalTitle = originalTitle || document.title;
if (0 === originalTitle.indexOf(piwik.siteName)) {
var dateString = ' - ' + piwikPeriods.parse(period, date).getPrettyString() + ' ';
document.title = piwik.siteName + dateString + originalTitle.substr(piwik.siteName.length);
}
}
}
angular.module('piwikApp.service').run(initPiwikService);
initPiwikService.$inject = ['piwik', '$rootScope'];
function initPiwikService(piwik, $rootScope) {
$rootScope.$on('$locationChangeSuccess', piwik.updatePeriodParamsFromUrl);
}
})();
|