Folders All the Way Down - Building a Safety Instruction Browser with the Hierarchy Control
ui

Published in ui , peakboardhub , dataflows

Folders All the Way Down - Building a Safety Instruction Browser with the Hierarchy Control

Feb 28, 2023 13 mins read

Every plant has them: safety instructions for the press, the welding station, the forklift, the acid cabinet. They are usually PDFs, and they usually live in a folder structure that somebody built years ago and that grows with every new machine. The people on the shop floor need them right next to the machine, and the people who write them want to drop a new PDF into a folder and be done with it.

In this article we build a terminal that does exactly that. The PDFs are stored in the file management of the Peakboard Hub, in as many nested folders as we like. On the board, a hierarchy control shows that folder tree, and a tap on an instruction opens the PDF in the PDF viewer next to it.

The interesting part is what we do not build: there is no list of folders anywhere in the project, no button per document, no screen per area. The structure we see on the board is created at runtime from the structure in the Hub. A new folder or a new PDF in the Hub shows up on the terminal with the next reload, and nobody has to open the Designer for it.

The folders in the Hub

We start in the file management of the Peakboard Hub. Under a new folder SafetyInstructions we create one folder per area and put the PDFs into them. To make it interesting, the depth is not the same everywhere: the general instructions sit one level down, the warehouse two levels down, and the assembly line in hall 2 even three levels down.

SafetyInstructions/
├── Chemicals/
│   ├── Compressed Gas Cylinders.pdf
│   └── Handling Acids and Alkalis.pdf
├── General/
│   ├── Emergency Procedures.pdf
│   └── Fire Safety.pdf
├── Logistics/
│   ├── Forklift Operation.pdf
│   └── Warehouse/
│       ├── High-Bay Racking.pdf
│       └── Loading Dock.pdf
└── Production/
    ├── Hall 1/
    │   ├── CNC Milling Machine.pdf
    │   └── Hydraulic Press.pdf
    └── Hall 2/
        ├── Paint Booth.pdf
        ├── Welding Station.pdf
        └── Assembly Line/
            └── Torque Tools.pdf

Folders are created with New folder, and the PDFs are uploaded with Add file. The sample PDFs we use here are available in the download section at the end of the article, so the same tree can be rebuilt in a few minutes. Folder SafetyInstructions with its subfolders in the file management of the Peakboard Hub

Reading the files from the Hub

In the Designer, we add a Peakboard Hub Files data source. It lists the files of a Hub folder, and with Check subfolders turned on it walks down the whole tree. We point it to /SafetyInstructions/, set the document type filter to PDF and let it reload every 300 seconds.

Each row of the result is one file. The column that matters most for us is Path: it holds the full Hub path of the file, for example /SafetyInstructions/Production/Hall 2/Welding Station.pdf. Folders themselves don’t appear as rows, only the files inside them. We will see in a moment why that is no problem at all. Peakboard Hub Files data source with path, subfolder option and PDF filter, and the preview with the Path column

The dataflow

We could connect the hierarchy control straight to the data source, but three small details would bother us:

  • The data source returns the newest files first. The hierarchy control builds the tree in the order of the rows, so the folders would appear in a more or less random order.
  • Every path starts with /SafetyInstructions/. Taken as it is, the whole tree would hang below one single root node called SafetyInstructions, and the operator would have to open it every time before seeing anything useful.
  • The file names end with .pdf, which nobody needs to read on a terminal.

A dataflow fixes all three. It is called InstructionTree and has three steps after the data source:

  1. Sort by Path, ascending. Now the areas and folders come in alphabetical order.
  2. Add column TreePath, which is the path without the root folder.
  3. Add column Title, which is the file name without the extension.

The InstructionTree dataflow with its steps and the new TreePath and Title columns in the preview

Both new columns are calculated with a short Lua expression that runs once per row. For TreePath we cut the root folder off the front of the path:

-- Path relative to the folder the data source reads from
local treePath = item.Path:gsub('^/SafetyInstructions/', '')
return treePath

Lua script of the TreePath column in the dataflow

The Title column works the same way with the file name:

-- File name without the extension
local title = item.Name:gsub('%.pdf$', '')
return title

One small Lua detail: gsub returns two values, the new string and the number of replacements. Assigning the result to a local variable keeps only the first one, which is exactly the string we want to return.

After the dataflow, the row for the welding station reads Production/Hall 2/Welding Station.pdf in TreePath and Welding Station in Title. The original Path column is still there with the full Hub path, and we will need it later to open the PDF.

The hierarchy control

Now for the main act. The hierarchy control shows rows of a data source as a tree with nodes that can be opened and closed. It can get the structure of the tree in two ways, set in the Structure property:

  • Parent ID column: every row has its own ID and the ID of its parent row. This is the classic way to store a tree in a database table, for example an organisation chart or a bill of materials.
  • Path column: every row has a path like A/B/C, and the tree is derived from the path segments.

For files, the second option is a perfect fit, because a file path is the position in the tree. We set:

  • Data source: InstructionTree, our dataflow
  • Structure: Path column
  • Path column: TreePath
  • Separator: /
  • Display column: Title
  • Expand on start: First level

Data properties of the hierarchy control with path column, separator, display column and expand on start

This is where the folders that are missing from the data come back. The control splits every path at the separator. For Production/Hall 2/Welding Station.pdf it needs a node Production, below it a node Hall 2, and below that the file. If a node doesn’t exist yet, the control simply creates it. So all folders on the way are created automatically from the paths of the files, and each folder appears only once, no matter how many files are in it. The last segment of each path is the row itself, and for these nodes the control shows the Display column, so the operator reads Welding Station instead of the file name.

That is the whole trick. The tree on the screen has exactly the shape of the folders in the Hub, because it is built from nothing else. Three levels in one area, one level in another, a folder that contains files and subfolders at the same time; the control doesn’t care.

Expand on start decides what the operator sees first: everything closed, only the first level open, or the complete tree. With First level, the four areas are open and the deeper folders such as Warehouse or Hall 2 wait for a tap.

The appearance settings are worth a look as well. The hierarchy control has three fonts: one for files, one for folders and one for the selected node. In our board the folders are bold, the files are regular, and the selected file is dark on the yellow Selection color. A Row height of 52 makes the nodes big enough for a finger on a touch screen. The board in the Designer with the hierarchy control selected and the tree already filled from the Hub

The PDF viewer

Next to the tree we place a PDF viewer called SafetyDocument. To have a document showing when the board starts, we give it a Hub resource: in the Explorer we right-click Resources, choose Add Peakboard Hub resource and pick /SafetyInstructions/General/Emergency Procedures.pdf. Fit to screen is set to Fit Height, so the whole page is visible. And because a script will change the document, Used in scripting must be turned on. Properties of the PDF viewer with the Hub resource and the Used in scripting option

Connecting the two

The last piece is the SelectionChanged event of the hierarchy control. It fires when the operator taps a file. Folder nodes don’t raise it; they only open and close. e.index is the index of the selected row, and it is -1 when the selection is cleared.

-- Only a file (a node without children) raises this event
if e.index < 0 then
  return
end

-- The selected row of the dataflow, with the full Hub path of the PDF
local row = screens['Screen1'].InstructionBrowser.selectedrow
screens['Screen1'].SafetyDocument.source = row.Path

selectedrow gives us the complete row of the dataflow behind the selected node, with all its columns. We take the original Path column and hand it to the PDF viewer. Because the viewer was set up with a Hub resource, a new source that contains a full path like /SafetyInstructions/Production/Hall 2/Welding Station.pdf is loaded straight from the Hub. There is no download step and no list of resources to keep in sync. The SelectionChanged script of the hierarchy control in the script editor

Besides selectedrow, the hierarchy control has a few more members for scripting. selectedkey, selectedtext and selectedpath return the selected node in other forms, and select, expand, collapse, expandall, collapseall and clearselection control the tree from a script, for example to jump to a specific instruction when a machine reports a fault.

The result

Here is the board in action. The operator opens Production, then Hall 2, then Assembly Line, and taps Torque Tools three levels down. The instruction appears on the right. The finished board with the Torque Tools instruction selected three levels deep in the tree

One tap on Welding Station one level higher, and the PDF on the right changes. The finished board with the Welding Station instruction selected

The best part is what happens when the safety officer adds a new instruction. They upload a PDF to a new folder, say Logistics/Warehouse/Cold Store/, in the Hub. With the next reload of the data source, the dataflow runs again, and the hierarchy control has a new folder Cold Store below Warehouse with the new instruction in it. The board itself is never touched.

Conclusion

The hierarchy control is the natural choice whenever data has a tree shape, and with the Path column structure, the shape doesn’t have to be defined anywhere. Files in the Peakboard Hub already have a path, and every folder on the way to a file becomes a node by itself. A small dataflow takes care of the rest: it sorts the rows, removes the root folder and makes the names readable.

The same pattern works for any data that comes with a path: documents in a SharePoint library, machine hierarchies like Plant/Hall/Line/Station, or the topics of an MQTT broker. The structure lives where the data lives, and the board just follows it.