jqh пре 5 година
родитељ
комит
c98a189e79

+ 102 - 0
resources/views/widgets/dialogtable.blade.php

@@ -0,0 +1,102 @@
+<span style="cursor: pointer" id="button-{{ $id }}">{!! $button !!}</span>
+
+<template id="temp-{$this->id()}">
+    <div {!! $attributes !!}>
+        <div class="p-2 dialog-body">{!! $table !!}</div>
+
+        @if($footer)
+        <div class="dialog-footer layui-layer-btn">{!! $footer !!}</div>
+        @endif
+    </div>
+</template>
+
+<script>
+    var id = replaceNestedFormIndex('{{ $id }}'),
+        area = screen.width <= 850 ? ['100%', '100%',] : '{{ $width }}',
+        offset = screen.width <= 850 ? 0 : '70px',
+        _id, _tempId, _btnId, _tb;
+
+    setId(id);
+
+    function hidden(index) {
+        {!! $events['hidden'] !!}
+
+        $(_id).trigger('dialog:hidden');
+    }
+
+    function open(btn) {
+        var index = layer.open({
+            type: 1,
+            title: '{!! $title !!}',
+            area: area,
+            offset: offset,
+            maxmin: false,
+            resize: false,
+            content: $(_tempId).html(),
+            success: function(layero, index) {
+                $(_id).attr('layer', index);
+
+                setDataId($(_id));
+
+                {!! $events['shown'] !!}
+
+                setTimeout(function () {
+                    Dcat.grid.AsyncTable({container: _tb});
+
+                    $(_tb).trigger('table:load');
+                }, 100);
+
+                $(_id).trigger('dialog:shown');
+
+                $(_id).on('dialog:open', openDialog);
+                $(_id).on('dialog:close', closeDialog)
+            },
+            cancel: function (index) {
+                btn && btn.removeAttr('layer');
+
+                hidden(index)
+            }
+        });
+
+        btn && btn.attr('layer', index);
+    }
+
+    function setDataId(obj) {
+        if (! obj.attr('data-id')) {
+            obj.attr('data-id', id);
+        }
+    }
+
+    function setId(val) {
+        if (! val) return;
+
+        id = val;
+        _id = '#'+id;
+        _tempId = '#temp-'+id;
+        _btnId = '#button-'+id;
+        _tb = _id+' .async-table';
+    }
+
+    function openDialog () {
+        setId($(this).attr('data-id'));
+        setDataId($(this));
+
+        if (! $(this).attr('layer')) {
+            open($(this));
+        }
+    }
+
+    function closeDialog() {
+        var index = $(this).attr('layer');
+
+        $(_id).removeAttr('layer');
+        $(_btnId).removeAttr('layer');
+
+        if (index) {
+            layer.close(index);
+            hidden(index);
+        }
+    }
+
+    $(_btnId).on('click', openDialog);
+</script>

+ 29 - 0
resources/views/widgets/dropdown.blade.php

@@ -0,0 +1,29 @@
+@if(! empty($button['text']) || $click)
+    <span class="dropdown" style="display:inline-block">
+        <a id="{{ $buttonId }}" class="dropdown-toggle {{ $button['class'] }}" data-toggle="dropdown" href="javascript:void(0)">
+            <stub>{!! $button['text'] !!}</stub>
+            <span class="caret"></span>
+        </a>
+        <ul class="dropdown-menu">{!! $options !!}</ul>
+    </span>
+@else
+    <ul class="dropdown-menu">{!! $options !!}</ul>
+@endif
+
+@if($click)
+<script>
+    var $btn = $('#{{ $buttonId }}'),
+        $a = $btn.parent().find('ul li a'),
+        text = $btn.text();
+
+    $a.on('click', function () {
+        $btn.find('stub').html($(this).html() + ' &nbsp;');
+    });
+
+    if (text) {
+        $btn.find('stub').html(text + ' &nbsp;');
+    } else {
+        (!$a.length) || $btn.find('stub').html($($a[0]).html() + ' &nbsp;');
+    }
+</script>
+@endif

+ 15 - 1
resources/views/widgets/form.blade.php

@@ -11,7 +11,6 @@
         @else
             @if($rows)
                 <div class="ml-2 mb-2">
-                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                     @foreach($rows as $row)
                         {!! $row->render() !!}
                     @endforeach
@@ -53,3 +52,18 @@
     </div>
     @endif
 {!! $end !!}
+
+@if(! empty($elementId))
+<script>
+    $('#{{ $elementId }}').form({
+        validate: true,
+        confirm: {!! json_encode($confirm) !!},
+        success: function (data) {
+            {!! $savedScript !!}
+        },
+        error: function (response) {
+            {!! $errorScript !!}
+        }
+    });
+</script>
+@endif

+ 5 - 0
resources/views/widgets/markdown.blade.php

@@ -0,0 +1,5 @@
+<div {!! $attributes !!}><textarea style="display:none;">{!! $content !!}</textarea></div>
+
+<script require="@editor-md">
+    editormd.markdownToHTML('{{ $id }}', {!! json_encode($options) !!});
+</script>

+ 22 - 0
src/Form/Field.php

@@ -1014,6 +1014,28 @@ class Field implements Renderable
         return $formId.' .'.implode('.', $elementClass);
     }
 
+    /**
+     * Get element class string.
+     *
+     * @return mixed
+     */
+    public function getElementClassString()
+    {
+        $elementClass = $this->getElementClass();
+
+        if (Arr::isAssoc($elementClass)) {
+            $classes = [];
+
+            foreach ($elementClass as $index => $class) {
+                $classes[$index] = is_array($class) ? implode(' ', $class) : $class;
+            }
+
+            return $classes;
+        }
+
+        return implode(' ', $elementClass);
+    }
+
     /**
      * @return $this
      */

+ 1 - 0
src/Form/Field/HasMany.php

@@ -557,6 +557,7 @@ class HasMany extends Field
             'relationName'   => $this->relationName,
             'options'        => $this->options,
             'templateScript' => implode(";\r\n", $scripts),
+            'count'          => count($this->value()),
         ]);
 
         return parent::render();

+ 3 - 1
src/Form/Field/SelectTable.php

@@ -146,11 +146,13 @@ class SelectTable extends Field
             ->defaultAttribute('name', $name)
             ->defaultAttribute('id', 'container-'.$this->getElementId());
 
+        $dialog = $this->dialog->render();
+
         $this->addVariables([
             'prepend'      => $this->prepend,
             'append'       => $this->append,
             'style'        => $this->style,
-            'dialog'       => $this->dialog->render(),
+            'dialog'       => $dialog,
             'placeholder'  => $this->placeholder(),
             'dialogId'     => $this->dialog->id(),
             'dialogScript' => $this->dialog->getScript(),

+ 19 - 3
src/Form/Field/WebUploader.php

@@ -116,7 +116,7 @@ trait WebUploader
     }
 
     /**
-     * Disable remove file.
+     * 禁用前端删除功能.
      *
      * @param bool $value
      *
@@ -130,7 +130,7 @@ trait WebUploader
     }
 
     /**
-     * Set upload server.
+     * 设置图片删除地址.
      *
      * @param string $server
      *
@@ -144,6 +144,8 @@ trait WebUploader
     }
 
     /**
+     * 设置上传表单数据.
+     *
      * @param array $data
      *
      * @return $this
@@ -170,7 +172,21 @@ trait WebUploader
     }
 
     /**
-     * Set default options form file field.
+     * 是否开启图片压缩.
+     *
+     * @param bool|array $compress
+     *
+     * @return $this
+     */
+    public function compress($compress)
+    {
+        $this->options['compress'] = $compress;
+
+        return $this;
+    }
+
+    /**
+     * 默认上传配置.
      *
      * @return void
      */

+ 0 - 30
src/Widgets/Alert.php

@@ -6,43 +6,13 @@ use Illuminate\Contracts\Support\Renderable;
 
 class Alert extends Widget
 {
-    /**
-     * @var string
-     */
     protected $view = 'admin::widgets.alert';
-
-    /**
-     * @var string
-     */
     protected $title;
-
-    /**
-     * @var string
-     */
     protected $content;
-
-    /**
-     * @var string
-     */
     protected $style;
-
-    /**
-     * @var string
-     */
     protected $icon;
-
-    /**
-     * @var bool
-     */
     protected $showCloseBtn = false;
 
-    /**
-     * Alert constructor.
-     *
-     * @param mixed  $content
-     * @param string $title
-     * @param string $style
-     */
     public function __construct($content = '', $title = null, $style = 'danger')
     {
         $this->content($content);

+ 0 - 25
src/Widgets/Box.php

@@ -7,37 +7,12 @@ use Illuminate\Contracts\Support\Renderable;
 
 class Box extends Widget
 {
-    /**
-     * @var string
-     */
     protected $view = 'admin::widgets.box';
-
-    /**
-     * @var string
-     */
     protected $title = 'Box header';
-
-    /**
-     * @var string
-     */
     protected $content = 'here is the box content.';
-
-    /**
-     * @var array
-     */
     protected $tools = [];
-
-    /**
-     * @var string
-     */
     protected $padding;
 
-    /**
-     * Box constructor.
-     *
-     * @param string $title
-     * @param string $content
-     */
     public function __construct($title = '', $content = '')
     {
         if ($title) {

+ 0 - 28
src/Widgets/Callout.php

@@ -6,44 +6,16 @@ use Illuminate\Contracts\Support\Renderable;
 
 class Callout extends Widget
 {
-    /**
-     * @var string
-     */
     protected $view = 'admin::widgets.alert';
-
-    /**
-     * @var string
-     */
     protected $title;
-
-    /**
-     * @var string
-     */
     protected $content;
-
-    /**
-     * @var string
-     */
     protected $style = 'default';
-
-    /**
-     * @var bool
-     */
     protected $showCloseBtn = false;
 
-    /**
-     * Alert constructor.
-     *
-     * @param mixed  $content
-     * @param string $title
-     * @param string $style
-     */
     public function __construct($content = '', ?string $title = null, ?string $style = null)
     {
         $this->content($content);
-
         $this->title($title);
-
         $this->style($style);
     }
 

+ 0 - 2
src/Widgets/Checkbox.php

@@ -8,9 +8,7 @@ use Illuminate\Support\Arr;
 class Checkbox extends Radio
 {
     protected $view = 'admin::widgets.checkbox';
-
     protected $type = 'checkbox';
-
     protected $checked = [];
 
     /**

+ 13 - 27
src/Widgets/Code.php

@@ -2,8 +2,6 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Illuminate\Contracts\Support\Renderable;
-
 class Code extends Markdown
 {
     /**
@@ -16,12 +14,12 @@ class Code extends Markdown
      * @param int    $start
      * @param int    $end
      */
-    public function __construct($content = '', int $start = 1, int $end = 10)
+    public function __construct($content = '', int $start = 1, int $end = 1000)
     {
         if (is_array($content) || is_object($content)) {
             $content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
         } elseif (is_file($content)) {
-            $this->read($content, $start, $end);
+            $this->fromFile($content, $start, $end);
             $content = '';
         }
 
@@ -29,7 +27,7 @@ class Code extends Markdown
     }
 
     /**
-     * 设置语言
+     * 设置语言.
      *
      * @param string $lang
      *
@@ -44,30 +42,22 @@ class Code extends Markdown
 
     public function javascript()
     {
-        $this->lang = 'javascript';
-
-        return $this;
+        return $this->lang('javascript');
     }
 
     public function html()
     {
-        $this->lang = 'html';
-
-        return $this;
+        return $this->lang('html');
     }
 
     public function java()
     {
-        $this->lang = 'java';
-
-        return $this;
+        return $this->lang('java');
     }
 
     public function python()
     {
-        $this->lang = 'python';
-
-        return $this;
+        return $this->lang('python');
     }
 
     /**
@@ -79,9 +69,9 @@ class Code extends Markdown
      *
      * @return $this
      */
-    public function padding($file, $lineNumber = 1, $padding = 5)
+    public function section($file, $lineNumber = 1, $context = 5)
     {
-        return $this->read($file, $lineNumber - $padding, $lineNumber + $padding);
+        return $this->fromFile($file, $lineNumber - $context, $lineNumber + $context);
     }
 
     /**
@@ -93,7 +83,7 @@ class Code extends Markdown
      *
      * @return $this
      */
-    public function read($file, $start = 1, $end = 10)
+    public function fromFile($file, $start = 1, $end = 10)
     {
         if (! $file or ! is_readable($file) || $end < $start) {
             return $this;
@@ -118,18 +108,14 @@ class Code extends Markdown
         return $this->content($source);
     }
 
-    protected function build()
+    protected function renderContent()
     {
-        if ($this->content instanceof Renderable) {
-            $this->content = $this->content->render();
-        }
+        $content = parent::renderContent();
 
         return <<<EOF
-<div {$this->formatHtmlAttributes()}><textarea style="display:none;">
 ```{$this->lang}
-{$this->content}
+{$content}
 ```
-</textarea></div>
 EOF;
     }
 }

+ 17 - 120
src/Widgets/DialogTable.php

@@ -9,6 +9,8 @@ use Illuminate\Support\Str;
 
 class DialogTable extends Widget
 {
+    protected $view = 'admin::widgets.dialogtable';
+
     /**
      * @var string
      */
@@ -186,136 +188,33 @@ class DialogTable extends Widget
         if ($this->events['load']) {
             $this->table->onLoad($this->events['load']);
         }
-
-        $this->script = <<<JS
-(function () {
-    var id = replaceNestedFormIndex('{$this->id()}'),
-        area = screen.width <= 850 ? ['100%', '100%',] : '{$this->width}',
-        offset = screen.width <= 850 ? 0 : '70px',
-        _id, _tempId, _btnId, _tb;
-    
-    setId(id);
-    
-    function hidden(index) {
-        {$this->events['hidden']}
-        
-        $(_id).trigger('dialog:hidden');
-    }
-    
-    function open(btn) {
-        var index = layer.open({
-          type: 1,
-          title: '{$this->title}',
-          area: area,
-          offset: offset,
-          maxmin: false,
-          resize: false,
-          content: $(_tempId).html(),
-          success: function(layero, index) {
-              $(_id).attr('layer', index);
-              
-              setDataId($(_id));
-              
-              {$this->events['shown']}
-              
-              setTimeout(function () {
-                  Dcat.grid.AsyncTable({container: _tb});
-                  
-                  $(_tb).trigger('table:load');
-              }, 100);
-              
-              $(_id).trigger('dialog:shown');
-              
-              $(_id).on('dialog:open', openDialog);
-              $(_id).on('dialog:close', closeDialog)
-          },
-          cancel: function (index, layero) {
-              btn && btn.removeAttr('layer');
-              
-              hidden(index)
-          }
-        });
-        
-        btn && btn.attr('layer', index);
-    }
-    
-    function setDataId(obj) {
-        if (! obj.attr('data-id')) {
-            obj.attr('data-id', id);
-        }
-    }
-    
-    function setId(val) {
-        if (! val) return;
-        
-        id = val;
-        _id = '#'+id;
-        _tempId = '#temp-'+id;
-        _btnId = '#button-'+id;
-        _tb = _id+' .async-table';
-    }
-    
-    function openDialog () {
-        setId($(this).attr('data-id'));
-        setDataId($(this));
-        
-        if (! $(this).attr('layer')) {
-            open($(this));
-        }
-    }
-    
-    function closeDialog() {
-        var index = $(this).attr('layer');
-        
-        $(_id).removeAttr('layer');
-        $(_btnId).removeAttr('layer');
-        
-        if (index) {
-            layer.close(index);
-            hidden(index);
-        }
-    }
-    
-    $(_btnId).on('click', openDialog);
-})();
-JS;
     }
 
-    public function html()
+    public function render()
     {
-        $table = $this->renderTable();
-
         $this->addScript();
 
-        return <<<HTML
-{$this->renderButton()}
-<template id="temp-{$this->id()}">
-    <div {$this->formatHtmlAttributes()}>
-        <div class="p-2 dialog-body">{$table}</div>
-        {$this->renderFooter()}
-    </div>
-</template>
-HTML;
+        $this->with([
+            'id'     => $this->id(),
+            'title'  => $this->title,
+            'width'  => $this->width,
+            'button' => $this->renderButton(),
+            'table'  => $this->renderTable(),
+            'footer' => $this->renderFooter(),
+            'events' => $this->events,
+        ]);
+
+        return parent::render();
     }
 
     protected function renderTable()
     {
-        return <<<HMLT
-{$this->table->render()}
-HMLT;
+        return $this->table->render();
     }
 
     protected function renderFooter()
     {
-        $footer = Helper::render($this->footer);
-
-        if (! $footer) {
-            return;
-        }
-
-        return <<<HTML
-<div class="dialog-footer layui-layer-btn">{$footer}</div>
-HTML;
+        return Helper::render($this->footer);
     }
 
     protected function renderButton()
@@ -331,8 +230,6 @@ HTML;
             $button = "<a href=\"javascript:void(0)\">{$button}</a>";
         }
 
-        return <<<HTML
-<span style="cursor: pointer" id="button-{$this->id()}">{$button}</span>
-HTML;
+        return $button;
     }
 }

+ 20 - 140
src/Widgets/Dropdown.php

@@ -2,10 +2,7 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Dcat\Admin\Admin;
-use Illuminate\Contracts\Support\Arrayable;
-use Illuminate\Contracts\Support\Renderable;
-use Illuminate\Support\Arr;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Support\Str;
 
 class Dropdown extends Widget
@@ -17,17 +14,14 @@ class Dropdown extends Widget
      */
     protected static $dividerHtml = '<li class="dropdown-divider"></li>';
 
-    /**
-     * @var string
-     */
-    protected $template = '<span class="dropdown" style="display:inline-block">%s<ul class="dropdown-menu">%s</ul></span>';
+    protected $view = 'admin::widgets.dropdown';
 
     /**
      * @var array
      */
     protected $button = [
         'text'  => null,
-        'class' => 'btn btn-sm btn-white  waves-effect',
+        'class' => 'btn btn-sm btn-white waves-effect',
         'style' => null,
     ];
 
@@ -51,11 +45,6 @@ class Dropdown extends Widget
      */
     protected $click = false;
 
-    /**
-     * @var array
-     */
-    protected $firstOptions = [];
-
     public function __construct(array $options = [])
     {
         $this->options($options);
@@ -69,23 +58,13 @@ class Dropdown extends Widget
      *
      * @return $this
      */
-    public function options($options = [], string $title = null)
+    public function options($options = [], ?string $title = null)
     {
         if (! $options) {
             return $this;
         }
 
-        if ($options instanceof Arrayable) {
-            $options = $options->toArray();
-        }
-
-        $options = (array) $options;
-
-        if (! $this->options) {
-            $this->firstOptions = &$options;
-        }
-
-        $this->options[] = [$title, &$options];
+        $this->options[] = [$title, Helper::array($options)];
 
         return $this;
     }
@@ -104,16 +83,6 @@ class Dropdown extends Widget
         return $this;
     }
 
-    /**
-     * Without text of button.
-     *
-     * @return $this
-     */
-    public function withoutTextButton()
-    {
-        return $this->button('');
-    }
-
     /**
      * Set the button class.
      *
@@ -121,7 +90,7 @@ class Dropdown extends Widget
      *
      * @return $this
      */
-    public function buttonClass(string $class)
+    public function buttonClass(?string $class)
     {
         $this->button['class'] = $class;
 
@@ -135,7 +104,7 @@ class Dropdown extends Widget
      *
      * @return $this
      */
-    public function buttonStyle(string $style)
+    public function buttonStyle(?string $style)
     {
         $this->button['style'] = $style;
 
@@ -181,7 +150,7 @@ class Dropdown extends Widget
     {
         $this->click = true;
 
-        $this->buttonId = 'dropd_'.Str::random(8);
+        $this->buttonId = 'dropd-'.Str::random(8);
 
         if ($defaultLabel !== null) {
             $this->button($defaultLabel);
@@ -190,62 +159,6 @@ class Dropdown extends Widget
         return $this;
     }
 
-    /**
-     * Set the template of dropdown menu.
-     *
-     * @param string|\Closure|Renderable $template
-     *
-     * @return $this
-     */
-    public function template($template)
-    {
-        $this->template = $this->toString($template);
-
-        return $this;
-    }
-
-    /**
-     * @return string
-     */
-    protected function renderButton()
-    {
-        if (is_null($this->button['text']) && ! $this->click) {
-            return;
-        }
-
-        $text = $this->button['text'];
-        $class = $this->button['class'];
-        $style = $this->button['style'];
-
-        if ($this->click && ! $text) {
-            if (Arr::isAssoc($this->firstOptions)) {
-                $text = array_keys($this->firstOptions)[0];
-            } else {
-                $text = $this->firstOptions[0] ?? '';
-            }
-
-            if (is_array($text)) {
-                $text = $text['label'] ?? current($text);
-            }
-        }
-
-        return str_replace(
-            ['{id}', '{class}', '{style}', '{text}'],
-            [
-                $this->buttonId,
-                $class,
-                $style ? "style='$style'" : '',
-                $text ? " $text &nbsp;" : '',
-            ],
-            <<<'HTML'
-<a id="{id}" class="{class} dropdown-toggle " data-toggle="dropdown" href="javascript:void(0)" {style}>
-    <stub>{text}</stub>
-    <span class="caret"></span>
-</a>
-HTML
-        );
-    }
-
     /**
      * @return string
      */
@@ -259,21 +172,21 @@ HTML
      */
     protected function renderOptions()
     {
-        $opt = '';
+        $html = '';
 
         foreach ($this->options as &$items) {
             [$title, $options] = $items;
 
             if ($title) {
-                $opt .= "<li class='dropdown-header'>$title</li>";
+                $html .= "<li class='dropdown-header'>$title</li>";
             }
 
             foreach ($options as $key => $val) {
-                $opt .= $this->renderOption($key, $val);
+                $html .= $this->renderOption($key, $val);
             }
         }
 
-        return $opt;
+        return $html;
     }
 
     /**
@@ -308,46 +221,13 @@ HTML
      */
     public function render()
     {
-        if (is_null($this->button['text']) && ! $this->options) {
-            return '';
-        }
-
-        $button = $this->renderButton();
-
-        if (! $this->options) {
-            return $button;
-        }
-
-        $opt = $this->renderOptions();
-
-        if (! $button) {
-            return sprintf('<ul class="dropdown-menu">%s</ul>', $opt);
-        }
-
-        $label = $this->button['text'];
-
-        if ($this->click) {
-            Admin::script(
-                <<<JS
-(function () {
-    var btn = $('#{$this->buttonId}'), _a = btn.parent().find('ul li a'), text = '$label';                
-    _a.on('click', function () {
-        btn.find('stub').html($(this).html() + ' &nbsp;');
-    });
-    if (text) {
-        btn.find('stub').html(text + ' &nbsp;');
-    } else {
-        (!_a.length) || btn.find('stub').html($(_a[0]).html() + ' &nbsp;');
-    }
-})();
-JS
-            );
-        }
-
-        return sprintf(
-            $this->template,
-            $button,
-            $opt
-        );
+        $this->with([
+            'options'  => $this->renderOptions(),
+            'button'   => $this->button,
+            'buttonId' => $this->buttonId,
+            'click'    => $this->click,
+        ]);
+
+        return parent::render();
     }
 }

+ 0 - 5
src/Widgets/Dump.php

@@ -6,11 +6,6 @@ use Illuminate\Contracts\Support\Renderable;
 
 class Dump extends Widget
 {
-    /**
-     * @var string
-     */
-    protected $view = 'admin::widgets.dump';
-
     /**
      * @var string
      */

+ 12 - 48
src/Widgets/Form.php

@@ -697,60 +697,17 @@ HTML;
         return $this->useAjaxSubmit === true;
     }
 
-    /**
-     * @return void
-     */
-    protected function setUpSubmitScript()
-    {
-        $confirm = json_encode($this->confirm);
-
-        Admin::script(
-            <<<JS
-$('#{$this->getElementId()}').form({
-    validate: true,
-    confirm: {$confirm},
-    success: function (data) {
-        {$this->buildSuccessScript()}
-        {$this->addSavedScript()}
-    },
-    error: function (response) {
-        {$this->buildErrorScript()}
-        {$this->addErrorScript()}
-    }
-});
-JS
-        );
-    }
-
-    /**
-     * @return string|void
-     *
-     * @deprecated 将在2.0版本中废弃,请用 addSavedScript 代替
-     */
-    protected function buildSuccessScript()
-    {
-    }
-
     /**
      * @return string|void
      */
-    protected function addSavedScript()
+    protected function savedScript()
     {
     }
 
     /**
      * @return string|void
-     *
-     * @deprecated 将在2.0版本中废弃,请用 addErrorScript 代替
      */
-    protected function buildErrorScript()
-    {
-    }
-
-    /**
-     * @return string|void
-     */
-    protected function addErrorScript()
+    protected function errorScript()
     {
     }
 
@@ -836,7 +793,12 @@ JS
         $this->prepareHandler();
 
         if ($this->allowAjaxSubmit()) {
-            $this->setUpSubmitScript();
+            $this->addVariables([
+                'elementId'   => $this->getElementId(),
+                'confirm'     => $this->confirm,
+                'savedScript' => $this->savedScript(),
+                'errorScript' => $this->errorScript(),
+            ]);
         }
 
         $tabObj = $this->getTab();
@@ -845,9 +807,11 @@ JS
             $tabObj->addScript();
         }
 
-        $this->addVariables(['tabObj' => $tabObj]);
+        $this->addVariables([
+            'tabObj' => $tabObj,
+        ]);
 
-        return view($this->view, $this->variables())->render();
+        return Admin::view($this->view, $this->variables());
     }
 
     /**

+ 13 - 18
src/Widgets/Markdown.php

@@ -2,14 +2,16 @@
 
 namespace Dcat\Admin\Widgets;
 
-use Dcat\Admin\Admin;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Contracts\Support\Renderable;
 use Illuminate\Support\Str;
 
 class Markdown extends Widget
 {
+    protected $view = 'admin::widgets.markdown';
+
     /**
-     * @var string
+     * @var string|Renderable
      */
     protected $content;
 
@@ -29,9 +31,7 @@ class Markdown extends Widget
 
     public function __construct($markdown = '')
     {
-        $markdown && $this->content($markdown);
-
-        Admin::collectAssets('@editor-md');
+        $this->content($markdown);
     }
 
     /**
@@ -46,27 +46,22 @@ class Markdown extends Widget
         return $this;
     }
 
-    protected function build()
+    protected function renderContent()
     {
-        if ($this->content instanceof Renderable) {
-            $this->content = $this->content->render();
-        }
-
-        return <<<EOF
-<div {$this->formatHtmlAttributes()}><textarea style="display:none;">{$this->content}</textarea></div>
-EOF;
+        return Helper::render($this->content);
     }
 
     public function render()
     {
-        $id = 'mkd-'.Str::random();
+        $id = 'mkd-'.Str::random(8);
 
         $this->defaultHtmlAttribute('id', $id);
 
-        $opts = json_encode($this->options);
-
-        Admin::script("editormd.markdownToHTML('$id', $opts);");
+        $this->with([
+            'id'      => $id,
+            'content' => $this->renderContent(),
+        ]);
 
-        return $this->build();
+        return parent::render();
     }
 }

+ 2 - 2
src/Widgets/Modal.php

@@ -287,7 +287,7 @@ class Modal extends Widget
         return $this->on('hidden.bs.modal', $script);
     }
 
-    protected function addEventScript()
+    protected function addScript()
     {
         if (! $this->events) {
             return;
@@ -332,7 +332,7 @@ JS
     public function render()
     {
         $this->addLoadRenderableScript();
-        $this->addEventScript();
+        $this->addScript();
 
         if ($this->join) {
             return $this->renderButton().parent::render();

+ 0 - 7
src/Widgets/Radio.php

@@ -7,19 +7,12 @@ use Illuminate\Contracts\Support\Arrayable;
 class Radio extends Widget
 {
     protected $view = 'admin::widgets.radio';
-
     protected $type = 'radio';
-
     protected $style = 'primary';
-
     protected $right = '16px';
-
     protected $checked;
-
     protected $disabledValues = [];
-
     protected $size;
-
     protected $inline = false;
 
     public function __construct(

+ 2 - 2
src/Widgets/Tooltip.php

@@ -115,7 +115,7 @@ class Tooltip extends Widget
         return $this;
     }
 
-    protected function setupScript()
+    protected function addScript()
     {
         $background = $this->background ?: Admin::color()->primary(-5);
         $title = $this->title;
@@ -147,6 +147,6 @@ JS
         }
         $this->built = true;
 
-        $this->setupScript();
+        $this->addScript();
     }
 }

+ 7 - 3
src/Widgets/Widget.php

@@ -120,10 +120,10 @@ abstract class Widget implements Renderable
      */
     public function variables()
     {
-        return array_merge($this->variables, [
+        return array_merge([
             'attributes' => $this->formatHtmlAttributes(),
             'options'    => $this->options,
-        ]);
+        ], $this->variables);
     }
 
     /**
@@ -209,7 +209,11 @@ abstract class Widget implements Renderable
             return;
         }
 
-        return Admin::view($this->view, $this->variables());
+        $result = Admin::resolveHtml(view($this->view, $this->variables()), ['runScript' => $this->runScript]);
+
+        $this->script = $result['script'];
+
+        return $result['html'];
     }
 
     /**