فهرست منبع

hasMany表单JS重构

jqh 4 سال پیش
والد
کامیت
79293be7f3

+ 0 - 5
resources/assets/dcat/js/dcat-app.js

@@ -126,8 +126,3 @@ function prepare(Dcat) {
 win.CreateDcat = function(config) {
     return prepare(new Dcat(config));
 };
-
-win.replaceNestedFormIndex = function (value) {
-    return value;
-};
-

+ 0 - 1
resources/views/form/hasmany.blade.php

@@ -76,7 +76,6 @@
 
         var template = replaceNestedFormIndex(tpl.html());
         $(forms).append(template);
-        {!! $templateScript !!}
     });
 
     $(container).on('click', '.remove', function () {

+ 0 - 1
resources/views/form/hasmanytab.blade.php

@@ -92,7 +92,6 @@
         $(container+' > .nav').append(navTabHtml);
         $(container+' > .tab-content').append(paneHtml);
         $(container+' > .nav > li:last-child a').click();
-        {!! $templateScript !!}
     });
 
     if ($('.has-error').length) {

+ 0 - 1
resources/views/form/hasmanytable.blade.php

@@ -95,7 +95,6 @@
 
         var template = replaceNestedFormIndex(tpl.html());
         $('.has-many-{{ $columnClass }}-forms').append(template);
-        {!! $templateScript !!}
     });
 
     $(container).on('click', '.remove', function () {

+ 2 - 2
resources/views/form/keyvalue.blade.php

@@ -46,7 +46,7 @@
                     <td class="form-group">
                         <div>
                             <div class="kv-remove btn btn-white btn-sm pull-right">
-                                <i class="feather icon-trash">&nbsp;</i>{{ __('admin.remove') }}
+                                <i class="feather icon-trash">&nbsp;</i>
                             </div>
                         </div>
                     </td>
@@ -89,7 +89,7 @@
             <td class="form-group">
                 <div>
                     <div class="kv-remove btn btn-white btn-sm pull-right">
-                        <i class="feather icon-trash">&nbsp;</i>{{ __('admin.remove') }}
+                        <i class="feather icon-trash">&nbsp;</i>
                     </div>
                 </div>
             </td>

+ 2 - 2
resources/views/form/listfield.blade.php

@@ -30,7 +30,7 @@
 
                     <td style="width: 85px;">
                         <div class="{{$class}}-remove btn btn-white btn-sm pull-right">
-                            <i class="feather icon-trash">&nbsp;</i>{{ __('admin.remove') }}
+                            <i class="feather icon-trash">&nbsp;</i>
                         </div>
                     </td>
                 </tr>
@@ -62,7 +62,7 @@
 
             <td style="width: 85px;">
                 <div class="list-remove btn btn-white btn-sm pull-right">
-                    <i class="feather icon-trash">&nbsp;</i>{{ __('admin.remove') }}
+                    <i class="feather icon-trash">&nbsp;</i>
                 </div>
             </td>
         </tr>

+ 7 - 1
src/Form/Field/Date.php

@@ -36,7 +36,13 @@ class Date extends Text
         $this->options['locale'] = config('app.locale');
         $this->options['allowInputToggle'] = true;
 
-        $this->script = "$('{$this->getElementClassSelector()}').datetimepicker(".admin_javascript_json($this->options).');';
+        $options = admin_javascript_json($this->options);
+
+        $this->script = <<<JS
+Dcat.init('{$this->getElementClassSelector()}', function (self) {
+    self.datetimepicker({$options});
+});
+JS;
 
         $this->prepend('<i class="fa fa-calendar fa-fw"></i>')
             ->defaultAttribute('style', 'width: 200px;flex:none');

+ 8 - 23
src/Form/Field/HasMany.php

@@ -495,15 +495,11 @@ class HasMany extends Field
         // specify a view to render.
         $this->view = $this->views[$this->viewMode];
 
-        [$template, $script] = $this->buildNestedForm()
-            ->getTemplateHtmlAndScript();
-
         $this->addVariables([
             'forms'          => $this->buildRelatedForms(),
-            'template'       => $template,
+            'template'       => $this->buildNestedForm()->getTemplate(),
             'relationName'   => $this->relationName,
             'options'        => $this->options,
-            'templateScript' => $script,
             'count'          => count($this->value()),
             'columnClass'    => $this->columnClass,
         ]);
@@ -523,12 +519,9 @@ class HasMany extends Field
         $headers = [];
         $fields = [];
         $hidden = [];
-        $scripts = [];
 
         /* @var Field $field */
         foreach ($this->buildNestedForm()->fields() as $field) {
-            $field->runScript(false);
-
             if (is_a($field, Hidden::class)) {
                 $hidden[] = $field->render();
             } else {
@@ -538,13 +531,6 @@ class HasMany extends Field
                 $fields[] = $field->render();
                 $headers[] = $field->label();
             }
-
-            /*
-             * Get and remove the last script of Admin::$script stack.
-             */
-            if ($script = $field->getScript()) {
-                $scripts[] = $script;
-            }
         }
 
         /* Build row elements */
@@ -561,14 +547,13 @@ class HasMany extends Field
         $this->view = $this->views[$this->viewMode];
 
         $this->addVariables([
-            'headers'        => $headers,
-            'forms'          => $this->buildRelatedForms(),
-            'template'       => $template,
-            'relationName'   => $this->relationName,
-            'options'        => $this->options,
-            'templateScript' => implode(";\r\n", $scripts),
-            'count'          => count($this->value()),
-            'columnClass'    => $this->columnClass,
+            'headers'      => $headers,
+            'forms'        => $this->buildRelatedForms(),
+            'template'     => $template,
+            'relationName' => $this->relationName,
+            'options'      => $this->options,
+            'count'        => count($this->value()),
+            'columnClass'  => $this->columnClass,
         ]);
 
         return parent::render();

+ 4 - 13
src/Form/NestedForm.php

@@ -379,28 +379,19 @@ class NestedForm
     /**
      * Get the html and script of template.
      *
-     * @return array
+     * @return string
      */
-    public function getTemplateHtmlAndScript()
+    public function getTemplate()
     {
         $html = '';
-        $scripts = [];
 
         /* @var Field $field */
         foreach ($this->fields() as $field) {
-
             //when field render, will push $script to Admin
-            $html .= $field->runScript(false)->render();
-
-            /*
-             * Get and remove the last script of Admin::$script stack.
-             */
-            if ($script = $field->getScript()) {
-                $scripts[] = $script;
-            }
+            $html .= $field->render();
         }
 
-        return [$html, implode(";\r\n", $scripts)];
+        return $html;
     }
 
     /**

+ 10 - 10
src/Grid/Filter/Presenter/SelectTable.php

@@ -118,8 +118,6 @@ class SelectTable extends Presenter
     protected function setUpTable()
     {
         $this->dialog
-            ->id('dialog-'.$this->id)
-            ->runScript(false)
             ->footer($this->renderFooter())
             ->button($this->renderButton());
     }
@@ -151,14 +149,16 @@ class SelectTable extends Presenter
 
         Admin::script(
             <<<JS
-{$this->dialog->getScript()}
-
-Dcat.grid.SelectTable({
-    dialog: '#{$this->dialog->id()}',
-    container: '#{$this->id}',
-    input: '#hidden-{$this->id}',
-    values: {$options},
-});
+Dcat.init('#{$this->id}', function (self) {
+    var dialogId = self.parent().find('{$this->dialog->getElementSelector()}').attr('id');
+    
+    Dcat.grid.SelectTable({
+        dialog: '[data-id="' + dialogId + '"]',
+        container: '#{$this->id}',
+        input: '#hidden-{$this->id}',
+        values: {$options},
+    });
+})
 JS
         );
     }

+ 2 - 0
src/Traits/HasHtml.php

@@ -122,6 +122,8 @@ trait HasHtml
             }
 
             if ($init = $element->getAttribute('init')) {
+                $init = str_replace("'", "\\'", $init);
+
                 $script = "Dcat.init('{$init}', function (\$this, id) { {$script}\n });";
             } else {
                 $script = "(function () {{$script}\n})();";