Afform - Insta-refresh menubar when adding a navigation item
authorColeman Watts <coleman@civicrm.org>
Mon, 18 Jul 2022 16:17:37 +0000 (12:17 -0400)
committerColeman Watts <coleman@civicrm.org>
Mon, 18 Jul 2022 16:25:19 +0000 (12:25 -0400)
ext/afform/admin/ang/afGuiEditor/afGuiEditor.component.js

index 12f4d927de4cc5d09ca4ea805d1fd858fecf9ae0..a1bdb7a7aae45e5e8548a6bb7771e55d789a14eb 100644 (file)
@@ -30,6 +30,7 @@
         undoHistory = [],
         undoPosition = 0,
         undoAction = null,
+        lastSaved,
         sortableOptions = {};
 
       // ngModelOptions to debounce input
         $scope.layoutHtml = '';
         $scope.entities = {};
         setEditorLayout();
+        setLastSaved();
 
         if (editor.afform.navigation) {
           loadNavigationMenu();
               snapshot.saved = index === undoPosition;
               snapshot.afform.name = data[0].name;
             });
+            if (!angular.equals(afform.navigation, lastSaved.navigation) ||
+              (afform.server_route !== lastSaved.server_route && afform.navigation)
+              (afform.icon !== lastSaved.icon && afform.navigation)
+            ) {
+              refreshMenubar();
+            }
+            setLastSaved();
           });
       };
 
         }
       });
 
+      // Sets last-saved form metadata (used to determine if the menubar needs refresh)
+      function setLastSaved() {
+        lastSaved = JSON.parse(angular.toJson(editor.afform));
+        delete lastSaved.layout;
+      }
+
+      // Force-refresh the menubar to instantly display the afform menu item
+      function refreshMenubar() {
+        CRM.menubar.destroy();
+        CRM.menubar.cacheCode = Math.random();
+        CRM.menubar.initialize();
+      }
+
       // Force editor panels to a fixed height, to avoid palette scrolling offscreen
       function fixEditorHeight() {
         var height = $(window).height() - $('#afGuiEditor').offset().top;