| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 | 
							- <?php
 
- namespace Encore\Admin;
 
- use Closure;
 
- use Encore\Admin\Tree\Tools;
 
- use Illuminate\Contracts\Support\Renderable;
 
- use Illuminate\Database\Eloquent\Model;
 
- class Tree implements Renderable
 
- {
 
-     /**
 
-      * @var array
 
-      */
 
-     protected $items = [];
 
-     /**
 
-      * @var string
 
-      */
 
-     protected $elementId = 'tree-';
 
-     /**
 
-      * @var Model
 
-      */
 
-     protected $model;
 
-     /**
 
-      * @var \Closure
 
-      */
 
-     protected $queryCallback;
 
-     /**
 
-      * View of tree to render.
 
-      *
 
-      * @var string
 
-      */
 
-     protected $view = [
 
-         'tree'   => 'admin::tree',
 
-         'branch' => 'admin::tree.branch',
 
-     ];
 
-     /**
 
-      * @var \Closure
 
-      */
 
-     protected $callback;
 
-     /**
 
-      * @var null
 
-      */
 
-     protected $branchCallback = null;
 
-     /**
 
-      * @var bool
 
-      */
 
-     public $useCreate = true;
 
-     /**
 
-      * @var bool
 
-      */
 
-     public $useSave = true;
 
-     /**
 
-      * @var bool
 
-      */
 
-     public $useRefresh = true;
 
-     /**
 
-      * @var array
 
-      */
 
-     protected $nestableOptions = [];
 
-     /**
 
-      * Header tools.
 
-      *
 
-      * @var Tools
 
-      */
 
-     public $tools;
 
-     /**
 
-      * Menu constructor.
 
-      *
 
-      * @param Model|null $model
 
-      */
 
-     public function __construct(Model $model = null, \Closure $callback = null)
 
-     {
 
-         $this->model = $model;
 
-         $this->path = app('request')->getPathInfo();
 
-         $this->elementId .= uniqid();
 
-         $this->setupTools();
 
-         if ($callback instanceof \Closure) {
 
-             call_user_func($callback, $this);
 
-         }
 
-         $this->initBranchCallback();
 
-     }
 
-     /**
 
-      * Setup tree tools.
 
-      */
 
-     public function setupTools()
 
-     {
 
-         $this->tools = new Tools($this);
 
-     }
 
-     /**
 
-      * Initialize branch callback.
 
-      *
 
-      * @return void
 
-      */
 
-     protected function initBranchCallback()
 
-     {
 
-         if (is_null($this->branchCallback)) {
 
-             $this->branchCallback = function ($branch) {
 
-                 $key = $branch[$this->model->getKeyName()];
 
-                 $title = $branch[$this->model->getTitleColumn()];
 
-                 return "$key - $title";
 
-             };
 
-         }
 
-     }
 
-     /**
 
-      * Set branch callback.
 
-      *
 
-      * @param \Closure $branchCallback
 
-      *
 
-      * @return $this
 
-      */
 
-     public function branch(\Closure $branchCallback)
 
-     {
 
-         $this->branchCallback = $branchCallback;
 
-         return $this;
 
-     }
 
-     /**
 
-      * Set query callback this tree.
 
-      *
 
-      * @return Model
 
-      */
 
-     public function query(\Closure $callback)
 
-     {
 
-         $this->queryCallback = $callback;
 
-         return $this;
 
-     }
 
-     /**
 
-      * Set nestable options.
 
-      *
 
-      * @param array $options
 
-      *
 
-      * @return $this
 
-      */
 
-     public function nestable($options = [])
 
-     {
 
-         $this->nestableOptions = array_merge($this->nestableOptions, $options);
 
-         return $this;
 
-     }
 
-     /**
 
-      * Disable create.
 
-      *
 
-      * @return void
 
-      */
 
-     public function disableCreate()
 
-     {
 
-         $this->useCreate = false;
 
-     }
 
-     /**
 
-      * Disable save.
 
-      *
 
-      * @return void
 
-      */
 
-     public function disableSave()
 
-     {
 
-         $this->useSave = false;
 
-     }
 
-     /**
 
-      * Disable refresh.
 
-      *
 
-      * @return void
 
-      */
 
-     public function disableRefresh()
 
-     {
 
-         $this->useRefresh = false;
 
-     }
 
-     /**
 
-      * Save tree order from a input.
 
-      *
 
-      * @param string $serialize
 
-      *
 
-      * @return bool
 
-      */
 
-     public function saveOrder($serialize)
 
-     {
 
-         $tree = json_decode($serialize, true);
 
-         if (json_last_error() != JSON_ERROR_NONE) {
 
-             throw new \InvalidArgumentException(json_last_error_msg());
 
-         }
 
-         $this->model->saveOrder($tree);
 
-         return true;
 
-     }
 
-     /**
 
-      * Build tree grid scripts.
 
-      *
 
-      * @return string
 
-      */
 
-     protected function script()
 
-     {
 
-         $deleteConfirm = trans('admin.delete_confirm');
 
-         $saveSucceeded = trans('admin.save_succeeded');
 
-         $refreshSucceeded = trans('admin.refresh_succeeded');
 
-         $deleteSucceeded = trans('admin.delete_succeeded');
 
-         $confirm = trans('admin.confirm');
 
-         $cancel = trans('admin.cancel');
 
-         $nestableOptions = json_encode($this->nestableOptions);
 
-         return <<<SCRIPT
 
-         $('#{$this->elementId}').nestable($nestableOptions);
 
-         $('.tree_branch_delete').click(function() {
 
-             var id = $(this).data('id');
 
-             swal({
 
-                 title: "$deleteConfirm",
 
-                 type: "warning",
 
-                 showCancelButton: true,
 
-                 confirmButtonColor: "#DD6B55",
 
-                 confirmButtonText: "$confirm",
 
-                 showLoaderOnConfirm: true,
 
-                 cancelButtonText: "$cancel",
 
-                 preConfirm: function() {
 
-                     return new Promise(function(resolve) {
 
-                         $.ajax({
 
-                             method: 'post',
 
-                             url: '{$this->path}/' + id,
 
-                             data: {
 
-                                 _method:'delete',
 
-                                 _token:LA.token,
 
-                             },
 
-                             success: function (data) {
 
-                                 $.pjax.reload('#pjax-container');
 
-                                 toastr.success('{$deleteSucceeded}');
 
-                                 resolve(data);
 
-                             }
 
-                         });
 
-                     });
 
-                 }
 
-             }).then(function(result) {
 
-                 var data = result.value;
 
-                 if (typeof data === 'object') {
 
-                     if (data.status) {
 
-                         swal(data.message, '', 'success');
 
-                     } else {
 
-                         swal(data.message, '', 'error');
 
-                     }
 
-                 }
 
-             });
 
-         });
 
-         $('.{$this->elementId}-save').click(function () {
 
-             var serialize = $('#{$this->elementId}').nestable('serialize');
 
-             $.post('{$this->path}', {
 
-                 _token: LA.token,
 
-                 _order: JSON.stringify(serialize)
 
-             },
 
-             function(data){
 
-                 $.pjax.reload('#pjax-container');
 
-                 toastr.success('{$saveSucceeded}');
 
-             });
 
-         });
 
-         $('.{$this->elementId}-refresh').click(function () {
 
-             $.pjax.reload('#pjax-container');
 
-             toastr.success('{$refreshSucceeded}');
 
-         });
 
-         $('.{$this->elementId}-tree-tools').on('click', function(e){
 
-             var action = $(this).data('action');
 
-             if (action === 'expand') {
 
-                 $('.dd').nestable('expandAll');
 
-             }
 
-             if (action === 'collapse') {
 
-                 $('.dd').nestable('collapseAll');
 
-             }
 
-         });
 
- SCRIPT;
 
-     }
 
-     /**
 
-      * Set view of tree.
 
-      *
 
-      * @param string $view
 
-      */
 
-     public function setView($view)
 
-     {
 
-         $this->view = $view;
 
-     }
 
-     /**
 
-      * Return all items of the tree.
 
-      *
 
-      * @param array $items
 
-      */
 
-     public function getItems()
 
-     {
 
-         return $this->model->withQuery($this->queryCallback)->toTree();
 
-     }
 
-     /**
 
-      * Variables in tree template.
 
-      *
 
-      * @return array
 
-      */
 
-     public function variables()
 
-     {
 
-         return [
 
-             'id'         => $this->elementId,
 
-             'tools'      => $this->tools->render(),
 
-             'items'      => $this->getItems(),
 
-             'useCreate'  => $this->useCreate,
 
-             'useSave'    => $this->useSave,
 
-             'useRefresh' => $this->useRefresh,
 
-         ];
 
-     }
 
-     /**
 
-      * Setup grid tools.
 
-      *
 
-      * @param Closure $callback
 
-      *
 
-      * @return void
 
-      */
 
-     public function tools(Closure $callback)
 
-     {
 
-         call_user_func($callback, $this->tools);
 
-     }
 
-     /**
 
-      * Render a tree.
 
-      *
 
-      * @return \Illuminate\Http\JsonResponse|string
 
-      */
 
-     public function render()
 
-     {
 
-         Admin::script($this->script());
 
-         view()->share([
 
-             'path'           => $this->path,
 
-             'keyName'        => $this->model->getKeyName(),
 
-             'branchView'     => $this->view['branch'],
 
-             'branchCallback' => $this->branchCallback,
 
-         ]);
 
-         return view($this->view['tree'], $this->variables())->render();
 
-     }
 
-     /**
 
-      * Get the string contents of the grid view.
 
-      *
 
-      * @return string
 
-      */
 
-     public function __toString()
 
-     {
 
-         return $this->render();
 
-     }
 
- }
 
 
  |