Skip to content

Commit a42cbbe

Browse files
committed
Add per-field default sort order to list view sort fields
`ListViewSortField` now accepts a `defaultSortOrder` argument (ASC or DESC). When the user switches to a different sort field via the sorting dropdown, the field's default sort order is applied instead of always defaulting to ASC.
1 parent b2f4575 commit a42cbbe

6 files changed

Lines changed: 25 additions & 20 deletions

File tree

com.woltlab.wcf/templates/shared_listView.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<ul class="dropdownMenu" id="{$view->getID()}_sorting">
5151
{foreach from=$view->getAvailableSortFields() item='sortField'}
5252
<li>
53-
<button type="button" class="listView__sorting__button" data-sort-id="{$sortField->id}">
53+
<button type="button" class="listView__sorting__button" data-sort-id="{$sortField->id}" data-default-sort-order="{$sortField->defaultSortOrder}">
5454
{unsafe:$sortField}
5555
</button>
5656
</li>

ts/WoltLabSuite/Core/Component/ListView/Sorting.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Sorting extends EventTarget {
3232

3333
this.#dropdownMenu?.querySelectorAll<HTMLElement>("[data-sort-id]").forEach((element) => {
3434
element.addEventListener("click", () => {
35-
this.#sort(element.dataset.sortId!);
35+
this.#sort(element.dataset.sortId!, element.dataset.defaultSortOrder ?? "ASC");
3636
});
3737
});
3838

@@ -79,12 +79,12 @@ export class Sorting extends EventTarget {
7979
});
8080
}
8181

82-
#sort(sortField: string): void {
83-
if (this.#sortField == sortField && this.#sortOrder == "ASC") {
84-
this.#sortOrder = "DESC";
82+
#sort(sortField: string, defaultSortOrder: string): void {
83+
if (this.#sortField === sortField) {
84+
this.#sortOrder = this.#sortOrder === "ASC" ? "DESC" : "ASC";
8585
} else {
8686
this.#sortField = sortField;
87-
this.#sortOrder = "ASC";
87+
this.#sortOrder = defaultSortOrder;
8888
}
8989

9090
this.#renderActiveSorting();

wcfsetup/install/files/js/WoltLabSuite/Core/Component/ListView/Sorting.js

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wcfsetup/install/files/lib/system/listView/ListViewSortField.class.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ final class ListViewSortField implements \Stringable
1717
public function __construct(
1818
public readonly string $id,
1919
public readonly string $languageItem,
20-
public readonly string $sortByDatabaseColumn = ''
21-
) {}
20+
public readonly string $sortByDatabaseColumn = '',
21+
public readonly string $defaultSortOrder = 'ASC',
22+
) {
23+
if ($this->defaultSortOrder !== 'ASC' && $this->defaultSortOrder !== 'DESC') {
24+
throw new \InvalidArgumentException("Invalid value '{$this->defaultSortOrder}' as default sort order given.");
25+
}
26+
}
2227

2328
#[\Override]
2429
public function __toString(): string

wcfsetup/install/files/lib/system/listView/user/ArticleListView.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ class ArticleListView extends AbstractListView
3535
public function __construct()
3636
{
3737
$this->addAvailableSortFields([
38-
new ListViewSortField('time', 'wcf.global.date'),
38+
new ListViewSortField('time', 'wcf.global.date', defaultSortOrder: 'DESC'),
3939
new ListViewSortField('title', 'wcf.global.title', 'title'),
40-
new ListViewSortField('views', 'wcf.article.sortField.views'),
40+
new ListViewSortField('views', 'wcf.article.sortField.views', defaultSortOrder: 'DESC'),
4141
]);
4242
if (\MODULE_LIKE) {
4343
$this->addAvailableSortFields([
44-
new ListViewSortField('cumulativeLikes', 'wcf.like.cumulativeLikes'),
44+
new ListViewSortField('cumulativeLikes', 'wcf.like.cumulativeLikes', defaultSortOrder: 'DESC'),
4545
]);
4646
}
4747
$this->addAvailableFilters([

wcfsetup/install/files/lib/system/listView/user/MembersListView.class.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ public function __construct()
3333
{
3434
$this->addAvailableSortFields([
3535
new ListViewSortField('username', 'wcf.user.sortField.username'),
36-
new ListViewSortField('registrationDate', 'wcf.user.sortField.registrationDate'),
37-
new ListViewSortField('activityPoints', 'wcf.user.sortField.activityPoints'),
38-
new ListViewSortField('likesReceived', 'wcf.user.sortField.likesReceived'),
39-
new ListViewSortField('lastActivityTime', 'wcf.user.sortField.lastActivityTime'),
36+
new ListViewSortField('registrationDate', 'wcf.user.sortField.registrationDate', defaultSortOrder: 'DESC'),
37+
new ListViewSortField('activityPoints', 'wcf.user.sortField.activityPoints', defaultSortOrder: 'DESC'),
38+
new ListViewSortField('likesReceived', 'wcf.user.sortField.likesReceived', defaultSortOrder: 'DESC'),
39+
new ListViewSortField('lastActivityTime', 'wcf.user.sortField.lastActivityTime', defaultSortOrder: 'DESC'),
4040
]);
4141

4242
$this->addAvailableFilters([

0 commit comments

Comments
 (0)