From 37b0369a474dcbeaaa36ffd9ff547a57c90efdfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Gannaz?= Date: Sun, 10 Jan 2021 17:36:26 +0100 Subject: [PATCH] partial rewrite --- README.md | 2 +- composer.json | 3 +- src/Fancytree.php | 88 ++++++++++++++++++++++------------------- src/FancytreeAssets.php | 28 ++++++------- 4 files changed, 63 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index eff16b2..58f9559 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ A richer code sample that shows the full syntax of this widget: ``` Url::to(['tree/children'], // default is "" (read from options["source"]) + 'url' => Url::to(['tree/children'], // default is "" (read from options["source"]) 'cache' => false, // default is true 'skin' => 'awesome', // default is "vista" 'options' => [], // genuine FancyTree configuration diff --git a/composer.json b/composer.json index afc2e4a..a7ef847 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "silecs/yii2-fancytree", "description": "Yii2 FancyTree widget", "type": "yii2-extension", + "version": "2.0.0', "keywords": [ "yii2", "tree", @@ -20,7 +21,7 @@ "require": { "yiisoft/yii2": "~2", "yiisoft/yii2-jui": "~2", - "bower-asset/fancytree": "~2", + "bower-asset/fancytree": "~2.37.0", "bower-asset/js-cookie": "~2" }, "autoload": { diff --git a/src/Fancytree.php b/src/Fancytree.php index 097de1c..e88ea9e 100644 --- a/src/Fancytree.php +++ b/src/Fancytree.php @@ -1,9 +1,5 @@ options['source'] = [ 'url' => $url, 'cache' => $this->cache ]; - $this->options['lazyLoad'] = new \yii\web\JsExpression(' -function(event, data){ - var node = data.node; - // Load child nodes via ajax GET url?mode=children&parent=1234 - data.result = { - url: "' . addslashes($url) . '", - data: { id: node.key }, - cache: ' . ($this->cache ? "true" : "false") . ' - }; -}' + $encodedUrl = json_encode($url); + $encodedCache = json_encode($this->cache); + $this->options['lazyLoad'] = new \yii\web\JsExpression( + <<skin) { - FancytreeAssets::$skin = $this->skin; + parent::init(); + if (!empty($this->options['skin'])) { + FancytreeAssets::$skin = $this->options['skin']; } if ( isset($this->options['extensions']) @@ -92,6 +95,7 @@ public function init() FancytreeAssets::$cookies = true; } FancytreeAssets::register($this->getView()); + if ($this->url) { $this->applyAjaxUrl($this->url); } @@ -100,23 +104,27 @@ public function init() /** * @inheritdoc */ - public function run() + public function run(): string { - $id = (empty($this->options['id']) ? 'fancytree-' . $this->getId() : $this->options['id']); + $id = empty($this->options['id']) ? 'fancytree-' . $this->getId() : $this->options['id']; + $selector = json_encode("#{$id}"); if ($this->activeNode) { - $this->options['init'] = new \yii\web\JsExpression(" -function(event, data) { -console.log(data); -\$('#{$id}').fancytree('getTree').activateKey('{$this->activeNode}'); -}" + $activeNode = json_encode($this->activeNode); + $this->options['init'] = new \yii\web\JsExpression( + << $id, 'class' => 'fancytree']); + // Loads jQuery and the initialisation JS code $this->getView()->registerJs( - "$('#{$id}').fancytree(" - . Json::encode($this->options) - . ");" + "$({$selector}).fancytree(" . Json::encode($this->options) . ");" ); + + $this->classes[] = 'fancytree'; + return Html::tag('div', '', ['id' => $id, 'class' => join(" ", $this->classes)]); } } diff --git a/src/FancytreeAssets.php b/src/FancytreeAssets.php index 99ae0eb..adeeda0 100644 --- a/src/FancytreeAssets.php +++ b/src/FancytreeAssets.php @@ -1,9 +1,5 @@ js[] = 'js-cookie/src/js.cookie.js'; } - $this->js[] = 'fancytree/dist/jquery.fancytree-all' - . (defined('YII_DEBUG') && YII_DEBUG ? '.js' : '.min.js'); + $this->js[] = 'fancytree/dist/jquery.fancytree-all-deps.min.js'; + $this->css[] = "fancytree/dist/skin-" . self::$skin . '/ui.fancytree' . (defined('YII_DEBUG') && YII_DEBUG ? '.css' : '.min.css'); + parent::registerAssetFiles($view); } }