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

model.js « manage « angularjs « CustomDimensions « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f2318e1dfc13269a794c6435ad7d37e1117e6cd8 (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
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
/*!
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
(function () {
    angular.module('piwikApp').factory('customDimensionsModel', customDimensionsModel);

    customDimensionsModel.$inject = ['piwikApi', '$q'];

    function customDimensionsModel(piwikApi, $q) {
        var fetchAllPromise;

        var model = {
            customDimensions : [],
            availableScopes: [],
            extractionDimensions: [],
            isLoading: false,
            isUpdating: false,
            fetchCustomDimensionsConfiguration: fetchCustomDimensionsConfiguration,
            findCustomDimension: findCustomDimension,
            createOrUpdateDimension: createOrUpdateDimension,
            reload: reload
        };

        return model;

        function reload()
        {
            model.customDimensions = [];
            model.availableScopes = [];
            model.extractionDimensions = [];
            fetchAllPromise = null;
            fetchCustomDimensionsConfiguration();
        }

        function fetchCustomDimensionsConfiguration() {
            if (fetchAllPromise) {
                return fetchAllPromise;
            }

            model.isLoading = true;

            var deferred = $q.defer();
            // .fetch does not return a proper promise
            piwikApi.fetch({method: 'CustomDimensions.getConfiguredCustomDimensions', filter_limit: '-1'}).then(function (customDimensions) {
                model.customDimensions = customDimensions;
                deferred.resolve(customDimensions);
            });

            fetchAllPromise = $q.all([deferred.promise, fetchAvailableScopes(), fetchAvailableExtractionDimensions()]).then(function () {
                model.isLoading = false;

                return model.customDimensions;
            });

            return fetchAllPromise;
        }

        function fetchAvailableExtractionDimensions() {
            var deferred = $q.defer();
            // .fetch does not return a proper promise
            piwikApi.fetch({method: 'CustomDimensions.getAvailableExtractionDimensions', filter_limit: '-1'}).then(function (availableExtractionDimensions) {

                model.extractionDimensions = [];
                angular.forEach(availableExtractionDimensions, function (value) {
                    model.extractionDimensions.push({key: value.value, value: value.name});
                });
                deferred.resolve(availableExtractionDimensions);
            });

            return deferred.promise;
        }

        function fetchAvailableScopes() {
            var deferred = $q.defer();

            // .fetch does not return a proper promise
            piwikApi.fetch({method: 'CustomDimensions.getAvailableScopes', filter_limit: '-1'}).then(function (availableScopes) {
                model.availableScopes = availableScopes;
                deferred.resolve(availableScopes);
            });

            return deferred.promise;
        }

        function findCustomDimension(customDimensionId) {
            return fetchCustomDimensionsConfiguration().then(function (customDimensions) {
                var found;
                angular.forEach(customDimensions, function (dimension) {
                    if (parseInt(dimension.idcustomdimension, 10) === customDimensionId) {
                        found = dimension;
                    }
                });

                return found;
            });
        }

        function createOrUpdateDimension(dimension, method) {
            dimension = angular.copy(dimension);
            dimension.active = dimension.active ? '1' : '0';
            dimension.method = method;
            var extractions = dimension.extractions;
            delete dimension.extractions;

            dimension.caseSensitive = dimension.case_sensitive ? '1' : '0';
            delete dimension.case_sensitive;

            model.isUpdating = true;

            return piwikApi.post(dimension, {extractions: extractions}).then(function (response) {
                model.isUpdating = false;

                return {type: 'success'};

            }, function (error) {
                model.isUpdating = false;
                return {type: 'error', message: error};
            });
        }
    }
})();