2022-02-07 14:02:26 +00:00
|
|
|
import {GristDoc} from 'app/client/components/GristDoc';
|
2022-03-11 10:48:36 +00:00
|
|
|
import {copyToClipboard} from 'app/client/lib/copyToClipboard';
|
|
|
|
import {setTestState} from 'app/client/lib/testState';
|
|
|
|
import {TableRec} from 'app/client/models/DocModel';
|
2022-04-01 18:17:06 +00:00
|
|
|
import {docListHeader, docMenuTrigger} from 'app/client/ui/DocMenuCss';
|
2022-09-28 23:01:46 +00:00
|
|
|
import {duplicateTable, DuplicateTableResponse} from 'app/client/ui/DuplicateTable';
|
2022-03-11 10:48:36 +00:00
|
|
|
import {showTransientTooltip} from 'app/client/ui/tooltips';
|
2022-04-27 17:46:24 +00:00
|
|
|
import {buildTableName} from 'app/client/ui/WidgetTitle';
|
2022-03-11 10:48:36 +00:00
|
|
|
import * as css from 'app/client/ui2018/cssVars';
|
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
2022-08-11 15:09:03 +00:00
|
|
|
import {loadingDots} from 'app/client/ui2018/loaders';
|
2022-03-11 10:48:36 +00:00
|
|
|
import {menu, menuItem, menuText} from 'app/client/ui2018/menus';
|
|
|
|
import {confirmModal} from 'app/client/ui2018/modals';
|
2022-04-27 17:46:24 +00:00
|
|
|
import {Computed, Disposable, dom, fromKo, makeTestId, Observable, styled} from 'grainjs';
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2022-03-11 10:48:36 +00:00
|
|
|
|
|
|
|
const testId = makeTestId('test-raw-data-');
|
2022-02-07 14:02:26 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('components.DataTables');
|
|
|
|
|
2022-02-07 14:02:26 +00:00
|
|
|
export class DataTables extends Disposable {
|
2022-04-27 17:46:24 +00:00
|
|
|
private _tables: Observable<TableRec[]>;
|
2022-08-03 07:18:21 +00:00
|
|
|
|
|
|
|
private readonly _rowCount = Computed.create(
|
|
|
|
this, this._gristDoc.docPageModel.currentDocUsage, (_use, usage) => {
|
|
|
|
return usage?.rowCount;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2022-08-11 15:09:03 +00:00
|
|
|
// TODO: Update this whenever the rest of the UI is internationalized.
|
|
|
|
private readonly _rowCountFormatter = new Intl.NumberFormat('en-US');
|
|
|
|
|
2022-02-07 14:02:26 +00:00
|
|
|
constructor(private _gristDoc: GristDoc) {
|
|
|
|
super();
|
2022-07-06 07:41:09 +00:00
|
|
|
this._tables = Computed.create(this, use => {
|
|
|
|
const dataTables = use(_gristDoc.docModel.rawDataTables.getObservable());
|
|
|
|
const summaryTables = use(_gristDoc.docModel.rawSummaryTables.getObservable());
|
|
|
|
// Remove tables that we don't have access to. ACL will remove tableId from those tables.
|
2022-10-28 16:11:08 +00:00
|
|
|
return [...dataTables, ...summaryTables].filter(table => Boolean(use(table.tableId)));
|
2022-07-06 07:41:09 +00:00
|
|
|
});
|
2022-02-07 14:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
2022-03-11 10:48:36 +00:00
|
|
|
return container(
|
2022-04-01 18:17:06 +00:00
|
|
|
cssTableList(
|
2022-03-11 10:48:36 +00:00
|
|
|
/*************** List section **********/
|
|
|
|
testId('list'),
|
2022-12-20 02:06:39 +00:00
|
|
|
cssHeader(t('RawDataTables')),
|
2022-03-11 10:48:36 +00:00
|
|
|
cssList(
|
2022-04-27 17:46:24 +00:00
|
|
|
dom.forEach(this._tables, tableRec =>
|
2022-03-11 10:48:36 +00:00
|
|
|
cssItem(
|
|
|
|
testId('table'),
|
2022-04-27 17:46:24 +00:00
|
|
|
cssLeft(
|
2022-09-06 01:51:57 +00:00
|
|
|
dom.domComputed((use) => cssTableTypeIcon(
|
2022-07-06 07:41:09 +00:00
|
|
|
use(tableRec.summarySourceTable) !== 0 ? 'PivotLight' : 'TypeTable',
|
|
|
|
testId(`table-id-${use(tableRec.tableId)}`)
|
|
|
|
)),
|
2022-04-27 17:46:24 +00:00
|
|
|
),
|
|
|
|
cssMiddle(
|
2022-08-03 07:18:21 +00:00
|
|
|
cssTitleRow(cssTableTitle(this._tableTitle(tableRec), testId('table-title'))),
|
|
|
|
cssDetailsRow(
|
|
|
|
cssTableIdWrapper(cssHoverWrapper(
|
|
|
|
cssUpperCase("Table ID: "),
|
2022-04-27 17:46:24 +00:00
|
|
|
cssTableId(
|
|
|
|
testId('table-id'),
|
|
|
|
dom.text(tableRec.tableId),
|
|
|
|
),
|
2022-10-28 16:11:08 +00:00
|
|
|
{ title : t('ClickToCopy') },
|
|
|
|
dom.on('click', async (e, d) => {
|
2022-04-27 17:46:24 +00:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
e.preventDefault();
|
2022-10-28 16:11:08 +00:00
|
|
|
showTransientTooltip(d, t('TableIDCopied'), {
|
2022-04-27 17:46:24 +00:00
|
|
|
key: 'copy-table-id'
|
|
|
|
});
|
|
|
|
await copyToClipboard(tableRec.tableId.peek());
|
|
|
|
setTestState({clipboard: tableRec.tableId.peek()});
|
|
|
|
})
|
2022-08-03 07:18:21 +00:00
|
|
|
)),
|
|
|
|
this._tableRows(tableRec),
|
2022-03-11 10:48:36 +00:00
|
|
|
),
|
|
|
|
),
|
2022-04-27 17:46:24 +00:00
|
|
|
cssRight(
|
|
|
|
docMenuTrigger(
|
|
|
|
testId('table-menu'),
|
|
|
|
icon('Dots'),
|
|
|
|
menu(() => this._menuItems(tableRec), {placement: 'bottom-start'}),
|
|
|
|
dom.on('click', (ev) => { ev.stopPropagation(); ev.preventDefault(); }),
|
|
|
|
)
|
|
|
|
),
|
2022-03-11 10:48:36 +00:00
|
|
|
dom.on('click', () => {
|
|
|
|
const sectionId = tableRec.rawViewSection.peek().getRowId();
|
|
|
|
if (!sectionId) {
|
|
|
|
throw new Error(`Table ${tableRec.tableId.peek()} doesn't have a raw view section.`);
|
|
|
|
}
|
|
|
|
this._gristDoc.viewModel.activeSectionId(sectionId);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
)
|
|
|
|
),
|
2022-02-07 14:02:26 +00:00
|
|
|
),
|
2022-03-11 10:48:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:41:09 +00:00
|
|
|
private _tableTitle(table: TableRec) {
|
|
|
|
return dom.domComputed((use) => {
|
|
|
|
const rawViewSectionRef = use(fromKo(table.rawViewSectionRef));
|
|
|
|
const isSummaryTable = use(table.summarySourceTable) !== 0;
|
|
|
|
if (!rawViewSectionRef || isSummaryTable) {
|
|
|
|
// Some very old documents might not have a rawViewSection, and raw summary
|
|
|
|
// tables can't currently be renamed.
|
|
|
|
const tableName = [
|
|
|
|
use(table.tableNameDef), isSummaryTable ? use(table.groupDesc) : ''
|
|
|
|
].filter(p => Boolean(p?.trim())).join(' ');
|
2022-09-06 01:51:57 +00:00
|
|
|
return cssTableName(tableName);
|
2022-07-06 07:41:09 +00:00
|
|
|
} else {
|
|
|
|
return dom('div', // to disable flex grow in the widget
|
|
|
|
dom.domComputed(fromKo(table.rawViewSection), vs =>
|
2022-07-18 16:24:26 +00:00
|
|
|
buildTableName(vs, testId('widget-title'))
|
2022-07-06 07:41:09 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-27 17:46:24 +00:00
|
|
|
private _menuItems(table: TableRec) {
|
2022-03-11 10:48:36 +00:00
|
|
|
const {isReadonly, docModel} = this._gristDoc;
|
|
|
|
return [
|
2022-09-28 23:01:46 +00:00
|
|
|
menuItem(
|
|
|
|
() => this._duplicateTable(table),
|
2022-10-28 16:11:08 +00:00
|
|
|
t('DuplicateTable'),
|
2022-09-28 23:01:46 +00:00
|
|
|
testId('menu-duplicate-table'),
|
|
|
|
dom.cls('disabled', use =>
|
|
|
|
use(isReadonly) ||
|
|
|
|
use(table.isHidden) ||
|
|
|
|
use(table.summarySourceTable) !== 0
|
|
|
|
),
|
|
|
|
),
|
2022-03-11 10:48:36 +00:00
|
|
|
menuItem(
|
2022-04-27 17:46:24 +00:00
|
|
|
() => this._removeTable(table),
|
2022-03-11 10:48:36 +00:00
|
|
|
'Remove',
|
|
|
|
testId('menu-remove'),
|
2022-04-27 17:46:24 +00:00
|
|
|
dom.cls('disabled', use => use(isReadonly) || (
|
2022-07-06 07:41:09 +00:00
|
|
|
// Can't delete last visible table, unless it is a hidden table.
|
|
|
|
use(docModel.visibleTables.getObservable()).length <= 1 && !use(table.isHidden)
|
2022-04-27 17:46:24 +00:00
|
|
|
))
|
2022-03-11 10:48:36 +00:00
|
|
|
),
|
2022-10-28 16:11:08 +00:00
|
|
|
dom.maybe(isReadonly, () => menuText(t("NoEditAccess"))),
|
2022-02-07 14:02:26 +00:00
|
|
|
];
|
|
|
|
}
|
2022-03-11 10:48:36 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
private _duplicateTable(r: TableRec) {
|
|
|
|
duplicateTable(this._gristDoc, r.tableId(), {
|
2022-09-28 23:01:46 +00:00
|
|
|
onSuccess: ({raw_section_id}: DuplicateTableResponse) =>
|
|
|
|
this._gristDoc.viewModel.activeSectionId(raw_section_id),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
private _removeTable(r: TableRec) {
|
2022-03-11 10:48:36 +00:00
|
|
|
const {docModel} = this._gristDoc;
|
|
|
|
function doRemove() {
|
2022-10-28 16:11:08 +00:00
|
|
|
return docModel.docData.sendAction(['RemoveTable', r.tableId()]);
|
2022-03-11 10:48:36 +00:00
|
|
|
}
|
2022-10-28 16:11:08 +00:00
|
|
|
confirmModal(t("DeleteData", {formattedTableName : r.formattedTableName()}), 'Delete', doRemove);
|
2022-03-11 10:48:36 +00:00
|
|
|
}
|
2022-08-03 07:18:21 +00:00
|
|
|
|
|
|
|
private _tableRows(table: TableRec) {
|
2022-08-11 15:09:03 +00:00
|
|
|
return dom.maybe(this._rowCount, (rowCounts) => {
|
|
|
|
if (rowCounts === 'hidden') { return null; }
|
2022-08-03 07:18:21 +00:00
|
|
|
|
2022-08-11 15:09:03 +00:00
|
|
|
return cssTableRowsWrapper(
|
|
|
|
cssUpperCase("Rows: "),
|
|
|
|
rowCounts === 'pending' ? cssLoadingDots() : cssTableRows(
|
|
|
|
rowCounts[table.getRowId()] !== undefined
|
|
|
|
? this._rowCountFormatter.format(rowCounts[table.getRowId()])
|
|
|
|
: '',
|
|
|
|
testId('table-rows'),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2022-08-03 07:18:21 +00:00
|
|
|
}
|
2022-02-07 14:02:26 +00:00
|
|
|
}
|
2022-03-11 10:48:36 +00:00
|
|
|
|
|
|
|
const container = styled('div', `
|
2022-04-01 18:17:06 +00:00
|
|
|
overflow-y: auto;
|
2022-03-11 10:48:36 +00:00
|
|
|
position: relative;
|
|
|
|
`);
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
const cssHeader = styled(docListHeader, `
|
|
|
|
display: inline-block;
|
|
|
|
`);
|
|
|
|
|
2022-03-11 10:48:36 +00:00
|
|
|
const cssList = styled('div', `
|
|
|
|
display: flex;
|
2022-08-03 07:18:21 +00:00
|
|
|
flex-direction: column;
|
|
|
|
gap: 12px;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssItem = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
cursor: pointer;
|
|
|
|
border-radius: 3px;
|
2022-08-03 07:18:21 +00:00
|
|
|
width: 100%;
|
|
|
|
height: calc(1em * 56/13); /* 56px for 13px font */
|
2022-03-11 10:48:36 +00:00
|
|
|
max-width: 750px;
|
2022-09-06 01:51:57 +00:00
|
|
|
border: 1px solid ${css.theme.rawDataTableBorder};
|
2022-03-11 10:48:36 +00:00
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
border-color: ${css.theme.rawDataTableBorderHover};
|
2022-03-11 10:48:36 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
2022-04-27 17:46:24 +00:00
|
|
|
// Holds icon in top left corner
|
|
|
|
const cssLeft = styled('div', `
|
|
|
|
padding-top: 11px;
|
|
|
|
padding-left: 12px;
|
2022-03-11 10:48:36 +00:00
|
|
|
margin-right: 8px;
|
2022-04-27 17:46:24 +00:00
|
|
|
align-self: flex-start;
|
|
|
|
display: flex;
|
2022-03-11 10:48:36 +00:00
|
|
|
flex: none;
|
2022-04-27 17:46:24 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
const cssMiddle = styled('div', `
|
|
|
|
flex-grow: 1;
|
|
|
|
min-width: 0px;
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin-top: 6px;
|
|
|
|
margin-bottom: 4px;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
2022-08-03 07:18:21 +00:00
|
|
|
const cssTitleRow = styled('div', `
|
|
|
|
min-width: 100%;
|
2022-07-18 16:24:26 +00:00
|
|
|
margin-right: 4px;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
2022-08-03 07:18:21 +00:00
|
|
|
const cssDetailsRow = styled('div', `
|
|
|
|
min-width: 100%;
|
2022-03-11 10:48:36 +00:00
|
|
|
display: flex;
|
2022-08-03 07:18:21 +00:00
|
|
|
gap: 8px;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
|
|
|
|
2022-04-27 17:46:24 +00:00
|
|
|
// Holds dots menu (which is 24px x 24px, but has its own 4px right margin)
|
|
|
|
const cssRight = styled('div', `
|
|
|
|
padding-right: 8px;
|
|
|
|
margin-left: 8px;
|
|
|
|
align-self: center;
|
2022-03-11 10:48:36 +00:00
|
|
|
display: flex;
|
2022-04-27 17:46:24 +00:00
|
|
|
flex: none;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
const cssTableTypeIcon = styled(icon, `
|
|
|
|
--icon-color: ${css.theme.accentIcon};
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
|
|
|
|
2022-04-27 17:46:24 +00:00
|
|
|
const cssLine = styled('span', `
|
|
|
|
white-space: nowrap;
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
overflow: hidden;
|
|
|
|
`);
|
|
|
|
|
2022-08-03 07:18:21 +00:00
|
|
|
const cssTableIdWrapper = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-grow: 1;
|
|
|
|
min-width: 0;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssTableRowsWrapper = styled('div', `
|
|
|
|
display: flex;
|
|
|
|
flex-shrink: 0;
|
|
|
|
width: 80px;
|
|
|
|
overflow: hidden;
|
|
|
|
align-items: baseline;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${css.theme.lightText};
|
2022-08-03 07:18:21 +00:00
|
|
|
line-height: 18px;
|
|
|
|
padding: 0px 2px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssHoverWrapper = styled('div', `
|
2022-03-11 10:48:36 +00:00
|
|
|
display: flex;
|
2022-04-27 17:46:24 +00:00
|
|
|
overflow: hidden;
|
2022-03-11 10:48:36 +00:00
|
|
|
cursor: default;
|
|
|
|
align-items: baseline;
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${css.theme.lightText};
|
2022-03-11 10:48:36 +00:00
|
|
|
transition: background 0.05s;
|
2022-08-03 07:18:21 +00:00
|
|
|
padding: 0px 2px;
|
2022-04-27 17:46:24 +00:00
|
|
|
line-height: 18px;
|
2022-03-11 10:48:36 +00:00
|
|
|
&:hover {
|
2022-09-06 01:51:57 +00:00
|
|
|
background: ${css.theme.lightHover};
|
2022-03-11 10:48:36 +00:00
|
|
|
}
|
|
|
|
`);
|
|
|
|
|
|
|
|
const cssTableId = styled(cssLine, `
|
|
|
|
font-size: ${css.vars.smallFontSize};
|
|
|
|
`);
|
|
|
|
|
2022-08-03 07:18:21 +00:00
|
|
|
const cssTableRows = cssTableId;
|
|
|
|
|
2022-07-18 16:24:26 +00:00
|
|
|
const cssTableTitle = styled('div', `
|
2022-09-06 01:51:57 +00:00
|
|
|
color: ${css.theme.text};
|
2022-07-18 16:24:26 +00:00
|
|
|
white-space: nowrap;
|
|
|
|
`);
|
|
|
|
|
2022-03-11 10:48:36 +00:00
|
|
|
const cssUpperCase = styled('span', `
|
|
|
|
text-transform: uppercase;
|
|
|
|
letter-spacing: 0.81px;
|
|
|
|
font-weight: 500;
|
|
|
|
font-size: 9px; /* xxsmallFontSize is to small */
|
|
|
|
margin-right: 2px;
|
|
|
|
flex: 0;
|
|
|
|
white-space: nowrap;
|
|
|
|
`);
|
|
|
|
|
2022-04-01 18:17:06 +00:00
|
|
|
const cssTableList = styled('div', `
|
|
|
|
overflow-y: auto;
|
|
|
|
position: relative;
|
|
|
|
margin-bottom: 56px;
|
2022-03-11 10:48:36 +00:00
|
|
|
`);
|
2022-08-11 15:09:03 +00:00
|
|
|
|
|
|
|
const cssLoadingDots = styled(loadingDots, `
|
|
|
|
--dot-size: 6px;
|
|
|
|
`);
|
2022-09-06 01:51:57 +00:00
|
|
|
|
|
|
|
const cssTableName = styled('span', `
|
|
|
|
color: ${css.theme.text};
|
|
|
|
`);
|