diff --git a/css/apcupsd.css b/css/apcupsd.css index ab44a4d..75f0919 100644 --- a/css/apcupsd.css +++ b/css/apcupsd.css @@ -17,7 +17,26 @@ * text colour via color-mix(currentColor ...) so the same rules read correctly * on every Cacti theme, with the per-theme css/.css files tuning accents. * Severity accents (up/recovering/down) use fixed, theme-independent colours. + * + * The card chrome and chart primitives now come from the shared Card API + * stylesheets (css/cards.css + css/card-widgets.css); the rules below are kept + * only where they also style apcupsd-specific content (tables, graph panels, + * the gauge grid, the UPS/timespan pickers). The shared grid reads its column + * count and accent palette from these custom properties. */ +/* The .ccToolbar above the grid is a sibling of #apcupsd_dashboard, not a child, + so bind the shared palette to it here as well; otherwise .ccToolbarLabel/.ccAdd + fall back to the light-theme #23362f default and render dark green on dark + themes such as deepness. */ +.ccToolbar, +#apcupsd_dashboard.ccGrid, +#apcupsd_dashboard_dialog { + --cc-cols: 8; + --cc-card: transparent; + --cc-text: currentColor; + --cc-border: color-mix(in srgb, currentColor 18%, transparent); + --cc-header-bg: color-mix(in srgb, currentColor 9%, transparent); +} .apcupsdDashboard { color: #1b6ca8; @@ -66,21 +85,9 @@ font-size: 12px; } -/* The 8-column card grid; each card spans a number of columns via a span class. */ -.apcupsdDashGrid { - display: grid; - grid-template-columns: repeat(8, minmax(0, 1fr)); - align-items: start; - gap: 14px; - margin: 10px 4px; - font-variant-numeric: tabular-nums; -} - -.apcupsdDashSpan2 { grid-column: span 2; } -.apcupsdDashSpan3 { grid-column: span 3; } -.apcupsdDashSpan4 { grid-column: span 4; } -.apcupsdDashSpan6 { grid-column: span 6; } -.apcupsdDashSpan8 { grid-column: span 8; } +/* The card grid and per-card widths come from the shared .ccGrid / .ccW* chrome + (include/css/cards.css); apcupsd only binds --cc-cols and the responsive + overrides at the bottom of this file. */ .apcupsdDashCard { box-sizing: border-box; @@ -555,8 +562,8 @@ font-size: 10px; } -.apcupsdDashTable th.apcupsdDashSortAsc::after { content: '\2191'; opacity: 0.9; } -.apcupsdDashTable th.apcupsdDashSortDesc::after { content: '\2193'; opacity: 0.9; } +.apcupsdDashTable th.apcupsdDashSortable[aria-sort="ascending"]::after { content: '\2191'; opacity: 0.9; } +.apcupsdDashTable th.apcupsdDashSortable[aria-sort="descending"]::after { content: '\2193'; opacity: 0.9; } /* Fleet total row under the UPSes table. */ .apcupsdDashTotalRow > td { @@ -571,30 +578,29 @@ color: color-mix(in srgb, currentColor 65%, transparent); } -/* Narrow the grid before cards get cramped. */ +/* Narrow the grid before cards get cramped; clamp the shared .ccW* spans to the + reduced column count so they stack instead of creating implicit columns. */ @media (max-width: 1200px) { - .apcupsdDashGrid { + #apcupsd_dashboard.ccGrid { grid-template-columns: repeat(4, minmax(0, 1fr)); } - .apcupsdDashSpan4, - .apcupsdDashSpan6, - .apcupsdDashSpan8 { + #apcupsd_dashboard.ccGrid .ccW4, + #apcupsd_dashboard.ccGrid .ccW5, + #apcupsd_dashboard.ccGrid .ccW6, + #apcupsd_dashboard.ccGrid .ccW7, + #apcupsd_dashboard.ccGrid .ccW8 { grid-column: 1 / -1; } } @media (max-width: 760px) { - .apcupsdDashGrid { + #apcupsd_dashboard.ccGrid { grid-template-columns: 1fr; gap: 10px; } - .apcupsdDashSpan2, - .apcupsdDashSpan3, - .apcupsdDashSpan4, - .apcupsdDashSpan6, - .apcupsdDashSpan8 { + #apcupsd_dashboard.ccGrid > .ccCard { grid-column: 1 / -1; } } diff --git a/css/card-widgets.css b/css/card-widgets.css new file mode 100644 index 0000000..52842f1 --- /dev/null +++ b/css/card-widgets.css @@ -0,0 +1,371 @@ +/* + +-------------------------------------------------------------------------+ + | Copyright (C) 2004-2026 The Cacti Group | + +-------------------------------------------------------------------------+ + | Cacti: The Complete RRDtool-based Graphing Solution | + +-------------------------------------------------------------------------+ +*/ + +/* + * cacti-card-widgets - shared, server-rendered chart primitives for the card + * API (companion to cards.css). These are the small, JS-free building blocks a + * dashboard card body is made of: a semicircular gauge, horizontal usage and + * distribution bars, a min/avg/max bullet, a stacked proportion bar, and stat + * tiles. Every element is plain markup + inline SVG so a card rendered by any + * plugin (including third-party panels surfaced inside Intropage) displays with + * no plugin-specific stylesheet or JavaScript. + * + * All colours derive from the active theme: neutrals from currentColor via + * color-mix, and severities from the shared --cc-sev-* custom properties (with + * sensible fallbacks) so a host or theme can retint them. The PHP helpers in + * lib/html.php (card_gauge(), card_bar(), card_bullet(), card_stackbar(), + * card_stat()) emit exactly this markup. + * + * Severity tokens: up (good), warn, down (bad), none (neutral/disabled). + */ + +/* ---- Semicircular gauge ---- */ +.ccGauge { + position: relative; + display: flex; + flex-direction: column; + align-items: center; + padding: 2px 0 4px; +} + +.ccGaugeSvg { + width: 100%; + max-width: 190px; + height: auto; +} + +.ccGaugeTrack { + stroke: color-mix(in srgb, currentColor 16%, transparent); + stroke-width: 11; + stroke-linecap: round; +} + +.ccGaugeValue { + stroke-width: 11; + stroke-linecap: round; + transition: stroke-dasharray 0.4s ease; +} + +.ccGaugeValue--up { stroke: var(--cc-sev-up, #3c9a5f); } +.ccGaugeValue--warn { stroke: var(--cc-sev-warn, #d9830f); } +.ccGaugeValue--down { stroke: var(--cc-sev-down, #cc0000); } +.ccGaugeValue--none { stroke: color-mix(in srgb, currentColor 45%, transparent); } + +/* Faint radial ticks marking the window min and max on the gauge arc. */ +.ccGaugeTick { + stroke: color-mix(in srgb, currentColor 55%, transparent); + stroke-width: 2; +} + +.ccGaugeText { + margin-top: -22px; + text-align: center; + line-height: 1.2; +} + +.ccGaugeValueText { + display: block; + font-size: 20px; + font-weight: 700; +} + +.ccGaugeSub { + display: block; + font-size: 11px; + color: color-mix(in srgb, currentColor 65%, transparent); +} + +/* ---- Horizontal usage bar ---- */ +.ccBar { + position: relative; + height: 14px; + min-width: 80px; + border-radius: 7px; + background: color-mix(in srgb, currentColor 14%, transparent); + overflow: hidden; +} + +.ccBarFill { + position: absolute; + top: 0; + left: 0; + bottom: 0; + border-radius: 7px; + background: color-mix(in srgb, currentColor 45%, transparent); +} + +.ccBarFill--up { background: var(--cc-sev-up, #3c9a5f); } +.ccBarFill--warn { background: var(--cc-sev-warn, #d9830f); } +.ccBarFill--down { background: var(--cc-sev-down, #cc0000); } +.ccBarFill--none { background: color-mix(in srgb, currentColor 40%, transparent); } + +.ccBarLabel { + display: block; + margin-top: 2px; + font-size: 11px; + color: color-mix(in srgb, currentColor 70%, transparent); +} + +/* Row wrapper: label + value above a bar. */ +.ccBarRow { + display: flex; + flex-direction: column; + gap: 3px; + margin: 6px 0; +} + +.ccBarHead { + display: flex; + justify-content: space-between; + font-size: 12px; +} + +/* ---- Min/avg/max bullet ---- */ +.ccBullet { + margin: 9px 0; +} + +.ccBulletHead { + display: flex; + justify-content: space-between; + align-items: baseline; + gap: 8px; + margin-bottom: 4px; +} + +.ccBulletLabel { + font-size: 12px; + font-weight: 600; +} + +.ccBulletAvg { + font-size: 12px; + font-variant-numeric: tabular-nums; + color: color-mix(in srgb, currentColor 78%, transparent); +} + +.ccBulletTrack { + position: relative; + height: 16px; + border-radius: 8px; + background: color-mix(in srgb, currentColor 12%, transparent); + overflow: hidden; +} + +.ccBulletRange { + position: absolute; + top: 0; + bottom: 0; + border-radius: 8px; +} + +.ccBulletRange--up { background: color-mix(in srgb, var(--cc-sev-up, #3c9a5f) 55%, transparent); } +.ccBulletRange--warn { background: color-mix(in srgb, var(--cc-sev-warn, #d9830f) 55%, transparent); } +.ccBulletRange--down { background: color-mix(in srgb, var(--cc-sev-down, #cc0000) 55%, transparent); } +.ccBulletRange--none { background: color-mix(in srgb, currentColor 32%, transparent); } + +.ccBulletMarker { + position: absolute; + top: -2px; + bottom: -2px; + width: 3px; + transform: translateX(-1px); + background: currentColor; + border-radius: 2px; +} + +/* A faint dashed tick marking the historical average, distinct from the strong + current-value marker, so both are visible when they differ. */ +.ccBulletAvgMarker { + position: absolute; + top: 1px; + bottom: 1px; + width: 0; + border-left: 2px dashed color-mix(in srgb, currentColor 70%, transparent); + transform: translateX(-1px); +} + +.ccBulletScale { + display: flex; + justify-content: space-between; + margin-top: 2px; + font-size: 10px; + font-variant-numeric: tabular-nums; + color: color-mix(in srgb, currentColor 60%, transparent); +} + +/* ---- Stacked proportion bar ---- */ +.ccStack { + display: flex; + height: 20px; + border-radius: 6px; + overflow: hidden; + background: color-mix(in srgb, currentColor 12%, transparent); +} + +.ccStackSeg { + height: 100%; +} + +.ccStackSeg--up { background: var(--cc-sev-up, #3c9a5f); } +.ccStackSeg--warn { background: var(--cc-sev-warn, #d9830f); } +.ccStackSeg--down { background: var(--cc-sev-down, #cc0000); } +.ccStackSeg--none { background: color-mix(in srgb, currentColor 35%, transparent); } + +.ccStackLegend { + display: flex; + flex-wrap: wrap; + gap: 10px; + margin-top: 8px; + font-size: 11px; +} + +.ccStackLegend span { + display: inline-flex; + align-items: center; + gap: 5px; +} + +.ccStackChip { + width: 10px; + height: 10px; + border-radius: 2px; + display: inline-block; +} + +.ccStackChip--up { background: var(--cc-sev-up, #3c9a5f); } +.ccStackChip--warn { background: var(--cc-sev-warn, #d9830f); } +.ccStackChip--down { background: var(--cc-sev-down, #cc0000); } +.ccStackChip--none { background: color-mix(in srgb, currentColor 35%, transparent); } + +/* ---- Stat tiles ---- */ +.ccStat { + display: flex; + flex-direction: column; + align-items: center; + gap: 2px; + padding: 6px 0; +} + +.ccStatValue { + font-size: 30px; + font-weight: 700; + line-height: 1; +} + +.ccStatLabel { + font-size: 12px; + color: color-mix(in srgb, currentColor 70%, transparent); +} + +.ccTiles { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(90px, 1fr)); + gap: 8px; +} + +.ccTile { + display: flex; + flex-direction: column; + align-items: center; + gap: 2px; + padding: 8px 6px; + border: 1px solid color-mix(in srgb, currentColor 18%, transparent); + border-radius: 6px; +} + +.ccTileValue { + font-size: 20px; + font-weight: 700; + line-height: 1; +} + +.ccTileLabel { + font-size: 11px; + color: color-mix(in srgb, currentColor 70%, transparent); +} + +.ccTile--up { border-color: color-mix(in srgb, var(--cc-sev-up, #3c9a5f) 55%, transparent); } +.ccTile--warn { border-color: color-mix(in srgb, var(--cc-sev-warn, #d9830f) 55%, transparent); } +.ccTile--down { border-color: color-mix(in srgb, var(--cc-sev-down, #cc0000) 55%, transparent); } +.ccTile--up .ccTileValue { color: var(--cc-sev-up, #3c9a5f); } +.ccTile--warn .ccTileValue { color: var(--cc-sev-warn, #d9830f); } +.ccTile--down .ccTileValue { color: var(--cc-sev-down, #cc0000); } + +/* ---- Distribution rows and in-table mini bars ---- */ +.ccDistRow { + display: grid; + grid-template-columns: minmax(80px, 34%) 1fr auto; + align-items: center; + gap: 8px; + margin: 6px 0; + font-size: 12px; +} + +.ccDistLabel { + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.ccDistTrack { + height: 12px; + border-radius: 6px; + background: color-mix(in srgb, currentColor 12%, transparent); + overflow: hidden; +} + +.ccDistFill { + display: block; + height: 100%; + border-radius: 6px; + background: color-mix(in srgb, currentColor 45%, transparent); +} + +.ccDistValue { + font-variant-numeric: tabular-nums; + color: color-mix(in srgb, currentColor 80%, transparent); +} + +.ccMiniBar { + display: block; + position: relative; + height: 10px; + min-width: 60px; + border-radius: 5px; + background: color-mix(in srgb, currentColor 12%, transparent); + overflow: hidden; +} + +.ccMiniBarFill { + position: absolute; + top: 0; + left: 0; + bottom: 0; + border-radius: 5px; + background: color-mix(in srgb, currentColor 45%, transparent); +} + +/* ---- Small shared helpers ---- */ +.ccEmpty { + color: color-mix(in srgb, currentColor 65%, transparent); + font-style: italic; +} + +.ccPill { + display: inline-block; + padding: 1px 8px; + border-radius: 10px; + font-size: 11px; + background: color-mix(in srgb, currentColor 12%, transparent); +} + +.ccPill--up { background: color-mix(in srgb, var(--cc-sev-up, #3c9a5f) 70%, #000); color: #fff; } +.ccPill--warn { background: color-mix(in srgb, var(--cc-sev-warn, #d9830f) 70%, #000); color: #fff; } +.ccPill--down { background: color-mix(in srgb, var(--cc-sev-down, #cc0000) 70%, #000); color: #fff; } +.ccPill--none { background: color-mix(in srgb, currentColor 20%, transparent); } diff --git a/css/cards.css b/css/cards.css new file mode 100644 index 0000000..d68ef00 --- /dev/null +++ b/css/cards.css @@ -0,0 +1,298 @@ +/* + +-------------------------------------------------------------------------+ + | Copyright (C) 2004-2026 The Cacti Group | + +-------------------------------------------------------------------------+ + | Cacti: The Complete RRDtool-based Graphing Solution | + +-------------------------------------------------------------------------+ +*/ + +/* + * cacti-cards vX.Y - shared "card chrome" (presentation layer). + * + * This file is bundled per-plugin (no cross-plugin or core dependency) and is + * kept byte-identical between the plugins that use it. It styles a grid of + * cards with a draggable header, per-card tools and optional expand/resize. + * All colours come from CSS custom properties the host sets, so it inherits + * the active Cacti theme without baking any palette in. + * + * HTML contract (emitted by the host's PHP): + * + *
+ *
+ *
+ * + *

Title

+ * + * + * + *
+ *
...
+ *
+ *
+ * + * Host theme variables (all optional, sensible fallbacks provided): + * --cc-cols grid column count (default 4) + * --cc-surface page surface behind the cards + * --cc-card card background + * --cc-text card foreground + * --cc-border card / divider border + * --cc-header-bg header background colour + * --cc-header-image header background image (gradient themes) + * --cc-header-fg header foreground + * --cc-sev-up/-warn/-down/-none severity hues (used as a translucent wash + * over the header + its bottom border) + */ + +.ccGrid { + display: grid; + grid-template-columns: repeat(var(--cc-cols, 4), minmax(0, 1fr)); + align-items: start; + gap: 14px; + margin: 10px 4px; + font-variant-numeric: tabular-nums; +} + +/* Column span (width). */ +.ccW1 { grid-column: span 1; } +.ccW2 { grid-column: span 2; } +.ccW3 { grid-column: span 3; } +.ccW4 { grid-column: span 4; } +.ccW5 { grid-column: span 5; } +.ccW6 { grid-column: span 6; } +.ccW7 { grid-column: span 7; } +.ccW8 { grid-column: span 8; } + +/* Row span (height) for the "steps" size model. */ +.ccH1 { grid-row: span 1; } +.ccH2 { grid-row: span 2; } +.ccH3 { grid-row: span 3; } + +.ccCard { + box-sizing: border-box; + min-width: 0; + display: flex; + flex-direction: column; + border: 1px solid var(--cc-border, #cbd8d1); + /* lift the card off the page surface on both light and dark themes */ + background: color-mix(in srgb, var(--cc-text, #23362f) 4%, var(--cc-card, #fff)); + color: var(--cc-text, #23362f); + box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); + overflow: hidden; +} + +.ccCardDragging { + opacity: 0.5; +} + +.ccCardHeader { + box-sizing: border-box; + /* fixed row height so a tool/handle hover highlight never reflows the header */ + min-height: 34px; + display: flex; + align-items: center; + gap: 8px; + padding: 7px 10px; + border-bottom: 1px solid var(--cc-border, #cbd8d1); + /* paint like the active theme's native html_start_box title bar */ + background-color: var(--cc-header-bg, #e5ece8); + background-image: var(--cc-header-image, none); + background-repeat: repeat-x; + color: var(--cc-header-fg, var(--cc-text, #23362f)); +} + +/* Drag handle: lives on the LEFT of the header; grab it to reorder the card. */ +.ccDrag { + order: -1; + flex: 0 0 auto; + box-sizing: border-box; + padding: 2px 5px; + /* reserve the border so a hover/focus highlight recolours it instead of resizing the header */ + border: 1px solid transparent; + border-radius: 4px; + background: transparent; + color: color-mix(in srgb, currentColor 50%, transparent); + font-size: 13px; + line-height: 1; + cursor: grab; +} + +.ccDrag:hover, +.ccDrag:focus-visible { + color: currentColor; + border-color: transparent; + background: color-mix(in srgb, currentColor 12%, transparent); + outline: none; +} + +.ccDrag:active { + cursor: grabbing; +} + +/* Host toggle to lock reordering (e.g. a "select text" mode): hide the handles. */ +.ccNoDrag .ccDrag { + display: none; +} + +.ccTitle { + flex: 1 1 auto; + min-width: 0; + margin: 0; + font-size: 13px; + font-weight: 600; + line-height: 1.3; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.ccTools { + flex: 0 0 auto; + display: flex; + align-items: center; + gap: 1px; +} + +.ccTool { + box-sizing: border-box; + padding: 2px 5px; + /* reserve the border so a hover/focus highlight recolours it instead of resizing the header */ + border: 1px solid transparent; + border-radius: 4px; + background: transparent; + color: color-mix(in srgb, currentColor 50%, transparent); + font-size: 12px; + line-height: 1; + cursor: pointer; +} + +.ccTool:hover, +.ccTool:focus-visible { + color: currentColor; + border-color: transparent; + background: color-mix(in srgb, currentColor 12%, transparent); + outline: none; +} + +/* Only the control matching the card's current state is shown. */ +.ccCard:not(.ccCardExpanded) .ccTool[data-tool="collapse"], +.ccCardExpanded .ccTool[data-tool="expand"] { + display: none; +} + +/* Expand/collapse is only meaningful on expandable cards. */ +.ccCard:not([data-expandable="1"]) .ccTool[data-tool="expand"], +.ccCard:not([data-expandable="1"]) .ccTool[data-tool="collapse"] { + display: none; +} + +/* Resize tools only apply to the "steps" size model, and clamp at the ends. */ +.ccCard:not([data-size-model="steps"]) .ccTool[data-tool="shrink"], +.ccCard:not([data-size-model="steps"]) .ccTool[data-tool="grow"] { + display: none; +} + +.ccCardBody { + padding: 4px 12px 10px; + overflow-x: auto; +} + +/* Expandable cards clip to a fixed height until "show more"; a fade hints at the + clipped content. */ +.ccCardExpandable:not(.ccCardExpanded) .ccCardBody { + position: relative; + max-height: 210px; + overflow: hidden; +} + +.ccCardExpandable:not(.ccCardExpanded) .ccCardBody::after { + content: ''; + position: absolute; + left: 0; + right: 0; + bottom: 0; + height: 30px; + pointer-events: none; + background: linear-gradient(transparent, color-mix(in srgb, var(--cc-text, #23362f) 4%, var(--cc-card, #fff))); +} + +/* + * Severity wash. Every card carries exactly one data-severity. A single + * translucent colour layer is drawn over the sampled native theme header so the + * strength stays consistent on solid and gradient themes, light and dark. The + * hue comes from a host variable so it follows the theme's colour family. + */ +.ccCard[data-severity="none"] .ccCardHeader { + background-image: + linear-gradient(var(--cc-sev-none, rgba(140, 149, 159, 0.20)), var(--cc-sev-none, rgba(140, 149, 159, 0.20))), + var(--cc-header-image, none); + border-bottom-color: color-mix(in srgb, var(--cc-sev-none-border, #8c959f) 45%, transparent); +} + +.ccCard[data-severity="up"] .ccCardHeader { + background-image: + linear-gradient(var(--cc-sev-up, rgba(148, 179, 107, 0.32)), var(--cc-sev-up, rgba(148, 179, 107, 0.32))), + var(--cc-header-image, none); + border-bottom-color: color-mix(in srgb, var(--cc-sev-up-border, #94b36b) 60%, transparent); +} + +.ccCard[data-severity="warn"] .ccCardHeader { + background-image: + linear-gradient(var(--cc-sev-warn, rgba(255, 143, 30, 0.32)), var(--cc-sev-warn, rgba(255, 143, 30, 0.32))), + var(--cc-header-image, none); + border-bottom-color: color-mix(in srgb, var(--cc-sev-warn-border, #ff8f1e) 60%, transparent); +} + +.ccCard[data-severity="down"] .ccCardHeader { + background-image: + linear-gradient(var(--cc-sev-down, rgba(204, 0, 0, 0.34)), var(--cc-sev-down, rgba(204, 0, 0, 0.34))), + var(--cc-header-image, none); + border-bottom-color: color-mix(in srgb, var(--cc-sev-down-border, #cc0000) 60%, transparent); +} + +/* "Add card" toolbar above the grid. */ +.ccToolbar { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; + margin: 10px 4px 0; +} + +.ccToolbarLabel { + font-size: 12px; + font-weight: 600; + color: var(--cc-text, #23362f); +} + +.ccAdd, +.ccRefreshInterval { + box-sizing: border-box; + padding: 4px 8px; + border: 1px solid var(--cc-border, #cbd8d1); + border-radius: 7px; + background: var(--cc-card, #fff); + color: var(--cc-text, #23362f); + font-size: 12px; +} + +.ccAdd { + min-width: 220px; +} + +.ccRefresh { + display: flex; + align-items: center; + gap: 8px; + margin-left: auto; +} + +/* Maximize dialog: reuse the card body typography on the themed dialog surface. */ +.ccDialogBody { + color: var(--cc-text, #23362f); + font-variant-numeric: tabular-nums; +} diff --git a/includes/dashboard.php b/includes/dashboard.php index f8e2e2d..49c1f38 100644 --- a/includes/dashboard.php +++ b/includes/dashboard.php @@ -42,6 +42,20 @@ function apcupsd_dashboard_print_css(): void { global $config; + // Shared card chrome + chart-widget primitives. On Cacti 1.2.32+ these ship + // in core and are loaded by the common page header; bundle the plugin's own + // copy only when core does not provide them. + // @codeCoverageIgnoreStart -- fallback runs only on cores lacking the bundle; + // the end-to-end run installs a core that ships it, so CI cannot reach here. + if (!file_exists($config['base_path'] . '/include/css/cards.css')) { + print get_md5_include_css('plugins/apcupsd/css/cards.css'); + } + + if (!file_exists($config['base_path'] . '/include/css/card-widgets.css')) { + print get_md5_include_css('plugins/apcupsd/css/card-widgets.css'); + } + // @codeCoverageIgnoreEnd + print get_md5_include_css('plugins/apcupsd/css/apcupsd.css'); $theme = get_selected_theme(); @@ -573,13 +587,13 @@ function apcupsd_dashboard_bullet(string $label, ?float $current, ?float $min, ? $summary[] = __('max %s', apcupsd_dashboard_format_value($max, $unit), 'apcupsd'); } - $head = '
' - . '' . html_escape($label) . '' - . '' . html_escape(implode(' · ', $summary)) . '' + $head = '
' + . '' . html_escape($label) . '' + . '' . html_escape(implode(' · ', $summary)) . '' . '
'; if ($marker_value === null || $scale <= 0) { - return '
' . $head . '
'; + return '
' . $head . '
'; } $lo = max(0.0, min($scale, (float) ($min !== null ? $min : $marker_value))); @@ -590,7 +604,7 @@ function apcupsd_dashboard_bullet(string $label, ?float $current, ?float $min, ? $width = max(0.75, ($hi - $lo) / $scale * 100); $mark = $mk / $scale * 100; - $sev = in_array($severity, ['up', 'recovering', 'down'], true) ? $severity : 'none'; + $sev = ($severity === 'recovering') ? 'warn' : (in_array($severity, ['up', 'down'], true) ? $severity : 'none'); // A faint tick marks the historical average, distinct from the strong marker // at the current reading, so both are visible when they differ. @@ -598,16 +612,16 @@ function apcupsd_dashboard_bullet(string $label, ?float $current, ?float $min, ? if ($avg !== null) { $avg_pos = max(0.0, min($scale, (float) $avg)) / $scale * 100; - $avg_marker = '
'; + $avg_marker = '
'; } - return '
' . $head - . '
' - . '
' + return '
' . $head + . '
' + . '
' . $avg_marker - . '
' + . '
' . '
' - . '
' . html_escape(apcupsd_dashboard_format_value(0.0, $unit)) . '' . html_escape(apcupsd_dashboard_format_value($scale, $unit)) . '
' + . '
' . html_escape(apcupsd_dashboard_format_value(0.0, $unit)) . '' . html_escape(apcupsd_dashboard_format_value($scale, $unit)) . '
' . '
'; } } @@ -630,7 +644,7 @@ function apcupsd_dashboard_bullet(string $label, ?float $current, ?float $min, ? * @return string The gauge HTML. */ function apcupsd_dashboard_gauge(?float $current, ?float $min, ?float $max, float $scale, string $severity, string $center_label, string $sub_label = ''): string { - $sev = in_array($severity, ['up', 'recovering', 'down'], true) ? $severity : 'none'; + $sev = ($severity === 'recovering') ? 'warn' : (in_array($severity, ['up', 'down'], true) ? $severity : 'none'); $frac = ($current !== null && $scale > 0) ? max(0.0, min(1.0, $current / $scale)) : 0.0; $dash = number_format($frac * 100, 2, '.', ''); @@ -649,19 +663,19 @@ function apcupsd_dashboard_gauge(?float $current, ?float $min, ?float $max, floa $ox = 60 + 56 * cos($angle); $oy = 60 - 56 * sin($angle); - return ''; }; - return '
' - . '' - . '' - . '' + return '
' + . '' + . '' + . '' . $tick($min) . $tick($max) . '' - . '
' - . '' . html_escape($center_label) . '' - . ($sub_label !== '' ? '' . html_escape($sub_label) . '' : '') + . '
' + . '' . html_escape($center_label) . '' + . ($sub_label !== '' ? '' . html_escape($sub_label) . '' : '') . '
'; } } @@ -682,7 +696,7 @@ function apcupsd_dashboard_render_bullets(string $group, $ups_id): string { $ups = apcupsd_dashboard_ups($ups_id); if (!cacti_sizeof($ups)) { - return '

' . __esc('No readings have been collected for this UPS yet.', 'apcupsd') . '

'; + return '

' . __esc('No readings have been collected for this UPS yet.', 'apcupsd') . '

'; } apcupsd_dashboard_init_timespan(); @@ -742,7 +756,7 @@ function apcupsd_dashboard_render_bullets(string $group, $ups_id): string { } if ($rows === 0) { - return '

' . __esc('No readings are available for this group yet.', 'apcupsd') . '

'; + return '

' . __esc('No readings are available for this group yet.', 'apcupsd') . '

'; } return $out; @@ -920,13 +934,13 @@ function apcupsd_dashboard_stats_caption(?float $min, ?float $avg, ?float $max, */ function apcupsd_dashboard_gauge_from_reading(?array $reading): string { if (!is_array($reading)) { - return '

' . __esc('No readings have been collected for this UPS yet.', 'apcupsd') . '

'; + return '

' . __esc('No readings have been collected for this UPS yet.', 'apcupsd') . '

'; } $value = $reading['current'] !== null ? $reading['current'] : $reading['avg']; if ($value === null && $reading['min'] === null && $reading['max'] === null) { - return '

' . __esc('No readings are available for this gauge yet.', 'apcupsd') . '

'; + return '

' . __esc('No readings are available for this gauge yet.', 'apcupsd') . '

'; } $unit = $reading['unit']; @@ -1132,19 +1146,19 @@ function apcupsd_dashboard_status_pill(string $status): string { $status = trim($status); $upper = strtoupper($status); - $class = 'apcupsdDashPill--unknown'; + $class = 'ccPill--none'; if ($upper === '') { $status = __('Unknown', 'apcupsd'); } elseif (strpos($upper, 'ONBATT') !== false || strpos($upper, 'LOWBATT') !== false) { - $class = 'apcupsdDashPill--down'; + $class = 'ccPill--down'; } elseif (strpos($upper, 'ONLINE') !== false) { - $class = 'apcupsdDashPill--up'; + $class = 'ccPill--up'; } elseif (strpos($upper, 'CAL') !== false || strpos($upper, 'TRIM') !== false || strpos($upper, 'BOOST') !== false) { - $class = 'apcupsdDashPill--warn'; + $class = 'ccPill--warn'; } - return '' . html_escape($status) . ''; + return '' . html_escape($status) . ''; } } @@ -1177,7 +1191,7 @@ function apcupsd_dashboard_card_body(string $key, $ups_id): string { } if (!$allowed) { - return '

' . __esc('This graph is no longer available.', 'apcupsd') . '

'; + return '

' . __esc('This graph is no longer available.', 'apcupsd') . '

'; } apcupsd_dashboard_init_timespan(); @@ -1202,7 +1216,7 @@ function apcupsd_dashboard_card_body(string $key, $ups_id): string { $reading = apcupsd_dashboard_est_power_reading($ups_id); if ($reading === null) { - return '

' . __esc('Estimated power needs a nominal power rating and a load reading.', 'apcupsd') . '

'; + return '

' . __esc('Estimated power needs a nominal power rating and a load reading.', 'apcupsd') . '

'; } return apcupsd_dashboard_gauge_from_reading($reading); @@ -1211,7 +1225,7 @@ function apcupsd_dashboard_card_body(string $key, $ups_id): string { $cards = apcupsd_dashboard_gauge_cards(); if (!isset($cards[$key])) { - return '

' . __esc('This gauge is no longer available.', 'apcupsd') . '

'; + return '

' . __esc('This gauge is no longer available.', 'apcupsd') . '

'; } return apcupsd_dashboard_gauge_card_body($cards[$key], $ups_id); @@ -1222,7 +1236,7 @@ function apcupsd_dashboard_card_body(string $key, $ups_id): string { switch ($key) { case 'identity': if (!cacti_sizeof($ups)) { - print '

' . __esc('No information has been collected for this UPS yet.', 'apcupsd') . '

'; + print '

' . __esc('No information has been collected for this UPS yet.', 'apcupsd') . '

'; break; } @@ -1289,7 +1303,7 @@ function apcupsd_dashboard_card_body(string $key, $ups_id): string { break; case 'events': if (!cacti_sizeof($ups)) { - print '

' . __esc('No event data has been collected for this UPS yet.', 'apcupsd') . '

'; + print '

' . __esc('No event data has been collected for this UPS yet.', 'apcupsd') . '

'; break; } @@ -1342,33 +1356,35 @@ function apcupsd_dashboard_card_chrome(string $key, string $title, int $span, bo $tools = ''; if ($expandable) { - $tools .= ''; - $tools .= ''; + $tools .= ''; + $tools .= ''; } - $tools .= ''; - $tools .= ''; - $tools .= ''; + $tools .= ''; + $tools .= ''; + $tools .= ''; - $classes = 'apcupsdDashCard apcupsdDashSpan' . $span; + $classes = 'ccCard ccW' . $span; if ($expandable) { - $classes .= ' apcupsdDashCardExpandable'; + $classes .= ' ccCardExpandable'; } if ($expanded) { - $classes .= ' apcupsdDashCardExpanded'; + $classes .= ' ccCardExpanded'; } - $header_class = 'apcupsdDashCardHeader' . ($state !== '' ? ' apcupsdDashCardHeader--' . $state : ''); + $severity = ($state === 'recovering') ? 'warn' : (($state === 'up' || $state === 'down') ? $state : 'none'); - return '
' - . '
' - . '' - . '

' . html_escape($title) . '

' - . '' . $tools . '' + return '
' + . '
' + . '' + . '

' . html_escape($title) . '

' + . '' . $tools . '' . '
' - . '
' . $body . '
' + . '
' . $body . '
' . '
'; } } @@ -1816,18 +1832,15 @@ function apcupsd_dashboard_layout_save(): string { * @return void */ function apcupsd_dashboard_print_refresh(int $seconds, string $page): void { - if ($seconds <= 0) { + if ($seconds <= 0 || !function_exists('set_page_refresh')) { return; } - print ''; + set_page_refresh([ + 'seconds' => $seconds, + 'page' => $page, + 'logout' => 'false' + ]); } } @@ -1932,6 +1945,11 @@ function apcupsd_dashboard(): void { // The dashboard script goes first: when Cacti loads this page through ajax the // scripts run in order, and the refresh script below needs its functions. + // @codeCoverageIgnoreStart -- bundled copy loads only on cores lacking it. + if (!file_exists($config['base_path'] . '/include/js/cards.js')) { + print ''; + } + // @codeCoverageIgnoreEnd print ''; apcupsd_dashboard_print_refresh($current_refresh, $refresh_page); @@ -1945,9 +1963,9 @@ function apcupsd_dashboard(): void { $page_refresh_interval = [0 => __('No Refresh', 'apcupsd'), 30 => __('30 Seconds', 'apcupsd'), 60 => __('1 Minute', 'apcupsd'), 300 => __('5 Minutes', 'apcupsd')]; } - print '
'; + print '
'; - print ''; + print ''; print ''; - print ''; + print ''; print ''; - print ''; - print ''; foreach ($absent as $key) { print ''; @@ -1986,9 +2004,9 @@ function apcupsd_dashboard(): void { print ''; - print '
'; - print ''; - print ''; print ''; foreach ($page_refresh_interval as $seconds => $display_text) { @@ -2000,12 +2018,12 @@ function apcupsd_dashboard(): void { } print ''; - print ''; + print ''; print '
'; print ''; print '
'; - print '
'; @@ -2404,7 +2422,7 @@ function apcupsd_fleet_card_body(string $key): string { switch ($key) { case 'overview': if ($total === 0) { - print '

' . __esc('No UPSes are configured yet.', 'apcupsd') . '

'; + print '

' . __esc('No UPSes are configured yet.', 'apcupsd') . '

'; break; } @@ -2434,7 +2452,7 @@ function apcupsd_fleet_card_body(string $key): string { $bar_total = array_sum(array_column($segments, 'count')); if ($bar_total === 0) { - print '

' . __esc('No UPSes are configured yet.', 'apcupsd') . '

'; + print '

' . __esc('No UPSes are configured yet.', 'apcupsd') . '

'; break; } @@ -2460,7 +2478,7 @@ function apcupsd_fleet_card_body(string $key): string { break; case 'readings': if ($enabled === 0) { - print '

' . __esc('No enabled UPSes are reporting readings yet.', 'apcupsd') . '

'; + print '

' . __esc('No enabled UPSes are reporting readings yet.', 'apcupsd') . '

'; break; } @@ -2480,7 +2498,7 @@ function apcupsd_fleet_card_body(string $key): string { break; case 'power': if ($est_count === 0 || $est_watts === null || $est_capacity === null || $est_capacity <= 0) { - print '

' . __esc('Estimated power needs an enabled UPS with a nominal power rating and a load reading.', 'apcupsd') . '

'; + print '

' . __esc('Estimated power needs an enabled UPS with a nominal power rating and a load reading.', 'apcupsd') . '

'; break; } @@ -2506,7 +2524,7 @@ function apcupsd_fleet_card_body(string $key): string { break; case 'gauges': if ($enabled === 0) { - print '

' . __esc('No enabled UPSes are reporting readings yet.', 'apcupsd') . '

'; + print '

' . __esc('No enabled UPSes are reporting readings yet.', 'apcupsd') . '

'; break; } @@ -2599,7 +2617,7 @@ function apcupsd_fleet_card_body(string $key): string { . ''; } } else { - print '' . __esc('No UPSes are configured yet.', 'apcupsd') . ''; + print '' . __esc('No UPSes are configured yet.', 'apcupsd') . ''; } print ''; @@ -2812,6 +2830,11 @@ function apcupsd_fleet_dashboard(): void { // The dashboard script goes first: when Cacti loads this page through ajax the // scripts run in order, and the refresh script below needs its functions. + // @codeCoverageIgnoreStart -- bundled copy loads only on cores lacking it. + if (!file_exists($config['base_path'] . '/include/js/cards.js')) { + print ''; + } + // @codeCoverageIgnoreEnd print ''; apcupsd_dashboard_print_refresh($current_refresh, $refresh_page); @@ -2824,9 +2847,9 @@ function apcupsd_fleet_dashboard(): void { $page_refresh_interval = [0 => __('No Refresh', 'apcupsd'), 30 => __('30 Seconds', 'apcupsd'), 60 => __('1 Minute', 'apcupsd'), 300 => __('5 Minutes', 'apcupsd')]; } - print '
'; + print '
'; - print ''; + print ''; print ''; - print ''; - print ''; foreach ($absent as $key) { print ''; @@ -2849,9 +2872,9 @@ function apcupsd_fleet_dashboard(): void { print ''; - print '
'; - print ''; - print ''; print ''; foreach ($page_refresh_interval as $seconds => $display_text) { @@ -2863,12 +2886,12 @@ function apcupsd_fleet_dashboard(): void { } print ''; - print ''; + print ''; print '
'; print ''; print '
'; - print '
'; diff --git a/js/apcupsd_dashboard.js b/js/apcupsd_dashboard.js index 3d7cc9d..ef45f00 100644 --- a/js/apcupsd_dashboard.js +++ b/js/apcupsd_dashboard.js @@ -16,26 +16,15 @@ +-------------------------------------------------------------------------+ */ -/* UPS Dashboard: the per-UPS drilldown card grid. Mirrors the hmib/intropage - * card interaction model (drag/keyboard reorder, per-card tools, an "Add card" - * catalogue, a Cacti timespan selector, auto-refresh interval + manual refresh) - * with the card order, expanded state and current UPS persisted server-side in - * settings_user. */ - -var apcupsdDashDragging = null; - -/** Cacti's browser auto-logout timer as it was when this script loaded, before the - * dashboard replaces it with its own refresh. Choosing "No Refresh" puts it back (a - * page reload used to). It is kept across re-runs of this script, which can find the - * dashboard's own override in place of the original. */ -var apcupsdDashCoreTimer = typeof apcupsdDashCoreTimer === 'undefined' ? null : apcupsdDashCoreTimer; - -if (typeof refreshIsLogout !== 'undefined' && refreshIsLogout === true && typeof refreshMSeconds !== 'undefined' && typeof refreshPage !== 'undefined') { - apcupsdDashCoreTimer = {ms: refreshMSeconds, page: refreshPage}; -} - -/** The endpoint/scope configuration for a grid, read from its data attributes so - * the per-UPS and fleet dashboards can share this engine. */ +/* UPS Dashboard adapter for the shared card chrome (js/cards.js). The + * CactiCards controller owns drag/keyboard reorder, the per-card tools and the + * debounced layout save; this adapter maps those hooks to apcupsd's upses.php + * endpoints and adds apcupsd's extras: a UPS picker, Cacti's timespan selector + * (its graph and RRD-bullet cards follow the session timespan), an auto-refresh + * interval and a manual refresh-all. Card markup uses the shared .cc* contract + * and the card bodies the shared .cc* chart primitives. */ + +/* The endpoint/scope configuration for a grid, read from its data attributes. */ function apcupsdDashCfg(grid) { return { page: grid.dataset.page || 'upses.php', @@ -44,91 +33,101 @@ function apcupsdDashCfg(grid) { refreshAction: grid.dataset.refreshAction || 'dashboard_refresh', scopeParam: grid.dataset.scopeParam || '', scope: grid.dataset.scope || '', - reload: grid.dataset.reload || 'upses.php', - refreshPage: grid.dataset.refreshPage || '' + reload: grid.dataset.reload || 'upses.php' }; } -/** Build a POST payload for a grid, adding the CSRF token and the grid's scope - * parameter (the UPS id for the per-UPS dashboard; absent for the fleet one). */ +/* Build a POST payload for a grid, adding the CSRF token and the grid's scope + * parameter (the UPS id for the per-UPS dashboard). */ function apcupsdDashPost(grid, extra) { - var cfg = apcupsdDashCfg(grid); var data = {__csrf_magic: csrfMagicToken}; + for (var key in extra) { if (Object.prototype.hasOwnProperty.call(extra, key)) { data[key] = extra[key]; } } + + var cfg = apcupsdDashCfg(grid); + if (cfg.scopeParam) { data[cfg.scopeParam] = cfg.scope; } + return data; } -/** Make a container's sortable headers focusable and screen-reader friendly. */ +/* Make a container's sortable headers focusable and screen-reader friendly. */ function apcupsdDashPrepSortHeaders(container) { if (!container) { return; } + container.querySelectorAll('th.apcupsdDashSortable').forEach(function(th) { if (!th.hasAttribute('tabindex')) { th.setAttribute('tabindex', '0'); } - // Keep the native columnheader role: aria-sort is only valid there, and the - // tabindex + Enter/Space keydown handler already makes it operable. if (!th.hasAttribute('aria-sort')) { th.setAttribute('aria-sort', 'none'); } }); } -/** Sort a card table by the clicked column header, toggling asc/desc. Numeric - * columns (data-sort="num") sort by the cell's data-sort-value when present. */ +/* Show (or clear) a layout-save error in the toolbar's live-region status span so + * a rejected save is not silently lost on the next reload. */ +function apcupsdDashShowSaveStatus(message) { + var el = document.querySelector('.apcupsdDashSaveStatus'); + + if (!el) { + return; + } + + if (message) { + el.textContent = message; + el.removeAttribute('hidden'); + } else { + el.textContent = ''; + el.setAttribute('hidden', 'hidden'); + } +} + +/* Sort a card table by the clicked column header, toggling asc/desc. Numeric + * columns (data-sort="num") sort by the cell's data-sort-value when present. */ function apcupsdDashSortTable(th) { var table = th.closest('table'); + if (!table) { return; } - var tbody = table.tBodies[0]; - if (!tbody) { - return; - } + + var tbody = table.tBodies[0]; var headers = Array.prototype.slice.call(th.parentNode.children); - var index = headers.indexOf(th); - var numeric = th.dataset.sort === 'num'; - var asc = !th.classList.contains('apcupsdDashSortAsc'); + var index = headers.indexOf(th); + var numeric = th.getAttribute('data-sort') === 'num'; + var asc = th.getAttribute('aria-sort') !== 'ascending'; headers.forEach(function(header) { - header.classList.remove('apcupsdDashSortAsc', 'apcupsdDashSortDesc'); - if (header.classList.contains('apcupsdDashSortable')) { + if (header !== th) { header.setAttribute('aria-sort', 'none'); } }); - th.classList.add(asc ? 'apcupsdDashSortAsc' : 'apcupsdDashSortDesc'); + th.setAttribute('aria-sort', asc ? 'ascending' : 'descending'); var rows = Array.prototype.slice.call(tbody.rows).filter(function(row) { return !row.classList.contains('apcupsdDashEmptyRow'); }); - var value = function(row) { - var cell = row.cells[index]; - if (!cell) { - return numeric ? 0 : ''; + rows.sort(function(a, b) { + var x = apcupsdDashCellValue(a.cells[index], numeric); + var y = apcupsdDashCellValue(b.cells[index], numeric); + + if (x < y) { + return asc ? -1 : 1; } - if (numeric) { - var raw = cell.dataset.sortValue !== undefined ? cell.dataset.sortValue : cell.textContent; - var num = parseFloat(String(raw).replace(/[^0-9.\-]/g, '')); - return isNaN(num) ? 0 : num; + if (x > y) { + return asc ? 1 : -1; } - return cell.textContent.trim().toLowerCase(); - }; - - rows.sort(function(a, b) { - var va = value(a); - var vb = value(b); - if (va < vb) { return asc ? -1 : 1; } - if (va > vb) { return asc ? 1 : -1; } return 0; }); @@ -137,37 +136,36 @@ function apcupsdDashSortTable(th) { }); } -/** Remember which header a card's table is sorted by, and in which direction. */ -function apcupsdDashSortSnapshot(card) { - var th = card.querySelector('th.apcupsdDashSortAsc, th.apcupsdDashSortDesc'); - if (!th) { - return null; +/* The sort key of a cell: its data-sort-value when set, else its text; numeric + * columns parse to a number so 9 sorts below 10. */ +function apcupsdDashCellValue(cell, numeric) { + if (!cell) { + return numeric ? 0 : ''; } - return {index: Array.prototype.indexOf.call(th.parentNode.children, th), asc: th.classList.contains('apcupsdDashSortAsc')}; -} -/** Re-apply a remembered sort to a freshly rendered card, so an in-place refresh - * does not throw away the order the user picked. */ -function apcupsdDashSortRestore(card, snapshot) { - if (!snapshot) { - return; + var raw = cell.hasAttribute('data-sort-value') ? cell.getAttribute('data-sort-value') : cell.textContent; + + if (numeric) { + var value = Number.parseFloat(raw); + return Number.isNaN(value) ? 0 : value; } - var th = card.querySelector('th.apcupsdDashSortable:nth-child(' + (snapshot.index + 1) + ')'); - if (!th) { + + return (raw || '').trim().toLowerCase(); +} + +/* Delegate clicks and keyboard activation on a container's sortable headers. */ +function apcupsdDashBindSort(container, namespace) { + if (!container) { return; } - // apcupsdDashSortTable() toggles the direction, so seed the opposite one. - th.classList.toggle('apcupsdDashSortAsc', !snapshot.asc); - apcupsdDashSortTable(th); -} -/** Wire click and Enter/Space sorting on the sortable headers under a root. The - * handlers are delegated, so cards that are refreshed or added later keep working. */ -function apcupsdDashBindSort(root) { - $(root).off('click.apcupsdsort').on('click.apcupsdsort', 'th.apcupsdDashSortable', function() { + apcupsdDashPrepSortHeaders(container); + + $(container).off('click.' + namespace).on('click.' + namespace, 'th.apcupsdDashSortable', function() { apcupsdDashSortTable(this); }); - $(root).off('keydown.apcupsdsort').on('keydown.apcupsdsort', 'th.apcupsdDashSortable', function(event) { + + $(container).off('keydown.' + namespace).on('keydown.' + namespace, 'th.apcupsdDashSortable', function(event) { if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar') { event.preventDefault(); apcupsdDashSortTable(this); @@ -175,558 +173,210 @@ function apcupsdDashBindSort(root) { }); } +/* Wire the per-UPS dashboard grid to the shared card chrome plus apcupsd's + * extras: the UPS picker, Cacti's timespan selector, the auto-refresh interval + * and the manual refresh-all. */ function initApcupsdDashboard() { $(function() { var grid = document.getElementById('apcupsd_dashboard'); - if (!grid) { + + if (!grid || !window.CactiCards) { return; } - // Delegated so the sortable headers also work in cards that are refreshed or - // added later. - apcupsdDashBindSort(grid); + var cfg = apcupsdDashCfg(grid); - grid.querySelectorAll('.apcupsdDashCard').forEach(function(card) { - apcupsdDashBindCard(grid, card); - }); + function catalogue() { + return typeof apcupsdDashCatalog !== 'undefined' ? apcupsdDashCatalog : null; + } - grid.addEventListener('dragover', function(event) { - if (!apcupsdDashDragging) { + function syncAddOptions() { + var add = document.getElementById('apcupsd_dashboard_add'); + var cat = catalogue(); + + if (!add || !cat) { return; } - event.preventDefault(); - event.dataTransfer.dropEffect = 'move'; - var before = apcupsdDashDragReference(grid, event.clientX, event.clientY); - if (before == null) { - grid.appendChild(apcupsdDashDragging); - } else if (before !== apcupsdDashDragging) { - grid.insertBefore(apcupsdDashDragging, before); + + var present = {}; + + grid.querySelectorAll('.ccCard').forEach(function(card) { + if (card.dataset.card) { + present[card.dataset.card] = true; + } + }); + + while (add.options.length > 1) { + add.remove(1); } - }); - // Per-card tool buttons bubble to the grid. - $(grid).off('click.apcupsddash').on('click.apcupsddash', '.apcupsdDashCardTool', function() { - var card = this.closest('.apcupsdDashCard'); - if (!card) { + Object.keys(cat).forEach(function(key) { + if (!present[key]) { + var option = document.createElement('option'); + option.value = key; + option.textContent = cat[key]; + add.appendChild(option); + } + }); + } + + function fetchCard(id, expanded) { + return $.post(cfg.page, apcupsdDashPost(grid, {action: cfg.cardAction, card: id, expanded: expanded ? '1' : '0'}), null, 'json').then(function(d) { + return d && d.html ? d.html : null; + }); + } + + function maximize(card) { + var dialog = document.getElementById('apcupsd_dashboard_dialog'); + + if (!dialog) { return; } - switch (this.dataset.tool) { - case 'expand': card.classList.add('apcupsdDashCardExpanded'); apcupsdDashSaveLayout(grid); break; - case 'collapse': card.classList.remove('apcupsdDashCardExpanded'); apcupsdDashSaveLayout(grid); break; - case 'maximize': apcupsdDashMaximize(card); break; - case 'refresh': apcupsdDashRefreshCard(grid, card); break; - case 'remove': apcupsdDashRemoveCard(grid, card); break; + + var title = card.querySelector('.ccTitle'); + var body = card.querySelector('.ccCardBody'); + + dialog.innerHTML = '
' + (body ? body.innerHTML : '') + '
'; + + apcupsdDashBindSort(dialog, 'apcupsddlgsort'); + + $(dialog).dialog({ + modal: true, + appendTo: 'body', + width: Math.min(900, $(window).width() - 40), + title: title ? title.textContent : '' + }); + } + + var adapter = { + addControl: 'apcupsd_dashboard_add', + + // The shared controller calls applyTheme after every swap (refresh, + // resize, add and manual refresh-all). apcupsd has no per-card palette + // to re-apply, but this is the hook that lets it re-enhance the sort + // headers of freshly inserted, server-rendered markup so keyboard + // users keep access to sorting after a refresh or re-add. + applyTheme: function(g) { + apcupsdDashPrepSortHeaders(g); + }, + + fetchCard: function(id, state) { + return fetchCard(id, state && state.expanded); + }, + + onAddCard: function(id) { + return fetchCard(id, false); + }, + + onLayoutChange: function(state) { + var expanded = {}; + + state.order.forEach(function(id) { + if (state.cards[id] && state.cards[id].expanded) { + expanded[id] = true; + } + }); + + $.post(cfg.page, apcupsdDashPost(grid, { + action: cfg.layoutAction, + layout: JSON.stringify({order: state.order, expanded: expanded}) + }), null, 'json').done(function(d) { + // Surface a server-side rejection (e.g. the 4096-char layout + // cap) instead of letting the change silently revert on reload. + apcupsdDashShowSaveStatus(d && d.error ? d.error : ''); + }); + + syncAddOptions(); + }, + + maximize: function(id, mode, card) { + maximize(card); } - }); - - var add = document.getElementById('apcupsd_dashboard_add'); - if (add) { - $(add).off('change.apcupsddash').on('change.apcupsddash', function() { - var key = this.value; - this.value = ''; - if (key) { - apcupsdDashAddCard(grid, key); + }; + + CactiCards.init(grid, adapter); + syncAddOptions(); + apcupsdDashBindSort(grid, 'apcupsdsort'); + + // Cacti 1.2.x upgrades the Add card