From 4a512151fea2e74262da6b721350d5abaa59beee Mon Sep 17 00:00:00 2001
From: Mingsong Hu <mingsonghu@Mingsongs-MBP.gateway>
Date: Mon, 30 Sep 2019 15:32:55 +1000
Subject: [PATCH] Support more theme options for jsTree

---
 js/Plugin/jstree/hm.jstree.js                 | 21 ++++++++++++-------
 src/Form/HmMenuForm.php                       |  8 +------
 src/Form/HmOverviewTerms.php                  |  8 +------
 .../HmDisplayPlugin/HmDisplayJstree.php       | 21 +++++++------------
 4 files changed, 23 insertions(+), 35 deletions(-)

diff --git a/js/Plugin/jstree/hm.jstree.js b/js/Plugin/jstree/hm.jstree.js
index a0d48b2..9cf24cf 100644
--- a/js/Plugin/jstree/hm.jstree.js
+++ b/js/Plugin/jstree/hm.jstree.js
@@ -14,13 +14,24 @@
           const treeContainer = $(this);
           const parentID = treeContainer.attr('parent-id');
           const searchTextID = (parentID) ? '#hm-jstree-search-' + parentID : '#hm-jstree-search';
-          const theme = treeContainer.attr("theme");
-          const dots = treeContainer.attr("dots");
+          const optionsJson = treeContainer.attr("options");
           const dataURL = treeContainer.attr('data-source') + '&parent=0';
           const updateURL = treeContainer.attr('url-update')
           let reload = true;
           let rollback = false;
           let after = 1;
+          let themes = {
+              dots: false,
+              name: 'default'
+          };
+          let options;
+          
+          if (optionsJson) {
+            options = JSON.parse(optionsJson);
+            if (options.theme) {
+              themes = options.theme;
+            }
+          }
           // Ajax callback to refresh the tree.
           if (reload) {
             // Build the tree.
@@ -36,11 +47,7 @@
                     return node;
                   }
                 },
-                themes: {
-                  dots: dots === '1',
-                  name: theme
-                },
-                'check_callback' : true,
+                themes: themes,
                 "multiple": false,
               },
               search: {
diff --git a/src/Form/HmMenuForm.php b/src/Form/HmMenuForm.php
index cb55707..896102f 100644
--- a/src/Form/HmMenuForm.php
+++ b/src/Form/HmMenuForm.php
@@ -98,13 +98,7 @@ class HmMenuForm extends MenuForm {
         $source_url = Url::fromRoute('hierarchy_manager.menu.tree.json', ['mid' => $mid], ['query' => ['token' => $token, 'destination' => $destination]])->toString();
         $update_url = Url::fromRoute('hierarchy_manager.menu.tree.update', ['mid' => $mid], ['query' => ['token' => $token]])->toString();       
         $config = $display_profile->get("config");
-        if (!empty($config)) {
-          $options = json_decode($config);
-        }
-        else {
-          $options = NULL;
-        }
-        return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state, $options);
+        return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state, $config);
       }
     }
     
diff --git a/src/Form/HmOverviewTerms.php b/src/Form/HmOverviewTerms.php
index f24f0da..c861cd4 100644
--- a/src/Form/HmOverviewTerms.php
+++ b/src/Form/HmOverviewTerms.php
@@ -62,13 +62,7 @@ class HmOverviewTerms extends OverviewTerms {
                 $source_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.json', ['vid' => $vid], ['query' => ['token' => $token, 'destination' => $destination]])->toString();
                 $update_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.update', ['vid' => $vid], ['query' => ['token' => $token]])->toString();
                 $config = $display_profile->get("config");
-                if (!empty($config)) {
-                  $options = json_decode($config);
-                }
-                else {
-                  $options = NULL;
-                }
-                return $instance->getForm($source_url, $update_url, $form, $form_state, $options);
+                return $instance->getForm($source_url, $update_url, $form, $form_state, $config);
               }
             }
           }
diff --git a/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php b/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php
index 62c73c6..d25e7b2 100644
--- a/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php
+++ b/src/Plugin/HmDisplayPlugin/HmDisplayJstree.php
@@ -28,22 +28,16 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte
         $parent_id = $parent_formObj->getFormId();
       }
       
-      // The jsTree theme.
+      // The jsTree default theme.
       $theme = 'default';
-      $dot = '';
       
-      if (isset($options->theme)) {
-        if (isset($options->theme->name)) {
-          // The name of the theme.
-          $theme =  $options->theme->name;
-        }
-        if (isset($options->theme->dot)) {
-          // The dot line setting.
-          $dot = $options->theme->dot ? '1' : '0';
+      if (!empty($options)) {
+        $jsonObj = json_decode($options);
+        if (isset($jsonObj->theme) && isset($jsonObj->theme->name)) {
+          $theme = $jsonObj->theme->name;
         }
       }
-
-     
+    
       // Search input.
       $form['search'] = [
         '#type' => 'textfield',
@@ -73,8 +67,7 @@ class HmDisplayJstree extends HmDisplayPluginBase implements HmDisplayPluginInte
           ],
           'id' => isset($parent_id) ? 'hm-jstree-' . $parent_id : 'hm-jstree',
           'parent-id' => isset($parent_id) ? $parent_id : '',
-          'theme' => $theme,
-          'dots' => $dot,
+          'options' => $options,
           'data-source' => $url_source,
           'url-update' => $url_update,
         ],
-- 
GitLab