Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and test nothing is GET sticky globally #1671

Merged
merged 4 commits into from
Apr 8, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions demos/basic/breadcrumb.php
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@
$model = new Country($app->db);
$model->setLimit(15);

if ($id = $app->stickyGet('country_id')) {
if ($id = $crumb->stickyGet('country_id')) {
// perhaps we edit individual country?
$model = $model->load($id);
$crumb->addCrumb($model->name, []);
@@ -37,7 +37,7 @@
// display list of countries
$table = \Atk4\Ui\Table::addTo($app);
$table->setModel($model);
$table->addDecorator($model->fieldName()->name, [\Atk4\Ui\Table\Column\Link::class, [], ['country_id' => 'id']]);
$table->addDecorator($model->fieldName()->name, [\Atk4\Ui\Table\Column\Link::class, [], ['country_id' => $model->fieldName()->id]]);
}

$crumb->popTitle();
4 changes: 2 additions & 2 deletions demos/basic/message.php
Original file line number Diff line number Diff line change
@@ -17,8 +17,8 @@

$msg = \Atk4\Ui\Message::addTo($seg, [
'This is a title of your message',
'type' => $app->stickyGet('type'),
'icon' => $app->stickyGet('icon'),
'type' => $seg->stickyGet('type'),
'icon' => $seg->stickyGet('icon'),
]);
$msg->text->addParagraph('You can add some more text here for your messages');

2 changes: 1 addition & 1 deletion demos/collection/crud.php
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@

$crud->setModel($model);

$crud->addDecorator($model->title_field, [\Atk4\Ui\Table\Column\Link::class, ['test' => false, 'path' => 'interfaces/page'], ['_id' => 'id']]);
$crud->addDecorator($model->title_field, [\Atk4\Ui\Table\Column\Link::class, ['test' => false, 'path' => 'interfaces/page'], ['_id' => $model->fieldName()->id]]);

\Atk4\Ui\View::addTo($app, ['ui' => 'divider']);

2 changes: 1 addition & 1 deletion demos/collection/grid.php
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@
// Adding Quicksearch on Name field using auto query.
$grid->addQuickSearch([$model->fieldName()->name], true);

if ($app->stickyGet('no-ajax')) {
if ($grid->stickyGet('no-ajax')) {
$grid->quickSearch->useAjax = false;
}

2 changes: 1 addition & 1 deletion demos/init-app.php
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ public static function get_class(\Closure $createAnonymousClassFx): string
}

// allow custom layout override
$app->initLayout([$app->stickyGet('layout') ?? \Atk4\Ui\Layout\Maestro::class]);
$app->initLayout([!isset($_GET['layout']) ? \Atk4\Ui\Layout\Maestro::class : $app->stickyGet('layout')]);

$layout = $app->layout;
if ($layout instanceof \Atk4\Ui\Layout\NavigableInterface) {
4 changes: 2 additions & 2 deletions demos/interactive/loader.php
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
ViewTester::addTo($app);

// Example 1 - Basic usage of a Loader.
\Atk4\Ui\Loader::addTo($app)->set(function ($p) {
\Atk4\Ui\Loader::addTo($app)->set(function (\Atk4\Ui\Loader $p) {
// set your time expensive function here.
sleep(1);
\Atk4\Ui\Header::addTo($p, ['Loader #1']);
@@ -37,7 +37,7 @@
\Atk4\Ui\LoremIpsum::addTo(\Atk4\Ui\View::addTo($p, ['ui' => $_GET['color'] . ' segment']), ['size' => 1]);

// don't forget to make your own argument sticky so that Components can communicate with themselves:
$p->getApp()->stickyGet('color');
$p->stickyGet('color');
ViewTester::addTo($p);

// This loader takes 2s to load because it needs to go through 2 sleep statements.
2 changes: 1 addition & 1 deletion demos/layout/layout-panel.php
Original file line number Diff line number Diff line change
@@ -98,10 +98,10 @@

// PANEL_3

$countryId = $app->stickyGet('id');
Header::addTo($app, ['UserAction Friendly', 'size' => 4, 'subHeader' => 'Panel can run model action.']);

$panel3 = Right::addTo($app);
$countryId = $panel3->stickyGet('id');
$msg = Message::addTo($panel3, ['Run Country model action below.']);

$deck = View::addTo($app, ['ui' => 'cards']);
2 changes: 1 addition & 1 deletion demos/others/recursive.php
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ protected function init(): void

$buttons->on('click', '.button', new \Atk4\Ui\JsReload($this, [$this->name => (new \Atk4\Ui\Jquery())->data('id')]));

switch ($this->getApp()->stickyGet($this->name)) {
switch ($this->stickyGet($this->name)) {
case 'yellow':
self::addTo(\Atk4\Ui\View::addTo($this, ['ui' => 'yellow segment']));

4 changes: 4 additions & 0 deletions demos/others/sticky.php
Original file line number Diff line number Diff line change
@@ -63,3 +63,7 @@ protected function renderView(): void
Button::addTo($app, [$app->url(['other', 'b' => 2])]);
Button::addTo($app, [$app->url('http://yahoo.com/')]);
Button::addTo($app, [$app->url('http://yahoo.com/?q=abc')]);

// unset app/global sticky
$app->stickyForget('xx');
$app->stickyForget('c');
1 change: 0 additions & 1 deletion demos/tutorial/actions.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,6 @@
require_once __DIR__ . '/../init-app.php';

$wizard = \Atk4\Ui\Wizard::addTo($app);
$app->stickyGet($wizard->name);

$wizard->addStep('Define User Action', function ($page) {
\Atk4\Ui\Header::addTo($page, ['What are User Actions?']);
7 changes: 5 additions & 2 deletions js/src/plugins/js-search.plugin.js
Original file line number Diff line number Diff line change
@@ -192,8 +192,10 @@ export default class JsSearch extends atkPlugin {
* @param options
*/
doSearch(uri, query, options, cb = function () {}) {
const queryKey = this.settings.uri_query_key;

if (query) {
options = $.extend(options, { _q: query });
options = $.extend(options, { [queryKey]: query });
}

if (this.settings.useAjax) {
@@ -207,7 +209,7 @@ export default class JsSearch extends atkPlugin {
onComplete: cb,
});
} else {
uri = $.atkRemoveParam(uri, '_q');
uri = $.atkRemoveParam(uri, queryKey);
if (options.__atk_reload) {
delete options.__atk_reload;
}
@@ -220,6 +222,7 @@ export default class JsSearch extends atkPlugin {
JsSearch.DEFAULTS = {
uri: null,
uri_options: {},
uri_query_key: null,
q: null,
autoQuery: false,
timeOut: 300,
2 changes: 1 addition & 1 deletion public/atkjs-ui.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/CardDeck.php
Original file line number Diff line number Diff line change
@@ -121,7 +121,7 @@ protected function addMenuBar()
$right = View::addTo($this->menu, ['ui' => 'four wide column']);
$this->search = $right->add(Factory::factory($this->search, ['context' => '#' . $this->container->name]));
$this->search->reload = $this->container;
$this->query = $this->getApp()->stickyGet($this->search->queryArg);
$this->query = $this->stickyGet($this->search->queryArg);
}
}

2 changes: 1 addition & 1 deletion src/Crud.php
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ protected function init(): void
parent::init();

if ($sortBy = $this->getSortBy()) {
$this->issetApp() ? $this->getApp()->stickyGet($this->name . '_sort') : $this->stickyGet($this->name . '_sort', $sortBy);
$this->stickyGet($this->name . '_sort', $sortBy);
}
}

24 changes: 19 additions & 5 deletions src/Grid.php
Original file line number Diff line number Diff line change
@@ -108,10 +108,24 @@ protected function init(): void
if ($this->paginator !== false) {
$seg = View::addTo($this->container, [], ['Paginator'])->addStyle('text-align', 'center');
$this->paginator = $seg->add(Factory::factory([Paginator::class, 'reload' => $this->container], $this->paginator));
$this->issetApp() ? $this->getApp()->stickyGet($this->paginator->name) : $this->stickyGet($this->paginator->name);
$this->stickyGet($this->paginator->name);
}

$this->issetApp() ? $this->getApp()->stickyGet('_q') : $this->stickyGet('_q');
// TODO dirty way to set stickyGet - add addQuickSearch to find the expected search input component ID and then remove it
if ($this->menu !== false) {
$appUniqueHashesBackup = $this->getApp()->unique_hashes;
$menuElementNameCountsBackup = \Closure::bind(fn () => $this->_element_name_counts, $this->menu, AbstractView::class)();
try {
$menuRight = $this->menu->addMenuRight(); // @phpstan-ignore-line
$menuItemView = View::addTo($menuRight->addItem()->setElement('div'));
$quickSearch = JsSearch::addTo($menuItemView);
$this->stickyGet($quickSearch->name . '_q');
$this->menu->removeElement($menuRight->short_name);
} finally {
$this->getApp()->unique_hashes = $appUniqueHashesBackup;
\Closure::bind(fn () => $this->_element_name_counts = $menuElementNameCountsBackup, $this->menu, AbstractView::class)();
}
}
}

protected function initTable(): Table
@@ -314,16 +328,16 @@ public function addQuickSearch($fields = [], $hasAutoQuery = false)
$view = View::addTo($this->menu
->addMenuRight()->addItem()->setElement('div'));

$q = trim($this->stickyGet('_q') ?? '');
$this->quickSearch = JsSearch::addTo($view, ['reload' => $this->container, 'autoQuery' => $hasAutoQuery]);
$q = trim($this->stickyGet($this->quickSearch->name . '_q') ?? '');
if ($q !== '') {
$scope = Model\Scope::createOr();
foreach ($fields as $field) {
$scope->addCondition($field, 'like', '%' . $q . '%');
}
$this->model->addCondition($scope);
}

$this->quickSearch = JsSearch::addTo($view, ['reload' => $this->container, 'autoQuery' => $hasAutoQuery, 'initValue' => $q]);
$this->quickSearch->initValue = $q;
}

/**
1 change: 1 addition & 0 deletions src/JsSearch.php
Original file line number Diff line number Diff line change
@@ -87,6 +87,7 @@ protected function renderView(): void
$this->js(true)->atkJsSearch([
'uri' => $this->reload->jsUrl(),
'uri_options' => array_merge(['__atk_reload' => $this->reload->name], $this->args),
'uri_query_key' => $this->name . '_q',
'autoQuery' => $this->autoQuery,
'q' => $this->initValue,
'useAjax' => $this->useAjax,
4 changes: 2 additions & 2 deletions src/Table/Column/ActionButtons.php
Original file line number Diff line number Diff line change
@@ -90,8 +90,8 @@ public function addModal($button, $defaults, \Closure $callback, $owner = null,

$modal->observeChanges(); // adds scrollbar if needed

$modal->set(function ($t) use ($callback) {
$callback($t, $this->getApp()->stickyGet($this->name));
$modal->set(function (\Atk4\Ui\Modal $t) use ($callback) {
$callback($t, $t->stickyGet($this->name));
});

return $this->addButton($button, $modal->show(array_merge([$this->name => $this->getOwner()->jsRow()->data('id')], $args)));
4 changes: 1 addition & 3 deletions src/Table/Column/Link.php
Original file line number Diff line number Diff line change
@@ -152,9 +152,7 @@ public function getHtmlTags(Model $row, $field)
$key = $val;
}

if ($row->hasField($val)) {
$p[$key] = $row->get($val);
}
$p[$key] = $row->get($val);
}

return ['c_' . $this->short_name => $this->table->url($p)];
14 changes: 14 additions & 0 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
@@ -136,6 +136,18 @@ public function callExit(): void
return $app;
}

protected function assertNoGlobalSticky(App $app): void
{
$appSticky = array_diff_assoc(
\Closure::bind(fn () => $app->sticky_get_arguments, null, App::class)(),
['__atk_json' => false, '__atk_tab' => false, 'APP_CALL_EXIT' => true, 'APP_CATCH_EXCEPTIONS' => true]
);
if ($appSticky !== []) {
throw (new \Atk4\Ui\Exception('Global GET sticky must never be set by any component'))
->addMoreInfo('appSticky', $appSticky);
}
}

protected function getClient(): Client
{
$handler = function (RequestInterface $request) {
@@ -151,6 +163,8 @@ protected function getClient(): Client
if (!$app->run_called) {
$app->run();
}

$this->assertNoGlobalSticky($app);
} catch (\Throwable $e) {
// session_start() or ini_set() functions can be used only with native HTTP tests
// override test expectation here to finish there tests cleanly (TODO better to make the code testable without calling these functions)
33 changes: 0 additions & 33 deletions tools/get-assets.php

This file was deleted.