Skip to content

Commit

Permalink
Fixes for cell annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomodwyer committed Nov 3, 2023
1 parent f16ce45 commit a103029
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
24 changes: 16 additions & 8 deletions assets/src/scripts/_file-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,27 @@ import "highlight.js/styles/github.css";
import { highlightJsName } from "./_utils";

/**
* @param {HTMLElement} el
* @param {string} ext
* @param {string} url
* @param {object} outcome
* @param {Object} params
* @param {HTMLElement} params.element
* @param {string} params.fileExtension
* @param {string} params.fileUrl
* @param {object} params.outcome
* @param {string} params.fileIndex
*/
export async function createTableElement(el, ext, url, outcome, index) {
const data = await fileLoader(ext, url);
export async function createTableElement({
element,
fileExtension,
fileUrl,
outcome,
fileIndex,
}) {
const data = await fileLoader(fileExtension, fileUrl);

tableBuilder({
csvString: data,
el,
el: element,
outcome,
index,
fileIndex,
});
}

Expand Down
15 changes: 8 additions & 7 deletions assets/src/scripts/_output-click.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default async function outputClick({ outputName, metadata }) {
const filePreviewContainer = document.getElementById("filePreviewContainer");
filePreviewContainer.innerHTML = "";

/** @type {HTMLTemplateElement|null} */
const filePreviewTemplate = document.querySelector(
`[data-sacro-el="file-preview-template"]`
);
Expand Down Expand Up @@ -75,13 +76,13 @@ export default async function outputClick({ outputName, metadata }) {
filePreviewLink.setAttribute("href", url);

if (isCsv(ext)) {
createTableElement(
filePreviewContent,
ext,
url,
openOutput.value.metadata.files?.[i].cell_index,
i
);
createTableElement({
element: filePreviewContent,
fileExtension: ext,
fileUrl: url,
outcome: filedata.cell_index,
fileIndex: i,
});
} else if (isImg(ext)) {
createImageElement(filePreviewContent, url);
} else if (isTxt(ext)) {
Expand Down
22 changes: 12 additions & 10 deletions assets/src/scripts/_table-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import Papa from "papaparse";
import { html } from "./_utils";

/**
* @param {{[x: string]: string[];}} cellIndex
* @param {string} tableId
* @param {Object} params
* @param {{[x: string]: string[];}} params.cellIndex
* @param {string} params.tableId
*/
function highlightFailingCells(cellIndex, tableId) {
function highlightFailingCells({ cellIndex, tableId }) {
/** @type {HTMLElement|null} */
const tableBody = document.getElementById(tableId);
if (!tableBody) return;
Expand Down Expand Up @@ -40,15 +41,13 @@ function highlightFailingCells(cellIndex, tableId) {
const tooltipEl = tooltipTemplateEl?.content?.firstElementChild;
if (!tooltipEl) return;

/** @type {HTMLSpanElement} */
/** @type {Node} */
const cellTooltip = tooltipEl.cloneNode(true);

cellTooltip.classList.add("flex", "-bottom-2");
cellTooltip.querySelector(`[data-sacro-el="tooltip-content"]`).innerHTML =
tooltipContent;
tableCell.appendChild(cellTooltip);

return tableCell;
});
}

Expand All @@ -58,9 +57,9 @@ function highlightFailingCells(cellIndex, tableId) {
* @param {Object.<string, string[]>} params.outcome
* @param {HTMLElement} params.el
* @param {string} params.csvString
* @param {string} params.index
* @param {string} params.fileIndex
*/
function tableBuilder({ csvString, el, outcome, index }) {
function tableBuilder({ csvString, el, outcome, fileIndex }) {
const csvToJson = Papa.parse(csvString).data;

const bodyCell = (row) =>
Expand All @@ -75,7 +74,7 @@ function tableBuilder({ csvString, el, outcome, index }) {

const table = html`
<table
id=${`csvTable${index}`}
id=${`csvTable${fileIndex}`}
class="min-w-full divide-y divide-gray-300 text-left text-sm text-gray-900"
>
<thead class="font-semibold bg-gray-200">
Expand All @@ -91,7 +90,10 @@ function tableBuilder({ csvString, el, outcome, index }) {

el.innerHTML = table; // eslint-disable-line no-param-reassign

highlightFailingCells(outcome, `csvTable${index}`);
highlightFailingCells({
cellIndex: outcome,
tableId: `csvTable${fileIndex}`,
});
}

export default tableBuilder;

0 comments on commit a103029

Please sign in to comment.