Skip to content

Commit

Permalink
Allow navigation through url & back button (#596)
Browse files Browse the repository at this point in the history
* Allow navigation through url & back button

* Minor fix

* Update navigationService.js
  • Loading branch information
pablotr9 authored and Jesús Ángel committed Mar 11, 2019
1 parent bd63932 commit c4db739
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 deletions.
4 changes: 2 additions & 2 deletions SplunkAppForWazuh/appserver/static/js/run/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ define(['./module'], function (module) {
$navigationService,
$currentDataService
) {
//Go to last state at login
$navigationService.goToLastState()
//Go to last state or to a specified tab if "currentTab" param is specified in the url
$navigationService.manageState()

async function checkBeforeTransition(state) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,106 @@
define(['../module'], function(module) {
define(['../module'], function (module) {
'use strict'

class navigationService {
constructor($state, $window) {
constructor($state, $window, $location) {
this.$state = $state
this.$window = $window
this.$location = $location

this.$window.onpopstate = function (event) {
try {
let lastState = sessionStorage.history.split(',')

let newHistory = lastState[0]
if (lastState.length > 1) { // if there are previous states
for (var i = 1; i < lastState.length - 2; i++) {
newHistory += ',' + lastState[i]
}
sessionStorage.history = newHistory
lastState = lastState[lastState.length - 2]
$state.go(
lastState,
{}, //routeParams
{ reload: true, notify: false }
)
}

} catch (error) {
sessionStorage.params
? $state.go(sessionStorage.params)
: $state.go('settings.api')
}
}
}

storeRoute(params) {
sessionStorage.params = params
this.addToHistory(params)

this.$location.search('currentTab', params) //set currentTab to the new state
}

addToHistory(url) {
try{
if (!sessionStorage.history) {
sessionStorage.history = url
} else {
const history = sessionStorage.history.split(',')
history.push(url)
let newHistory = history[0]
const len = history.length
if (history.length < 20) {
for (let i = 1; i < len; i++) {
if (history[i - 1] !== history[i])
newHistory += ',' + history[i]
}
} else {
for (let i = 11; i < len; i++) {
if (history[i - 11] !== history[i])
newHistory += ',' + history[i]
}
}
sessionStorage.history = newHistory

}
}catch(error){
this.goToLastState()
}
}

goToLastState() {
sessionStorage.params
? this.$state.go(sessionStorage.params)
: this.$state.go('settings.api')
? this.$state.go(sessionStorage.params)
: this.$state.go('settings.api') // redirects to settings.api if no previous states were visited
}

/* *
* Redirects the user to the tab specified in the url (index?currentTab=)
* if no tab is specified it redirects to the last state visited
* */
manageState(){
try{
const url_params = this.$location.search()
if(url_params.currentTab){
this.$state.go(url_params.currentTab)
} else{
this.goToLastState()
}
}catch(error){
this.$state.go('settings.api')
}
}

getLastState() {
if (sessionStorage.params) return sessionStorage.params
return sessionStorage.params || undefined
}

setCurrentAgent(currentAgentId) {
sessionStorage.currentAgent = currentAgentId
}

getCurrentAgent() {
if (sessionStorage.currentAgent) return sessionStorage.currentAgent
return sessionStorage.currentAgent || undefined
}

setCurrentDevTools(current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ define([
'splunkjs/mvc',
'splunkjs/mvc/simpleform/formutils',
'splunkjs/mvc/simplexml/urltokenmodel'
], function(module, mvc, FormUtils, UrlTokenModel) {
], function (module, mvc, FormUtils, UrlTokenModel) {
'use strict'

class urlTokenModel {
Expand All @@ -23,13 +23,15 @@ define([

this.urlTokenModel.on('url:navigate', () => {
this.defaultTokenModel.set(this.urlTokenModel.toJSON())
if (
!_.isEmpty(this.urlTokenModel.toJSON()) && // eslint-disable-line
!_.all(this.urlTokenModel.toJSON(), _.isUndefined) // eslint-disable-line
) {
this.submitTokens()
} else {
this.submittedTokenModel.clear()
if (typeof _.isEmpty !== "undefined") {
if (
!_.isEmpty(this.urlTokenModel.toJSON()) && // eslint-disable-line
!_.all(this.urlTokenModel.toJSON(), _.isUndefined) // eslint-disable-line
) {
this.submitTokens()
} else {
this.submittedTokenModel.clear()
}
}
})
}
Expand Down

0 comments on commit c4db739

Please sign in to comment.