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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/**
* Matomo - free/libre analytics platform
*
* Visitor profile popup control.
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function ($, require) {
var piwik = require('piwik'),
exports = require('piwik/UI'),
UIControl = exports.UIControl;
/**
* Sets up and handles events for the visitor profile popup.
*
* @param {Element} element The HTML element returned by the Live.getVisitorLog controller
* action. Should have the CSS class 'visitor-profile'.
* @constructor
*/
var VisitorProfileControl = function (element) {
UIControl.call(this, element);
this._setupControl();
this._bindEventCallbacks();
};
/**
* Initializes all elements w/ the .visitor-profile CSS class as visitor profile popups,
* if the element has not already been initialized.
*/
VisitorProfileControl.initElements = function () {
UIControl.initElements(this, '.visitor-profile');
};
/**
* Shows the visitor profile popover for a visitor ID. This should not be called directly.
* Instead broadcast.propagateNewPopoverParameter('visitorProfile', visitorId) should be
* called. This would make sure the popover would be opened if the URL is copied and pasted
* in a new tab/window.
*
* @param {String} visitorId The string visitor ID.
*/
VisitorProfileControl.showPopover = function (visitorId, idSite) {
var url = 'module=Live&action=getVisitorProfilePopup&visitorId=' + encodeURIComponent(visitorId);
if (idSite) {
url += '&idSite=' + idSite;
}
// if there is already a map shown on the screen, do not show the map in the popup. kartograph seems
// to only support showing one map at a time.
if ($('.RealTimeMap').length > 0) {
url += '&showMap=0';
}
var ajaxRequest = new ajaxHelper();
ajaxRequest.removeDefaultParameter('segment');
Piwik_Popover.createPopupAndLoadUrl(url, _pk_translate('Live_VisitorProfile'), 'visitor-profile-popup', ajaxRequest);
};
$.extend(VisitorProfileControl.prototype, UIControl.prototype, {
_setupControl: function () {
// focus the popup so it will accept key events
this.$element.focus();
},
_bindEventCallbacks: function () {
var self = this,
$element = this.$element;
// if there are no popover params in stack, simply close the popover
if (!broadcast.popoverParamStack.length) {
$element.on('click', '.visitor-profile-close', function (e) {
e.preventDefault();
try {
$element.tooltip('destroy');
} catch (e) {
}
Piwik_Popover.close();
});
} else {
$element.on('click', '.visitor-profile-close', function (e) {
e.preventDefault();
try {
$element.tooltip('destroy');
} catch (e) {
}
broadcast.propagateNewPopoverParameter(false);
return false;
});
}
$element.on('click', '.visitor-profile-toggle-actions', function (e) {
e.preventDefault();
$(this).toggleClass('minimized');
if ($(this).hasClass('minimized')) {
$('.visitor-profile-actions', $element).slideUp();
} else {
$('.visitor-profile-actions', $element).slideDown();
}
return false;
});
$element.on('click', '.visitor-profile-more-info>a', function (e) {
e.preventDefault();
self._loadMoreVisits();
return false;
});
$element.on('click', '.visitor-profile-visit-title', function () {
$('.visitor-profile-visit-details-extended', $(this).parents('li')).slideToggle();
});
$element.on('click', '.visitor-profile-show-actions', function () {
$('.visitor-profile-actions', $(this).parents('li')).slideToggle();
return false;
});
$element.on('click', '.visitor-profile-prev-visitor', function (e) {
e.preventDefault();
self._loadPreviousVisitor();
return false;
});
$element.on('click', '.visitor-profile-next-visitor', function (e) {
e.preventDefault();
self._loadNextVisitor();
return false;
});
$element.on('keydown', function (e) {
if (e.which == 37 && !e.altKey) { // on <- key press, load previous visitor
self._loadPreviousVisitor();
} else if (e.which == 39 && !e.altKey) { // on -> key press, load next visitor
self._loadNextVisitor();
}
});
$element.on('click', '.visitor-profile-show-map', function (e) {
e.preventDefault();
self.toggleMap();
return false;
});
// append token_auth dynamically to export link
$element.on('mousedown', '.visitor-profile-export', function (e) {
var url = $(this).attr('href');
if (url.indexOf('&token_auth=') == -1) {
$(this).attr('href', url + '&token_auth=' + piwik.token_auth);
}
});
initializeVisitorActions($element);
},
toggleMap: function () {
var $element = this.$element,
$map = $('.visitor-profile-map', $element);
if (!$map.children().length) { // if the map hasn't been loaded, load it
this._loadMap($map);
return;
}
if ($map.is(':hidden')) { // show the map if it is hidden
if ($map.height() < 1) {
$map.resize();
}
$map.slideDown('slow');
var newLabel = 'Live_HideMap';
piwikHelper.lazyScrollTo($('.visitor-profile-location', $element)[0], 400);
} else { // hide the map if it is shown
$map.slideUp('slow');
var newLabel = 'Live_ShowMap';
}
newLabel = _pk_translate(newLabel).replace(' ', '\xA0');
$('.visitor-profile-show-map', $element).text('(' + newLabel + ')');
},
_loadMap: function ($map) {
var self = this;
var ajax = new ajaxHelper();
ajax.setUrl($map.attr('data-href'));
ajax.setCallback(function (response) {
$map.html(response);
self.toggleMap();
});
ajax.setFormat('html');
ajax.setLoadingElement($('.visitor-profile-location > p > .loadingPiwik', self.$element));
ajax.send();
},
_loadMoreVisits: function () {
var self = this,
$element = this.$element;
var loading = $('.visitor-profile-more-info > .loadingPiwik', $element);
loading.show();
var ajax = new ajaxHelper();
ajax.removeDefaultParameter('segment');
ajax.addParams({
module: 'Live',
action: 'getVisitList',
period: '',
date: '',
visitorId: $element.attr('data-visitor-id'),
filter_offset: $('.visitor-profile-visits>li', $element).length,
start_number: $('.visitor-profile-visits>li:last', $element).data('number') - 1
}, 'GET');
ajax.setCallback(function (response) {
if (response == "") { // no more visits left
self._showNoMoreVisitsSpan();
} else {
response = $(response);
loading.hide();
$('.visitor-profile-visits', $element).append(response);
if (response.filter('li').length < 10) {
self._showNoMoreVisitsSpan();
}
piwikHelper.lazyScrollTo($(response)[0], 400, true);
}
});
ajax.setFormat('html');
ajax.send();
},
_showNoMoreVisitsSpan: function () {
var noMoreSpan = $('<span/>').text(_pk_translate('Live_NoMoreVisits')).addClass('visitor-profile-no-visits');
$('.visitor-profile-more-info', this.$element).html(noMoreSpan);
},
_loadPreviousVisitor: function () {
this._gotoAdjacentVisitor(this.$element.attr('data-prev-visitor'));
},
_loadNextVisitor: function () {
this._gotoAdjacentVisitor(this.$element.attr('data-next-visitor'));
},
_gotoAdjacentVisitor: function (idVisitor) {
if (!idVisitor) {
return;
}
if (this._inPopover()) {
broadcast.propagateNewPopoverParameter('visitorProfile', idVisitor);
} else if (this._inWidget()) {
this.$element.closest('[widgetid]').dashboardWidget('reload', false, true, {visitorId: idVisitor});
}
},
_getFirstVisitId: function () {
return $('.visitor-profile-visits>li:first-child>h2', this.$element).attr('data-idvisit');
},
_inPopover: function () {
return !! this.$element.closest('#Piwik_Popover').length;
},
_inWidget: function () {
return !! this.$element.closest('.widget').length;
}
});
exports.VisitorProfileControl = VisitorProfileControl;
// add the popup handler that creates a visitor profile
broadcast.addPopoverHandler('visitorProfile', VisitorProfileControl.showPopover);
})(jQuery, require);
|