Skip to content

Commit

Permalink
refs #57 added possibility to place a widget multiple times (in case …
Browse files Browse the repository at this point in the history
…you want to see the same wdget with different visualizations and/or different parameters like different filters to make it easier comparable), removed some controls and save parameter changes in dashboard
  • Loading branch information
tsteur committed Mar 3, 2014
1 parent b647357 commit 8110e9d
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 86 deletions.
2 changes: 1 addition & 1 deletion plugins/Dashboard/javascripts/widgetMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ widgetsHelper.loadWidgetAjax = function (widgetUniqueId, widgetParameters, onWid
}

// delay widget preview a few millisconds
$('li:not(.' + settings.unavailableClass + ')', widgetList).on('mouseenter', function () {
$('li', widgetList).on('mouseenter', function () {
var that = this,
widgetUniqueId = $(this).attr('uniqueid');
clearTimeout(widgetPreview);
Expand Down
34 changes: 20 additions & 14 deletions plugins/Insights/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
*/
class API extends \Piwik\Plugin\API
{

public function getOverallMoversAndShakers($idSite, $period, $date)
const FILTER_BY_NEW = 'new';
const FILTER_BY_MOVERS = 'movers';
const FILTER_BY_DISAPPEARED = 'disappeared';
const ORDER_BY_RELATIVE = 'relative';
const ORDER_BY_ABSOLUTE = 'absolute';
const ORDER_BY_IMPORTANCE = 'importance';

public function getInsightsOverview($idSite, $period, $date)
{
/** @var DataTable[] $tables */
$tables = array(
Expand All @@ -35,6 +41,7 @@ public function getOverallMoversAndShakers($idSite, $period, $date)
);

// post event to add other reports?
// display new and disappeared only if very high impact

$map = new DataTable\Map();

Expand All @@ -48,8 +55,8 @@ public function getOverallMoversAndShakers($idSite, $period, $date)
// force $limitX and ignore minVisitsPercent, minGrowthPercent
public function getInsights(
$idSite, $period, $date, $reportUniqueId, $limitIncreaser = 5, $limitDecreaser = 5,
$basedOnTotalMetric = false, $minVisitsPercent = 2, $minGrowthPercent = 20,
$comparedToXPeriods = 1, $orderBy = 'absolute', $filterBy = '', $segment = false)
$filterBy = '', $basedOnTotalMetric = false, $minVisitsPercent = 2, $minGrowthPercent = 20,
$comparedToXPeriods = 1, $orderBy = 'absolute', $segment = false)
{
Piwik::checkUserHasViewAccess(array($idSite));

Expand All @@ -71,20 +78,19 @@ public function getInsights(
$considerDisappeared = false;

switch ($filterBy) {
case '':
case self::FILTER_BY_MOVERS:
$considerMovers = true;
break;
case self::FILTER_BY_NEW:
$considerNew = true;
break;
case self::FILTER_BY_DISAPPEARED:
$considerDisappeared = true;
break;
case 'movers':
default:
$considerMovers = true;
break;
case 'new':
$considerNew = true;
break;
case 'disappeared':
$considerDisappeared = true;
break;
}

$dataTable = new DataTable();
Expand Down Expand Up @@ -202,11 +208,11 @@ private function getTotalValue($idSite, $period, $date, $basedOnTotalMetric, Dat

private function getOrderByColumn($orderBy)
{
if ('relative' == $orderBy) {
if (self::ORDER_BY_RELATIVE == $orderBy) {
$orderByColumn = 'growth_percent_numeric';
} elseif ('absolute' == $orderBy) {
} elseif (self::ORDER_BY_ABSOLUTE == $orderBy) {
$orderByColumn = 'difference';
} elseif ('importance' == $orderBy) {
} elseif (self::ORDER_BY_IMPORTANCE == $orderBy) {
$orderByColumn = 'importance';
} else {
throw new \Exception('Unsupported orderBy');
Expand Down
4 changes: 2 additions & 2 deletions plugins/Insights/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class Controller extends \Piwik\Plugin\Controller
{

public function getOverviewMoversAndShakers()
public function getInsightOverview()
{
$idSite = Common::getRequestVar('idSite', null, 'int');
$period = Common::getRequestVar('period', null, 'string');
Expand All @@ -26,7 +26,7 @@ public function getOverviewMoversAndShakers()
$view = new View('@Insights/index.twig');
$this->setBasicVariablesView($view);

$view->moversAndShakers = API::getInstance()->getOverallMoversAndShakers($idSite, $period, $date);
$view->moversAndShakers = API::getInstance()->getInsightsOverview($idSite, $period, $date);
$view->showNoDataMessage = false;
$view->showInsightsControls = false;
$view->properties = array(
Expand Down
2 changes: 1 addition & 1 deletion plugins/Insights/Insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getAvailableVisualizations(&$visualizations)

public function addWidgets()
{
WidgetsList::add('Insights_Category', 'Insights_OverviewWidgetTitle', 'Insights', 'getOverviewMoversAndShakers');
WidgetsList::add('Insights_Category', 'Insights_OverviewWidgetTitle', 'Insights', 'getInsightOverview');
}

public function getJsFiles(&$jsFiles)
Expand Down
23 changes: 20 additions & 3 deletions plugins/Insights/Visualizations/Insight.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ public function beforeLoadDataTable()
$report = $this->requestConfig->apiMethodToRequestDataTable;
$report = str_replace('.', '_', $report);

if (!$this->requestConfig->filter_limit) {
$this->requestConfig->filter_limit = 25;
}

$limit = $this->requestConfig->filter_limit;
$limitDecrease = 0;
$limitIncrease = 0;

if ($this->requestConfig->limit_increaser && !$this->requestConfig->limit_decreaser) {
$limitIncrease = $limit;
} elseif (!$this->requestConfig->limit_increaser && $this->requestConfig->limit_decreaser) {
$limitDecrease = $limit;
} elseif ($this->requestConfig->limit_increaser && $this->requestConfig->limit_decreaser) {
$limitIncrease = round($limit / 2);
$limitDecrease = $limit - $limitIncrease;
}

$this->requestConfig->apiMethodToRequestDataTable = 'Insights.getInsights';
$this->requestConfig->request_parameters_to_modify = array(
'reportUniqueId' => $report,
Expand All @@ -42,8 +59,8 @@ public function beforeLoadDataTable()
'comparedToXPeriods' => $this->requestConfig->compared_to_x_periods_ago,
'orderBy' => $this->requestConfig->order_by,
'filterBy' => $this->requestConfig->filter_by,
'limitIncreaser' => $this->requestConfig->limit_increaser,
'limitDecreaser' => $this->requestConfig->limit_decreaser,
'limitIncreaser' => $limitIncrease,
'limitDecreaser' => $limitDecrease,
);
}

Expand Down Expand Up @@ -72,7 +89,7 @@ public function afterAllFiltersAreApplied()
public function beforeRender()
{
$this->config->datatable_js_type = 'InsightsDataTable';
$this->config->show_limit_control = false;
$this->config->show_limit_control = true;
$this->config->show_pagination_control = false;
$this->config->show_offset_information = false;
$this->config->show_search = false;
Expand Down
7 changes: 4 additions & 3 deletions plugins/Insights/Visualizations/Insight/RequestConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class RequestConfig extends VisualizationRequestConfig
public $compared_to_x_periods_ago = 1;
public $order_by = 'absolute';
public $filter_by = '';
public $limit_increaser = 5;
public $limit_decreaser = 5;
public $limit_increaser = '13';
public $limit_decreaser = '12';

public function __construct()
{
Expand All @@ -35,7 +35,8 @@ public function __construct()
'compared_to_x_periods_ago',
'filter_by',
'limit_increaser',
'limit_decreaser'
'limit_decreaser',
'filter_limit'
);

$this->addPropertiesThatShouldBeAvailableClientSide($properties);
Expand Down
54 changes: 38 additions & 16 deletions plugins/Insights/javascripts/insightsDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
DataTable = exports.DataTable,
dataTablePrototype = DataTable.prototype;

function getValueFromEvent(event)
{
return event.target.value ? event.target.value : $(event.target).attr('value');
}

/**
* UI control that handles extra functionality for Actions datatables.
*
Expand Down Expand Up @@ -38,62 +43,79 @@
this.initFilterBy(domElem);
},

_changeParameter: function (params) {

var widgetParams = {};

for (var index in params) {
if (params.hasOwnProperty(index)) {
this.param[index] = params[index];
widgetParams[index] = params[index];
}
}

this.notifyWidgetParametersChange(this.$element, widgetParams);
},

_changeParameterAndReload: function (params) {
this._changeParameter(params);
this.reloadAjaxDataTable(true);
},

initShowIncreaseOrDecrease: function (domElem) {
var self = this;
$('[name=showIncreaseOrDecrease]', domElem).bind('change', function (event) {
var value = event.target.value;
var value = getValueFromEvent(event);

self.param.limit_increaser = (value == 'both' || value == 'increase') ? '5' : '0';
self.param.limit_decreaser = (value == 'both' || value == 'decrease') ? '5' : '0';
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({
limit_increaser: (value == 'both' || value == 'increase') ? '5' : '0',
limit_decreaser: (value == 'both' || value == 'decrease') ? '5' : '0'
});
});
},

initMinGrowthPercentage: function (domElem) {
var self = this;
$('[name=minGrowthPercent]', domElem).bind('change', function (event) {
self.param.min_growth_percent = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({min_growth_percent: getValueFromEvent(event)});
});
},

initOrderBy: function (domElem) {
var self = this;
$('[name=orderBy]', domElem).bind('change', function (event) {
self.param.order_by = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({order_by: getValueFromEvent(event)});
});
$('th[name=orderBy]', domElem).bind('click', function (event) {
self._changeParameterAndReload({order_by: getValueFromEvent(event)});
});
},

initMinVisitsPercent: function (domElem) {
var self = this;
$('[name=minVisitsPercent]', domElem).bind('change', function (event) {
self.param.min_visits_percent = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({min_visits_percent: getValueFromEvent(event)});
});
},

initBasedOnTotalMetric: function (domElem) {
var self = this;
$('[name=basedOnTotalMetric]', domElem).bind('change', function (event) {
self.param.based_on_total_metric = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({based_on_total_metric: getValueFromEvent(event)});
});
},

initComparedToXPeriodsAgo: function (domElem) {
var self = this;
$('[name=comparedToXPeriodsAgo]', domElem).bind('change', function (event) {
self.param.compared_to_x_periods_ago = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({compared_to_x_periods_ago: getValueFromEvent(event)});
});
},

initFilterBy: function (domElem) {
var self = this;
$('[name=filterBy]', domElem).bind('change', function (event) {
self.param.filter_by = event.target.value;
self.reloadAjaxDataTable(true);
self._changeParameterAndReload({filter_by: getValueFromEvent(event)});
});
}
});
Expand Down
2 changes: 1 addition & 1 deletion plugins/Insights/lang/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Insights": {
"OverviewWidgetTitle": "Overview of movers and shakers",
"OverviewWidgetTitle": "Insights Overview",
"Category": "Insights"
}
}
Loading

0 comments on commit 8110e9d

Please sign in to comment.