| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace Dcat\Admin\Actions;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Traits\HasAuthorization;
- trait HasActionHandler
- {
- use HasAuthorization {
- failedAuthorization as parentFailedAuthorization;
- }
- /**
- * @var Response
- */
- protected $response;
- /**
- * @return Response
- */
- public function response()
- {
- if (is_null($this->response)) {
- $this->response = new Response();
- }
- return $this->response;
- }
- /**
- * @return string
- */
- public function method()
- {
- return $this->method;
- }
- /**
- * @return array
- */
- protected function parameters()
- {
- return [];
- }
- /**
- * Confirm message of action.
- *
- * @return string|void
- */
- public function confirm()
- {
- }
- /**
- * @return mixed
- */
- public function makeCalledClass()
- {
- return str_replace('\\', '_', get_called_class());
- }
- /**
- * @return string
- */
- public function handlerRoute()
- {
- return route('dcat.api.action');
- }
- /**
- * @return void
- */
- protected function addHandlerScript()
- {
- $data = json_encode($this->parameters());
- $script = <<<JS
- Dcat.grid.RowAction({
- selector: '{$this->selector()}',
- event: '{$this->event}',
- method: '{$this->method()}',
- key: '{$this->getKey()}',
- url: '{$this->handlerRoute()}',
- data: {$data},
- calledClass: '{$this->makeCalledClass()}',
- before: {$this->actionScript()},
- html: {$this->handleHtmlResponse()},
- success: {$this->resolverScript()},
- error: {$this->rejectScript()},
- });
- JS;
- Admin::script($script);
- Admin::js('@admin/dcat/extra/grid-row-action.js');
- }
- /**
- * @return string
- */
- protected function actionScript()
- {
- // 发起请求之前回调,返回false可以中断请求
- return <<<'JS'
- function (data, target) { }
- JS;
- }
- /**
- * @return string
- */
- protected function resolverScript()
- {
- // 请求成功回调,返回false可以中断默认的成功处理逻辑
- return <<<'JS'
- function (target, results) {}
- JS;
- }
- /**
- * @return string
- */
- protected function handleHtmlResponse()
- {
- return <<<'JS'
- function (target, html, data) { // 处理返回的HTML代码
- target.html(html);
- }
- JS;
- }
- /**
- * @return string
- */
- protected function rejectScript()
- {
- // // 请求出错回调,返回false可以中断默认的错误处理逻辑
- return <<<'JS'
- function (target, results) {}
- JS;
- }
- /**
- * @return Response
- */
- public function failedAuthorization()
- {
- return $this->response()->error(__('admin.deny'));
- }
- }
|