Skip to content

Feature/Executor refactor#1550

Merged
mvorisek merged 71 commits intodevelopfrom
feature/Executor-refactor
Mar 25, 2021
Merged

Feature/Executor refactor#1550
mvorisek merged 71 commits intodevelopfrom
feature/Executor-refactor

Conversation

@ibelar
Copy link
Copy Markdown
Contributor

@ibelar ibelar commented Nov 30, 2020

Remove all dependency to atk4\Data\UserAction::ui property

Fixes #1548

Use of an ExecutorFactory class to properly wired UI element from its model action.

  • Factory method to create action executor based on action definition;

  • Enable registration of specific Ui executor for certain model UserAction
    $app->getExecutorFactory()->registerExecutor($country->getUserAction('confirm'), [ConfirmationExecutor::class]);

  • Allow overriding UI element responsible to trigger the action for a specific component like Crud or Grid

$app->getExecutorFactory()->registerTrigger(
    $app->getExecutorFactory()::TABLE_BUTTON,
    [Button::class, 'ui' => 'atk-test button', 'icon' => 'pencil'],
    $m->getUserAction('edit')
);
  • Allow overriding action caption per action or per model/action
    $app->getExecutorFactory()->registerCaption($model->getUserAction('add'), 'Add new record');

  • Add new method for Grid to setup button using column action.

// Creating a button for executing model test user action.
$grid->addExecutorButton($grid->getExecutorFactory()->create($model->getUserAction('test'), $grid));

Change look and buttons default label by overriding ExcutorFactory

// Overriding basic ExecutorFactory in order to change Table and Modal button.
// and also changing default add action label.
class MyFactory extends ExecutorFactory {
    public const BUTTON_PRIMARY_COLOR = 'green';

    protected $triggerSeed = [
        self::TABLE_BUTTON => [
            'edit' => [Button::class, null, 'icon' => 'pencil'],
            'delete' => [Button::class, null, 'icon' => 'times red'],
        ],
        self::CARD_BUTTON => [
            'edit' => [Button::class, 'Edit', 'icon' => 'pencil', 'ui' => 'tiny button'],
            'delete' => [Button::class, 'Remove', 'icon' => 'times', 'ui' => 'tiny button'],
        ],
    ];

    protected $triggerCaption = [
        'add' => 'Add New Record',
    ];
}

Screen Shot 2021-01-26 at 10 56 29 AM

ExecutorFactory can be applied to a specific View object instance:

// Overriding basic ExecutorFactory in order to change Card button.
class SpecialCardFactory extends ExecutorFactory {
    public const BUTTON_PRIMARY_COLOR = 'green';

    protected $actionIcon = [
        'callback' => 'sync',
        'preview' => 'eye',
        'edit_argument' => 'user edit',
        'edit_argument_prev' => 'pen square',
        'edit_iso' => 'pencil',
        'confirm' => 'check circle',
        'multi_step' => 'window maximize outline',
    ];

    public function __construct()
    {
        // registering card button default with our own method handler.
        $this->triggerSeed = array_merge(
            $this->triggerSeed,
            [self::CARD_BUTTON => ['default' => [$this, 'getCardButton']]]
        );
    }

    protected function getCardButton($action, $type)
    {
        return [Button::class, 'icon' => $this->actionIcon[$action->short_name]];
    }
}

$card = Card::addTo($app, ['useLabel' => true, 'executorFactory' => new SpecialCardFactory()]);
$card->setModel($country);

Screen Shot 2021-01-26 at 11 10 24 AM

BC Break

Setting Executor via the $action-ui property might break your code since because the executor uses for the action will be revert to the default provide by executor factory. If you need to use a specific executor class for your action, you will need to register one:

$app->getExecutorFactory()->registerExecutor($file->getUserAction('edit'), [$myExecutorClass]);

If you were relying heavily on Model\UserAction::$ui property to set up your action button then this PR may break the look of these buttons.
You will need to register your user action ui property into an action trigger in ExecutorFactory to restore them.

Previous action definition using ui property:

$country->addUserAction('mail', [
    'appliesTo' => \atk4\data\Model\UserAction::APPLIES_TO_SINGLE_RECORD,
    'preview' => function ($model) { return 'here is email preview for ' . $model->get('name'); },
    'callback' => function ($model) { return 'email sent to ' . $model->get('name'); },
    'description' => 'Email testing',
    'ui' => ['icon' => 'mail', 'button' => [null, 'icon' => 'green mail']],
 ]);

From this pr - you will need to remove ui property in your action definition and register the button in ExecutorFactory:
New action definition

$country->addUserAction('mail', [
    'appliesTo' => \atk4\data\Model\UserAction::APPLIES_TO_SINGLE_RECORD,
    'preview' => function ($model) { return 'here is email preview for ' . $model->get('name'); },
    'callback' => function ($model) { return 'email sent to ' . $model->get('name'); },
    'description' => 'Email testing',
]);

// Register a trigger for mail action in Crud
$app->getExecutorFactory()->registerTrigger(
    $app->getExecutorFactory()::TABLE_BUTTON,
    [Button::class, null, 'icon' => 'green mail'],
    $country->getUserAction('mail')
);

Important

Please note that your code should not break if you where using ui property for buttons - only the look of the button since they will be revert to ExecutorFactory default.

- fix behat test
- remove ui dependency
- fix card deck
- fix demo file using model user action
- switch executorFactory property at View level
@ibelar ibelar marked this pull request as draft November 30, 2020 21:14
@mvorisek
Copy link
Copy Markdown
Member

mvorisek commented Dec 1, 2020

Remove all dependency to atk4\Data\UserAction::ui property

more formally remove ui property from atk4/data, right? (as atk4/data does not depend on atk4/ui)

@ibelar
Copy link
Copy Markdown
Contributor Author

ibelar commented Dec 1, 2020

more formally remove ui property from atk4/data, right? (as atk4/data does not depend on atk4/ui)

Yes but this pr is for ui property in Model\UserAction only, not Field.

@ibelar ibelar added the BC-break label Dec 2, 2020
@mvorisek mvorisek closed this Dec 2, 2020
@mvorisek mvorisek reopened this Dec 2, 2020
@mvorisek
Copy link
Copy Markdown
Member

@ibelar can we merge it?

@ibelar
Copy link
Copy Markdown
Contributor Author

ibelar commented Mar 23, 2021

@mvorisek - This is ready. Waiting for @romaninsh or @DarkSide666 to approve the changes.

Copy link
Copy Markdown
Member

@DarkSide666 DarkSide666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't go in details of this now and not so good in frontend things.
I will approve just in case you need that, but does this mean we can't define UI executor in atk4/data action ui property anymore?

@mvorisek
Copy link
Copy Markdown
Member

I can't go in details of this now and not so good in frontend things.
I will approve just in case you need that, but does this mean we can't define UI executor in atk4/data action ui property anymore?

You can define executor factory in App, see https://github.com/atk4/ui/pull/1550/files#diff-ad46c7350003ccf7f086dccbecc6fcb7706a1558b14eb5e437a856b8c619e71fR227, thus I would say you can, as long as you will provided factory that does that :)

@mvorisek mvorisek merged commit 68f89d6 into develop Mar 25, 2021
@mvorisek mvorisek deleted the feature/Executor-refactor branch March 25, 2021 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

Remove Data\UserAction::ui property dependency in UI

3 participants