File size: 15,095 Bytes
58ce155 | 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | /*!
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
(function ($) {
$.widget('piwik.dashboardWidget', {
/**
* Boolean indicating weather the widget is currently maximised
* @type {Boolean}
*/
isMaximised: false,
/**
* Unique Id of the widget
* @type {String}
*/
uniqueId: null,
/**
* Object holding the widget parameters
* @type {Object}
*/
widgetParameters: {},
/**
* Options available for initialization
*/
options: {
uniqueId: null,
isHidden: false,
onChange: null,
widgetParameters: {},
title: null,
onRemove: null,
onRefresh: null,
onMaximise: null,
onMinimise: null,
autoMaximiseVisualizations: ['tableAllColumns', 'tableGoals']
},
/**
* creates a widget object
*/
_create: function () {
if (!this.options.uniqueId) {
piwikHelper.error('widgets can\'t be created without an uniqueId');
return;
} else {
this.uniqueId = this.options.uniqueId;
}
if (this.options.widgetParameters) {
this.widgetParameters = this.options.widgetParameters;
}
this._createDashboardWidget(this.uniqueId);
var self = this;
this.element.on('setParameters.dashboardWidget', function (e, params) { self.setParameters(params); });
this.reload(true, true);
},
/**
* Cleanup some events and dialog
* Called automatically upon removing the widgets domNode
*/
destroy: function () {
if (this.isMaximised) {
$('[widgetId="' + this.uniqueId + '"]').dialog('destroy');
}
$('*', this.element).off('.dashboardWidget'); // unbind all events
$('.widgetContent', this.element).trigger('widget:destroy');
require('piwik/UI').UIControl.cleanupUnusedControls();
return this;
},
/**
* Returns the data currently set for the widget
* @return {object}
*/
getWidgetObject: function () {
return {
uniqueId: this.uniqueId,
parameters: this.widgetParameters,
isHidden: this.options.isHidden
};
},
/**
* Show the current widget in an ui.dialog
*/
maximise: function () {
this.isMaximised = true;
if (this.options.onMaximise) {
this.options.onMaximise(this.element);
} else {
this._maximiseImpl();
}
$('.widgetContent', this.element).trigger('widget:maximise');
return this;
},
/**
* Reloads the widgets content with the currently set parameters
*/
reload: function (hideLoading, notJQueryUI, overrideParams) {
if (!notJQueryUI) {
piwikHelper.log('widget.reload() was called by jquery.ui, ignoring', arguments.callee.caller);
return;
}
var self = this, currentWidget = this.element;
$('.widgetContent', currentWidget).trigger('widget:reload');
function onWidgetLoadedReplaceElementWithContent(loadedContent) {
var $widgetContent = $('.widgetContent', currentWidget);
$widgetContent.html(loadedContent);
/* move widget icons into datatable top actions
var $buttons = currentWidget.find('.buttons .button');
var $controls = currentWidget.find('.dataTableControls .dataTableAction').first();
if ($buttons.length && $controls.length) {
$buttons.find('.button').addClass('dataTableAction');
$buttons.insertBefore($controls);
}*/
if (currentWidget.parents('body').length) {
// there might be race conditions, eg widget might be just refreshed while whole dashboard is also
// removed from DOM
piwikHelper.compileVueEntryComponents($widgetContent);
}
$widgetContent.removeClass('loading');
$widgetContent.trigger('widget:create', [self]);
window.CoreHome.NotificationsStore.parseNotificationDivs();
}
// Reading segment from hash tag (standard case) or from the URL (when embedding dashboard)
['segment'].forEach(function (paramName) {
var value = broadcast.getValueFromHash(paramName) || broadcast.getValueFromUrl(paramName);
if (value.length) {
self.widgetParameters[paramName] = value;
}
});
['compareSegments', 'comparePeriods', 'compareDates'].forEach(function (paramName) {
var value = broadcast.getValueFromHash(paramName) || broadcast.getValueFromUrl(paramName);
if (value.length) {
self.widgetParameters[paramName] = value;
} else {
delete self.widgetParameters[paramName];
}
});
if (!hideLoading) {
$('.widgetContent', currentWidget).addClass('loading');
}
var params = $.extend(
this.widgetParameters,
overrideParams || {},
window.CoreHome.SearchFiltersPersistenceStore.getSearchFilters(this.uniqueId)
);
widgetsHelper.loadWidgetAjax(this.uniqueId, params, onWidgetLoadedReplaceElementWithContent, function (deferred, status) {
if (status == 'abort' || !deferred || deferred.status < 400 || deferred.status >= 600) {
return;
}
var errorMessage;
$('.widgetContent', currentWidget).removeClass('loading');
if (deferred.status === 429) {
errorMessage = `<div class="alert alert-danger">${_pk_translate('General_ErrorRateLimit')}>',
'</a>'])}</div>`;
if($('#loadingRateLimitError').html()) {
errorMessage = $('#loadingRateLimitError')
.html();
}
} else {
var errorMessage = _pk_translate('General_ErrorRequest', ['', '']);
if ($('#loadingError').html()) {
errorMessage = $('#loadingError').html();
}
}
$('.widgetContent', currentWidget).html('<div class="widgetLoadingError">' + errorMessage + '</div>');
});
return this;
},
/**
* Update widget parameters
*
* @param {object} parameters
*/
setParameters: function (parameters) {
if (!this.isMaximised
&& this.options.autoMaximiseVisualizations.indexOf(parameters.viewDataTable) !== -1
) {
this.maximise();
}
for (var name in parameters) {
this.widgetParameters[name] = parameters[name];
}
if (!this.isMaximised) {
this.options.onChange();
}
return this;
},
/**
* Get widget parameters
*
* @param {object} parameters
*/
getParameters: function () {
return $.extend({}, this.widgetParameters);
},
/**
* Creates the widget markup for the given uniqueId
*
* @param {String} uniqueId
*/
_createDashboardWidget: function (uniqueId) {
var self = this;
widgetsHelper.getWidgetNameFromUniqueId(uniqueId, function(widgetName) {
if (!widgetName) {
// when widget not found hide it.
$('[widgetId="' + uniqueId + '"]').hide();
widgetName = _pk_translate('Dashboard_WidgetNotFound');
}
var title = self.options.title === null ? $('<span/>').text(widgetName) : self.options.title;
var emptyWidgetContent = require('piwik/UI/Dashboard').WidgetFactory.make(uniqueId, title);
self.element.html(emptyWidgetContent);
var widgetElement = $('[id="' + uniqueId + '"]', self.element);
widgetElement
.on('mouseenter.dashboardWidget', function () {
if (!self.isMaximised) {
$(this).addClass('widgetHover');
$('.widgetTop', this).addClass('widgetTopHover');
}
})
.on('mouseleave.dashboardWidget', function () {
if (!self.isMaximised) {
$(this).removeClass('widgetHover');
$('.widgetTop', this).removeClass('widgetTopHover');
}
});
if (self.options.isHidden) {
$('.widgetContent', widgetElement).toggleClass('hidden').closest('.widget').toggleClass('hiddenContent');
}
$('.button#close', widgetElement)
.on('click.dashboardWidget', function (ev) {
piwikHelper.modalConfirm('#confirm', {yes: function () {
if (self.options.onRemove) {
self.options.onRemove(self.element);
} else {
self.element.remove();
self.options.onChange();
}
}});
});
$('.button#maximise', widgetElement)
.on('click.dashboardWidget', function (ev) {
if (self.options.onMaximise) {
self.options.onMaximise(self.element);
} else {
if ($('.widgetContent', $(this).parents('.widget')).hasClass('hidden')) {
self.showContent();
} else {
self.maximise();
}
}
});
$('.button#minimise', widgetElement)
.on('click.dashboardWidget', function (ev) {
if (self.options.onMinimise) {
self.options.onMinimise(self.element);
} else {
if (!self.isMaximised) {
self.hideContent();
} else {
self.element.dialog("close");
}
}
});
$('.button#refresh', widgetElement)
.on('click.dashboardWidget', function (ev) {
if (self.options.onRefresh) {
self.options.onRefresh(self.element);
} else {
self.reload(false, true);
}
});
});
},
/**
* Hide the widget content. Triggers the onChange event.
*/
hideContent: function () {
$('.widgetContent', this.element.find('.widget').addClass('hiddenContent')).addClass('hidden');
this.options.isHidden = true;
this.options.onChange();
},
/**
* Show the widget content. Triggers the onChange event.
*/
showContent: function () {
this.isMaximised = false;
this.options.isHidden = false;
this.element.find('.widget').removeClass('hiddenContent').find('.widgetContent').removeClass('hidden');
this.element.find('.widget').find('div.piwik-graph').trigger('resizeGraph');
this.options.onChange();
$('.widgetContent', this.element).trigger('widget:minimise');
},
/**
* Default maximise behavior. Will create a dialog that is 70% of the document's width,
* displaying the widget alone.
*/
_maximiseImpl: function () {
this.detachWidget();
var width = Math.floor($('body').width() * 0.7);
var self = this;
this.element.dialog({
title: '',
dialogClass: 'widgetoverlay',
modal: true,
width: width,
resizable: true,
autoOpen: true,
close: function (event, ui) {
self.isMaximised = false;
$('body').off('.dashboardWidget');
$(this).dialog("destroy");
$('[id="' + self.uniqueId + '-placeholder"]').replaceWith(this);
$(this).removeAttr('style');
self.options.onChange();
$(this).find('div.piwik-graph').trigger('resizeGraph');
$('.widgetContent', self.element).trigger('widget:minimise');
}
});
this.element.find('div.piwik-graph').trigger('resizeGraph');
// remove all previously shown tooltips as they might not be destroyed automatically
// see https://github.com/matomo-org/matomo/issues/17625
$('.ui-tooltip').remove();
var currentWidget = this.element;
$('body').on('click.dashboardWidget', function (ev) {
if (/ui-widget-overlay/.test(ev.target.className)) {
$(currentWidget).dialog("close");
}
});
},
/**
* Detaches the widget from the DOM and replaces it with a placeholder element.
* The placeholder element will have the save dimensions as the widget and will have
* the widgetPlaceholder CSS class.
*
* @return {jQuery} the detached widget
*/
detachWidget: function () {
this.element.before('<div id="' + this.uniqueId + '-placeholder" class="widgetPlaceholder widget"> </div>');
var placeholder = $('[id="' + self.uniqueId + '-placeholder"]');
$('#' + this.uniqueId + '-placeholder').height(this.element.height());
$('#' + this.uniqueId + '-placeholder').width(this.element.width() - 16);
return this.element.detach();
}
});
})(jQuery);
|