Scripting Back and Forward Button

Guys, I'm trying to code my navigation bar so that it has a back/forward function. I did what's below but It's not working. Can anybody help me?

#Event on View (Script onStartup)
def runAction(self):
    Petrobras.navigation.update_history(self)

# 'next', 'previous', 'up', 'down', 'back', 'forward'
def runAction(self, event):
    system.perspective.sendMessage('mainNavigation', {'command': 'previous'})

#Code inside my views
def onMessageReceived(self, payload):
    Petrobras.navigation.default_navigation(self.view, payload)

#Global Script
def default_navigation(view, payload):
    # Get the custom property from the message
    index = view.session.custom.navigation.index
    history = view.session.custom.navigation.history
    system.perspective.print(index)
    system.perspective.print(history)
    command = payload.get('command')
    # Check if the custom property is not None and not blank
    if command is not None and command.strip():
        # Perform a "Switch-case" and navigate to the corresponding URL for one of the variables
        if command in ['next', 'previous', 'up', 'down']:
            system.perspective.navigate(view=view.custom.navigation[command])
        elif command == 'forward':
            system.perspective.print("Forward function")
            index += 1
            next_page = view.session.custom.navigation.history[index]
            system.perspective.navigate(view=next_page)
            session.custom.navigation.index = index
        elif command == 'back':
            if index == 0:
                index = 9
                previous_page = view.session.custom.navigation.history[index]
                system.perspective.navigate(view=previous_page)
                session.custom.navigation.index = index
            else:
                index -= 1
                previous_page = view.session.custom.navigation.history[index]
                system.perspective.navigate(view=previous_page)
                session.custom.navigation.index = index

def update_history(view_current):
    history = view_current.session.custom.navigation.history
    index = view_current.session.custom.navigation.index
    
    if len(history) == 0:
        index = 0
        history.append(view_current.page.props.primaryView)
        index += 1
    else:
        history.append(view_current.page.props.primaryView)
        index += 1
        history = history[-10:]
        index = len(history) - 1
    
    view_current.session.custom.navigation.history = history
    view_current.session.custom.navigation.index = index



Hi Lazaro, and welcome.

This forum has a "preformatted text" tool in its comment editor that you really need to use with posted code/errors/logs for them to be readable/reviewable. Please edit your post, highlight the pasted code, and click that button. (Use the pencil icon to edit.)

Roger that. Thank you!

Few things. First, I recommend putting your code in Code Blocks to make it easier to read:
image

Second, is there a reason you're not using the system.perspective.navigate back and system.perspective.navigateForward functions, or a navigate event action?

Lastly, I suspect one issue is that your code references session properties in a project script file. I don't believe the script library will have context of those session props, but I could be wrong.

Lazaro is passing the view object into the scripts, so the scripts certainly have access to all the Perspective session and view properties through it.

3 Likes

You're likely to get better and more focused assistance when you can provide us an explanation as to EXACTLY what you expect your code to do, and EXACTLY what it is not doing to your meet your expectations. This is especially true when code you provide references custom properties because we won't have those in place, meaning your code will fail to compile on our end. Please provide your expectations, as well as a screenshot of the properties this code expects to be in place.