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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
/*!
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
function createDashboard() {
$(makeSelectorLastId('createDashboardName')).val('');
piwikHelper.modalConfirm(makeSelectorLastId('createDashboardConfirm'), {yes: function () {
var dashboardName = $(makeSelectorLastId('createDashboardName')).val();
var addDefaultWidgets = ($('[id=dashboard_type_empty]:last:checked').length > 0) ? 0 : 1;
var ajaxRequest = new ajaxHelper();
ajaxRequest.setLoadingElement();
ajaxRequest.withTokenInUrl();
ajaxRequest.addParams({
module: 'API',
method: 'Dashboard.createNewDashboardForUser',
format: 'json'
}, 'get');
ajaxRequest.addParams({
dashboardName: dashboardName,
addDefaultWidgets: addDefaultWidgets,
login: piwik.userLogin
}, 'post');
ajaxRequest.setCallback(
function (response) {
var id = response.value;
angular.element(document).injector().invoke(function ($location, reportingMenuModel, dashboardsModel) {
dashboardsModel.reloadAllDashboards().then(function () {
$('#dashboardWidgetsArea').dashboard('loadDashboard', id);
$('#dashboardWidgetsArea').dashboard('rebuildMenu');
});
});
}
);
ajaxRequest.send();
}});
}
function makeSelectorLastId(domElementId)
{
// there can be many elements with this id, we prefer the last one
return '[id=' + domElementId + ']:last';
}
function resetDashboard() {
piwikHelper.modalConfirm(makeSelectorLastId('resetDashboardConfirm'), {yes:
function () { $('#dashboardWidgetsArea').dashboard('resetLayout');
}});
}
function renameDashboard() {
$(makeSelectorLastId('newDashboardName')).val($('#dashboardWidgetsArea').dashboard('getDashboardName'));
piwikHelper.modalConfirm(makeSelectorLastId('renameDashboardConfirm'), {yes: function () {
var newDashboardName = $(makeSelectorLastId('newDashboardName')).val();
$('#dashboardWidgetsArea').dashboard('setDashboardName', newDashboardName);
}});
}
function removeDashboard() {
$(makeSelectorLastId('removeDashboardConfirm')).find('h2 span').text($('#dashboardWidgetsArea').dashboard('getDashboardName'));
piwikHelper.modalConfirm(makeSelectorLastId('removeDashboardConfirm'), {yes: function () {
$('#dashboardWidgetsArea').dashboard('removeDashboard');
}});
}
function showChangeDashboardLayoutDialog() {
$('#columnPreview').find('>div').removeClass('choosen');
$('#columnPreview').find('>div[layout=' + $('#dashboardWidgetsArea').dashboard('getColumnLayout') + ']').addClass('choosen');
var id = makeSelectorLastId('changeDashboardLayout');
piwikHelper.modalConfirm(id, {yes: function () {
var layout = $(id).find('.choosen').attr('layout');
$('#dashboardWidgetsArea').dashboard('setColumnLayout', layout);
}}, {fixedFooter: true});
}
function showEmptyDashboardNotification() {
piwikHelper.modalConfirm(makeSelectorLastId('dashboardEmptyNotification'), {
resetDashboard: function () { $('#dashboardWidgetsArea').dashboard('resetLayout'); },
addWidget: function () { $('.dashboardSettings > a').trigger('click'); }
});
}
function setAsDefaultWidgets() {
piwikHelper.modalConfirm(makeSelectorLastId('setAsDefaultWidgetsConfirm'), {
yes: function () {
$('#dashboardWidgetsArea').dashboard('saveLayoutAsDefaultWidgetLayout');
}
});
}
function copyDashboardToUser() {
$(makeSelectorLastId('copyDashboardName')).val($('#dashboardWidgetsArea').dashboard('getDashboardName'));
var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams({
module: 'API',
method: 'UsersManager.getUsers',
format: 'json',
filter_limit: '-1'
}, 'get');
ajaxRequest.setCallback(
function (availableUsers) {
$(makeSelectorLastId('copyDashboardUser')).empty();
$(makeSelectorLastId('copyDashboardUser')).append(
$('<option></option>').val(piwik.userLogin).text(piwik.userLogin)
);
$.each(availableUsers, function (index, user) {
if (user.login != 'anonymous' && user.login != piwik.userLogin) {
$(makeSelectorLastId('copyDashboardUser')).append(
$('<option></option>').val(user.login).text(user.login + ' (' + user.alias + ')')
);
}
});
}
);
ajaxRequest.send();
piwikHelper.modalConfirm(makeSelectorLastId('copyDashboardToUserConfirm'), {
yes: function () {
var copyDashboardName = $(makeSelectorLastId('copyDashboardName')).val();
var copyDashboardUser = $(makeSelectorLastId('copyDashboardUser')).val();
var ajaxRequest = new ajaxHelper();
ajaxRequest.addParams({
module: 'API',
method: 'Dashboard.copyDashboardToUser',
format: 'json'
}, 'get');
ajaxRequest.addParams({
dashboardName: encodeURIComponent(copyDashboardName),
idDashboard: $('#dashboardWidgetsArea').dashboard('getDashboardId'),
copyToUser: copyDashboardUser
}, 'post');
ajaxRequest.setCallback(
function (response) {
$('#alert').find('h2').text(_pk_translate('Dashboard_DashboardCopied'));
piwikHelper.modalConfirm('#alert', {});
}
);
ajaxRequest.withTokenInUrl();
ajaxRequest.send();
}
});
}
(function () {
var exports = window.require('piwik/UI');
var UIControl = exports.UIControl;
/**
* Contains logic common to all dashboard management controls. This is the JavaScript analog of
* the DashboardSettingsControlBase PHP class.
*
* @param {Element} element The HTML element generated by the SegmentSelectorControl PHP class. Should
* have the CSS class 'segmentEditorPanel'.
* @constructor
*/
var DashboardSettingsControlBase = function (element) {
UIControl.call(this, element);
// on menu item click, trigger action event on this
var self = this;
this.$element.on('click', 'ul.submenu li[data-action]', function (e) {
if (!$(this).attr('disabled')) {
self.$element.removeClass('expanded');
$(self).trigger($(this).attr('data-action'));
}
});
// open manager on open
this.$element.on('click', function (e) {
if ($(e.target).is('.dashboardSettings') || $(e.target).closest('.dashboardSettings').length) {
self.onOpen();
}
});
// handle manager close
this.onBodyMouseUp = function (e) {
if (!$(e.target).closest('.dashboardSettings').length
&& !$(e.target).is('.dashboardSettings')
) {
self.$element.widgetPreview('reset');
self.$element.removeClass('expanded');
}
};
$('body').on('mouseup', this.onBodyMouseUp);
// setup widgetPreview
this.$element.widgetPreview({
isWidgetAvailable: function (widgetUniqueId) {
return self.isWidgetAvailable(widgetUniqueId);
},
onSelect: function (widgetUniqueId) {
widgetsHelper.getWidgetObjectFromUniqueId(widgetUniqueId, function(widget){
self.$element.removeClass('expanded');
self.widgetSelected(widget);
});
},
resetOnSelect: true
});
// on enter widget list category, reset widget preview
this.$element.on('mouseenter', '.submenu > li', function (event) {
if (!$('.widgetpreview-categorylist', event.target).length) {
self.$element.widgetPreview('reset');
}
});
};
$.extend(DashboardSettingsControlBase.prototype, UIControl.prototype, {
_destroy: function () {
UIControl.prototype._destroy.call(this);
$('body').off('mouseup', null, this.onBodyMouseUp);
}
});
exports.DashboardSettingsControlBase = DashboardSettingsControlBase;
/**
* Sets up and handles events for the dashboard manager control.
*
* @param {Element} element The HTML element generated by the SegmentSelectorControl PHP class. Should
* have the CSS class 'segmentEditorPanel'.
* @constructor
*/
var DashboardManagerControl = function (element) {
DashboardSettingsControlBase.call(this, element);
$(this).on('resetDashboard', function () {
this.hide();
resetDashboard();
});
$(this).on('showChangeDashboardLayoutDialog', function () {
this.hide();
showChangeDashboardLayoutDialog();
});
$(this).on('renameDashboard', function () {
this.hide();
renameDashboard();
});
$(this).on('removeDashboard', function () {
this.hide();
removeDashboard();
});
$(this).on('setAsDefaultWidgets', function () {
this.hide();
setAsDefaultWidgets();
});
$(this).on('copyDashboardToUser', function () {
this.hide();
copyDashboardToUser();
});
$(this).on('createDashboard', function () {
this.hide();
createDashboard();
});
};
$.extend(DashboardManagerControl.prototype, DashboardSettingsControlBase.prototype, {
onOpen: function () {
if ($('#dashboardWidgetsArea').dashboard('isDefaultDashboard')) {
$('[data-action=removeDashboard]', this.$element).attr('disabled', 'disabled');
$(this.$element).tooltip({
items: '[data-action=removeDashboard]',
show: false,
hide: false,
track: true,
content: function() {
return _pk_translate('Dashboard_RemoveDefaultDashboardNotPossible')
},
tooltipClass: 'small'
});
} else {
$('[data-action=removeDashboard]', this.$element).removeAttr('disabled');
// try to remove tooltip if any
try {
$(this.$element).tooltip('destroy');
} catch (e) { }
}
},
hide: function () {
this.$element.removeClass('expanded');
},
isWidgetAvailable: function (widgetUniqueId) {
return !$('#dashboardWidgetsArea').find('[widgetId="' + widgetUniqueId + '"]').length;
},
widgetSelected: function (widget) {
$('#dashboardWidgetsArea').dashboard('addWidget', widget.uniqueId, 1, widget.parameters, true, false);
}
});
DashboardManagerControl.initElements = function () {
UIControl.initElements(this, '.dashboard-manager');
$('.top_controls .dashboard-manager').hide(); // initially hide the manager
};
exports.DashboardManagerControl = DashboardManagerControl;
}());
|