Skip to content

Commit

Permalink
Fix App::jsUrl() to always append "__atk_json=1" (#2248)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Feb 1, 2025
1 parent 48213c1 commit e008029
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 32 deletions.
2 changes: 1 addition & 1 deletion js/src/Service/accordionService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class AccordionService {

onOpening() {
if ($(this).data('url')) {
$(this).atkReloadView({ url: $(this).data('url'), urlOptions: { __atk_json: 1 } });
$(this).atkReloadView({ url: $(this).data('url') });
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions js/src/Service/modalService.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ class ModalService {
args = data.args;
}

// check for data type, usually JSON or HTML
if (data.type === 'json') {
args = $.extend(true, args, { __atk_json: 1 });
}

// does modal content need to be loaded dynamically
if (data.url) {
$modal.data('closeOnLoadingError', true);
Expand Down
12 changes: 1 addition & 11 deletions public/js/atkjs-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -1987,10 +1987,7 @@ class AccordionService {
onOpening() {
if (external_jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).data('url')) {
external_jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).atkReloadView({
url: external_jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).data('url'),
urlOptions: {
__atk_json: 1
}
url: external_jquery__WEBPACK_IMPORTED_MODULE_0___default()(this).data('url')
});
}
}
Expand Down Expand Up @@ -2689,13 +2686,6 @@ class ModalService {
args = data.args;
}

// check for data type, usually JSON or HTML
if (data.type === 'json') {
args = external_jquery__WEBPACK_IMPORTED_MODULE_5___default().extend(true, args, {
__atk_json: 1
});
}

// does modal content need to be loaded dynamically
if (data.url) {
$modal.data('closeOnLoadingError', true);
Expand Down
2 changes: 1 addition & 1 deletion public/js/atkjs-ui.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/atkjs-ui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/atkjs-ui.min.js.map

Large diffs are not rendered by default.

35 changes: 27 additions & 8 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,22 @@ public function stickyForget(string $name): void
unset($this->stickyGetArguments[$name]);
}

/**
* @return array<0|string, string>
*/
private function explodeUrlPage(string $page): array
{
$pageExploded = explode('?', $page, 2);

$pagePath = $pageExploded[0] !== ''
? $pageExploded[0]
: null;

parse_str($pageExploded[1] ?? '', $pageArgs);

return [$pagePath] + $pageArgs;
}

/**
* Build a URL that application can use for loading HTML data.
*
Expand All @@ -787,14 +803,12 @@ public function stickyForget(string $name): void
public function url($page = [], array $extraRequestUrlArgs = []): string
{
if (is_string($page)) {
$pageExploded = explode('?', $page, 2);
parse_str($pageExploded[1] ?? '', $page);
$pagePath = $pageExploded[0] !== '' ? $pageExploded[0] : null;
} else {
$pagePath = $page[0] ?? null;
unset($page[0]);
$page = $this->explodeUrlPage($page);
}

$pagePath = $page[0] ?? null;
unset($page[0]);

$request = $this->getRequest();

if ($pagePath === null) {
Expand Down Expand Up @@ -842,8 +856,13 @@ public function url($page = [], array $extraRequestUrlArgs = []): string
*/
public function jsUrl($page = [], array $extraRequestUrlArgs = []): string
{
// append to the end but allow override
$extraRequestUrlArgs = array_merge($extraRequestUrlArgs, ['__atk_json' => 1], $extraRequestUrlArgs);
if (is_string($page)) {
$page = $this->explodeUrlPage($page);
}

if (($page['__atk_json'] ?? null) !== false) {
$page['__atk_json'] = 1;
}

return $this->url($page, $extraRequestUrlArgs);
}
Expand Down
4 changes: 3 additions & 1 deletion src/JsSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ protected function renderView(): void
$this->template->set('ButtonRemoveIcon', $this->buttonRemoveIcon);

$this->js(true)->atkJsSearch([
'url' => $this->reload->jsUrl(),
'url' => $this->useAjax
? $this->reload->jsUrl()
: $this->reload->url(),
'urlOptions' => array_merge(['__atk_reload' => $this->reload->name], $this->args),
'urlQueryKey' => $this->name . '_q',
'autoQuery' => $this->autoQuery,
Expand Down
2 changes: 1 addition & 1 deletion src/Tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function addTab($name, ?\Closure $callback = null, array $settings = [])
// if there is callback action, then use VirtualPage
if ($callback) {
$vp = VirtualPage::addTo($sub, ['ui' => '']);
$item->setUrl($vp->getJsUrl('cut'));
$item->setUrl($vp->getUrl('cut'));

$vp->set($callback);
}
Expand Down
2 changes: 1 addition & 1 deletion src/VirtualPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function getHtml(): string
}

// render and terminate
if ($this->getApp()->hasRequestQueryParam('__atk_json')) {
if ($this->getApp()->isJsUrlRequest()) {
$this->getApp()->terminateJson($this);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public static function provideUrlCases(): iterable
yield ['/?v=V', ['v' => false], ['x'], [], 'x.php'];
yield ['/', ['v' => false], ['x', 'v' => 'page'], [], 'x.php?v=page'];
yield ['/', ['v' => false], ['x'], ['v' => 'extra'], 'x.php'];
yield ['/', ['__atk_json' => false], ['x'], [], 'x.php'];

// /wo page path
yield ['/x', [], [], [], '/x.php'];
Expand Down Expand Up @@ -202,7 +203,7 @@ public function testUrl(string $requestUrl, array $appStickyGetArguments, array
self::assertSame($expectedUrl, $app->url($page, $extraRequestUrlArgs));
$pageAssocOnly = array_diff_key($page, [true]);
self::assertSame($expectedUrl, $app->url(($page[0] ?? '') . (count($pageAssocOnly) > 0 ? '?' . implode('&', array_map(static fn ($k) => $k . '=' . $pageAssocOnly[$k], array_keys($pageAssocOnly))) : ''), $extraRequestUrlArgs));
self::assertSame($expectedUrl, $app->jsUrl($page, array_merge(['__atk_json' => null], $extraRequestUrlArgs)));
self::assertSame($expectedUrl . (str_contains($expectedUrl, '?') ? '&' : '?') . '__atk_json=1', $app->jsUrl($page, $extraRequestUrlArgs));

$makeExpectedUrlFx = static function (string $indexPage, string $ext) use ($page, $expectedUrl) {
return preg_replace_callback('~^[^?]*?\K([^/?]*)(\.php)(?=\?|$)~', static function ($matches) use ($page, $indexPage, $ext) {
Expand Down

0 comments on commit e008029

Please sign in to comment.