| |
| |
| |
| |
| |
| |
|
|
| (function ($) { |
|
|
| $.widget('piwik.dashboardWidget', { |
|
|
| |
| |
| |
| |
| isMaximised: false, |
| |
| |
| |
| |
| uniqueId: null, |
| |
| |
| |
| |
| widgetParameters: {}, |
|
|
| |
| |
| |
| options: { |
| uniqueId: null, |
| isHidden: false, |
| onChange: null, |
| widgetParameters: {}, |
| title: null, |
| onRemove: null, |
| onRefresh: null, |
| onMaximise: null, |
| onMinimise: null, |
| autoMaximiseVisualizations: ['tableAllColumns', 'tableGoals'] |
| }, |
|
|
| |
| |
| |
| _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); |
| }, |
|
|
| |
| |
| |
| |
| destroy: function () { |
| if (this.isMaximised) { |
| $('[widgetId="' + this.uniqueId + '"]').dialog('destroy'); |
| } |
| $('*', this.element).off('.dashboardWidget'); |
| $('.widgetContent', this.element).trigger('widget:destroy'); |
| require('piwik/UI').UIControl.cleanupUnusedControls(); |
| return this; |
| }, |
|
|
| |
| |
| |
| |
| getWidgetObject: function () { |
| return { |
| uniqueId: this.uniqueId, |
| parameters: this.widgetParameters, |
| isHidden: this.options.isHidden |
| }; |
| }, |
|
|
| |
| |
| |
| 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; |
| }, |
|
|
| |
| |
| |
| 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); |
|
|
| |
| |
| |
| |
| |
| |
| |
|
|
| if (currentWidget.parents('body').length) { |
| |
| |
| piwikHelper.compileVueEntryComponents($widgetContent); |
| } |
| $widgetContent.removeClass('loading'); |
| $widgetContent.trigger('widget:create', [self]); |
|
|
| window.CoreHome.NotificationsStore.parseNotificationDivs(); |
| } |
|
|
| |
| ['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; |
| }, |
|
|
| |
| |
| |
| |
| |
| 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; |
| }, |
|
|
| |
| |
| |
| |
| |
| getParameters: function () { |
| return $.extend({}, this.widgetParameters); |
| }, |
|
|
| |
| |
| |
| |
| |
| _createDashboardWidget: function (uniqueId) { |
|
|
| var self = this; |
|
|
| widgetsHelper.getWidgetNameFromUniqueId(uniqueId, function(widgetName) { |
| if (!widgetName) { |
| |
| $('[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); |
| } |
| }); |
| }); |
| }, |
|
|
| |
| |
| |
| hideContent: function () { |
| $('.widgetContent', this.element.find('.widget').addClass('hiddenContent')).addClass('hidden'); |
| this.options.isHidden = true; |
| this.options.onChange(); |
| }, |
|
|
| |
| |
| |
| 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'); |
| }, |
|
|
| |
| |
| |
| |
| _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'); |
| |
| |
| $('.ui-tooltip').remove(); |
|
|
| var currentWidget = this.element; |
| $('body').on('click.dashboardWidget', function (ev) { |
| if (/ui-widget-overlay/.test(ev.target.className)) { |
| $(currentWidget).dialog("close"); |
| } |
| }); |
| }, |
|
|
| |
| |
| |
| |
| |
| |
| |
| 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); |
|
|