Skip to content

Commit 0104488

Browse files
marforonivanbarlog
authored andcommitted
Added doNotChangeUrl option and made handler service public (#4)
* Added doNotChangeUrl option and public handler Added doNotChangeUrl option and public handler
1 parent 2d14162 commit 0104488

5 files changed

Lines changed: 33 additions & 2 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ The AjaxcomBundle will add all scripts within `javascripts` block to your page a
203203

204204
You can easily replace class in any DOM object you want by invoking `replaceClass()` with two arguments - first is CSS selector of your choice and second is class which you want to replace current one with.
205205

206+
### `doNotChangeUrl()`
207+
208+
You can easily avoid replacing url of the target page by invoking `doNotChangeUrl()`.
209+
206210
### Flash messages
207211

208212
The flash messages are automatically handled by Ajaxcom bundle. When the request is called via Ajax the flashes which are in the session bag are rendered automatically.

src/Controller/AjaxcomTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ protected function replaceClass(string $selector, string $class): self
5353

5454
return $this;
5555
}
56+
57+
protected function doNotChangeUrl(): self
58+
{
59+
$this->get('ajaxcom.handler')->doNotChangeUrl();
60+
61+
return $this;
62+
}
5663
}

src/Handler/ChangeUrl.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class ChangeUrl
2020
private $request;
2121
/** @var UrlGeneratorInterface */
2222
private $router;
23+
/** @var bool */
24+
private $changeUrl = true;
2325

2426
public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router)
2527
{
@@ -29,6 +31,10 @@ public function __construct(RequestStack $requestStack, UrlGeneratorInterface $r
2931

3032
public function handle(Handler $ajax): Handler
3133
{
34+
if (false === $this->changeUrl) {
35+
return $ajax;
36+
}
37+
3238
$ajax->changeUrl(
3339
$this->router->generate(
3440
$this->request->attributes->get('_route'),
@@ -38,4 +44,11 @@ public function handle(Handler $ajax): Handler
3844

3945
return $ajax;
4046
}
47+
48+
public function doNotChangeUrl(): self
49+
{
50+
$this->changeUrl = false;
51+
52+
return $this;
53+
}
4154
}

src/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717

1818
everlution.ajaxcom:
1919
class: Everlution\Ajaxcom\Handler
20-
public: false
20+
public: true
2121

2222
ajaxcom.render_block:
2323
class: Everlution\AjaxcomBundle\Service\RenderBlock

src/Service/Ajaxcom.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ public function addCallback(AjaxCallback $callback): self
116116
return $this;
117117
}
118118

119-
public function replaceClass(string $selector, string $class)
119+
public function replaceClass(string $selector, string $class): self
120120
{
121121
$this->replaceClass->add($selector, $class);
122122

123123
return $this;
124124
}
125+
126+
public function doNotChangeUrl(): self
127+
{
128+
$this->changeUrl->doNotChangeUrl();
129+
130+
return $this;
131+
}
125132
}

0 commit comments

Comments
 (0)