123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace Dcat\Admin\Widgets;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Support\Helper;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Support\Facades\URL;
- class DialogForm
- {
- const QUERY_NAME = '_form_win_';
- /**
- * @var string
- */
- public static $contentView = 'admin::contents.dialog-form';
- /**
- * @var array
- */
- protected $options = [
- 'title' => 'Form',
- 'area' => ['700px', '670px'],
- 'defaultUrl' => null,
- 'buttonSelector' => null,
- 'query' => null,
- 'lang' => null,
- 'forceRefresh' => false,
- 'disableReset' => false,
- ];
- /**
- * @var array
- */
- protected $handlers = [
- 'saved' => null,
- 'success' => null,
- 'error' => null,
- ];
- public function __construct(?string $title = null, $url = null)
- {
- $this->title($title);
- $this->url($url);
- }
- /**
- *
- * @param array $options
- * @return $this
- */
- public function options($options = [])
- {
- if ($options instanceof Arrayable) {
- $options = $options->toArray();
- }
- $this->options = array_merge($this->options, $options);
- return $this;
- }
- /**
- * 设置弹窗标题
- *
- * @param string $title
- * @return $this
- */
- public function title(?string $title)
- {
- $this->options['title'] = $title;
- return $this;
- }
- /**
- * 绑定点击按钮
- *
- * @param string $buttonSelector
- * @return $this
- */
- public function click(string $buttonSelector)
- {
- $this->options['buttonSelector'] = $buttonSelector;
- return $this;
- }
- /**
- * 强制每次点击按钮都重新渲染表单弹窗
- *
- * @return $this
- */
- public function forceRefresh()
- {
- $this->options['forceRefresh'] = true;
- return $this;
- }
- /**
- * 禁用重置按钮
- *
- * @return $this
- */
- public function disableResetButton()
- {
- $this->options['disableReset'] = true;
- return $this;
- }
- /**
- * 保存后触发的js的代码(不论成功还是失败)
- *
- * @param string $script
- * @return $this
- */
- public function saved(string $script)
- {
- $this->handlers['saved'] = $script;
- return $this;
- }
- /**
- * 保存失败时触发的js代码
- *
- * @param string $script
- * @return $this
- */
- public function error(string $script)
- {
- $this->handlers['error'] = $script;
- return $this;
- }
- /**
- * 保存成功后触发的js代码
- *
- * @param string $script
- * @return $this
- */
- public function success(string $script)
- {
- $this->handlers['success'] = $script;
- return $this;
- }
- /**
- * 设置弹窗宽高
- * 支持百分比和"px"
- *
- * @param string $width
- * @param string $height
- * @return $this
- */
- public function dimensions(string $width, string $height)
- {
- $this->options['area'] = [$width, $height];
- return $this;
- }
- /**
- * 设置弹窗宽度
- * 支持百分比和"px"
- *
- * @param string|null $width
- * @return $this
- */
- public function width(?string $width)
- {
- $this->options['area'][0] = $width;
- return $this;
- }
- /**
- * 设置弹窗高度
- * 支持百分比和"px"
- *
- * @param string|null $height
- * @return $this
- */
- public function height(?string $height)
- {
- $this->options['area'][1] = $height;
- return $this;
- }
- /**
- * 设置默认的表单页面url
- *
- * @param null|string $url
- * @return $this
- */
- public function url(?string $url)
- {
- if ($url) {
- $this->options['defaultUrl'] = Helper::urlWithQuery(
- admin_url($url),
- [static::QUERY_NAME => 1]
- );
- }
- return $this;
- }
- /**
- * @return string
- */
- public function render()
- {
- $this->setupOptions();
- $opts = json_encode($this->options);
- Admin::script(
- <<<JS
- (function () {
- var opts = {$opts};
-
- opts.success = function (success, response) {
- {$this->handlers['success']}
- };
- opts.error = function (success, response) {
- {$this->handlers['error']}
- };
- opts.saved = function (success, response) {
- {$this->handlers['saved']}
- };
-
- LA.DialogForm(opts);
- })();
- JS
- );
- }
- protected function setupOptions()
- {
- $this->options['lang'] = [
- 'submit' => trans('admin.submit'),
- 'reset' => trans('admin.reset'),
- 'save_failed' => trans('admin.save_failed'),
- ];
- $this->options['query'] = static::QUERY_NAME;
- }
- /**
- * 判断是否是获取弹窗表单内容的请求
- *
- * @return bool
- */
- public static function is()
- {
- return request(static::QUERY_NAME) ? true : false;
- }
- /**
- * 对弹窗要渲染的表单对象进行前置处理
- *
- * @param Form $form
- */
- public static function prepare(Form $form)
- {
- if (!static::is()) {
- return;
- }
- Admin::$baseCss = [];
- Admin::$baseJs = [];
- Admin::$fonts = '';
- Admin::$disableSkinCss = true;
- $form->wrap(function ($v) {
- return $v;
- });
- $form->disableHeader();
- $form->disableFooter();
- $form->setWidth(9, 2);
- $form->hidden('_token')->value(csrf_token());
- Content::composing(function (Content $content) {
- $content->setView(static::$contentView);
- });
- }
- }
|