bayrell / BayLang

BayLang compiler source code
https://bayrell.org/ru/docs/baylang
Apache License 2.0
4 stars 0 forks source link

Формы #167

Open ildar-ceo opened 2 months ago

ildar-ceo commented 2 months ago

Код формы

use Runtime.BaseModel;
use Runtime.Widget.Form.Form;

class FormModel extends BaseModel
{
    string component = classof Form;
    string widget_name = "form";

    /* Fields */
    Vector<Map> fields = [];
    Dict<Vector> fields_error = {};

    /* Data */
    Dict pk = null;
    Dict data = {};
    FormStorageInterface storage = null;

    /* Form result */
    WidgetResultModel load = null;
    WidgetResultModel result = null;

    /**
     * Returns api name
     */
    string getApiName() => "";

    /**
     * Create data storage
     */
    BaseObject createDataStorage() => null;

    /**
     * Returns data
     */
    Dict getPostData() => this.data;

    /**
     * Setup widget
     */
    void setup(Dict params)
    {
        if (params == null) return;

        /* Setup storage */
        if (this.storage == null)
        {
            this.storage = this.createDataStorage(params);
        }

        /* Setup primary key */
        if (params.has("primary_key"))
        {
            this.pk = params.get("primary_key");
        }
    }

    /**
     * Init widget
     */
    void init(Dict params)
    {
        /* Setup foreign key */
        if (this.storage and params.has("foreign_key"))
        {
            this.storage.setForeignKey(params.get("foreign_key"));
        }
    }

    /**
     * Set api result
     */
    void setApiResult(ApiResult res, string action)
    {
        if (res == null) return;

        /* Load */
        if (action == "load")
        {
            this.load.setApiResult(res);
        }

        /* Submit */
        if (action == "submit")
        {
            if (res.data.has("fields"))
            {
                this.fields_error = res.data.get("fields");
            }
            this.result.setApiResult(res);
        }
    }

    /**
     * Load form data
     */
    async void loadData(RenderContainer container)
    {
        ApiResult res = await this.storage.load();
        this.setApiResult(res, "load");
        this.emit("load", [res]);
    }

    /**
     * Submit form
     */
    async void submit()
    {
        this.result.setWaitMessage();
        ApiResult res = await this.storage.submit();
        this.setApiResult(res, "submit");
        this.emit("submit", [res]);
    }
}

interface FormStorageInterface
{
    void setForeignKey(Dict foreign_key);
    async void load();
    async void submit();
}

class FormSaveStorage implements FormStorageInterface
{
    FormModel form = null;
    Dict foreign_key = null;

    /**
     * Constructor
     */
    void constructor(FormModel form)
    {
        this.form = form;
    }

    /**
     * Set foreign key
     */
    void setForeignKey(Dict foreign_key)
    {
        this.foreign_key = foreign_key;
    }

    /**
     * Merge data
     */
    Dict mergeData(Dict pos_data, string action)
    {
        if (this.foreign_key)
        {
            post_data = post_data.concat(this.foreign_key);
            if (pos_data.has("item"))
            {
                post_data.set("item", post_data.get("item").concat(this.foreign_key));
            }
        }
    }

    /**
     * Load form
     */
    async ApiResult load()
    {
        Dict post_data =
        {
            "pk": this.form.pk,
        };
        post_data = this.mergeData(post_data, "load");
        ApiResult res = await this.layout.callApi({
            "api_name": this.form.getApiName(),
            "method_name": "actionItem",
            "data": post_data
        });
        return res;
    }

    /**
     * Submit form
     */
    async ApiResult submit()
    {
        Dict post_data =
        {
            "pk": this.form.pk,
            "item": this.form.getPostData(),
        };
        post_data = this.mergeData(post_data, "submit");
        ApiResult res = await this.layout.callApi({
            "api_name": this.form.getApiName(),
            "method_name": "actionSave",
            "data": post_data
        });
        return res;
    }
}

class FormDeleteStorage extends FormSaveStorage
{
    /**
     * Submit form
     */
    async ApiResult submit()
    {
        Dict post_data =
        {
            "pk": this.form.pk,
        };
        post_data = this.mergeData(post_data, "submit");
        ApiResult res = await this.layout.callApi({
            "api_name": this.form.getApiName(),
            "method_name": "actionDelete",
            "data": post_data
        });
        return res;
    }
}