From 4651e0ab6064662fdc83216cec42f7065d56b449 Mon Sep 17 00:00:00 2001 From: Emil Momchev <80454439+Mrgoblings@users.noreply.github.com> Date: Fri, 29 Nov 2024 14:03:12 +0200 Subject: [PATCH] [Dashboard] Load Navigation Groups from externel module (#4479) * add: navigation-groups extensionpoint and handler * fix: corrected the group uri --- .../NavigationGroupsService.js | 22 ++++ ...dashboard-navigation-groups.extensionpoint | 4 + .../portal/js/navigation-controller.js | 117 +++++++++--------- 3 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/NavigationGroupsService.js create mode 100644 components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/dashboard-navigation-groups.extensionpoint diff --git a/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/NavigationGroupsService.js b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/NavigationGroupsService.js new file mode 100644 index 0000000000..93f934c351 --- /dev/null +++ b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/NavigationGroupsService.js @@ -0,0 +1,22 @@ +import { extensions } from "sdk/extensions"; +import { response } from "sdk/http"; + +const groupList = []; +const groupExtensions = extensions.getExtensions("dashboard-navigation-groups"); + +for (let i = 0; i < groupExtensions.length; i++) { + const extensionPath = groupExtensions[i]; + + let path = `../../../${extensionPath}`; + + const { getGroup } = await import(path); + + try { + const group = getGroup(); + groupList.push(group); + } catch (err) { + console.error(err) + } +} + +response.println(JSON.stringify(groupList)); diff --git a/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/dashboard-navigation-groups.extensionpoint b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/dashboard-navigation-groups.extensionpoint new file mode 100644 index 0000000000..be05b029ba --- /dev/null +++ b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/api/NavigationGroupsExtension/dashboard-navigation-groups.extensionpoint @@ -0,0 +1,4 @@ +{ + "name": "dashboard-navigation-groups", + "description": "ExtensionPoint For the Navigation Groups" +} \ No newline at end of file diff --git a/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/js/navigation-controller.js b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/js/navigation-controller.js index 33d5bcd443..ffc37da46c 100644 --- a/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/js/navigation-controller.js +++ b/components/resources/resources-portal/src/main/resources/META-INF/dirigible/portal/js/navigation-controller.js @@ -4,63 +4,40 @@ navigation.controller("LaunchpadViewController", ["$scope", "messageHub", "$http $scope.extraExtensionPoints = ['app', "dashboard-navigations", "dashboard-widgets"]; $scope.groups = []; - - $scope.switchView = function (id, event) { - if (event) event.stopPropagation(); - $scope.currentViewId = id; - }; - - $scope.isGroupVisible = function (group) { - const items = $scope.groupItems[group.label.toLowerCase()]; - return items.some(function (item) { - return $scope.currentViewId === item.id; - }); - }; - - messageHub.onDidReceiveMessage('launchpad.switch.perspective', function (msg) { - $scope.$apply(function () { - $scope.switchView(msg.data.viewId); - }); - }, true) - $scope.groupItems = []; - $scope.groupItems['assets'] = []; - $scope.groupItems["purchasing"] = []; - $scope.groupItems["sales"] = []; - $scope.groupItems["inventory"] = []; - $scope.groupItems["reports"] = []; - $scope.groupItems["products"] = []; - $scope.groupItems["employees"] = []; - $scope.groupItems["partners"] = []; - $scope.groupItems["configurations"] = []; - - $scope.groups = [ - { "label": "Assets", "icon": "it-host" }, - { "label": "Purchasing", "icon": "credit-card" }, - { "label": "Sales", "icon": "currency" }, - { "label": "Inventory", "icon": "retail-store" }, - { "label": "Reports", "icon": "area-chart" }, - { "label": "Products", "icon": "product" }, - { "label": "Employees", "icon": "company-view" }, - { "label": "Partners", "icon": "customer-and-contacts" }, - { "label": "Configurations", "icon": "wrench" } - ] + function loadNavigationGroups() { + return $http.get("/services/js/portal/api/NavigationGroupsExtension/NavigationGroupsService.js") + .then(function (response) { + $scope.groups = response.data; - $http.get("/services/js/portal/api/NavigationExtension/NavigationService.js") - .then(function (response) { - $scope.navigationList = response.data; + response.data.forEach(elem => { + $scope.groupItems[elem.label.toLowerCase()] = []; + }); + }) + .catch(function (error) { + console.error('Error fetching navigation groups:', error); + $scope.state = { error: true, errorMessage: 'Failed to load navigation groups' }; + return async () => { }; + }); + } - $scope.navigationList.forEach(e => addNavigationItem(e)); + function loadNavigationItems() { + return $http.get("/services/js/portal/api/NavigationExtension/NavigationService.js") + .then(function (response) { + $scope.navigationList = response.data; - $scope.groupItems.forEach(e => e.sort((a, b) => a.order - b.order)); + $scope.navigationList.forEach(e => addNavigationItem(e)); - }) - .catch(function (error) { - console.error('Error fetching navigation list:', error); - $scope.state.error = true; - $scope.errorMessage = 'Failed to load navigation list'; - }); + Object.values($scope.groupItems).forEach(items => { + items.sort((a, b) => a.order - b.order); + }); + }) + .catch(function (error) { + console.error('Error fetching navigation items:', error); + $scope.state = { error: true, errorMessage: 'Failed to load navigation items' }; + }); + } function addNavigationItem(itemData) { if (!itemData || !itemData.label || !itemData.group || !itemData.order || !itemData.link) { @@ -68,16 +45,40 @@ navigation.controller("LaunchpadViewController", ["$scope", "messageHub", "$http return; } - itemData.group = itemData.group.toLowerCase(); - if (!$scope.groupItems[itemData.group]) { - console.error('Group key not found:', itemData.group); + const groupKey = itemData.group.toLowerCase(); + if (!$scope.groupItems[groupKey]) { + console.error('Group key not found:', groupKey); return; } - $scope.groupItems[itemData.group].push({ - "id": itemData.id, - "label": itemData.label, - "link": itemData.link + $scope.groupItems[groupKey].push({ + id: itemData.id, + label: itemData.label, + link: itemData.link }); } + + loadNavigationGroups() + .then(loadNavigationItems) + .catch(function (error) { + console.error('Error during initialization:', error); + }); + + $scope.switchView = function (id, event) { + if (event) event.stopPropagation(); + $scope.currentViewId = id; + }; + + $scope.isGroupVisible = function (group) { + const items = $scope.groupItems[group.label.toLowerCase()]; + return items.some(function (item) { + return $scope.currentViewId === item.id; + }); + }; + + messageHub.onDidReceiveMessage('launchpad.switch.perspective', function (msg) { + $scope.$apply(function () { + $scope.switchView(msg.data.viewId); + }); + }, true) }]); \ No newline at end of file