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

dataTable_manager.js « javascripts « CoreHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e9bf4256e9902259e7491a8fb73d705ea868b31 (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
/*!
 * Piwik - Web Analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

(function ($, require) {

    /**
     * The DataTableManager class manages the initialization of JS dataTable
     * instances. It's main purpose is to give each dataTable div a unique ID.
     * This is done in the browser since it's possible for any report to be
     * loaded via AJAX, some more than once.
     *
     * The singleton instance can be accessed via piwik.DataTableManager.
     */
    var DataTableManager = function () {
        this.nextId = 0;
    };

    DataTableManager.prototype = {

        /**
         * Gets the next available dataTable ID.
         *
         * @return {string}
         */
        getNextId: function () {
            this.nextId += 1;
            return 'dataTable_' + this.nextId;
        },

        /**
         * Gets the ID used for the last table or 0 if a DataTable hasn't been
         * initialized yet.
         */
        getLastId: function () {
            return 'dataTable_' + this.nextId;
        },

        /**
         * Initializes all uninitialized datatable elements. Uninitialized
         * datatable elements do not have an ID set.
         */
        initNewDataTables: function () {
            var self = this;

            // find each datatable that hasn't been initialized (has no id attribute),
            // and initialize it
            $('div.dataTable').each(function () {
                if (!$(this).attr('id')) {
                    var params = JSON.parse($(this).attr('data-params') || '{}');
                    var props = JSON.parse($(this).attr('data-props') || '{}');
                    var tableType = $(this).attr('data-table-type') || 'DataTable';

                    // convert values in params that are arrays to comma separated string lists
                    for (var key in params) {
                        if (params[key] instanceof Array) {
                            params[key] = params[key].join(',');
                        }
                    }
                    
                    var klass = require('piwik/UI')[tableType];
                    self.initSingleDataTable(this, klass, params, props);
                }
            });
        },

        /**
         * Initializes a single datatable element.
         *
         * @param {Element} domElem The DataTable div element.
         * @param {Function} klass The DataTable's JS class.
         * @param {Object} params The request params used.
         * @param {Object} props The view properties that should be visible to the JS.
         */
        initSingleDataTable: function (domElem, klass, params, props) {
            var newId = this.getNextId();

            $(domElem).attr('id', newId);

            var table = new klass($(domElem));
            table.param = params;
            table.props = props;
            table.init();
        },

        /**
         * Returns the first datatable div displaying a specific report.
         *
         * @param {string} report  The report, eg, UserSettings.getWideScreen
         * @return {Element} The datatable div displaying the report, or undefined if
         *                   it cannot be found.
         */
        getDataTableByReport: function (report) {
            var result = undefined;
            $('.dataTable').each(function () {
                if ($(this).attr('data-report') == report) {
                    result = this;
                    return false;
                }
            });
            return result;
        },

        /**
         * Returns the datatable instance of the first datatable div displaying
         * a specific report.
         *
         * @param {string} report  The report, eg, UserSettings.getWideScrren
         * @return {dataTable} The DataTable instance created for the element, if
         *                     the element can be found. undefined, if it can't be found.
         */
        getDataTableInstanceByReport: function (report) {
            var dataTableElement = this.getDataTableByReport(report);
            return dataTableElement ? $(dataTableElement).data('uiControlObject') : undefined;
        }
    };

    piwik.DataTableManager = new DataTableManager();

}(jQuery, require));