Actions.php 4.7 KB
<?php

namespace App\Admin\Rewrite\Grid\Displayers;

use App\Admin\Rewrite\Actions\RowAction;
use Encore\Admin\Admin;
use Encore\Admin\Grid\Displayers\Actions as EncoreActions;
use Illuminate\Support\Arr;

class Actions extends EncoreActions
{
    /**
     * @var array
     */
    protected $custom = [];

    /**
     * @param RowAction $action
     *
     * @return \Encore\Admin\Grid\Displayers\DropdownActions
     */
    public function add(RowAction $action)
    {
        $this->prepareAction($action);

        array_push($this->custom, $action);

        return $this;
    }

    /**
     * @param RowAction $action
     */
    protected function prepareAction(RowAction $action)
    {
        $action->setGrid($this->grid)
            ->setColumn($this->column)
            ->setRow($this->row);
    }

    /**
     * Render view action.
     *
     * @return string
     */
    protected function renderView()
    {
        $action = __('view');

        $url = "{$this->getResource()}/{$this->getRouteKey()}";
        $params = explode('?', request()->getUri());
        // 如果有参数
        if (count($params) > 1) {
            $url .= '?'.Arr::last($params);
        }

        return <<<EOT
<a href="{$url}" class="btn btn-info {$this->grid->getGridRowName()}-view">
    <i class="fa fa-eye"></i> {$action}
</a>
EOT;
    }

    /**
     * Render edit action.
     *
     * @return string
     */
    protected function renderEdit()
    {
        $url = "{$this->getResource()}/{$this->getRouteKey()}/edit";
        $params = explode('?', request()->getUri());

        // 如果有参数
        if (count($params) > 1) {
            $url .= '?'.Arr::last($params);
        }

        $action = __('edit');
        return <<<EOT
<a href="{$url}" class="btn btn-primary {$this->grid->getGridRowName()}-edit">
    <i class="fa fa-edit"></i> {$action}
</a>
EOT;
    }

    /**
     * Render delete action.
     *
     * @return string
     */
    protected function renderDelete()
    {
        $this->setupDeleteScript();
        $action = __('delete');

        return <<<EOT
<a href="javascript:void(0);" data-id="{$this->getKey()}" class="btn btn-danger {$this->grid->getGridRowName()}-delete">
    <i class="fa fa-trash"></i> {$action}
</a>
EOT;
    }

    protected function setupDeleteScript()
    {
        $trans = [
            'delete_confirm' => trans('admin.delete_confirm'),
            'confirm'        => trans('admin.confirm'),
            'cancel'         => trans('admin.cancel'),
        ];

        $trans = array_merge($trans, $this->trans);

        $script = <<<SCRIPT

$('.{$this->grid->getGridRowName()}-delete').unbind('click').click(function() {

    var id = $(this).data('id');

    swal({
        title: "{$trans['delete_confirm']}",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "{$trans['confirm']}",
        showLoaderOnConfirm: true,
        cancelButtonText: "{$trans['cancel']}",
        preConfirm: function() {
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '{$this->getResource()}/' + id,
                    data: {
                        _method:'delete',
                        _token:LA.token,
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
//                swal(data.message, '', 'success');
                swal_success(data.message);

            } else {
//                swal(data.message, '', 'error');
                swal_error(data.message);

            }
        }
    });
});

SCRIPT;

        Admin::script($script);
    }

    /**
     * {@inheritdoc}
     */
    public function display($callback = null)
    {
        if ($callback instanceof \Closure) {
            $callback->call($this, $this);
        }

        if ($this->disableAll) {
            return '';
        }

        $actions = $this->prepends;

        foreach ($this->actions as $action) {
            $method = 'render'.ucfirst($action);
            array_push($actions, $this->{$method}());
        }

        $actions = array_merge($actions, $this->appends);

        foreach ($this->custom as $custom) {
            $actions[] = $custom->render();
        }

        return implode('', $actions);
    }

    /**
     * diy delete translate.
     *
     * @param $tans
     */
    public function setTrans($tans)
    {
        $this->trans = $tans;
    }
}