Navigating windows using relative paths

Having searched this forum, and the ideas site, I couldn’t find something about my “situation”.

Consider I have the following window hierarchy:

Windows
˫ Folder 1
  ˫ Subfolder 1
    ˫ Window 1
    ˫ Window 2
  ˪ Subfolder 2
    ...
˪ Folder 2

Whenever I want to navigate from one window to another I usually perform a swap, say:

system.nav.swapWindow(
    'Folder 1/Subfolder 1/Window 1', # or system.nav.getCurrentWindow()
    'Folder 1/Subfolder 1/Window 2',
    params
)

What would be extremely awesome would be the following:

# From: 'Folder 1/Subfolder 1/Window 1'
# To:  'Folder 1/Subfolder 1/Window 2'
system.nav.swapWindow(
    system.nav.getCurrentWindow(),
    './Window 2',
    params
)

# Or
system.nav.swapTo(
    './Window 2',
    params
)

Thoughts?

Should I just build it myself?

Which window is the current window? There can be any number open at any given time. Including none open at all.

In my example, the current window is 'Folder 1/Subfolder 1/Window 1'.

Modified code for clarity.

# From: 'Folder 1/Subfolder 1/Window 1'
# To:  'Folder 1/Subfolder 1/Window 2'
system.nav.swapWindow(
    'Folder 1/Subfolder 1/Window 1',
    './Window 2', # Full path: 'Folder 1/Subfolder 1/Window 2'
    params
)

This type of stuff is fun to build. Enclosed is how I would do it. Place the following script inside of a shared script, like shared.nav

from com.inductiveautomation.ignition.common.model import ApplicationScope

PATH_SEPARATOR = "/"
DIR_UP_CHAR = ".."
PARENT_DIR_CHAR = "."

SCOPE = ApplicationScope.toCode(ApplicationScope.getGlobalScope())
def swapWindow(window1,window2,params = {}):
	
	if SCOPE in ("C","D"):
		pathParts = []
		wp1 = window1.split("/")
		wp2 = window2.split("/")
		if wp2[0] not in (DIR_UP_CHAR, PARENT_DIR_CHAR):
			pathPaths = wp2
		elif wp2[0] == DIR_UP_CHAR:
			pathParts = wp1[:-2] + wp2[1:]
		elif wp2[0] == PARENT_DIR_CHAR:
			pathParts = wp1[:-1] + wp2[1:]
			
		window = []
		for part in pathParts:
			if part == DIR_UP_CHAR and len(window):
				window = window[:-1]
			elif part == PARENT_DIR_CHAR:
				pass
			else:
				window.append(part)
		windowPath = "/".join(window)

		system.nav.swapWindow(window1,windowPath,params)	

Then, instead of using system.nav.swapWindow, use shared.nav.swapWindow

swapWindow("Hello/This/Is/My/Path","../OtherWindow",{"someId":"alf"})```
1 Like

Thanks, @Kyle_Chase! :ok_hand:

This is exactly what I had in mind.

Do you mind if I borrow this piece of code and incorporate it into a library I’ve shared on GitHub? (Shameless plug)

I’ll make a pull request tomorrow and you can pull it in that way?

Sure thing, Kyle!!! (Additional exclamation points added because posts must be at least 20 characters long.)

Sorry, Kyle, but I couldn’t stop myself; I jumped the gun, and incorporated your function into incendium,

Thanks!

See here: incendium.nav