Browse Source

增加Tree::maxDepth方法

jqh 4 years ago
parent
commit
726ea81f70
3 changed files with 24 additions and 13 deletions
  1. 2 2
      resources/views/tree/container.blade.php
  2. 1 0
      src/Http/Controllers/MenuController.php
  3. 21 11
      src/Tree.php

+ 2 - 2
resources/views/tree/container.blade.php

@@ -49,11 +49,11 @@
     </div>
 </div>
 
-<script>
+<script require="@jquery.nestable">
     var id = '{{ $id }}';
     var tree = $('#'+id);
 
-    tree.nestable({!! json_encode($nestableOptions) !!});
+    tree.nestable({!! admin_javascript_json($nestableOptions) !!});
 
     $('.'+id+'-save').on('click', function () {
         var serialize = tree.nestable('serialize'), _this = $(this);

+ 1 - 0
src/Http/Controllers/MenuController.php

@@ -68,6 +68,7 @@ class MenuController extends AdminController
             $tree->disableCreateButton();
             $tree->disableQuickCreateButton();
             $tree->disableEditButton();
+            $tree->maxDepth(3);
 
             $tree->actions(function (Tree\Actions $actions) {
                 if ($actions->getRow()->extension) {

+ 21 - 11
src/Tree.php

@@ -19,6 +19,11 @@ use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Str;
 use Illuminate\Support\Traits\Macroable;
 
+/**
+ * Class Tree
+ *
+ * @see https://github.com/dbushell/Nestable
+ */
 class Tree implements Renderable
 {
     use HasBuilderEvents;
@@ -144,8 +149,7 @@ class Tree implements Renderable
 
         $this->elementId .= Str::random(8);
 
-        $this->setupTools();
-        $this->requireAssets();
+        $this->setUpTools();
 
         if ($callback instanceof \Closure) {
             call_user_func($callback, $this);
@@ -157,7 +161,7 @@ class Tree implements Renderable
     /**
      * Setup tree tools.
      */
-    public function setupTools()
+    public function setUpTools()
     {
         $this->tools = new Tools($this);
     }
@@ -186,14 +190,6 @@ class Tree implements Renderable
         return $repository;
     }
 
-    /**
-     * Collect assets.
-     */
-    protected function requireAssets()
-    {
-        Admin::requireAssets('jquery.nestable');
-    }
-
     /**
      * Initialize branch callback.
      *
@@ -237,6 +233,20 @@ class Tree implements Renderable
         return $this;
     }
 
+    /**
+     * number of levels an item can be nested (default 5)
+     *
+     * @see https://github.com/dbushell/Nestable
+     *
+     * @param int $max
+     *
+     * @return $this
+     */
+    public function maxDepth(int $max)
+    {
+        return $this->nestable(['maxDepth' => $max]);
+    }
+
     /**
      * Set nestable options.
      *