Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sparkline.component.js « sparkline « angularjs « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91b70bd3a03debf02f21fd1000aa6ea38f5ddb6e (plain)
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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

/**
 * Displays a sparkline. 'module', 'action' and 'date' are required elements of the
 * params attribute.
 *
 * Usage:
 * <piwik-sparkline params="{'module': 'API', 'action': 'get', 'date': '...'}"></piwik-sparkline>
 */
(function () {
    angular.module('piwikApp').component('piwikSparkline', {
        template: '<img />',
        bindings: {
            seriesIndices: '<',
            params: '<'
        },
        controller: SparklineController
    });

    SparklineController.$inject = ['$element', '$httpParamSerializer', 'piwikApi', 'piwik', 'piwikPeriods'];

    function SparklineController($element, $httpParamSerializer, piwikApi, piwik, piwikPeriods) {
        var vm = this;
        vm.$onChanges = $onChanges;

        function $onChanges() {
            // done manually due to 'random' query param. since it changes the URL on each digest, depending on angular
            // results in an infinite digest
            $element.find('img').attr('src', getSparklineUrl());
        }

        function getSparklineUrl() {
            var seriesIndices = vm.seriesIndices;
            var sparklineColors = piwik.getSparklineColors();

            if (seriesIndices) {
                sparklineColors.lineColor = sparklineColors.lineColor.filter(function (c, index) {
                    return seriesIndices.indexOf(index) !== -1;
                });
            }

            var colors = JSON.stringify(sparklineColors);

            var defaultParams = {
                forceView: '1',
                viewDataTable: 'sparkline',
                widget: $element.closest('[widgetId]').length ? '1' : '0',
                showtitle: '1',
                colors: colors,
                random: Date.now(),
                date: getDefaultDate()
            };

            var urlParams = piwikApi.mixinDefaultGetParams($element.extend(defaultParams, vm.params));

            // Append the token_auth to the URL if it was set (eg. embed dashboard)
            var token_auth = piwik.broadcast.getValueFromUrl("token_auth");
            if (token_auth.length && piwik.shouldPropagateTokenAuth) {
                urlParams.token_auth = token_auth;
            }

            return '?' + $httpParamSerializer(urlParams);
        }

        function getDefaultDate() {
            if (piwik.period === 'range') {
                return piwik.startDateString + ',' + piwik.endDateString;
            }

            var dateRange = piwikPeriods.get('range').getLastNRange(piwik.period, 30, piwik.currentDateString).getDateRange();

            var piwikMinDate = new Date(piwik.minDateYear, piwik.minDateMonth - 1, piwik.minDateDay);
            if (dateRange[0] < piwikMinDate) {
                dateRange[0] = piwikMinDate;
            }

            var startDateStr = piwikPeriods.format(dateRange[0]);
            var endDateStr = piwikPeriods.format(dateRange[1]);
            return startDateStr + ',' + endDateStr;
        }
    }
})();