NAME

App::Netdisco::Util::Web

DESCRIPTION

A set of helper subroutines to support parts of the Netdisco application.

There are no default exports, however the :all tag will export all subroutines.

EXPORT_OK

request_is_device

Client has requested device content under .../device or .../device/ports.

request_is_api

Client has requested JSON format data and an endpoint under /api.

request_is_api_report

Same as request_is_api but also requires path to start "/api/v1/report/...".

Same as request_is_api but also requires path to start "/api/v1/search/...".

sql_match( $value, $exact? )

Convert wildcard characters "*" and "?" to "%" and "_" respectively.

Pass a true value to $exact to only substitute the existing wildcards, and not also add "*" to each end of the value.

In list context, returns two values, the translated value, and also an SQL::Abstract LIKE clause.

sort_port( $a, $b )

Sort port names of various types used by device vendors. Interface is as Perl's own sort - two input args and an integer return value.

sort_modules( $modules )

Sort devices modules into tree hierarchy based upon position and parent - input arg is module list.

interval_to_daterange( $interval )

Takes an interval in days, weeks, months, or years in a format like '7 days' and returns a date range in the format 'YYYY-MM-DD to YYYY-MM-DD' by subtracting the interval from the current date.

If $interval is not passed, epoch zero (1970-01-01) is used as the start.

device_display_name( $device )

The name shown for a Device row in the web interface: its DNS name, or its IP when another device answers to the same DNS name.

page_title( $page, $tab )

The browser title for the $tab pane of $page, where $page is one of device, search, report or admin.

Returns undef where the interface has no title for the tab, so that a caller emitting this into a response leaves the current title alone rather than replacing it with something wrong.

pane_history_header( $page, $tab )

The HX-Push-Url or HX-Replace-Url header for the $tab pane of $page, as a name and value pair for "header" in Dancer::Response. htmx puts the value in the address bar, and reads the value false as an instruction to leave the history alone.

The value is the address of the page the pane belongs to, carrying the query the sidebar form submitted, so that opening it fresh renders the same tab with the same results.

pane_chrome( $page, $tab )

The chrome around the $tab pane of $page that changes with the tab: the CSV download link, and on the device page the sidebar reset link. Returned as hx-swap-oob markup for the same response that carries the pane, so that one answer paints the pane and everything around it.

Returns the empty string where the page shell carries neither, which is also what an unregistered tag gets. That is not tidiness: htmx drops an out-of-band element naming an id the page does not have, and says nothing at all about it, so netdisco.js reports the shortfall as a script error.

The sidebar itself is deliberately absent. Whether a tab has one is declared by the sidebar templates, both by which of them exist on an include path that site-local plugins extend and by two of them reporting that they carry only hidden fields, so no route can answer it without duplicating template text.

escape_for_script_context( $json )

Makes a JSON string safe to embed as a JavaScript literal inside an HTML <script> element, which is how the report and search templates ship their result sets. Returns anything that is not a defined plain scalar unchanged, so a resultset or an arrayref passed to a CSV or API template is left alone.

escape_results_token( $tokens )

Applies escape_for_script_context to the results token of a template token hash, in place, and returns the hash.

Only when the key is already there. The API serializer chooses between emitting results alone and walking the whole token hash on exists $tokens->{results}, and a handler that renders without that token, as node search does, needs the second. Assigning unconditionally would autovivify the key and silently move such a handler onto the first branch.