Skip to content

Commit 14515cc

Browse files
authored
Merge pull request #6468 from WoltLab/6.2-grid-view-default-sort-order
Don't include default sort order in URLs generated by grid views
2 parents acf8c72 + 435c483 commit 14515cc

50 files changed

Lines changed: 161 additions & 74 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

com.woltlab.wcf/templates/shared_gridView.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
'{unsafe:$view->getBaseUrl()|encodeJS}',
9393
'{unsafe:$view->getSortField()|encodeJS}',
9494
'{unsafe:$view->getSortOrder()|encodeJS}',
95+
'{unsafe:$view->getDefaultSortField()|encodeJS}',
96+
'{unsafe:$view->getDefaultSortOrder()|encodeJS}',
9597
'{unsafe:$view->getBulkInteractionProviderClassName()|encodeJS}',
9698
new Map([
9799
{foreach from=$view->getParameters() key='name' item='value'}

ts/WoltLabSuite/Core/Component/GridView.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export class GridView {
3232
baseUrl: string = "",
3333
sortField = "",
3434
sortOrder = "ASC",
35+
defaultSortField = "",
36+
defaultSortOrder = "ASC",
3537
bulkInteractionProviderClassName: string,
3638
gridViewParameters?: Map<string, string>,
3739
) {
@@ -42,7 +44,7 @@ export class GridView {
4244
this.#gridViewParameters = gridViewParameters;
4345

4446
this.#initInteractions();
45-
this.#state = this.#setupState(gridId, pageNo, baseUrl, sortField, sortOrder);
47+
this.#state = this.#setupState(gridId, pageNo, baseUrl, sortField, sortOrder, defaultSortField, defaultSortOrder);
4648
this.#initEventListeners();
4749
}
4850

@@ -117,8 +119,25 @@ export class GridView {
117119
});
118120
}
119121

120-
#setupState(gridId: string, pageNo: number, baseUrl: string, sortField: string, sortOrder: string): State {
121-
const state = new State(gridId, this.#table, pageNo, baseUrl, sortField, sortOrder);
122+
#setupState(
123+
gridId: string,
124+
pageNo: number,
125+
baseUrl: string,
126+
sortField: string,
127+
sortOrder: string,
128+
defaultSortField: string,
129+
defaultSortOrder: string,
130+
): State {
131+
const state = new State(
132+
gridId,
133+
this.#table,
134+
pageNo,
135+
baseUrl,
136+
sortField,
137+
sortOrder,
138+
defaultSortField,
139+
defaultSortOrder,
140+
);
122141
state.addEventListener("grid-view:change", (event) => {
123142
void this.#loadRows(event.detail.source);
124143
});

ts/WoltLabSuite/Core/Component/GridView/Sorting.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ export class Sorting extends EventTarget {
1515
#sortOrder: string;
1616
#table: HTMLTableElement;
1717

18-
constructor(table: HTMLTableElement, sortField: string, sortOrder: string) {
18+
constructor(
19+
table: HTMLTableElement,
20+
sortField: string,
21+
sortOrder: string,
22+
defaultSortField: string,
23+
defaultSortOrder: string,
24+
) {
1925
super();
2026

2127
this.#sortField = sortField;
22-
this.#defaultSortField = sortField;
28+
this.#defaultSortField = defaultSortField;
2329
this.#sortOrder = sortOrder;
24-
this.#defaultSortOrder = sortOrder;
30+
this.#defaultSortOrder = defaultSortOrder;
2531
this.#table = table;
2632

2733
this.#table
@@ -49,6 +55,14 @@ export class Sorting extends EventTarget {
4955
return [];
5056
}
5157

58+
if (this.#sortField === this.#defaultSortField) {
59+
if (this.#sortOrder !== this.#defaultSortOrder) {
60+
return [["sortOrder", this.#sortOrder]];
61+
} else {
62+
return [];
63+
}
64+
}
65+
5266
return [
5367
["sortField", this.#sortField],
5468
["sortOrder", this.#sortOrder],

ts/WoltLabSuite/Core/Component/GridView/State.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export class State extends EventTarget {
3434
baseUrl: string,
3535
sortField: string,
3636
sortOrder: string,
37+
defaultSortField: string,
38+
defaultSortOrder: string,
3739
) {
3840
super();
3941

@@ -52,7 +54,7 @@ export class State extends EventTarget {
5254
this.#switchPage(1, StateChangeCause.Change);
5355
});
5456

55-
this.#sorting = new Sorting(table, sortField, sortOrder);
57+
this.#sorting = new Sorting(table, sortField, sortOrder, defaultSortField, defaultSortOrder);
5658
this.#sorting.addEventListener("grid-view:change", () => {
5759
this.#switchPage(1, StateChangeCause.Change);
5860
});

wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView.js

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

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

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

wcfsetup/install/files/js/WoltLabSuite/Core/Component/GridView/State.js

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

wcfsetup/install/files/lib/system/gridView/AbstractGridView.class.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ abstract class AbstractGridView
5454
private GridViewRowLink $rowLink;
5555
private int $rowsPerPage = 20;
5656
private string $baseUrl = '';
57+
private string $defaultSortField = '';
58+
private string $defaultSortOrder = '';
5759
private string $sortField = '';
5860
private string $sortOrder = 'ASC';
5961
private int $pageNo = 1;
@@ -456,6 +458,44 @@ public function getBaseUrl(): string
456458
return $this->baseUrl;
457459
}
458460

461+
/**
462+
* Sets the default sort field of the grid view.
463+
*/
464+
public function setDefaultSortField(string $sortField): void
465+
{
466+
$this->defaultSortField = $sortField;
467+
$this->setSortField($sortField);
468+
}
469+
470+
/**
471+
* Sets the default sort order of the grid view.
472+
*/
473+
public function setDefaultSortOrder(string $sortOrder): void
474+
{
475+
if ($sortOrder !== 'ASC' && $sortOrder !== 'DESC') {
476+
throw new \InvalidArgumentException("Invalid value '{$sortOrder}' as default sort order given.");
477+
}
478+
479+
$this->defaultSortOrder = $sortOrder;
480+
$this->setSortOrder($sortOrder);
481+
}
482+
483+
/**
484+
* Returns the default sort field of the grid view.
485+
*/
486+
public function getDefaultSortField(): string
487+
{
488+
return $this->defaultSortField;
489+
}
490+
491+
/**
492+
* Returns the sort order of the grid view.
493+
*/
494+
public function getDefaultSortOrder(): string
495+
{
496+
return $this->defaultSortOrder;
497+
}
498+
459499
/**
460500
* Sets the sort field of the grid view.
461501
*/
@@ -648,6 +688,10 @@ protected function fireInitializedEvent(): void
648688
*/
649689
protected function validate(): void
650690
{
691+
if ($this->getDefaultSortField() === '') {
692+
throw new \InvalidArgumentException("Undefined default sort field.");
693+
}
694+
651695
if ($this->getSortField()) {
652696
if (!\in_array($this->getSortField(), \array_map(fn($column) => $column->getID(), $this->getSortableColumns()))) {
653697
if (\ENABLE_DEBUG_MODE) {

wcfsetup/install/files/lib/system/gridView/admin/ACPSessionGridView.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function __construct(private readonly int $sessionLogID)
6868
),
6969
]);
7070

71-
$this->setSortField('time');
72-
$this->setSortOrder('DESC');
71+
$this->setDefaultSortField('time');
72+
$this->setDefaultSortOrder('DESC');
7373
}
7474

7575
#[\Override]

wcfsetup/install/files/lib/system/gridView/admin/ACPSessionLogGridView.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public function __construct()
7575
]);
7676

7777
$this->addRowLink(new GridViewRowLink(ACPSessionLogPage::class));
78-
$this->setSortField('lastActivityTime');
79-
$this->setSortOrder('DESC');
78+
$this->setDefaultSortField('lastActivityTime');
79+
$this->setDefaultSortOrder('DESC');
8080
}
8181

8282
#[\Override]

0 commit comments

Comments
 (0)