From 1969831eac9aa2af74df459d2214f223d0025488 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Sun, 29 Nov 2026 11:32:39 -0700 Subject: [PATCH 1/3] Add shared lab result pivot customizer with reference range flags Moves reference range highlighting, arrow markers, and the legend for pivoted lab results into the ehr API so any EHR module can opt in from query metadata. --- .../ehr/table/LabResultsPivotCustomizer.java | 264 ++++++++++++++++++ ehr/resources/web/ehr/labResultsPivot.js | 60 ++++ ehr/src/org/labkey/ehr/EHRModule.java | 7 + 3 files changed, 331 insertions(+) create mode 100644 ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java create mode 100644 ehr/resources/web/ehr/labResultsPivot.js diff --git a/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java b/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java new file mode 100644 index 000000000..2c29f7806 --- /dev/null +++ b/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java @@ -0,0 +1,264 @@ +/* + * Copyright (c) 2026 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.labkey.api.ehr.table; + +import org.apache.commons.collections4.MultiValuedMap; +import org.apache.commons.collections4.multimap.ArrayListValuedHashMap; +import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.junit.Assert; +import org.junit.Test; +import org.labkey.api.data.AbstractTableInfo; +import org.labkey.api.data.ColumnInfo; +import org.labkey.api.data.DataColumn; +import org.labkey.api.data.RenderContext; +import org.labkey.api.data.TableCustomizer; +import org.labkey.api.data.TableInfo; +import org.labkey.api.query.FieldKey; +import org.labkey.api.util.DOM; +import org.labkey.api.view.template.ClientDependency; +import org.labkey.api.writer.HtmlWriter; + +import java.util.Collection; +import java.util.Objects; +import java.util.Set; + +import static org.labkey.api.util.DOM.Attribute.aria_hidden; +import static org.labkey.api.util.DOM.Attribute.title; +import static org.labkey.api.util.DOM.DIV; +import static org.labkey.api.util.DOM.SPAN; +import static org.labkey.api.util.DOM.at; +import static org.labkey.api.util.DOM.cl; + +/** + * Highlights and flags each pivoted lab result cell, and gives it a reference range popover, using the status and range + * pivoted alongside it ({@code ::status} and {@code ::range}). Status values are expected to be "High" or "Low". + *

+ * Properties: {@code highColor}, {@code lowColor}, {@code noRangeColor} (hex without '#'; an empty value turns off that + * highlight and its marker) and {@code noRangeText} (popover text for a result with no reference range). + */ +public class LabResultsPivotCustomizer implements TableCustomizer +{ + public static final String DEFAULT_HIGH_COLOR = "FF7377"; + public static final String DEFAULT_LOW_COLOR = "ADD8E6"; + public static final String DEFAULT_NO_RANGE_COLOR = "FFFFC0"; + + private static final String DEFAULT_NO_RANGE_TEXT = "None defined"; + private static final String RESULTS_SUFFIX = "::results"; + private static final String STATUS_SUFFIX = "::status"; + private static final String RANGE_SUFFIX = "::range"; + private static final String CLIENT_SCRIPT = "ehr/labResultsPivot.js"; + + // labResultsPivot.js keys its legend off FLAG_CLASS and repeats these symbols + private static final String FLAG_CLASS = "ehr-ref-flag"; + + enum Flag + { + HIGH("↑", "high"), + LOW("↓", "low"), + NO_RANGE("*", "no reference range"); + + private final String _symbol; + private final String _srText; + + Flag(String symbol, String srText) + { + _symbol = symbol; + _srText = srText; + } + } + + private final @Nullable String _highColor; + private final @Nullable String _lowColor; + private final @Nullable String _noRangeColor; + private final @NotNull String _noRangeText; + + public LabResultsPivotCustomizer(MultiValuedMap props) + { + _highColor = getProp(props, "highColor", DEFAULT_HIGH_COLOR); + _lowColor = getProp(props, "lowColor", DEFAULT_LOW_COLOR); + _noRangeColor = getProp(props, "noRangeColor", DEFAULT_NO_RANGE_COLOR); + _noRangeText = Objects.requireNonNullElse(getProp(props, "noRangeText", DEFAULT_NO_RANGE_TEXT), DEFAULT_NO_RANGE_TEXT); + } + + private static @Nullable String getProp(MultiValuedMap props, String name, String defaultValue) + { + Collection values = props.get(name); + return values.isEmpty() ? defaultValue : StringUtils.trimToNull(values.iterator().next()); + } + + /** @param status comma-separated, since a cell can hold several results for one test */ + @Nullable Flag getFlag(@Nullable String status, boolean hasResult) + { + Flag flag; + if (status != null) + flag = status.contains("High") ? Flag.HIGH : status.contains("Low") ? Flag.LOW : null; + else + flag = hasResult ? Flag.NO_RANGE : null; + return getColor(flag) == null ? null : flag; + } + + private @Nullable String getColor(@Nullable Flag flag) + { + return flag == null ? null : switch (flag) + { + case HIGH -> _highColor; + case LOW -> _lowColor; + case NO_RANGE -> _noRangeColor; + }; + } + + @Override + public void customize(TableInfo tableInfo) + { + if (!(tableInfo instanceof AbstractTableInfo ti)) + return; + + for (var col : ti.getMutableColumns()) + { + String name = col.getName(); + if (name.endsWith(STATUS_SUFFIX) || name.endsWith(RANGE_SUFFIX)) + { + col.setHidden(true); + } + else if (name.endsWith(RESULTS_SUFFIX)) + { + String testId = name.substring(0, name.length() - RESULTS_SUFFIX.length()); + // QueryPivot appends the aggregate label ("WBC Results") when pivoting more than one aggregate + col.setLabel(testId); + FieldKey statusKey = FieldKey.fromParts(testId + STATUS_SUFFIX); + FieldKey rangeKey = FieldKey.fromParts(testId + RANGE_SUFFIX); + if (ti.getColumn(statusKey) != null && ti.getColumn(rangeKey) != null) + col.setDisplayColumnFactory(colInfo -> new RefRangeStatusColumn(colInfo, testId, statusKey, rangeKey)); + } + } + } + + private class RefRangeStatusColumn extends DataColumn + { + private final String _testId; + private final FieldKey _statusKey; + private final FieldKey _rangeKey; + + RefRangeStatusColumn(ColumnInfo col, String testId, FieldKey statusKey, FieldKey rangeKey) + { + super(col); + _testId = testId; + _statusKey = statusKey; + _rangeKey = rangeKey; + } + + @Override + public void addQueryFieldKeys(Set keys) + { + super.addQueryFieldKeys(keys); + keys.add(_statusKey); + keys.add(_rangeKey); + } + + private boolean hasResult(RenderContext ctx) + { + return !StringUtils.isBlank(Objects.toString(getValue(ctx), null)); + } + + // group_concat yields an empty value, not null, when every input is null + private @Nullable String getString(RenderContext ctx, FieldKey key) + { + return StringUtils.trimToNull(ctx.get(key, String.class)); + } + + private @Nullable Flag getFlag(RenderContext ctx) + { + return LabResultsPivotCustomizer.this.getFlag(getString(ctx, _statusKey), hasResult(ctx)); + } + + @Override + public @NotNull String getCssStyle(RenderContext ctx) + { + String style = super.getCssStyle(ctx); + String color = getColor(getFlag(ctx)); + return color == null ? style : style + ";background-color:#" + color; + } + + @Override + public @NotNull Set getClientDependencies() + { + Set dependencies = super.getClientDependencies(); + dependencies.add(ClientDependency.fromPath(CLIENT_SCRIPT)); + return dependencies; + } + + // A block element widens the popover's hover target to the cell + @Override + public void renderGridCellContents(RenderContext ctx, HtmlWriter out) + { + if (!hasResult(ctx)) + { + super.renderGridCellContents(ctx, out); + return; + } + + String range = getString(ctx, _rangeKey); + String status = getString(ctx, _statusKey); + String heading = range == null ? "No reference range" : status == null ? null : status.replace(",", ", "); + String body = range == null ? _noRangeText : "Range: " + range; + Flag flag = getFlag(ctx); + + DIV(at(title, _testId + (heading == null ? "" : ": " + heading)).data("pp", "popover").data("content", body), + (DOM.Renderable) ret -> { + super.renderGridCellContents(ctx, out); + return ret; + }, + flag == null ? null : SPAN(cl(FLAG_CLASS).at(aria_hidden, "true"), " " + flag._symbol), + flag == null ? null : SPAN(cl("sr-only"), " " + flag._srText) + ).appendTo(out); + } + } + + public static class TestCase extends Assert + { + private static LabResultsPivotCustomizer create(String... keyValues) + { + MultiValuedMap props = new ArrayListValuedHashMap<>(); + for (int i = 0; i < keyValues.length; i += 2) + props.put(keyValues[i], keyValues[i + 1]); + return new LabResultsPivotCustomizer(props); + } + + @Test + public void testFlags() + { + LabResultsPivotCustomizer customizer = create(); + assertEquals(Flag.HIGH, customizer.getFlag("High", true)); + assertEquals(Flag.LOW, customizer.getFlag("Low", true)); + assertEquals(Flag.HIGH, customizer.getFlag("Low,High", true)); + assertNull(customizer.getFlag("Normal", true)); + assertEquals(Flag.NO_RANGE, customizer.getFlag(null, true)); + assertNull(customizer.getFlag(null, false)); + } + + @Test + public void testEmptyColorTurnsOffFlag() + { + LabResultsPivotCustomizer customizer = create("noRangeColor", "", "lowColor", "FBEC5D"); + assertNull(customizer.getFlag(null, true)); + assertEquals(Flag.LOW, customizer.getFlag("Low", true)); + assertEquals("FBEC5D", customizer.getColor(Flag.LOW)); + assertEquals(DEFAULT_HIGH_COLOR, customizer.getColor(Flag.HIGH)); + } + } +} diff --git a/ehr/resources/web/ehr/labResultsPivot.js b/ehr/resources/web/ehr/labResultsPivot.js new file mode 100644 index 000000000..6008a6aab --- /dev/null +++ b/ehr/resources/web/ehr/labResultsPivot.js @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2026 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 + */ + +// Delegated from body, like core.js's data-tt tooltips, so grids re-rendered after paging or sorting are covered. +// Bootstrap ignores a repeat call once body holds a popover instance. +jQuery(function($) { + var selector = '[data-pp="popover"]'; + $(document.body).popover({ + selector: selector, + trigger: 'hover', + placement: 'top', + container: 'body' + }); + + // The top-placed arrow overlaps the cell's top pixel, so a cursor entering from above lands on it, leaves the cell, and hides the popover + $(document.body).on('inserted.bs.popover', selector, function() { + $(this).data('bs.popover').tip().css('pointer-events', 'none'); + }); +}); + +// Symbols match LabResultsPivotCustomizer.Flag +jQuery(function($) { + var flagSelector = '.ehr-ref-flag'; + var legendPart = 'ehrrefrange'; + var legend = '↑ High · ↓ Low · * No reference range'; + + var findRegion = function(form) { + var match; + $.each(LABKEY.DataRegions, function(name, region) { + if (region.domId && form.id === region.domId + '-form') { + match = region; + return false; + } + }); + return match; + }; + + var addLegends = function(root) { + $(root).find(flagSelector).closest('form').each(function() { + var region = findRegion(this); + if (region && region.msgbox && !region.hasMessage(legendPart)) + region.addMessage(legend, legendPart); + }); + }; + + addLegends(document.body); + + // Grids loaded or re-rendered after page load (paging, sorting, report tabs) reset the region's messages + new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + mutation.addedNodes.forEach(function(node) { + if (node.nodeType === Node.ELEMENT_NODE && (node.matches(flagSelector) || node.querySelector(flagSelector))) + addLegends(node.matches(flagSelector) ? node.parentNode : node); + }); + }); + }).observe(document.body, { childList: true, subtree: true }); +}); diff --git a/ehr/src/org/labkey/ehr/EHRModule.java b/ehr/src/org/labkey/ehr/EHRModule.java index 4034603d6..a9831fa4e 100644 --- a/ehr/src/org/labkey/ehr/EHRModule.java +++ b/ehr/src/org/labkey/ehr/EHRModule.java @@ -43,6 +43,7 @@ import org.labkey.api.ehr.history.DefaultCasesCloseDataSource; import org.labkey.api.ehr.history.DefaultCasesDataSource; import org.labkey.api.ehr.history.DefaultTreatmentOrdersDataSource; +import org.labkey.api.ehr.table.LabResultsPivotCustomizer; import org.labkey.api.ehr.security.EHRDataAdminPermission; import org.labkey.api.ehr.security.EHRSecurityEscalatorAuditProvider; import org.labkey.api.ehr.security.EHRSnomedEditPermission; @@ -145,6 +146,12 @@ public String getName() return Set.of(EHRController.TestCase.class); } + @Override + public @NotNull Set> getUnitTests() + { + return Set.of(LabResultsPivotCustomizer.TestCase.class); + } + @Override protected void init() { From 73ca97dc88eadcbb7d1b8629d14d917ea382f195 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Sun, 29 Nov 2026 15:42:13 -0700 Subject: [PATCH 2/3] Delay lab result pivot popovers by half a second --- ehr/resources/web/ehr/labResultsPivot.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ehr/resources/web/ehr/labResultsPivot.js b/ehr/resources/web/ehr/labResultsPivot.js index 6008a6aab..e9bd2c9cb 100644 --- a/ehr/resources/web/ehr/labResultsPivot.js +++ b/ehr/resources/web/ehr/labResultsPivot.js @@ -12,7 +12,8 @@ jQuery(function($) { selector: selector, trigger: 'hover', placement: 'top', - container: 'body' + container: 'body', + delay: { show: 500, hide: 0 } }); // The top-placed arrow overlaps the cell's top pixel, so a cursor entering from above lands on it, leaves the cell, and hides the popover From c00fdbd9933cc7a946b8ebb6dfb3e649a5403352 Mon Sep 17 00:00:00 2001 From: Marty Pradere Date: Sun, 29 Nov 2026 15:53:39 -0700 Subject: [PATCH 3/3] Flag lab result pivot cells by range so marker and popover agree --- .../ehr/table/LabResultsPivotCustomizer.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java b/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java index 2c29f7806..d61f89388 100644 --- a/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java +++ b/ehr/api-src/org/labkey/api/ehr/table/LabResultsPivotCustomizer.java @@ -101,14 +101,18 @@ public LabResultsPivotCustomizer(MultiValuedMap props) return values.isEmpty() ? defaultValue : StringUtils.trimToNull(values.iterator().next()); } - /** @param status comma-separated, since a cell can hold several results for one test */ - @Nullable Flag getFlag(@Nullable String status, boolean hasResult) + /** @param status comma-separated, since a cell can hold several results for one test; null for a qualitative result */ + @Nullable Flag getFlag(@Nullable String status, @Nullable String range, boolean hasResult) { Flag flag; - if (status != null) - flag = status.contains("High") ? Flag.HIGH : status.contains("Low") ? Flag.LOW : null; + if (!hasResult) + flag = null; + else if (range == null) + flag = Flag.NO_RANGE; + else if (status == null) + flag = null; else - flag = hasResult ? Flag.NO_RANGE : null; + flag = status.contains("High") ? Flag.HIGH : status.contains("Low") ? Flag.LOW : null; return getColor(flag) == null ? null : flag; } @@ -183,7 +187,7 @@ private boolean hasResult(RenderContext ctx) private @Nullable Flag getFlag(RenderContext ctx) { - return LabResultsPivotCustomizer.this.getFlag(getString(ctx, _statusKey), hasResult(ctx)); + return LabResultsPivotCustomizer.this.getFlag(getString(ctx, _statusKey), getString(ctx, _rangeKey), hasResult(ctx)); } @Override @@ -243,20 +247,22 @@ private static LabResultsPivotCustomizer create(String... keyValues) public void testFlags() { LabResultsPivotCustomizer customizer = create(); - assertEquals(Flag.HIGH, customizer.getFlag("High", true)); - assertEquals(Flag.LOW, customizer.getFlag("Low", true)); - assertEquals(Flag.HIGH, customizer.getFlag("Low,High", true)); - assertNull(customizer.getFlag("Normal", true)); - assertEquals(Flag.NO_RANGE, customizer.getFlag(null, true)); - assertNull(customizer.getFlag(null, false)); + assertEquals(Flag.HIGH, customizer.getFlag("High", "1-5", true)); + assertEquals(Flag.LOW, customizer.getFlag("Low", "1-5", true)); + assertEquals(Flag.HIGH, customizer.getFlag("Low,High", "1-5", true)); + assertNull(customizer.getFlag("Normal", "1-5", true)); + assertNull(customizer.getFlag(null, "1-5", true)); + assertEquals(Flag.NO_RANGE, customizer.getFlag(null, null, true)); + assertEquals(Flag.NO_RANGE, customizer.getFlag("High", null, true)); + assertNull(customizer.getFlag(null, null, false)); } @Test public void testEmptyColorTurnsOffFlag() { LabResultsPivotCustomizer customizer = create("noRangeColor", "", "lowColor", "FBEC5D"); - assertNull(customizer.getFlag(null, true)); - assertEquals(Flag.LOW, customizer.getFlag("Low", true)); + assertNull(customizer.getFlag(null, null, true)); + assertEquals(Flag.LOW, customizer.getFlag("Low", "1-5", true)); assertEquals("FBEC5D", customizer.getColor(Flag.LOW)); assertEquals(DEFAULT_HIGH_COLOR, customizer.getColor(Flag.HIGH)); }