From 228d0df5be32af7a5eef9477d9d4dba5e177a4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 13 Jul 2016 17:48:42 +0200 Subject: [PATCH] Feature #4211 #3278: Keep navigation history --- src/sunstone/public/app/app.js | 8 +- src/sunstone/public/app/main.js | 5 +- src/sunstone/public/app/sunstone.js | 82 +++++++++++++++++-- .../public/app/tabs/vms-tab/panels/info.js | 7 +- .../public/app/tabs/vnets-topology-tab.js | 21 +---- .../public/app/utils/tab-datatable.js | 2 +- src/sunstone/public/bower.json | 6 +- src/sunstone/sunstone-server.rb | 2 + 8 files changed, 95 insertions(+), 38 deletions(-) diff --git a/src/sunstone/public/app/app.js b/src/sunstone/public/app/app.js index c1f8fc866e..7c4e5733db 100644 --- a/src/sunstone/public/app/app.js +++ b/src/sunstone/public/app/app.js @@ -27,7 +27,6 @@ define(function(require) { _setupDataTableSearch(); - var DASHBOARD_TAB_ID = require('tabs/dashboard-tab/tabId'); var SETTINGS_TAB_ID = require('tabs/settings-tab/tabId'); var PROVISION_TAB_ID = require('tabs/provision-tab/tabId'); var Sunstone = require('sunstone'); @@ -61,6 +60,7 @@ define(function(require) { Menu.insertProvision(); }else{ Menu.insert(); + Sunstone.setupNavigoRoutes(); } _setupAccordion(); @@ -69,11 +69,9 @@ define(function(require) { if (Config.isTabEnabled(PROVISION_TAB_ID)) { Sunstone.showTab(PROVISION_TAB_ID); - $('#loading').hide(); - } else if (Config.isTabEnabled(DASHBOARD_TAB_ID)) { - Sunstone.showTab(DASHBOARD_TAB_ID); - $('#loading').hide(); } + + $('#loading').hide(); }); function _setupCloseDropdownsOnClick() { diff --git a/src/sunstone/public/app/main.js b/src/sunstone/public/app/main.js index d44342f59b..be3b269442 100644 --- a/src/sunstone/public/app/main.js +++ b/src/sunstone/public/app/main.js @@ -107,7 +107,10 @@ require.config({ 'spice-filexfer': '../bower_components/spice-html5/filexfer', /* vis.js */ - 'vis': '../bower_components/vis/dist/vis.min' + 'vis': '../bower_components/vis/dist/vis.min', + + /* navigo */ + 'Navigo': '../bower_components/navigo/lib/navigo.min' }, shim: { /* Tabs */ diff --git a/src/sunstone/public/app/sunstone.js b/src/sunstone/public/app/sunstone.js index bafb45f9e6..7e9ef24866 100644 --- a/src/sunstone/public/app/sunstone.js +++ b/src/sunstone/public/app/sunstone.js @@ -25,10 +25,14 @@ define(function(require) { var Notifier = require('utils/notifier'); var Menu = require('utils/menu'); var Tips = require('utils/tips'); + var Navigo = require('Navigo'); + + var router; var TOP_INTERVAL = 10000; //ms var CONFIRM_DIALOG_ID = require('utils/dialogs/confirm/dialogId'); var CONFIRM_WITH_SELECT_DIALOG_ID = require('utils/dialogs/confirm-with-select/dialogId'); + var DASHBOARD_TAB_ID = require('tabs/dashboard-tab/tabId'); var SunstoneCfg = { "actions" : {}, @@ -121,7 +125,7 @@ define(function(require) { //Inserts all main tabs in the DOM var _insertTabs = function() { - for (tabName in SunstoneCfg["tabs"]) { + for (var tabName in SunstoneCfg["tabs"]) { _insertTab(tabName); _insertButtonsInTab(tabName); _setupDataTable(tabName); @@ -505,7 +509,7 @@ define(function(require) { // Button to return to the list view from the detailed view $(document).on("click", "button[href='back']", function(e) { - $(".navigation-active-li a", $("#navigation")).click(); + window.history.back(); e.preventDefault(); }); } @@ -572,6 +576,18 @@ define(function(require) { } var _showTab = function(tabName) { + if (_getTab() == tabName && _rightListVisible()){ + _routerShowTab(tabName); + } else { + if (router != undefined){ + router.navigate(tabName); + }else{ + _routerShowTab(tabName); + } + } + } + + var _routerShowTab = function(tabName) { $('.labels-tree', '#navigation').remove(); if (!SunstoneCfg['tabs'][tabName]) { @@ -614,8 +630,28 @@ define(function(require) { return $(".tab:visible").attr("id"); } - var _showElement = function(tabName, infoAction, elementId) { - _showTab(tabName); + var _showElement = function(tabName, elementId) { + if(!Config.isTabEnabled(tabName)){ + return; + } + + if (router != undefined){ + router.navigate(tabName + "/" + elementId); + }else{ + _routerShowElement(tabName, elementId); + } + } + + var _routerShowElement = function(tabName, elementId) { + var resource = SunstoneCfg["tabs"][tabName].resource; + + if (resource == undefined){ + return; + } + + var infoAction = resource + ".show"; + + _routerShowTab(tabName); var context = $('#' + tabName); @@ -993,6 +1029,10 @@ define(function(require) { } function _popFormPanelLoading(tabId) { + if (!_formPanelVisible($("#"+tabId)) && router != undefined){ + router.navigate(tabId+"/form"); + } + var context = $("#" + tabId); $(".sunstone-list", context).hide(); $(".sunstone-info", context).hide(); @@ -1093,6 +1133,36 @@ define(function(require) { return dialogInstance; } + var _setupNavigoRoutes = function() { + router = new Navigo(null, true); + + for (var tabName in SunstoneCfg["tabs"]) { + router.on(new RegExp("(?:#|/)"+tabName+"/form"), function(){ + }.bind(tabName)); + + router.on(new RegExp("(?:#|/)"+tabName+"/(\\d+)"), function(id){ + _routerShowElement(this, id) + }.bind(tabName)); + + router.on(new RegExp("(?:#|/)"+tabName), function(){ + _routerShowTab(this); + }.bind(tabName)); + } + + router.on(function(){ + _routerShowTab(DASHBOARD_TAB_ID); + }); + + $(document).on("click", "a", function(e){ + if ($(this).attr("href") != undefined && + $(this).attr("href").startsWith("#")){ + e.preventDefault(); + } + }); + + router.resolve(); + } + var Sunstone = { "addMainTabs": _addMainTabs, "addDialogs": _addDialogs, @@ -1128,7 +1198,9 @@ define(function(require) { "insertButtonsInTab": _insertButtonsInTab, - "TOP_INTERVAL": TOP_INTERVAL + "TOP_INTERVAL": TOP_INTERVAL, + + "setupNavigoRoutes": _setupNavigoRoutes } return Sunstone; diff --git a/src/sunstone/public/app/tabs/vms-tab/panels/info.js b/src/sunstone/public/app/tabs/vms-tab/panels/info.js index 739704df67..19ceea2e36 100644 --- a/src/sunstone/public/app/tabs/vms-tab/panels/info.js +++ b/src/sunstone/public/app/tabs/vms-tab/panels/info.js @@ -127,12 +127,7 @@ define(function(require) { PermissionsTable.setup(TAB_ID, RESOURCE, this.element, context); $("a.vrid", context).on("click", function(){ - // TODO: this should be checked internally in showElement, - // but it won't work because of bug #4198 - - if (Config.isTabEnabled("vrouters-tab")){ - Sunstone.showElement("vrouters-tab", "VirtualRouter.show", $(this).text()); - } + Sunstone.showElement("vrouters-tab", $(this).text()); }); // Get rid of the unwanted (for show) SCHED_* keys diff --git a/src/sunstone/public/app/tabs/vnets-topology-tab.js b/src/sunstone/public/app/tabs/vnets-topology-tab.js index feb7175e1e..4318839de1 100644 --- a/src/sunstone/public/app/tabs/vnets-topology-tab.js +++ b/src/sunstone/public/app/tabs/vnets-topology-tab.js @@ -485,26 +485,11 @@ define(function(require) { _network.on("doubleClick", function(params) { if (params.nodes.length == 1 && _network.isCluster(params.nodes[0]) == false) { if ( params.nodes[0].match(/vm\d+/) ){ - // TODO: this should be checked internally in showElement, - // but it won't work because of bug #4198 - - if (Config.isTabEnabled("vms-tab")){ - Sunstone.showElement("vms-tab", "VM.show", params.nodes[0].split("vm")[1]); - } + Sunstone.showElement("vms-tab", params.nodes[0].split("vm")[1]); } else if ( params.nodes[0].match(/vnet\d+/) ){ - // TODO: this should be checked internally in showElement, - // but it won't work because of bug #4198 - - if (Config.isTabEnabled("vnets-tab")){ - Sunstone.showElement("vnets-tab", "Network.show", params.nodes[0].split("vnet")[1]); - } + Sunstone.showElement("vnets-tab", params.nodes[0].split("vnet")[1]); } else if ( params.nodes[0].match(/vr\d+/) ){ - // TODO: this should be checked internally in showElement, - // but it won't work because of bug #4198 - - if (Config.isTabEnabled("vrouters-tab")){ - Sunstone.showElement("vrouters-tab", "VirtualRouter.show", params.nodes[0].split("vr")[1]); - } + Sunstone.showElement("vrouters-tab", params.nodes[0].split("vr")[1]); } } }); diff --git a/src/sunstone/public/app/utils/tab-datatable.js b/src/sunstone/public/app/utils/tab-datatable.js index 8963a0b5c4..071819ad7b 100644 --- a/src/sunstone/public/app/utils/tab-datatable.js +++ b/src/sunstone/public/app/utils/tab-datatable.js @@ -402,7 +402,7 @@ define(function(require) { var id = $(aData[0]).val(); if (!id) return true; - Sunstone.showElement(tableObj.tabId, tableObj.resource + ".show", id); + Sunstone.showElement(tableObj.tabId, id); return false; } diff --git a/src/sunstone/public/bower.json b/src/sunstone/public/bower.json index 2ee1b6d279..abf81ad0ba 100644 --- a/src/sunstone/public/bower.json +++ b/src/sunstone/public/bower.json @@ -22,10 +22,12 @@ "requirejs": "2.1.22", "foundation-sites": "6.2.1", "jquery": "2.2.3", - "datatables": "1.10.12" + "datatables": "1.10.12", + "navigo": "2.1.1" }, "authors": [ - "Daniel Molina " + "Daniel Molina ", + "Carlos Martin " ], "description": "OpenNebula Sunstone", "moduleType": [ diff --git a/src/sunstone/sunstone-server.rb b/src/sunstone/sunstone-server.rb index 4e4259c625..6750fe6b75 100755 --- a/src/sunstone/sunstone-server.rb +++ b/src/sunstone/sunstone-server.rb @@ -479,6 +479,8 @@ get '/login' do content_type 'text/html', :charset => 'utf-8' if !authorized? erb :login + else + redirect to('/') end end