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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
/*!
* 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('PeriodSelectorController', PeriodSelectorController);
PeriodSelectorController.$inject = ['piwik', '$location', 'piwikPeriods'];
function PeriodSelectorController(piwik, $location, piwikPeriods) {
var piwikMinDate = new Date(piwik.minDateYear, piwik.minDateMonth - 1, piwik.minDateDay),
piwikMaxDate = new Date(piwik.maxDateYear, piwik.maxDateMonth - 1, piwik.maxDateDay);
var vm = this;
// the period & date currently being viewed
vm.periodValue = null;
vm.dateValue = null;
vm.selectedPeriod = null;
vm.startRangeDate = null;
vm.endRangeDate = null;
vm.isRangeValid = null;
vm.isLoadingNewPage = false;
vm.getCurrentlyViewingText = getCurrentlyViewingText;
vm.changeViewedPeriod = changeViewedPeriod;
vm.setPiwikPeriodAndDate = setPiwikPeriodAndDate;
vm.onApplyClicked = onApplyClicked;
vm.updateSelectedValuesFromHash = updateSelectedValuesFromHash;
vm.getPeriodDisplayText = getPeriodDisplayText;
vm.$onChanges = $onChanges;
vm.onRangeChange = onRangeChange;
vm.isApplyEnabled = isApplyEnabled;
vm.$onInit = init;
function init() {
vm.updateSelectedValuesFromHash();
initTopControls(); // must be called when a top control changes width
}
function $onChanges(changesObj) {
if (changesObj.periods) {
removeUnrecognizedPeriods();
}
}
function onRangeChange(start, end) {
if (!start || !end) {
vm.isRangeValid = false;
return;
}
vm.isRangeValid = true;
vm.startRangeDate = start;
vm.endRangeDate = end;
}
function isApplyEnabled() {
if (vm.selectedPeriod === 'range'
&& !vm.isRangeValid
) {
return false;
}
return true;
}
function removeUnrecognizedPeriods() {
vm.periods = vm.periods.filter(function (periodLabel) {
return piwikPeriods.isRecognizedPeriod(periodLabel);
});
}
function updateSelectedValuesFromHash() {
var strDate = getQueryParamValue('date');
var strPeriod = getQueryParamValue('period');
vm.periodValue = strPeriod;
vm.selectedPeriod = strPeriod;
vm.dateValue = vm.startRangeDate = vm.endRangeDate = null;
if (strPeriod === 'range') {
var period = piwikPeriods.get(strPeriod).parse(strDate);
vm.dateValue = period.startDate;
vm.startRangeDate = formatDate(period.startDate);
vm.endRangeDate = formatDate(period.endDate);
} else {
vm.dateValue = piwikPeriods.parseDate(strDate);
setRangeStartEndFromPeriod(strPeriod, strDate);
}
}
function getQueryParamValue(name) {
// $location doesn't parse the URL before the hashbang, but it can hold the query param
// values, if the page doesn't have the hashbang.
var result = $location.search()[name];
if (!result) {
result = broadcast.getValueFromUrl(name);
}
return result;
}
function getPeriodDisplayText(periodLabel) {
return piwikPeriods.get(periodLabel).getDisplayText();
}
function getCurrentlyViewingText() {
var date;
if (vm.periodValue === 'range') {
date = vm.startRangeDate + ',' + vm.endRangeDate;
} else {
date = formatDate(vm.dateValue);
}
try {
return piwikPeriods.parse(vm.periodValue, date).getPrettyString();
} catch (e) {
return _pk_translate('General_Error');
}
}
function changeViewedPeriod(period) {
// only change period if it's different from what's being shown currently
if (period === vm.periodValue) {
return;
}
// can't just change to a range period, w/o setting two new dates
if (period === 'range') {
return;
}
setPiwikPeriodAndDate(period, vm.dateValue);
}
function onApplyClicked() {
if (vm.selectedPeriod === 'range') {
var dateFrom = vm.startRangeDate,
dateTo = vm.endRangeDate,
oDateFrom = piwikPeriods.parseDate(dateFrom),
oDateTo = piwikPeriods.parseDate(dateTo);
if (!isValidDate(oDateFrom)
|| !isValidDate(oDateTo)
|| oDateFrom > oDateTo
) {
// TODO: use a notification instead?
$('#alert').find('h2').text(_pk_translate('General_InvalidDateRange'));
piwik.helper.modalConfirm('#alert', {});
return;
}
vm.periodValue = 'range';
propagateNewUrlParams(dateFrom + ',' + dateTo, 'range');
return;
}
setPiwikPeriodAndDate(vm.selectedPeriod, vm.dateValue);
}
function setPiwikPeriodAndDate(period, date) {
vm.periodValue = period;
vm.selectedPeriod = period;
vm.dateValue = date;
var currentDateString = formatDate(date);
setRangeStartEndFromPeriod(period, currentDateString);
propagateNewUrlParams(currentDateString, vm.selectedPeriod);
initTopControls();
}
function setRangeStartEndFromPeriod(period, dateStr) {
var dateRange = piwikPeriods.parse(period, dateStr).getDateRange();
vm.startRangeDate = formatDate(dateRange[0] < piwikMinDate ? piwikMinDate : dateRange[0]);
vm.endRangeDate = formatDate(dateRange[1] > piwikMaxDate ? piwikMaxDate : dateRange[1]);
}
function propagateNewUrlParams(date, period) {
if (piwik.helper.isAngularRenderingThePage()) {
vm.closePeriodSelector(); // defined in directive
var $search = $location.search();
if (date !== $search.date || period !== $search.period) {
// eg when using back button the date might be actually already changed in the URL and we do not
// want to change the URL again
$search.date = date;
$search.period = period;
$location.search($search);
}
return;
}
vm.isLoadingNewPage = true;
// not in an angular context (eg, embedded dashboard), so must actually
// change the URL
broadcast.propagateNewPage('date=' + date + '&period=' + period);
}
function isValidDate(d) {
if (Object.prototype.toString.call(d) !== "[object Date]") {
return false;
}
return !isNaN(d.getTime());
}
function formatDate(date) {
return $.datepicker.formatDate('yy-mm-dd', date);
}
}
})();
|