Agile UI

repository·develop·Indexed 19 days ago

https://github.com/atk4/ui

A robust PHP framework for rapid development of back-end UIs, admin interfaces, and data management systems. It features a server-side rendering engine driven by an abstract data layer, allowing developers to build complex interfaces with minimal code. The ecosystem includes the atkjs-ui Javascript library (v1.0.0) and integrates with ATK Data and ATK Core to provide components like Crud, Accordion, and Form.

Tokens
82.7K
Snippets
308
Records
385
Agent score
66%

What's inside atk4-ui

  1. What is a Seed in Agile UI

    develop

    A Seed is a shorthand syntax (using strings or arrays) used to represent an object before it is actually instantiated. Instead of manually creating an object instance, you provide a seed that Agile UI uses to dynamically locate and load the correct class only when it is needed.

    Advantages of using Seeds:

    • Readability: Shorter and cleaner syntax.
    • Performance: Objects are only initialized when required.
    • Recursive Injection: Allows for nested property and constructor argument injection.
    • Flexibility: Can be namespace-specific and allows application logic to enhance mechanics.
    // Manual object creation
    $icon = new Icon('book');
    $button = new Button('Hello');
    $button->icon = $icon;
    
    // Using a Seed (string/array) instead of an object
    $button = new Button('Hello');
    $button->icon = 'book';
  2. What is a Theme in Agile UI?

    develop

    In Agile UI, a "Theme" is a conceptual package that provides three main components to change the application's appearance and structure:

    1. Custom CSS: A build based on Fomantic-UI.
    2. Custom Layout(s): Unique layout implementations accompanied by documentation.
    3. Views: Additional or tweaked View components.

    Custom layouts can serve as stand-in replacements for standard layouts or as entirely new structural patterns.

  3. How JavaScript mapping works in Agile UI

    develop

    Agile UI provides a bridge between PHP and JavaScript, allowing you to generate and execute client-side events and actions directly from your PHP Views. Instead of writing raw JavaScript logic in PHP, you should use Agile UI to bind generic JavaScript routines (like jQuery) to your UI components via actions and events.

    Basic integration example:

    $b = new Button();
    $b->js('click')->hide();
    $b = new Button();
    $b->js('click')->hide();
  4. How to structure a Seed array

    develop

    When using an array as a seed, the structure determines how the object is constructed and how properties are assigned:

    1. Index 0: The class name (mapped to the \Atk4\Ui namespace by default).
    2. Numeric Indexes: Values passed as arguments to the object's constructor.
    3. Named Arguments (String keys): Values assigned to object properties after the constructor has been called.

    Example of a complex seed:

    $seed = [
        Button::class,           // Class name
        'hello',                 // Constructor argument 1
        'class.big red' => true, // Property assignment
        'icon' => ['book', 'red'] // Recursive seed for a property
    ];
    $seed = [Button::class, 'hello', 'class.big red' => true, 'icon' => ['book', 'red']];
  5. Best practices for responsiveness and large datasets

    develop

    While the Columns class supports some responsiveness, for complex layouts requiring fine-grained control over CSS classes, it is recommended to create your own custom component templates.

    For displaying large amounts of data, use the Lister class with a custom template instead of using Columns to ensure better performance.

  6. Understand the Component hierarchy in Agile UI

    develop

    In Agile UI, all components are classes that extend from Atk4\Ui\View. This inheritance allows them to inherit the ability to render themselves via the render method. Components are categorized into four main types based on their purpose and behavior:

    1. Core Components: Foundational components that serve as bases for other components. Many qualities implemented in a core component are inherited by its descendants (e.g., view, lister, table, field).
    2. Simple Components: Used for abstraction and providing a clean interface for UI elements. They abstract HTML while allowing control over small elements (e.g., button, label, icon, tabs).
    3. Interactive Components: Components that communicate with the PHP realm using callback, virtualpage, or sse. They trigger additional AJAX requests to load data or execute code (e.g., console, loader, wizard, popup).
    4. Composite Components: Components that manage and delegate rendering and interactivity to multiple sub-components. A component becomes composite if you use the View::add() method to include sub-elements (e.g., grid, form, crud).
  7. Use the Crud class for interactive data management

    develop

    The Atk4\Ui\Crud class extends the Grid class to provide a complete interface for managing records. It automatically handles adding, updating, deleting, and reading records by linking them to Model actions.

    When to use Crud vs other components:

    • Use Crud when you need a full interactive interface (Add/Edit/Delete/Read).
    • Use Grid if you need a data grid with custom actions that are not the standard CRUD operations, or if you want to use a custom editing mechanism (like a separate page instead of a modal).
    • Use Table if you only need to display a non-interactive table of data.
    Crud::addTo($app)->setModel(new Country($app->db));
  8. Understand the difference between Data Presentation and Decoration

    develop

    Agile UI distinguishes between how data is formatted and how it is visually enhanced to ensure consistency across the system.

    • Data Presentation: Refers to displaying the value of the data in a different format (e.g., changing decimal separators like 123,123.00 to 123.123,00). Presentation is handled globally by Persistence\Ui to ensure a Form, Table, and custom View all show the same format.
    • Data Decoration: Refers to adding visual elements like currency symbols or icons (e.g., a calendar icon for a date picker). Decoration is context-dependent and is performed by helper classes like Form\Control\Calendar or Table\Column\Money. The decorator controls the final output and can choose to use the presentation value or apply its own logic.
  9. Use Dependency Injection via the $app property

    develop

    Every View has an $app property (provided via \Atk4\Core\AppScopeTrait). The framework automatically passes the App instance to all views in the render tree. This enables Dependency Injection, allowing you to access shared services like loggers or database connections from anywhere within a view.

    Example of accessing a service from a view:

    $view->getApp()->logger->log('Foo Bar');
  10. Understand the ATK ecosystem dependencies

    develop

    ATK UI is part of a larger ecosystem and relies on the following frameworks to function. To build full-featured applications, you should be familiar with the documentation for all three:

    • ATK UI: The user interface framework.
    • ATK Data: Handles database integration and data modeling.
    • ATK Core: The foundational framework for the ecosystem.

    For deep technical implementation, refer to the official documentation sites for each component.

  11. Use the Seed syntax for concise component configuration

    develop

    Agile UI supports a dynamic syntax called "Seed" to allow for concise component creation and property assignment. This pattern is used with the add() method (and its alias addTo()) and for direct property assignment.

    // Using Seed in addTo()
    Button::addTo($app, ['Hello']);
    
    // Using Seed for property assignment
    $button->icon = 'book';
  12. Avoid rendering issues when combining JsModal and JsReload

    develop

    When using JsModal (which requires a VirtualPage) alongside JsReload (to refresh a table on the main page), the order of declaration in your PHP code is critical due to how the render tree is processed.

    The Rule: If a reload is triggered during rendering, the process stops and only renders what was requested. To ensure the JsReload target is available and the VirtualPage is correctly handled, you must follow this order:

    1. Declare the target of the reload first (e.g., the Table).
    2. Declare the VirtualPage (which contains the JsModal content).
    3. Declare the trigger (e.g., the Button that opens the modal).

    If you declare the Button or VirtualPage before the Table, the reload might fail because the table hasn't been registered in the render tree yet.

    // CORRECT ORDER
    
    // 1. Target first
    $table = \Atk4\Ui\Table::addTo($app);
    $table->setModel($model);
    
    // 2. VirtualPage second
    $vp = \Atk4\Ui\VirtualPage::addTo($app);
    $vp->set(function (\Atk4\Ui\VirtualPage $p) use ($table, $model) {
        $form = \Atk4\Ui\Form::addTo($p);
        $form->setEntity($model);
        $form->onSubmit(function (Form $form) use ($table) {
            $form->entity->save();
            return new \Atk4\Ui\Js\JsBlock([
                $table->jsReload(),
                $form->jsSuccess('ok'),
            ]);
        });
    });
    
    // 3. Trigger last
    $button = \Atk4\Ui\Button::addTo($app, ['Add Item']);
    $button->on('click', new \Atk4\Ui\Js\JsModal('Title', $vp));