Skip to content

Commit

Permalink
refactor(header.html): split template into directives
Browse files Browse the repository at this point in the history
New directives and controllers created:
* `ngTableFilterRow` directive paired with `ngTableFilterRowController`
* `ngTableSorterRow` directive paired with `ngTableSorterRowController`

BREAKING CHANGE:

The sortBy function previously declared by `ngTableController` has been moved to the new controller
- `ngTableSorterRowController`.
  • Loading branch information
ccrowhurstram committed Jul 25, 2015
1 parent e563ecd commit 47460d6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 42 deletions.
8 changes: 8 additions & 0 deletions src/ng-table/filterRow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<tr ng-show="show_filter" class="ng-table-filters">
<th data-title-text="{{$column.titleAlt(this) || $column.title(this)}}" ng-repeat="$column in $columns" ng-if="$column.show(this)" class="filter">
<div ng-repeat="(name, filter) in $column.filter(this)">
<div ng-if="filter.indexOf('/') !== -1" ng-include="filter"></div>
<div ng-if="filter.indexOf('/') === -1" ng-include="'ng-table/filters/' + filter + '.html'"></div>
</div>
</th>
</tr>
28 changes: 2 additions & 26 deletions src/ng-table/header.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,2 @@
<tr>
<th title="{{$column.headerTitle(this)}}"
ng-repeat="$column in $columns"
ng-class="{
'sortable': $column.sortable(this),
'sort-asc': params.sorting()[$column.sortable(this)]=='asc',
'sort-desc': params.sorting()[$column.sortable(this)]=='desc'
}"
ng-click="sortBy($column, $event)"
ng-if="$column.show(this)"
ng-init="template = $column.headerTemplateURL(this)"
class="header {{$column.class(this)}}">
<div ng-if="!template" class="ng-table-header" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'div'}">
<span ng-bind="$column.title(this)" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'span'}"></span>
</div>
<div ng-if="template" ng-include="template"></div>
</th>
</tr>
<tr ng-show="show_filter" class="ng-table-filters">
<th data-title-text="{{$column.titleAlt(this) || $column.title(this)}}" ng-repeat="$column in $columns" ng-if="$column.show(this)" class="filter">
<div ng-repeat="(name, filter) in $column.filter(this)">
<div ng-if="filter.indexOf('/') !== -1" ng-include="filter"></div>
<div ng-if="filter.indexOf('/') === -1" ng-include="'ng-table/filters/' + filter + '.html'"></div>
</div>
</th>
</tr>
<ng-table-sorter-row></ng-table-sorter-row>
<ng-table-filter-row></ng-table-filter-row>
18 changes: 18 additions & 0 deletions src/ng-table/sorterRow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<tr>
<th title="{{$column.headerTitle(this)}}"
ng-repeat="$column in $columns"
ng-class="{
'sortable': $column.sortable(this),
'sort-asc': params.sorting()[$column.sortable(this)]=='asc',
'sort-desc': params.sorting()[$column.sortable(this)]=='desc'
}"
ng-click="sortBy($column, $event)"
ng-if="$column.show(this)"
ng-init="template = $column.headerTemplateURL(this)"
class="header {{$column.class(this)}}">
<div ng-if="!template" class="ng-table-header" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'div'}">
<span ng-bind="$column.title(this)" ng-class="{'sort-indicator': params.settings().sortingIndicator == 'span'}"></span>
</div>
<div ng-if="template" ng-include="template"></div>
</th>
</tr>
61 changes: 45 additions & 16 deletions src/scripts/04-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
}

$scope.$watch('params.$params', function(newParams, oldParams) {

// We don't want to watch for changes to $params whilst the NgTableParams.reload function is executing
// (ie $loading === true).
// This is important for cases where you have a want to *chain* a subsequent call to reload.
Expand Down Expand Up @@ -185,21 +185,6 @@ function($scope, NgTableParams, $timeout, $parse, $compile, $attrs, $element, ng
});
}
};

$scope.sortBy = function($column, event) {
var parsedSortable = $column.sortable && $column.sortable();
if (!parsedSortable) {
return;
}
var defaultSort = $scope.params.settings().defaultSort;
var inverseSort = (defaultSort === 'asc' ? 'desc' : 'asc');
var sorting = $scope.params.sorting() && $scope.params.sorting()[parsedSortable] && ($scope.params.sorting()[parsedSortable] === defaultSort);
var sortingParams = (event.ctrlKey || event.metaKey) ? $scope.params.sorting() : {};
sortingParams[parsedSortable] = (sorting ? inverseSort : defaultSort);
$scope.params.parameters({
sorting: sortingParams
});
};
}]);


Expand Down Expand Up @@ -270,3 +255,47 @@ app.factory('ngTableColumn', [function () {
buildColumn: buildColumn
};
}]);

(function(){
'use strict';

angular.module('ngTable')
.controller('ngTableSorterRowController', ngTableSorterRowController);

ngTableSorterRowController.$inject = ['$scope'];

function ngTableSorterRowController($scope){

$scope.sortBy = sortBy;

///////////

function sortBy($column, event) {
var parsedSortable = $column.sortable && $column.sortable();
if (!parsedSortable) {
return;
}
var defaultSort = $scope.params.settings().defaultSort;
var inverseSort = (defaultSort === 'asc' ? 'desc' : 'asc');
var sorting = $scope.params.sorting() && $scope.params.sorting()[parsedSortable] && ($scope.params.sorting()[parsedSortable] === defaultSort);
var sortingParams = (event.ctrlKey || event.metaKey) ? $scope.params.sorting() : {};
sortingParams[parsedSortable] = (sorting ? inverseSort : defaultSort);
$scope.params.parameters({
sorting: sortingParams
});
}
}
})();

(function(){
'use strict';

angular.module('ngTable')
.controller('ngTableFilterRowController', ngTableFilterRowController);

ngTableFilterRowController.$inject = [];

function ngTableFilterRowController(){

}
})();
40 changes: 40 additions & 0 deletions src/scripts/05-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,43 @@ app.directive('ngTableDynamic', ['$parse', function ($parse){
}
};
}]);

(function(){
'use strict';

angular.module('ngTable')
.directive('ngTableFilterRow', ngTableFilterRow);

ngTableFilterRow.$inject = [];

function ngTableFilterRow(){
var directive = {
restrict: 'E',
replace: true,
templateUrl: 'ng-table/filterRow.html',
scope: true,
controller: 'ngTableFilterRowController'
};
return directive;
}
})();

(function(){
'use strict';

angular.module('ngTable')
.directive('ngTableSorterRow', ngTableSorterRow);

ngTableSorterRow.$inject = [];

function ngTableSorterRow(){
var directive = {
restrict: 'E',
replace: true,
templateUrl: 'ng-table/sorterRow.html',
scope: true,
controller: 'ngTableSorterRowController'
};
return directive;
}
})();

0 comments on commit 47460d6

Please sign in to comment.