From 4008c8046aecc4f7a4e4112c3b75a61309e5e183 Mon Sep 17 00:00:00 2001 From: Ezra Obiwale Date: Fri, 20 May 2022 14:04:52 +0100 Subject: [PATCH 1/4] Custom default sort direction --- src/ColumnSortable/SortableLink.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index 986b0f2..f9aa5e0 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -13,6 +13,8 @@ class SortableLink { + private static ?string $defaultDirection; + /** * @param array $parameters * @@ -21,6 +23,8 @@ class SortableLink */ public static function render(array $parameters) { + self::$defaultDirection = null; + list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters); $title = self::applyFormatting($title, $sortColumn); @@ -53,6 +57,13 @@ public static function render(array $parameters) */ public static function parseParameters(array $parameters) { + if (Str::contains($parameters[0], ':')) { + $parts = explode(':', $parameters[0]); + + $parameters[0] = $parts[0]; + self::$defaultDirection = $parts[1]; + } + //TODO: let 2nd parameter be both title, or default query parameters //TODO: needs some checks before determining $title $explodeResult = self::explodeSortParameter($parameters[0]); @@ -137,7 +148,7 @@ private static function determineDirection($sortColumn, $sortParameter) return [$icon, $direction]; } else { $icon = config('columnsortable.sortable_icon'); - $direction = config('columnsortable.default_direction_unsorted', 'asc'); + $direction = self::$defaultDirection ?? config('columnsortable.default_direction_unsorted', 'asc'); return [$icon, $direction]; } From 978364090a574e6c74ae7399a88b4c740ceca270 Mon Sep 17 00:00:00 2001 From: Ezra Obiwale Date: Fri, 20 May 2022 14:12:43 +0100 Subject: [PATCH 2/4] Adds test --- tests/SortableLinkTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php index b30dfa1..baf206e 100644 --- a/tests/SortableLinkTest.php +++ b/tests/SortableLinkTest.php @@ -114,6 +114,14 @@ public function testCustomHrefAttribute() $this->assertSame('ColumnTitle ', $link); } + + + public function testCustomHrefWithCustomSortDirectionAttribute() + { + $link = SortableLink::render(['column:desc', 'ColumnTitle', ['a' => 'b'], ['c' => 'd', 'href' => 'http://localhost/custom-path']]); + + $this->assertSame('ColumnTitle ', $link); + } public function testParseParameters() From 374da5f71f47c5af788eece15bffa37548b95559 Mon Sep 17 00:00:00 2001 From: Ezra Obiwale Date: Wed, 6 Jul 2022 09:28:33 +0100 Subject: [PATCH 3/4] Uses query parameter for direction change --- src/ColumnSortable/SortableLink.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index f9aa5e0..22fb3b5 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -13,9 +13,7 @@ class SortableLink { - private static ?string $defaultDirection; - - /** + /** * @param array $parameters * * @return string @@ -23,8 +21,6 @@ class SortableLink */ public static function render(array $parameters) { - self::$defaultDirection = null; - list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters); $title = self::applyFormatting($title, $sortColumn); @@ -33,7 +29,7 @@ public static function render(array $parameters) request()->merge([$mergeTitleAs => $title]); } - list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter); + list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter, $queryParameters); $trailingTag = self::formTrailingTag($icon); @@ -57,13 +53,6 @@ public static function render(array $parameters) */ public static function parseParameters(array $parameters) { - if (Str::contains($parameters[0], ':')) { - $parts = explode(':', $parameters[0]); - - $parameters[0] = $parts[0]; - self::$defaultDirection = $parts[1]; - } - //TODO: let 2nd parameter be both title, or default query parameters //TODO: needs some checks before determining $title $explodeResult = self::explodeSortParameter($parameters[0]); @@ -133,10 +122,11 @@ private static function applyFormatting($title, $sortColumn) /** * @param $sortColumn * @param $sortParameter + * @param $queryParameters * * @return array */ - private static function determineDirection($sortColumn, $sortParameter) + private static function determineDirection($sortColumn, $sortParameter, $queryParameters) { $icon = self::selectIcon($sortColumn); @@ -148,7 +138,7 @@ private static function determineDirection($sortColumn, $sortParameter) return [$icon, $direction]; } else { $icon = config('columnsortable.sortable_icon'); - $direction = self::$defaultDirection ?? config('columnsortable.default_direction_unsorted', 'asc'); + $direction = $queryParameters['direction'] ?? config('columnsortable.default_direction_unsorted', 'asc'); return [$icon, $direction]; } From e070b51a8990dec4cd5b3e9dfd4fca58e056cf29 Mon Sep 17 00:00:00 2001 From: Ezra Obiwale Date: Wed, 6 Jul 2022 09:33:02 +0100 Subject: [PATCH 4/4] Updates test --- tests/SortableLinkTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SortableLinkTest.php b/tests/SortableLinkTest.php index baf206e..8eaec86 100644 --- a/tests/SortableLinkTest.php +++ b/tests/SortableLinkTest.php @@ -118,9 +118,9 @@ public function testCustomHrefAttribute() public function testCustomHrefWithCustomSortDirectionAttribute() { - $link = SortableLink::render(['column:desc', 'ColumnTitle', ['a' => 'b'], ['c' => 'd', 'href' => 'http://localhost/custom-path']]); + $link = SortableLink::render(['column', 'ColumnTitle', ['a' => 'b', 'direction' => 'desc'], ['c' => 'd', 'href' => 'http://localhost/custom-path']]); - $this->assertSame('ColumnTitle ', $link); + $this->assertSame('ColumnTitle ', $link); }