Skip to content
Extraits de code Groupes Projets
dashboardWidget.js 9,72 ko
Newer Older
  • Learn to ignore specific revisions
  • /*!
     * Piwik - Web Analytics
     *
     * @link http://piwik.org
     * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
     */
    
    
        $.widget('piwik.dashboardWidget', {
    
    
            /**
             * Boolean indicating wether the widget is currently maximised
    
             * @type {Boolean}
    
             * @type {String}
    
            /**
             * Object holding the widget parameters
    
             * @type {Object}
    
            /**
             * Options available for initialization
             */
    
            options: {
                uniqueId: null,
                isHidden: false,
                onChange: null,
    
                title: null,
                onRemove: null
    
            },
    
            /**
             * creates a widget object
             */
    
    Gregor Aisch's avatar
    Gregor Aisch a validé
                    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
    
            getWidgetObject: function () {
    
                return {
                    uniqueId: this.uniqueId,
                    parameters: this.widgetParameters,
                    isHidden: this.options.isHidden
                };
            },
    
            /**
             * Show the current widget in an ui.dialog
             */
    
                this.isMaximised = true;
    
    
                this.element.before('<div id="' + this.uniqueId + '-placeholder" class="widgetPlaceholder widget"> </div>');
                $('#' + this.uniqueId + '-placeholder').height(this.element.height());
                $('#' + this.uniqueId + '-placeholder').width(this.element.width() - 16);
    
                var width = Math.floor($('body').width() * 0.7);
    
                var self = this;
    
                this.element.dialog({
                    title: '',
                    modal: true,
    
                    width: width,
    
                    position: ['center', 'center'],
                    resizable: true,
                    autoOpen: true,
    
                        self.isMaximised = false;
                        $('body').off('.dashboardWidget');
                        $(this).dialog("destroy");
    
                        $('#' + 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');
    
                var currentWidget = this.element;
    
                $('body').on('click.dashboardWidget', function (ev) {
    
                    if (/ui-widget-overlay/.test(ev.target.className)) {
    
                        $(currentWidget).dialog("close");
                    }
                });
    
                $('.widgetContent', currentWidget).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);
    
    Gregor Aisch's avatar
    Gregor Aisch a validé
    
    
                var self = this, currentWidget = this.element;
    
    
                function onWidgetLoadedReplaceElementWithContent(loadedContent) {
    
                    $('.widgetContent', currentWidget).html(loadedContent);
    
                    $('.widgetContent', currentWidget).removeClass('loading');
    
                    $('.widgetContent', currentWidget).trigger('widget:create', [self]);
    
                // Reading segment from hash tag (standard case) or from the URL (when embedding dashboard)
    
    mattpiwik's avatar
    mattpiwik a validé
                var segment = broadcast.getValueFromHash('segment') || broadcast.getValueFromUrl('segment');
    
                    this.widgetParameters.segment = segment;
                }
    
    
                if (!hideLoading) {
                    $('.widgetContent', currentWidget).addClass('loading');
                }
    
                var params = $.extend(this.widgetParameters, overrideParams || {});
                widgetsHelper.loadWidgetAjax(this.uniqueId, params, onWidgetLoadedReplaceElementWithContent);
    
    
                return this;
            },
    
            /**
             * Update widget parameters
    
            setParameters: function (parameters) {
    
    
                if (!this.isMaximised && (parameters.viewDataTable == 'tableAllColumns' || parameters.viewDataTable == 'tableGoals')) {
                    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 this.widgetParameters;
            },
    
    
            /**
             * Creaates the widget markup for the given uniqueId
    
            _createDashboardWidget: function (uniqueId) {
    
    
                var widgetName = widgetsHelper.getWidgetNameFromUniqueId(uniqueId);
    
                    widgetName = _pk_translate('Dashboard_WidgetNotFound');
    
                var title = this.options.title === null ? $('<span/>').text(widgetName) : this.options.title;
                var emptyWidgetContent = require('piwik/UI/Dashboard').WidgetFactory.make(uniqueId, title);
    
                this.element.html(emptyWidgetContent);
    
    
                var widgetElement = $('#' + uniqueId, this.element);
    
                var self = this;
                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');
                        }
                    });
    
                    $('.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 ($('.widgetContent', $(this).parents('.widget')).hasClass('hidden')) {
    
                            self.isMaximised = false;
                            self.options.isHidden = false;
    
                            $(this).closest('.widget').removeClass('hiddenContent').find('.widgetContent').removeClass('hidden');
                            $(this).closest('.widget').find('div.piwik-graph').trigger('resizeGraph');
    
                            self.options.onChange();
    
                            $('.widgetContent', widgetElement).trigger('widget:minimise');
    
                        } else {
                            self.maximise();
                        }
                    });
    
                $('.button#minimise', widgetElement)
    
                    .on('click.dashboardWidget', function (ev) {
                        if (!self.isMaximised) {
    
                            $('.widgetContent', $(this).closest('.widget').addClass('hiddenContent')).addClass('hidden');
    
                            self.options.isHidden = true;
                            self.options.onChange();
                        } else {
                            self.element.dialog("close");
                        }
                    });
    
    
                $('.button#refresh', widgetElement)
    
                    .on('click.dashboardWidget', function (ev) {
    
                        self.reload(false, true);
    
                widgetElement.show();
            }
    
    Gregor Aisch's avatar
    Gregor Aisch a validé