Skip to content

Add pfSimpleSort directive with unit tests and ng-docs #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ module.exports = function (grunt) {
src: ['filters/**/*.html'],
dest: 'templates/filters.js'
},
'patternfly.sort': {
cwd: 'src/',
src: ['sort/**/*.html'],
dest: 'templates/sort.js'
},
'patternfly.views': {
cwd: 'src/',
src: ['views/**/*.html'],
Expand Down
244 changes: 244 additions & 0 deletions dist/angular-patternfly.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,20 @@ angular.module('patternfly', [
'patternfly.form',
'patternfly.notification',
'patternfly.select',
'patternfly.sort',
'patternfly.utils',
'patternfly.validation',
'patternfly.views'
]);

;/**
* @name patternfly card
*
* @description
* Sort module for patternfly.
*
*/
angular.module('patternfly.sort', []);
;
angular.module( 'patternfly.utils', [] );
;/**
Expand Down Expand Up @@ -2469,6 +2478,233 @@ angular.module('patternfly.select', []).directive('pfSelect', ["$timeout", funct
}
};
}]);
;/**
* @ngdoc directive
* @name patternfly.sort.directive:pfSimpleSort
*
* @description
* Directive for a simple sort component
* <br><br>
*
* @param {object} config configuration settings for the sort:<br/>
* <ul style='list-style-type: none'>
* <li>.fields - (Array) List of sortable fields containing:
* <ul style='list-style-type: none'>
* <li>.id - (String) Unique Id for the sort field
* <li>.title - (String) The title to display for the sort field
* <li>.sortType - (String) The sort type, 'alpha' or 'numeric'
* </ul>
* <li>.sortId - (Object) Id of the current sort field
* <li>.isAscending - (boolean) Current sort direction is ascending. True for ascending, False for descending
* <li>.onSortChange - ( function(sortId, sortDirection ) Function to call when the current sort params change
* </ul>
*
* @example
<example module="patternfly.sort">
<file name="index.html">
<div ng-controller="ViewCtrl" class="row example-container">
<div class="col-md-12">
<div pf-simple-sort id="exampleSimpleSort" config="sortConfig"></div>
</div>
<hr class="col-md-12">
<div class="col-md-12">
<label class="events-label">Items: </label>
</div>
<div class="col-md-12">
<div ng-repeat="item in items" class="col-md-12 cfme-row-column">
<div class="row">
<div class="col-md-3">
<span>{{item.name}}</span>
</div>
<div class="col-md-3">
<span>{{item.count}}</span>
</div>
<div class="col-md-3">
<span>{{item.description}}</span>
</div>
</div>
</div>
</div>
</div>
</file>

<file name="script.js">
angular.module('patternfly.sort').controller('ViewCtrl', ['$scope',
function ($scope) {
$scope.items = [
{
name: "Item 7",
count: 432,
description: 'Very nice item'
},
{
name: "Item 6",
count: 22,
description: 'It lasts forever'
},
{
name: "Item 3",
count: 632,
description: 'Good stuff cheap'
},
{
name: "Item 2",
count: 12,
description: 'Fantastic'
},
{
name: "Item 9",
count: 99,
description: 'It does alright'
},
{
name: "Item 4",
count: 442,
description: 'Horrible'
},
{
name: "Item 1",
count: 42,
description: 'Most excellent'
},
{
name: "Item 8",
count: 2,
description: 'Get it while it lasts'
},
{
name: "Item 5",
count: 321,
description: 'Beautiful style'
}
];

var compareFn = function(item1, item2) {
var compValue = 0;
if ($scope.sortConfig.currentField.id === 'name') {
compValue = item1.name.localeCompare(item2.name);
} else if ($scope.sortConfig.currentField.id === 'count') {
compValue = item1.count - item2.count;
} else if ($scope.sortConfig.currentField.id === 'description') {
compValue = item1.description.localeCompare(item2.description);
}

if (!$scope.sortConfig.isAscending) {
compValue = compValue * -1;
}

return compValue;
};

var sortChange = function (sortId, isAscending) {
$scope.items.sort(compareFn);
};

$scope.sortConfig = {
fields: [
{
id: 'name',
title: 'Name',
sortType: 'alpha'
},
{
id: 'count',
title: 'Count',
sortType: 'numeric'
},
{
id: 'description',
title: 'Description',
sortType: 'alpha'
}
],
onSortChange: sortChange
};
}
]);
</file>
</example>
*/
angular.module('patternfly.sort').directive('pfSimpleSort',
function () {
'use strict';
return {
restrict: 'A',
scope: {
config: '='
},
templateUrl: 'sort/simple-sort.html',
controller: ["$scope", function ($scope) {
$scope.setupConfig = function () {
var updated = false;

if ($scope.config.fields === undefined) {
$scope.config.fields = [];
}

if ($scope.config.fields.length > 0) {
if ($scope.config.currentField === undefined) {
$scope.config.currentField = $scope.config.fields[0];
updated = true;
}
if ($scope.config.isAscending === undefined) {
$scope.config.isAscending = true;
updated = true;
}
}

if (updated === true && $scope.config.onSortChange) {
$scope.config.onSortChange($scope.config.currentField, $scope.config.isAscending);
}
};

$scope.selectField = function (field) {
$scope.config.currentField = field;

if ($scope.config.onSortChange) {
$scope.config.onSortChange($scope.config.currentField, $scope.config.isAscending);
}
};

$scope.changeDirection = function () {
$scope.config.isAscending = !$scope.config.isAscending;

if ($scope.config.onSortChange) {
$scope.config.onSortChange($scope.config.currentField, $scope.config.currentDirection);
}
};

$scope.getSortIconClass = function () {
var iconClass;

if ($scope.config.currentField.sortType === 'numeric') {
if ($scope.config.isAscending) {
iconClass = 'fa fa-sort-numeric-asc';
} else {
iconClass = 'fa fa-sort-numeric-desc';
}
} else {
if ($scope.config.isAscending) {
iconClass = 'fa fa-sort-alpha-asc';
} else {
iconClass = 'fa fa-sort-alpha-desc';
}
}

return iconClass;
};

$scope.setupConfig();
}],

link: function (scope, element, attrs) {
scope.$watch('config', function () {
scope.setupConfig();
}, true);
}
};
}
);
;
/**
* @ngdoc directive
Expand Down Expand Up @@ -3793,6 +4029,14 @@ angular.module('patternfly.views').directive('pfDataToolbar',
"<div class=\"alert alert-{{pfNotificationType}}\"><button ng-show=pfNotificationPersistent type=button class=close ng-click=$parent.notifications.remove($index)><span aria-hidden=true>&times;</span><span class=sr-only>Close</span></button> <span class=\"pficon pficon-ok\" ng-show=\"pfNotificationType === 'success'\"></span> <span class=\"pficon pficon-info\" ng-show=\"pfNotificationType === 'info'\"></span> <span class=\"pficon pficon-error-circle-o\" ng-show=\"pfNotificationType === 'danger'\"></span> <span class=\"pficon pficon-warning-triangle-o\" ng-show=\"pfNotificationType === 'warning'\"></span> <strong>{{pfNotificationHeader}}</strong> {{pfNotificationMessage}}</div>"
);

}]);
;angular.module('patternfly.sort').run(['$templateCache', function($templateCache) {
'use strict';

$templateCache.put('sort/simple-sort.html',
"<div class=simple-sort><form><div class=form-group><div class=input-group><div dropdown class=input-group-btn><button dropdown-toggle type=button class=\"btn btn-default dropdown-toggle sort-fields\" data-toggle=dropdown aria-haspopup=true aria-expanded=false>{{config.currentField.title}} <span class=caret></span></button><ul class=dropdown-menu><li ng-repeat=\"item in config.fields\" ng-class=\"{'selected': item === config.currentField}\"><a class=sort-field role=menuitem tabindex=-1 ng-click=selectField(item)>{{item.title}}</a></li></ul></div><a><i class=sort-direction ng-class=getSortIconClass() ng-click=changeDirection()></i></a></div></div></form></div>"
);

}]);
;angular.module('patternfly.views').run(['$templateCache', function($templateCache) {
'use strict';
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-patternfly.min.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions dist/styles/angular-patternfly.css
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,24 @@
background-image: none;
color: #999999;
}

.simple-sort .sort-direction {
font-size: 22px;
color: #222222;
margin-left: 5px;
border: none;
padding: 2px;
}
.simple-sort .sort-direction:hover {
color: #0099d3;
}

.simple-sort a {
cursor: pointer;
}

.input-group .input-group-btn .dropdown-menu > .selected > a {
background-color: #0099d3 !important;
border-color: #0076b7 !important;
color: #fff !important;
}
2 changes: 1 addition & 1 deletion dist/styles/angular-patternfly.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions misc/demo.css
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ hr {
height: 90px;
width: 300px;
}

.dropdown-menu > li > a:hover {
background-image: none;
}
1 change: 1 addition & 0 deletions src/patternfly.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ angular.module('patternfly', [
'patternfly.form',
'patternfly.notification',
'patternfly.select',
'patternfly.sort',
'patternfly.utils',
'patternfly.validation',
'patternfly.views'
Expand Down
Loading