Newer
Older
/*!
* Piwik - Web Analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
(function ($) {
/**
* The DataTableManager class manages the initialization of JS dataTable
* instances. It's main purpose is to give each dataTable div a unique ID
* when it's possible that any report will 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.
*
* @param {Function} The DataTable's JS class.
*/
initNewDataTables: function (klass) {
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') || '{}');
// 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(',');
}
}
self.initSingleDataTable(this, klass, params);
}
});
},
/**
* 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.
*/
initSingleDataTable: function (domElem, klass, params) {
var newId = this.getNextId();
$(domElem).attr('id', newId);
var table = new klass();
$(domElem).data('dataTableInstance', table);
table.param = params;
table.init(newId);
// if the datatable has a graph, init the graph
var graphElement = $('.piwik-graph', domElem);
if (graphElement[0]) {
this.initJQPlotGraph(graphElement, newId);
}
},
/**
* Initializes and renders a JQPlot graph contained in a
* dataTable.
*
* @param {Element} graphElement The empty graph div element. Will
* usually have the .piwik-graph class.
* @param {String} dataTableId The ID of the containing datatable.
*/
initJQPlotGraph: function (graphElement, dataTableId) {
graphElement = $(graphElement);
// set a unique ID for the graph element
var graphId = dataTableId + 'Chart';
graphElement.attr('id', graphId);
var graphData;
try {
graphData = JSON.parse(graphElement.attr('data-data'));
} catch (e) {
console.error('JSON.parse Error: "' + e + "\" in:\n" + graphElement.attr('data-data'));
return;
}
var plot = new JQPlot(graphData, dataTableId);
// add external series toggle if it should be added
var externalSeriesToggle = graphElement.attr('data-external-series-toggle');
if (externalSeriesToggle) {
plot.addExternalSeriesToggle(
window[externalSeriesToggle], // get the function w/ string name
graphId,
graphElement.attr('data-external-series-show-all') == 1
);
}
// render the graph (setTimeout is required, otherwise the graph will not
// render initially)
setTimeout(function () {
plot.render(graphElement.attr('data-graph-type'), graphId, {
noData: _pk_translate('General_NoDataForGraph_js'),
exportTitle: _pk_translate('General_ExportAsImage_js'),
exportText: _pk_translate('General_SaveImageOnYourComputer_js'),
metricsToPlot: _pk_translate('General_MetricsToPlot_js'),
metricToPlot: _pk_translate('General_MetricToPlot_js'),
recordsToPlot: _pk_translate('General_RecordsToPlot_js')
});
}, 1);
},
/**
* 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 reportWithoutDot = report.replace('.', '');
var result = undefined;
$('.dataTable').each(function () {
if ($(this).attr('data-report') == reportWithoutDot) {
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('dataTableInstance') : undefined;
};
piwik.DataTableManager = new DataTableManager();
}(jQuery));