forked from patternfly/angular-patternfly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-component.js
More file actions
99 lines (81 loc) · 2.4 KB
/
Copy pathsort-component.js
File metadata and controls
99 lines (81 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
angular.module('patternfly.sort').component('pfSort', {
bindings: {
config: '='
},
templateUrl: 'sort/sort.html',
controller: function () {
'use strict';
var ctrl = this;
var prevConfig;
ctrl.$onInit = function () {
if (angular.isDefined(ctrl.config) && angular.isUndefined(ctrl.config.show)) {
// default to true
ctrl.config.show = true;
}
angular.extend(ctrl, {
selectField: selectField,
changeDirection: changeDirection,
getSortIconClass: getSortIconClass
});
};
ctrl.$onChanges = function () {
setupConfig();
};
ctrl.$doCheck = function () {
// do a deep compare on config
if (!angular.equals(ctrl.config, prevConfig)) {
setupConfig();
}
};
function setupConfig () {
var updated = false;
prevConfig = angular.copy(ctrl.config);
if (ctrl.config.fields === undefined) {
ctrl.config.fields = [];
}
if (ctrl.config.fields.length > 0) {
if (ctrl.config.currentField === undefined) {
ctrl.config.currentField = ctrl.config.fields[0];
updated = true;
}
if (ctrl.config.isAscending === undefined) {
ctrl.config.isAscending = true;
updated = true;
}
}
if (updated === true && ctrl.config.onSortChange) {
ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
}
}
function selectField (evt, field) {
evt.preventDefault();
ctrl.config.currentField = field;
if (ctrl.config.onSortChange) {
ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
}
}
function changeDirection () {
ctrl.config.isAscending = !ctrl.config.isAscending;
if (ctrl.config.onSortChange) {
ctrl.config.onSortChange(ctrl.config.currentField, ctrl.config.isAscending);
}
}
function getSortIconClass () {
var iconClass;
if (ctrl.config.currentField.sortType === 'numeric') {
if (ctrl.config.isAscending) {
iconClass = 'fa fa-sort-numeric-asc';
} else {
iconClass = 'fa fa-sort-numeric-desc';
}
} else {
if (ctrl.config.isAscending) {
iconClass = 'fa fa-sort-alpha-asc';
} else {
iconClass = 'fa fa-sort-alpha-desc';
}
}
return iconClass;
}
}
});