Skip to content

Commit c5b1cdf

Browse files
authored
Merge pull request #76 from kwahn20/sort-cms-docs
CMS Sorting: Add in ability to sort CMS list pages
2 parents e95e2ff + d9204bd commit c5b1cdf

3 files changed

Lines changed: 81 additions & 13 deletions

File tree

projects/lib/src/admin/components/page-list/page-list.component.html

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<!-- LIST VIEW -->
22
<div *ngIf="!selected" class="main-content">
33
<div class="d-flex align-items-center justify-content-start p-3">
4-
<div class="flex-grow-0 flex-shrink-1 pr-2">
5-
<button
6-
type="button"
7-
(click)="selectPage()"
8-
class="btn btn-outline-primary w-100"
9-
>
10-
New Page
4+
<div class="d-flex flex-wrap align-items-center justify-content-between my-3">
5+
<button type="button" (click)="selectPage()" class="btn btn-outline-primary">
6+
New Page
117
</button>
128
</div>
139
<!-- TODO: enable search/filter once it works better -->
@@ -40,12 +36,58 @@
4036
<table class="table table-hover page-list-table">
4137
<thead>
4238
<tr>
43-
<th>Title</th>
44-
<th>Created By</th>
45-
<th>Date Created</th>
46-
<th>Last Updated</th>
39+
<th (click)="changeSortStrategy('Title')">Title
40+
<fa-layers class="fa-fw">
41+
<fa-icon
42+
[icon]="faSortUp"
43+
[ngClass]="{ inactive: sortBy != '!Title' }"
44+
></fa-icon>
45+
<fa-icon
46+
[icon]="faSortDown"
47+
[ngClass]="{ inactive: sortBy != 'Title' }"
48+
></fa-icon>
49+
</fa-layers>
50+
</th>
51+
<th (click)="changeSortStrategy('Author')">Created By
52+
<fa-layers class="fa-fw">
53+
<fa-icon
54+
[icon]="faSortUp"
55+
[ngClass]="{ inactive: sortBy != '!Author' }"
56+
></fa-icon>
57+
<fa-icon
58+
[icon]="faSortDown"
59+
[ngClass]="{ inactive: sortBy != 'Author' }"
60+
></fa-icon>
61+
</fa-layers>
62+
</th>
63+
<th (click)="changeSortStrategy('DateCreated')">Date Created
64+
<fa-layers class="fa-fw">
65+
<fa-icon
66+
[icon]="faSortUp"
67+
[ngClass]="{ inactive: sortBy != 'DateCreated' }"
68+
></fa-icon>
69+
<fa-icon
70+
[icon]="faSortDown"
71+
[ngClass]="{ inactive: sortBy != '!DateCreated' }"
72+
73+
></fa-icon>
74+
</fa-layers>
75+
</th>
76+
<th (click)="changeSortStrategy('DateLastUpdated')">Last Updated
77+
<fa-layers class="fa-fw">
78+
<fa-icon
79+
[icon]="faSortUp"
80+
[ngClass]="{ inactive: sortBy != 'DateLastUpdated' }"
81+
></fa-icon>
82+
<fa-icon
83+
[icon]="faSortDown"
84+
[ngClass]="{ inactive: sortBy != '!DateLastUpdated' }"
85+
86+
></fa-icon>
87+
</fa-layers>
88+
</th>
4789
</tr>
48-
</thead>
90+
</thead>
4991
<tbody>
5092
<tr *ngFor="let contentDoc of list" (click)="selectPage(contentDoc)">
5193
<td>

projects/lib/src/admin/components/page-list/page-list.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@
4040
.table-responsive>.table>tbody>tr:first-of-type>td {
4141
border-top: 0;
4242
}
43+
44+
.inactive {
45+
color: lighten($color: #000000, $amount: 65);
46+
}

projects/lib/src/admin/components/page-list/page-list.component.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import {
1414
ListArgs,
1515
Asset,
1616
AssetUpload,
17-
} from '@ordercloud/cms-sdk'
17+
} from '@ordercloud/cms-sdk';
18+
import {
19+
faSortDown,
20+
faSortUp,
21+
} from '@fortawesome/free-solid-svg-icons';
1822
import { NgxSpinnerService } from 'ngx-spinner';
1923
import { Subject } from 'rxjs';
2024
import { debounceTime, distinctUntilChanged } from 'rxjs/internal/operators';
@@ -56,6 +60,9 @@ export class PageListComponent implements OnInit, OnChanges {
5660
loading = true;
5761
list: JDocument[];
5862
selected?: JDocument;
63+
sortBy: string;
64+
faSortUp = faSortUp;
65+
faSortDown = faSortDown;
5966

6067
constructor(private spinner: NgxSpinnerService) { }
6168

@@ -98,6 +105,20 @@ export class PageListComponent implements OnInit, OnChanges {
98105
changes[propertyName].previousValue !== changes[propertyName].currentValue;
99106
}
100107

108+
changeSortStrategy(sortBy: string): JDocument[] {
109+
if (this.sortBy && this.sortBy === sortBy) {
110+
sortBy = '!' + this.sortBy;
111+
}
112+
this.sortBy = sortBy;
113+
const compareFn = (a: JDocument, b: JDocument): 1 | -1 => {
114+
const sort = sortBy.includes('!')
115+
? a.Doc[sortBy.replace('!', '')] > b.Doc[sortBy.replace('!', '')]
116+
: a.Doc[sortBy] < b.Doc[sortBy];
117+
return sort ? 1 : -1;
118+
};
119+
return this.list.sort((a, b) => compareFn(a, b));
120+
}
121+
101122
listDocs(): Promise<void> {
102123
this.spinner.show();
103124
if (!this.resourceType || !this.resourceID) {
@@ -123,6 +144,7 @@ export class PageListComponent implements OnInit, OnChanges {
123144
...PAGE_SCHEMA,
124145
ID: this.pageSchemaID
125146
} as any;
147+
126148
return ContentManagementClient.Schemas.Create(schema).then(() =>
127149
this.listDocs()
128150
);

0 commit comments

Comments
 (0)