123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?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;
- class DialogForm
- {
- const QUERY_NAME = '_dialog_form_';
- /**
- * @var string
- */
- public static $contentView = 'admin::layouts.form-content';
- /**
- * @var array
- */
- protected $options = [
- 'title' => 'Form',
- 'area' => ['700px', '670px'],
- 'defaultUrl' => null,
- 'buttonSelector' => null,
- 'query' => null,
- 'lang' => null,
- 'forceRefresh' => false,
- 'reset' => true,
- ];
- /**
- * @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;
- }
- /**
- * 重置按钮.
- *
- * @param bool $value
- *
- * @return $this
- */
- public function resetButton(bool $value = true)
- {
- $this->options['reset'] = $value;
- 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
- */
- protected 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']}
- };
-
- Dcat.DialogForm(opts);
- })();
- JS
- );
- }
- /**
- * 配置选项初始化.
- *
- * @return void
- */
- protected function setUpOptions()
- {
- $this->options['lang'] = [
- 'submit' => trans('admin.submit'),
- 'reset' => trans('admin.reset'),
- ];
- $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([], false);
- Admin::baseJs([], false);
- Admin::fonts(false);
- Admin::style('.form-content{ padding-top: 7px }');
- $form->wrap(function ($v) {
- return $v;
- });
- $form->disableHeader();
- $form->disableFooter();
- $form->width(9, 2);
- $form->composing(function ($form) {
- static::addScript($form);
- });
- Content::composing(function (Content $content) {
- $content->view(static::$contentView);
- });
- }
- protected static function addScript(Form $form)
- {
- $confirm = json_encode($form->builder()->confirm);
- Admin::script(
- <<<JS
- Dcat.FormConfirm = {$confirm};
- JS
- );
- }
- public function __destruct()
- {
- if ($results = Helper::render($this->render())) {
- Admin::html($results);
- }
- }
- }
|