diff --git a/src/assets/scss/main.scss b/src/assets/scss/main.scss index 7830f4d0a..a3aa9bc29 100644 --- a/src/assets/scss/main.scss +++ b/src/assets/scss/main.scss @@ -527,6 +527,98 @@ table tr.is-edited + .detail { min-height: 100px; } +// Scrollbars that are always drawn. macOS (and Windows 11 by default) show overlay scrollbars only +// while scrolling, which hides that a table scrolls sideways from mouse users. Chrome, Edge and Safari +// draw these; Firefox keeps its own. Keep the size in step with MIN_BAR_HEIGHT_PX in FloatingScrollbar. +// Setting scrollbar-width or scrollbar-color here would make Chrome ignore these rules. +@mixin visible-scrollbars { + &::-webkit-scrollbar { + width: 12px; + height: 12px; + } + + &::-webkit-scrollbar-track, + &::-webkit-scrollbar-corner { + background-color: $white-ter; + } + + &::-webkit-scrollbar-thumb { + background-color: $grey-light; + border: 3px solid $white-ter; + border-radius: 6px; + + &:hover { + background-color: $grey; + } + } +} + +// Tables taller than the window scroll inside a box (sized by BoundedTableMixin) so the header row and +// horizontal scrollbar stay on screen. Shorter tables look the same as unbounded ones. +.bounded-table { + .table-wrapper.has-sticky-header, + .bounded-table-scroll { + // Buefy gives a sticky-header wrapper a fixed 300px height: cap the height instead. + height: auto; + max-height: var(--bounded-table-max-height, calc(100vh - 10rem)); + overflow: auto; + @include visible-scrollbars; + + // Collapsed borders belong to the table rather than the pinned header cells, so rows would show + // through them while scrolling. Bulma only draws bottom borders, so this looks the same. + > .table { + border-collapse: separate; + border-spacing: 0; + } + + // The box scrolls on any overflow, so keep content that used to spill harmlessly past the table + // inside it: Buefy's sort arrow sits just past its label, and the empty message's Bulma columns + // have negative margins. + thead th:last-child .th-wrap { + padding-right: 1rem; + } + + tr.is-empty > td { + overflow: hidden; + } + } + + // Pin the whole header, including the column filter row, not only Buefy's first header row. + // Its z-index stays below the sticky .main-header (30). + .table-wrapper.has-sticky-header thead, + .bounded-table-scroll thead { + position: sticky; + top: 0; + z-index: 2; + + th, + td { + background-color: $white; + } + } +} + +.table-result-count { + margin-bottom: 0.5rem; +} + +// Stand-in for a bounded table's horizontal scrollbar while the box's own is below the window +// (FloatingScrollbar). Above the table's pinned header (2), below the sticky .main-header (30). +.floating-scrollbar { + position: fixed; + bottom: 0; + z-index: 20; + overflow-x: auto; + overflow-y: hidden; + background-color: $white; + border-top: 1px solid $grey-lighter; + @include visible-scrollbars; + + > div { + height: 1px; + } +} + .menu-scroll { position: absolute; height: 100%; @@ -920,6 +1012,70 @@ tr:nth-child(odd) td.db-filled { } } +// An experiment's description and fields, in a box whose header collapses it to bring the +// datasets up. The header and body set their own padding so the header's hover fills the box. +.experiment-details { + padding: 0; + margin-bottom: 1.5rem; + // Same indent as the dataset tabs and .tab-content below it. + margin-left: 2em; + // Flat, like the tabs, table and dataset summary around it. + border: 1px solid $grey-lighter; + box-shadow: none; +} + +// A dataset's figures on one line above its table. +.dataset-summary { + display: flex; + flex-wrap: wrap; + column-gap: 2.5rem; + row-gap: 0.25rem; +} + +.experiment-details-toggle { + display: flex; + align-items: center; + gap: 0.5rem; + width: 100%; + padding: 0.75rem 1.25rem; + border: none; + border-radius: inherit; + background: none; + // Link blue, as DeltaBreed marks text actions. + color: $link; + font: inherit; + font-weight: $weight-bold; + text-align: left; + cursor: pointer; + + &:hover { + background-color: $white-ter; + } + + &:focus-visible { + outline: 2px solid $link; + outline-offset: -2px; + } +} + +.experiment-details-body { + padding: 1rem 1.25rem 0.5rem; + border-top: 1px solid $grey-lighter; + + // Line the fields up with the description. + ul { + margin-left: 0; + } +} + +// At a readable line length; long citations carry unbroken URLs. +.experiment-description { + max-width: 60rem; + margin-bottom: 1rem; + overflow-wrap: break-word; + word-break: break-word; +} + .collaborators-section { display: flex; align-items: flex-start; diff --git a/src/breeding-insight/model/view_models/TableResultRange.ts b/src/breeding-insight/model/view_models/TableResultRange.ts new file mode 100644 index 000000000..d7a04ebc5 --- /dev/null +++ b/src/breeding-insight/model/view_models/TableResultRange.ts @@ -0,0 +1,94 @@ +/* + * See the NOTICE file distributed with this work for additional information + * regarding copyright ownership. + * + * 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. + */ + +/** + * The pagination fields the range needs. Both PaginationController and the server's Pagination have + * them; only PaginationController has showAll. + */ +export interface TablePageState { + currentPage: number; + pageSize: number; + totalCount: number; + showAll?: boolean; +} + +/** + * The rows a paginated table is showing: rows start to end of total (1-based), or all of them. + * + * When the table's own column filters narrow a single page of server results, the filter only sees + * that page: matchedOnPage of the page's pageCount rows match, and total is the server's total. + */ +export class TableResultRange { + constructor( + public readonly start: number, + public readonly end: number, + public readonly total: number, + public readonly showAll: boolean, + public readonly matchedOnPage: number | null = null, + public readonly pageCount: number | null = null + ) {} + + static readonly EMPTY = new TableResultRange(0, 0, 0, false); + + /** True when a column filter is narrowing one page of server results. */ + get filteredOnPage(): boolean { + return this.matchedOnPage !== null; + } + + /** + * Works out the range for a table whose records are either one page from the server, or every + * record with the table paging (and possibly filtering) them in the browser. + * + * @param pagination the table's pagination state + * @param recordCount the number of records passed to the table + * @param filteredCount the number of records left after the table's own (client-side) filters + */ + static of(pagination: TablePageState, recordCount: number, filteredCount: number): TableResultRange { + const pageSize = Math.max(pagination.pageSize, 1); + const showAll = !!pagination.showAll; + // The server holds more rows than it sent, so the records are a single page of server results. + const serverPaged = pagination.totalCount > recordCount; + + if (serverPaged) { + // No rows yet, e.g. while the next page loads: say nothing rather than a range that isn't there. + if (recordCount === 0) { + return TableResultRange.EMPTY; + } + const total = pagination.totalCount; + if (filteredCount < recordCount) { + return new TableResultRange(0, 0, total, showAll, filteredCount, recordCount); + } + const start = Math.min((Math.max(pagination.currentPage, 1) - 1) * pageSize + 1, total); + const end = Math.max(Math.min(start + recordCount - 1, total), start); + return new TableResultRange(start, end, total, showAll); + } + + const total = filteredCount; + if (total === 0) { + return new TableResultRange(0, 0, 0, showAll); + } + if (showAll || total <= pageSize) { + // Matches Buefy, which shows every row when they all fit on one page, whatever the current page. + return new TableResultRange(1, total, total, showAll); + } + const lastPage = Math.ceil(total / pageSize); + const page = Math.min(Math.max(pagination.currentPage, 1), lastPage); + const start = (page - 1) * pageSize + 1; + const end = Math.min(start + pageSize - 1, total); + return new TableResultRange(start, end, total, showAll); + } +} diff --git a/src/breeding-insight/utils/LayoutUtils.ts b/src/breeding-insight/utils/LayoutUtils.ts new file mode 100644 index 000000000..13e2f7dbb --- /dev/null +++ b/src/breeding-insight/utils/LayoutUtils.ts @@ -0,0 +1,94 @@ +/* + * See the NOTICE file distributed with this work for additional information + * regarding copyright ownership. + * + * 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. + */ + +// ResizeObserver is in every supported browser, but not in this TypeScript version's DOM types. +interface ResizeObserverLike { + observe(element: Element): void; + disconnect(): void; +} + +type ResizeObserverConstructor = new (callback: () => void) => ResizeObserverLike; + +export interface FrameThrottle { + /** Runs the function on the next animation frame, once however often this is called before then. */ + schedule(): void; + /** Drops a scheduled run, e.g. when the component is destroyed. */ + cancel(): void; +} + +export class LayoutUtils { + + private static pageResizeSubscribers: Set<() => void> = new Set(); + private static pageResizeObserver: ResizeObserverLike | null = null; + + /** Wraps a function so that it runs at most once per animation frame. */ + static frameThrottle(fn: () => void): FrameThrottle { + let frame: number | null = null; + return { + schedule() { + if (frame !== null) return; + frame = window.requestAnimationFrame(() => { + frame = null; + fn(); + }); + }, + cancel() { + if (frame !== null) { + window.cancelAnimationFrame(frame); + frame = null; + } + } + }; + } + + /** + * Calls the callback when any of the elements changes size. Returns a function that stops observing. + * Does nothing in browsers without ResizeObserver. + */ + static observeResize(elements: Element[], callback: () => void): () => void { + const observer = LayoutUtils.createResizeObserver(callback); + if (!observer) return () => {}; + elements.forEach(element => observer.observe(element)); + return () => observer.disconnect(); + } + + /** + * Calls the callback when the page's height or width changes, e.g. when content above a table + * collapses. Every subscriber shares one observer. Returns a function that unsubscribes. + */ + static onPageResize(callback: () => void): () => void { + LayoutUtils.pageResizeSubscribers.add(callback); + if (!LayoutUtils.pageResizeObserver) { + LayoutUtils.pageResizeObserver = LayoutUtils.createResizeObserver( + () => LayoutUtils.pageResizeSubscribers.forEach(subscriber => subscriber()) + ); + if (LayoutUtils.pageResizeObserver) LayoutUtils.pageResizeObserver.observe(document.body); + } + return () => { + LayoutUtils.pageResizeSubscribers.delete(callback); + if (LayoutUtils.pageResizeSubscribers.size === 0 && LayoutUtils.pageResizeObserver) { + LayoutUtils.pageResizeObserver.disconnect(); + LayoutUtils.pageResizeObserver = null; + } + }; + } + + private static createResizeObserver(callback: () => void): ResizeObserverLike | null { + const ResizeObserverClass = (window as unknown as { ResizeObserver?: ResizeObserverConstructor }).ResizeObserver; + return ResizeObserverClass ? new ResizeObserverClass(callback) : null; + } +} diff --git a/src/breeding-insight/utils/StringFormatters.ts b/src/breeding-insight/utils/StringFormatters.ts index be2f7b6cd..bf31ad90c 100644 --- a/src/breeding-insight/utils/StringFormatters.ts +++ b/src/breeding-insight/utils/StringFormatters.ts @@ -24,4 +24,10 @@ export class StringFormatters { return undefined; } + /** A count with thousands separators, e.g. 25218 as "25,218". Values that aren't numbers pass through. */ + static formatCount(value: number | string): string { + const count = Number(value); + return value === '' || isNaN(count) ? String(value) : count.toLocaleString('en-US'); + } + } \ No newline at end of file diff --git a/src/components/admin/AdminProgramsTable.vue b/src/components/admin/AdminProgramsTable.vue index 614ee0be7..1fc5d0c8d 100644 --- a/src/components/admin/AdminProgramsTable.vue +++ b/src/components/admin/AdminProgramsTable.vue @@ -119,6 +119,8 @@ File contains data errors
- - - - - - - @@ -90,6 +92,9 @@ showExpandControls!: boolean; @Prop() showRowIcon!: boolean; + // Scroll the rows inside a box with the header pinned; the parent sizes the box (see BoundedTableMixin). + @Prop({default: false}) + bounded!: boolean; private initialUpdate: boolean = false; private tableRows: Array> = new Array>(); @@ -160,6 +165,14 @@ } } + // Tells the parent the sort changed, so a bounded table can return to its first row. + onHeaderClick(column: TableColumn) { + column.changeSortColumn(column.sortLabel); + if (column.isSortable) { + this.$emit('sort-click', column.sortLabel); + } + } + emitAndUpdateIsMobile(event: string) { this.$emit(event); if (this.isMobile) { diff --git a/src/components/tables/FloatingScrollbar.vue b/src/components/tables/FloatingScrollbar.vue new file mode 100644 index 000000000..2a6bd6a82 --- /dev/null +++ b/src/components/tables/FloatingScrollbar.vue @@ -0,0 +1,147 @@ + + + + + diff --git a/src/components/tables/SidePanelTable.vue b/src/components/tables/SidePanelTable.vue index ac33a8eca..870172dd4 100644 --- a/src/components/tables/SidePanelTable.vue +++ b/src/components/tables/SidePanelTable.vue @@ -1,5 +1,8 @@ ) { let editForm = wrapper.findComponent(EditDataRowForm); expect(editForm.exists()).toBeTruthy(); } + +describe('Expandable table bounds its height by default', () => { + + function mountTable(propsData: object = {}) { + return mount(ExpandableTable, { + localVue, + propsData: {records: [], loading: false, pagination: new PaginationController(), ...propsData} + }); + } + + it('Pins the header in a bounded scroll box by default', () => { + const wrapper = mountTable(); + expect(wrapper.classes()).toContain('bounded-table'); + expect(wrapper.find('.table-wrapper').classes()).toContain('has-sticky-header'); + }); + + it('Opts out with bounded set to false', () => { + const wrapper = mountTable({bounded: false}); + expect(wrapper.classes()).not.toContain('bounded-table'); + expect(wrapper.find('.table-wrapper').classes()).not.toContain('has-sticky-header'); + }); +}); diff --git a/tests/unit/components/tables/TableResultCount.spec.ts b/tests/unit/components/tables/TableResultCount.spec.ts new file mode 100644 index 000000000..c9b0dff67 --- /dev/null +++ b/tests/unit/components/tables/TableResultCount.spec.ts @@ -0,0 +1,67 @@ +/* + * See the NOTICE file distributed with this work for additional information + * regarding copyright ownership. + * + * 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. + */ + +import {mount} from '@vue/test-utils'; +import localVue from '../../index'; +import TableResultCount from '@/components/tables/TableResultCount.vue'; +import {TableResultRange} from '@/breeding-insight/model/view_models/TableResultRange'; + +function render(range: TableResultRange, props: object = {}) { + return mount(TableResultCount, { + localVue, + propsData: {range, recordNoun: 'observation units', recordNounSingular: 'observation unit', ...props} + }); +} + +describe('TableResultCount says which rows a table shows.', () => { + + it('Shows the range and total with thousands separators', () => { + const wrapper = render(new TableResultRange(1, 200, 25218, false)); + expect(wrapper.text()).toEqual('Showing 1–200 of 25,218 observation units'); + }); + + it('Shows a single row without a range', () => { + const wrapper = render(new TableResultRange(1, 1, 1, false)); + expect(wrapper.text()).toEqual('Showing 1 of 1 observation unit'); + }); + + it('Shows every row with Show All', () => { + const wrapper = render(new TableResultRange(1, 2000, 2000, true)); + expect(wrapper.text()).toEqual('Showing all 2,000 observation units'); + }); + + it('Shows matches on the page when a column filter narrows a server page', () => { + const wrapper = render(new TableResultRange(0, 0, 2000, false, 3, 200)); + expect(wrapper.text()).toEqual('Showing 3 matching observation units of the 200 on this page (2,000 in all)'); + }); + + it('Uses "results" when the table names no noun', () => { + const wrapper = mount(TableResultCount, {localVue, propsData: {range: new TableResultRange(1, 5, 5, false)}}); + expect(wrapper.text()).toEqual('Showing 1–5 of 5 results'); + }); + + it('Shows nothing when there are no rows', () => { + const wrapper = render(TableResultRange.EMPTY); + expect(wrapper.find('.table-result-count').exists()).toBe(false); + }); + + it('Shows nothing while loading', () => { + const wrapper = render(new TableResultRange(1, 200, 2000, false), {loading: true}); + expect(wrapper.find('.table-result-count').exists()).toBe(false); + }); + +}); diff --git a/tests/unit/models/tableResultRange.spec.ts b/tests/unit/models/tableResultRange.spec.ts new file mode 100644 index 000000000..2b31322ad --- /dev/null +++ b/tests/unit/models/tableResultRange.spec.ts @@ -0,0 +1,80 @@ +/* + * See the NOTICE file distributed with this work for additional information + * regarding copyright ownership. + * + * 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. + */ + +import {TableResultRange} from "@/breeding-insight/model/view_models/TableResultRange"; +import {PaginationController} from "@/breeding-insight/model/view_models/PaginationController"; + +function pagination(currentPage: number, pageSize: number, totalCount: number, showAll = false): PaginationController { + const controller = new PaginationController(currentPage, pageSize, undefined, totalCount); + controller.showAll = showAll; + return controller; +} + +describe('TableResultRange works out the rows a table is showing.', () => { + + it('Returns the first page when every record is in the browser', () => { + const range = TableResultRange.of(pagination(1, 200, 2000), 2000, 2000); + expect([range.start, range.end, range.total, range.showAll]).toEqual([1, 200, 2000, false]); + }); + + it('Returns a partial last page when every record is in the browser', () => { + const range = TableResultRange.of(pagination(3, 50, 120), 120, 120); + expect([range.start, range.end, range.total]).toEqual([101, 120, 120]); + }); + + it('Returns every row when they fit on one page, whatever the current page', () => { + const range = TableResultRange.of(pagination(4, 50, 120), 120, 30); + expect([range.start, range.end, range.total]).toEqual([1, 30, 30]); + }); + + it('Keeps a page past the end of the filtered rows within range', () => { + const range = TableResultRange.of(pagination(5, 50, 300), 300, 120); + expect([range.start, range.end, range.total]).toEqual([101, 120, 120]); + }); + + it('Returns every row with Show All', () => { + const range = TableResultRange.of(pagination(1, 2000, 2000, true), 2000, 2000); + expect([range.start, range.end, range.total, range.showAll]).toEqual([1, 2000, 2000, true]); + }); + + it('Returns the server page when the records are one page of server results', () => { + const range = TableResultRange.of(pagination(2, 200, 2000), 200, 200); + expect([range.start, range.end, range.total, range.filteredOnPage]).toEqual([201, 400, 2000, false]); + }); + + it('Returns a short last server page', () => { + const range = TableResultRange.of(pagination(10, 200, 1850), 50, 50); + expect([range.start, range.end, range.total]).toEqual([1801, 1850, 1850]); + }); + + it('Returns an empty range while a server page has no records yet', () => { + const range = TableResultRange.of(pagination(2, 200, 2000), 0, 0); + expect(range.total).toEqual(0); + }); + + it('Reports matches on the page when a column filter narrows a server page', () => { + const range = TableResultRange.of(pagination(2, 200, 2000), 200, 3); + expect(range.filteredOnPage).toBe(true); + expect([range.matchedOnPage, range.pageCount, range.total]).toEqual([3, 200, 2000]); + }); + + it('Returns an empty range when there are no records', () => { + const range = TableResultRange.of(pagination(1, 50, 0), 0, 0); + expect([range.start, range.end, range.total]).toEqual([0, 0, 0]); + }); + +});
- - {{ column.label }} - -