Newer
Older
Benaka Moorthi
a validé
/**
* Piwik - free/libre analytics platform
Benaka Moorthi
a validé
*
Benaka Moorthi
a validé
* Series Picker control addition for DataTable visualizations.
Benaka Moorthi
a validé
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
diosmosis
a validé
(function ($, require) {
Benaka Moorthi
a validé
/**
* This class creates and manages the Series Picker for certain DataTable visualizations.
* To add the series picker to your DataTable visualization, create a SeriesPicker instance
* and after your visualization has been rendered, call the 'init' method.
* To customize SeriesPicker placement and behavior, you can bind callbacks to the following
* events before calling 'init':
* 'placeSeriesPicker': Triggered after the DOM element for the series picker link is created.
Benaka Moorthi
a validé
* You must use this event to add the link to the dataTable. YOu can also
* use this event to position the link however you want.
* Callback Signature: function () {}
* 'seriesPicked': Triggered when the user selects one or more columns/rows.
* Callback Signature: function (eventInfo, columns, rows) {}
* Events are triggered via jQuery, so you bind callbacks to them like this:
* var picker = new SeriesPicker(dataTable);
* $(picker).bind('placeSeriesPicker', function () {
* $(this.domElem).doSomething(...);
* });
* @param {dataTable} dataTable The dataTable instance to add a series picker to.
Benaka Moorthi
a validé
* @constructor
diosmosis
a validé
* @deprecated use the piwik-series-picker directive instead
Benaka Moorthi
a validé
*/
var SeriesPicker = function (dataTable) {
this.domElem = null;
Benaka Moorthi
a validé
this.dataTableId = dataTable.workingDivId;
Benaka Moorthi
a validé
// the columns that can be selected
this.selectableColumns = dataTable.props.selectable_columns;
// the rows that can be selected
this.selectableRows = dataTable.props.selectable_rows;
// render the picker?
this.show = !! dataTable.props.show_series_picker
&& (this.selectableColumns || this.selectableRows);
Benaka Moorthi
a validé
// can multiple rows we selected?
this.multiSelect = !! dataTable.props.allow_multi_select_series_picker;
};
SeriesPicker.prototype = {
/**
* Initializes the series picker by creating the element. Must be called when
Benaka Moorthi
a validé
* the datatable the picker is being attached to is ready for it to be drawn.
Benaka Moorthi
a validé
*/
Benaka Moorthi
a validé
if (!this.show) {
return;
}
Benaka Moorthi
a validé
var self = this;
diosmosis
a validé
var selectedColumns = this.selectableColumns
.filter(isItemDisplayed)
.map(function (columnConfig) {
return columnConfig.column;
});
var selectedRows = this.selectableRows
.filter(isItemDisplayed)
.map(function (rowConfig) {
return rowConfig.matcher;
});
Benaka Moorthi
a validé
// initialize dom element
diosmosis
a validé
var seriesPicker = '<piwik-series-picker'
+ ' multiselect="' + (this.multiSelect ? 'true' : 'false') + '"'
+ ' selectable-columns="selectableColumns"'
+ ' selectable-rows="selectableRows"'
+ ' selected-columns="selectedColumns"'
+ ' selected-rows="selectedRows"'
+ ' on-select="selectionChanged(columns, rows)"/>';
Benaka Moorthi
a validé
diosmosis
a validé
this.domElem = $(seriesPicker); // TODO: don't know if this will work without a root scope
Benaka Moorthi
a validé
diosmosis
a validé
$(this).trigger('placeSeriesPicker');
Benaka Moorthi
a validé
diosmosis
a validé
piwikHelper.compileAngularComponents(this.domElem, {
scope: {
selectableColumns: this.selectableColumns,
selectableRows: this.selectableRows,
selectedColumns: selectedColumns,
selectedRows: selectedRows,
selectionChanged: function selectionChanged(columns, rows) {
if (columns.length === 0 && rows.length === 0) {
return;
Benaka Moorthi
a validé
}
diosmosis
a validé
$(self).trigger('seriesPicked', [columns, rows]);
// inform dashboard widget about changed parameters (to be restored on reload)
var UI = require('piwik/UI');
var params = {
columns: columns,
columns_to_display: columns,
rows: rows,
rows_to_display: rows
};
var tableNode = $('#' + self.dataTableId);
diosmosis
a validé
UI.DataTable.prototype.notifyWidgetParametersChange(tableNode, params);
Benaka Moorthi
a validé
}
diosmosis
a validé
}
});
Benaka Moorthi
a validé
diosmosis
a validé
function isItemDisplayed(columnOrRowConfig) {
return columnOrRowConfig.displayed;
}
Benaka Moorthi
a validé
},
Benaka Moorthi
a validé
/**
* Returns the translation of a metric that can be selected.
Benaka Moorthi
a validé
* @param {String} metric The name of the metric, ie, 'nb_visits' or 'nb_actions'.
* @return {String} The metric translation. If one cannot be found, the metric itself
* is returned.
*/
getMetricTranslation: function (metric) {
diosmosis
a validé
for (var i = 0; i !== this.selectableColumns.length; ++i) {
if (this.selectableColumns[i].column === metric) {
Benaka Moorthi
a validé
return this.selectableColumns[i].translation;
}
}
return metric;
Benaka Moorthi
a validé
};
Benaka Moorthi
a validé
var exports = require('piwik/DataTableVisualizations/Widgets');
exports.SeriesPicker = SeriesPicker;
Benaka Moorthi
a validé
diosmosis
a validé
})(jQuery, require);