Skip to content

Commit

Permalink
Allow sorting alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
arikfr committed Jun 28, 2017
1 parent e543e0c commit c8adf32
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
1 change: 1 addition & 0 deletions client/app/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export { default as rdTimeAgo } from './rd-time-ago';
export { default as overlay } from './overlay';
export { default as routeStatus } from './route-status';
export { default as filters } from './filters';
export { default as sortIcon } from './sort-icon';
26 changes: 26 additions & 0 deletions client/app/components/sort-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export default function (ngModule) {
ngModule.component('sortIcon', {
template: '<span ng-if="$ctrl.showIcon"><i class="fa fa-sort-{{$ctrl.icon}}"></i></span>',
bindings: {
column: '<',
sortColumn: '<',
reverse: '<',
},
controller() {
this.$onChanges = (changes) => {
['column', 'sortColumn', 'reverse'].forEach((v) => {
if (v in changes) {
this[v] = changes[v].currentValue;
}
});

this.showIcon = false;

if (this.column === this.sortColumn) {
this.showIcon = true;
this.icon = this.reverse ? 'desc' : 'asc';
}
};
},
});
}
12 changes: 6 additions & 6 deletions client/app/pages/alerts-list/alerts-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
</page-header>

<div class="container">
<div class="container bg-white">
<div class="bg-white">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>Name</th>
<th>Created By</th>
<th>State</th>
<th>Created At</th>
<th class="sortable-column" ng-click="$ctrl.alerts.orderBy('name')">Name <sort-icon column="'name'" sort-column="$ctrl.alerts.orderByField" reverse="$ctrl.alerts.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.alerts.orderBy('created_by')">Created By <sort-icon column="'created_by'" sort-column="$ctrl.alerts.orderByField" reverse="$ctrl.alerts.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.alerts.orderBy('state')">State <sort-icon column="'state'" sort-column="$ctrl.alerts.orderByField" reverse="$ctrl.alerts.orderByReverse"></sort-icon></th>
<th class="sortable-column" ng-click="$ctrl.alerts.orderBy('created_at')">Created By <sort-icon column="'created_at'" sort-column="$ctrl.alerts.orderByField" reverse="$ctrl.alerts.orderByReverse"></sort-icon></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in $ctrl.alerts.getPageRows()">
<td><a href="alerts/{{row.id}}">{{row.name}}</a></td>
<td>{{row.user.name}}</td>
<td>{{row.created_by}}</td>
<td><span ng-class="row.class">{{row.state | uppercase}}</span> since <span am-time-ago="row.updated_at"></span></td>
<td><span am-time-ago="row.created_at"></span></td>
</tr>
Expand Down
26 changes: 14 additions & 12 deletions client/app/pages/alerts-list/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { Paginator } from '../../utils';
import template from './alerts-list.html';

const stateClass = {
ok: 'label label-success',
triggered: 'label label-danger',
unknown: 'label label-warning',
};

class AlertsListCtrl {
constructor(Events, Alert) {
Events.record('view', 'page', 'alerts');

this.alerts = new Paginator([], { itemsPerPage: 20 });

Alert.query((alerts) => {
const stateClass = {
ok: 'label label-success',
triggered: 'label label-danger',
unknown: 'label label-warning',
};

alerts.forEach((alert) => {
alert.class = stateClass[alert.state];
});

this.alerts.updateRows(alerts);
this.alerts.updateRows(alerts.map(alert => ({
name: alert.name,
state: alert.state,
class: stateClass[alert.state],
created_by: alert.user.name,
created_at: alert.created_at,
updated_at: alert.updated_at,
})));
});
}
}
Expand Down
20 changes: 20 additions & 0 deletions client/app/utils/paginator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { sortBy } from 'underscore';

export default class Paginator {
constructor(rows, { page = 1, itemsPerPage = 20, totalCount = undefined } = {}) {
this.page = page;
this.itemsPerPage = itemsPerPage;
this.updateRows(rows, totalCount);
this.orderByField = undefined;
this.orderByReverse = false;
}

setPage(page) {
Expand All @@ -24,4 +28,20 @@ export default class Paginator {
this.totalCount = 0;
}
}

orderBy(column) {
if (column === this.orderByField) {
this.orderByReverse = !this.orderByReverse;
} else {
this.orderByField = column;
this.orderByReverse = false;
}

if (this.orderByField) {
this.rows = sortBy(this.rows, this.orderByField);
if (this.orderByReverse) {
this.rows = this.rows.reverse();
}
}
}
}

0 comments on commit c8adf32

Please sign in to comment.