(core) Polish Record Cards

Summary:
Improvements
 - Widget and column descriptions are now copied when duplicating a table.
 - A Grist Plugin API command to open a Record Card is now available.
 - New Card widgets set initial settings based on those used by their table's
 Record Card.

Fixes
 - Opening a reference in a Record Card from a Raw Data popup now opens
 the correct reference.

Test Plan: Browser and python tests.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D4164
This commit is contained in:
George Gevoian
2024-01-30 09:49:00 -05:00
parent 11afc08f65
commit b1f7ca353a
19 changed files with 361 additions and 152 deletions

View File

@@ -1599,6 +1599,14 @@ class TestUserActions(test_engine.EngineTestCase):
'formula': '$A == "Foo"',
}])
# Add a column and widget description.
self.apply_user_action(['UpdateRecord', '_grist_Tables_column', 23, {
'description': 'A column description.',
}])
self.apply_user_action(['UpdateRecord', '_grist_Views_section', 2, {
'description': 'A widget description.',
}])
# Duplicate Table1 as Foo without including any of its data.
self.apply_user_action(['DuplicateTable', 'Table1', 'Foo', False])
@@ -1642,6 +1650,16 @@ class TestUserActions(test_engine.EngineTestCase):
["id", "A", "B", "C", "D", "E", "F", "G", "H", "gristHelper_ConditionalRule",
"gristHelper_RowConditionalRule", "manualSort"],
])
self.assertTableData('_grist_Tables_column', rows='subset', cols='subset', data=[
['id', 'description'],
[23, 'A column description.'],
[34, 'A column description.'],
])
self.assertTableData('_grist_Views_section', rows='subset', cols='subset', data=[
['id', 'description'],
[2, 'A widget description.'],
[4, 'A widget description.'],
])
# Duplicate Table1 as FooData and include all of its data.
self.apply_user_action(['DuplicateTable', 'Table1', 'FooData', True])
@@ -1686,7 +1704,7 @@ class TestUserActions(test_engine.EngineTestCase):
duplicated_times = self.engine.fetch_table('FooData').columns['E']
self.assertEqual(existing_times, duplicated_times)
def test_duplicate_table2(self):
def test_duplicate_table_untie_col_id_bug(self):
# This test case verifies a bug fix: when a column doesn't match its label despite
# untieColIdFromLabel being False (which is possible), ensure that duplicating still works.
@@ -1703,3 +1721,53 @@ class TestUserActions(test_engine.EngineTestCase):
self.apply_user_action(['DuplicateTable', 'Table1', 'Foo', True])
self.assertTableData('Table1', data=[["id", "State2", 'manualSort'], [1, 'NY', 1.0]])
self.assertTableData('Foo', data=[["id", "State2", 'manualSort'], [1, 'NY', 1.0]])
def test_duplicate_table_record_card(self):
self.load_sample(self.sample)
self.apply_user_action(['AddEmptyTable', None])
self.apply_user_action(['AddColumn', 'Table1', None, {
'type': 'Ref:Table1',
'visibleCol': 23,
}])
self.apply_user_action(['AddColumn', 'Table1', None, {
'type': 'RefList:Table1',
'visibleCol': 24,
}])
self.apply_user_action(['BulkUpdateRecord', '_grist_Views_section_field', [11, 13], {
'visibleCol': [23, 24],
}])
self.apply_user_action(['UpdateRecord', '_grist_Views_section', 3, {
'layoutSpec': '{"children":[{"children":[{"leaf":7},{"leaf":8}]},{"leaf":9},{"leaf":11}]}',
'options': '{"verticalGridlines":true,"horizontalGridlines":true,"zebraStripes":false,' +
'"customView":"","numFrozen":0,"disabled":true}',
'theme': 'compact',
}])
self.apply_user_action(['DuplicateTable', 'Table1', 'Foo', False])
self.assertTableData('_grist_Views_section', rows="subset", cols="subset", data=[
["id", "parentId", "tableRef", "layoutSpec", "options", "theme"],
# The original record card section.
[3, 0, 2, '{"children":[{"children":[{"leaf":7},{"leaf":8}]},{"leaf":9},{"leaf":11}]}',
'{"verticalGridlines":true,"horizontalGridlines":true,"zebraStripes":false,' +
'"customView":"","numFrozen":0,"disabled":true}', 'compact'],
# The duplicated record card section.
[5, 0, 3,
'{"children": [{"children": [{"leaf": 19}, {"leaf": 20}]}, {"leaf": 21}, ' +
'{"leaf": 22}]}',
'{"verticalGridlines":true,"horizontalGridlines":true,"zebraStripes":false,' +
'"customView":"","numFrozen":0,"disabled":true}', 'compact'],
])
self.assertTableData('_grist_Views_section_field', rows="subset", cols="subset", data=[
["id", "parentId", "parentPos", "visibleCol"],
# The original record card fields.
[7, 3, 7.0, 0],
[8, 3, 8.0, 0],
[9, 3, 9.0, 0],
[11, 3, 11.0, 23],
[13, 3, 13.0, 24],
[19, 5, 6.5, 0],
[20, 5, 7.5, 0],
[21, 5, 8.5, 0],
[22, 5, 10.5, 29],
[23, 5, 12.5, 30],
])

View File

@@ -921,9 +921,10 @@
// Record card widget
["AddRecord", "_grist_Views_section", 3, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "single", "tableRef": 4, "title": ""}],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 3}],
["AddRecord", "_grist_Views_section_field", 3, {"colRef": 34, "parentId": 3, "parentPos": 3.0}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2, "recordCardViewSectionRef": 3}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2}],
// Actions generated from AddColumn.
["AddColumn", "Bar", "world",
@@ -937,7 +938,7 @@
],
"direct": [true, true, true, true, true, true, true,
true, true, true, true, true, true, true,
true, true, true],
true, true, true, true],
"undo": [
["RemoveTable", "Bar"],
["RemoveRecord", "_grist_Tables", 4],
@@ -950,8 +951,9 @@
["RemoveRecord", "_grist_Views_section", 2],
["RemoveRecord", "_grist_Views_section_field", 2],
["RemoveRecord", "_grist_Views_section", 3],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 0}],
["RemoveRecord", "_grist_Views_section_field", 3],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0, "recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0}],
["RemoveColumn", "Bar", "world"],
["RemoveRecord", "_grist_Tables_column", 35],
["RemoveRecord", "_grist_Views_section_field", 4],
@@ -1267,15 +1269,16 @@
["AddRecord", "_grist_Views_section", 2, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "record", "tableRef": 4, "title": ""}],
["BulkAddRecord", "_grist_Views_section_field", [3, 4], {"colRef": [31, 32], "parentId": [2, 2], "parentPos": [3.0, 4.0]}],
["AddRecord", "_grist_Views_section", 3, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "single", "tableRef": 4, "title": ""}],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 3}],
["BulkAddRecord", "_grist_Views_section_field", [5, 6], {"colRef": [31, 32], "parentId": [3, 3], "parentPos": [5.0, 6.0]}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2, "recordCardViewSectionRef": 3}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2}],
["BulkRemoveRecord", "_grist_Views_section_field", [1, 3, 5]],
["RemoveRecord", "_grist_Tables_column", 31],
["RemoveColumn", "ViewTest", "hello"]
],
"direct": [true, true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true],
true, true, true, true, true, true, true, true],
"undo": [
["RemoveTable", "ViewTest"],
["RemoveRecord", "_grist_Tables", 4],
@@ -1288,8 +1291,9 @@
["RemoveRecord", "_grist_Views_section", 2],
["BulkRemoveRecord", "_grist_Views_section_field", [3, 4]],
["RemoveRecord", "_grist_Views_section", 3],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 0}],
["BulkRemoveRecord", "_grist_Views_section_field", [5, 6]],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0, "recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0}],
["BulkAddRecord", "_grist_Views_section_field", [1, 3, 5],
{"colRef": [31, 31, 31], "parentId": [1, 2, 3], "parentPos": [1.0, 3.0, 5.0]}],
["AddRecord", "_grist_Tables_column", 31,
@@ -2213,7 +2217,8 @@
"parentId": 1, "parentKey": "record", "sortColRefs": "[]", "title": ""}],
["AddRecord", "_grist_Views_section", 2, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "record", "tableRef": 4, "title": ""}],
["AddRecord", "_grist_Views_section", 3, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "single", "tableRef": 4, "title": ""}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2, "recordCardViewSectionRef": 3}],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 3}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2}],
["AddTable", "Bar", [
{"id": "manualSort", "formula": "", "isFormula": false, "type": "ManualSortPos"},
{"isFormula": false, "formula": "", "type": "Text", "id": "hello"},
@@ -2245,15 +2250,16 @@
["AddRecord", "_grist_Views_section", 5, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "record", "tableRef": 5, "title": ""}],
["BulkAddRecord", "_grist_Views_section_field", [4, 5, 6], {"colRef": [32, 33, 34], "parentId": [5, 5, 5], "parentPos": [4.0, 5.0, 6.0]}],
["AddRecord", "_grist_Views_section", 6, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "single", "tableRef": 5, "title": ""}],
["UpdateRecord", "_grist_Tables", 5, {"recordCardViewSectionRef": 6}],
["BulkAddRecord", "_grist_Views_section_field", [7, 8, 9], {"colRef": [32, 33, 34], "parentId": [6, 6, 6], "parentPos": [7.0, 8.0, 9.0]}],
["UpdateRecord", "_grist_Tables", 5, {"primaryViewId": 2, "rawViewSectionRef": 5, "recordCardViewSectionRef": 6}],
["UpdateRecord", "_grist_Tables", 5, {"primaryViewId": 2, "rawViewSectionRef": 5}],
["AddRecord", "Bar", 1, {"foo": 0, "hello": "a", "manualSort": 1.0}],
["AddRecord", "Bar", 2, {"foo": 1, "hello": "b", "manualSort": 2.0}],
["AddRecord", "Bar", 3, {"foo": 1, "hello": "c", "manualSort": 3.0}],
["BulkUpdateRecord", "Bar", [1, 2, 3], {"world": ["A", "B", "C"]}]
],
"direct": [true, true, true, true, true, true, true, true,
true, true, true, true, true, true,
true, true, true, true, true, true, true, true,
true, true, true, true, true, true, true, true, true,
true, true, true, false],
"undo": [
@@ -2266,7 +2272,8 @@
["RemoveRecord", "_grist_Views_section", 1],
["RemoveRecord", "_grist_Views_section", 2],
["RemoveRecord", "_grist_Views_section", 3],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0, "recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0}],
["RemoveTable", "Bar"],
["RemoveRecord", "_grist_Tables", 5],
["BulkRemoveRecord", "_grist_Tables_column", [31,32,33,34]],
@@ -2278,8 +2285,9 @@
["RemoveRecord", "_grist_Views_section", 5],
["BulkRemoveRecord", "_grist_Views_section_field", [4, 5, 6]],
["RemoveRecord", "_grist_Views_section", 6],
["UpdateRecord", "_grist_Tables", 5, {"recordCardViewSectionRef": 0}],
["BulkRemoveRecord", "_grist_Views_section_field", [7, 8, 9]],
["UpdateRecord", "_grist_Tables", 5, {"primaryViewId": 0, "rawViewSectionRef": 0, "recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 5, {"primaryViewId": 0, "rawViewSectionRef": 0}],
["RemoveRecord", "Bar", 1],
["RemoveRecord", "Bar", 2],
["RemoveRecord", "Bar", 3]
@@ -2355,9 +2363,10 @@
// Record card widget
["AddRecord", "_grist_Views_section", 3, {"borderWidth": 1, "defaultWidth": 100, "parentKey": "single", "tableRef": 4, "title": ""}],
// As part of adding a table, we also set the primaryViewId.
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2, "recordCardViewSectionRef": 3}]
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 3}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 1, "rawViewSectionRef": 2}]
],
"direct": [true, true, true, true, true, true, true, true, true, true],
"direct": [true, true, true, true, true, true, true, true, true, true, true],
"undo": [
["RemoveTable", "Foo"],
["RemoveRecord", "_grist_Tables", 4],
@@ -2368,7 +2377,8 @@
["RemoveRecord", "_grist_Views_section", 1],
["RemoveRecord", "_grist_Views_section", 2],
["RemoveRecord", "_grist_Views_section", 3],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0, "recordCardViewSectionRef": 0}]
["UpdateRecord", "_grist_Tables", 4, {"recordCardViewSectionRef": 0}],
["UpdateRecord", "_grist_Tables", 4, {"primaryViewId": 0, "rawViewSectionRef": 0}]
]
}
}],

View File

@@ -1930,19 +1930,16 @@ class UserActions(object):
)
if record_card_section:
record_card_section = self.create_plain_view_section(
record_card_section = self._create_record_card_view_section(
result["id"],
table_id,
self._docmodel.view_sections,
"single",
""
self._docmodel.view_sections
)
if primary_view or raw_section or record_card_section:
if primary_view or raw_section:
self.UpdateRecord('_grist_Tables', result["id"], {
'primaryViewId': primary_view["id"] if primary_view else 0,
'rawViewSectionRef': raw_section.id if raw_section else 0,
'recordCardViewSectionRef': record_card_section.id if record_card_section else 0,
})
return result
@@ -1994,10 +1991,10 @@ class UserActions(object):
new_raw_section = new_table.rawViewSectionRef
new_record_card_section = new_table.recordCardViewSectionRef
# Copy view section options to the new raw and record card view sections.
self._docmodel.update(
[new_raw_section, new_record_card_section],
options=[raw_section.options, record_card_section.options]
# Copy view section description and options to the new raw view section.
self._docmodel.update([new_raw_section],
description=raw_section.description,
options=raw_section.options,
)
old_to_new_col_refs = {}
@@ -2034,6 +2031,7 @@ class UserActions(object):
recalcWhen=existing_column.recalcWhen,
recalcDeps=new_recalc_deps,
formula=formula_updates.get(new_column, existing_column.formula),
description=existing_column.description,
)
self.maybe_copy_display_formula(existing_column, new_column)
@@ -2049,6 +2047,8 @@ class UserActions(object):
for rule in existing_column.rules:
self.doAddRule(new_table_id, None, new_column.id, rule.formula)
self._copy_record_card_settings(record_card_section, new_record_card_section)
# Copy all row conditional styles to the new table.
for rule in raw_section.rules:
self.doAddRule(new_table_id, None, None, rule.formula)
@@ -2064,6 +2064,55 @@ class UserActions(object):
'raw_section_id': new_raw_section.id,
}
def _copy_record_card_settings(self, src_record_card_section, dst_record_card_section):
"""
Helper that copies settings from `src_record_card_section` to `dst_record_card_section`.
"""
old_to_new_col_refs = {}
old_to_new_field_refs = {}
for existing_field, new_field in zip(src_record_card_section.fields,
dst_record_card_section.fields):
old_to_new_col_refs[existing_field.colRef.id] = new_field.colRef
old_to_new_field_refs[existing_field.id] = new_field.id
for existing_field, new_field in zip(src_record_card_section.fields,
dst_record_card_section.fields):
# Copy field settings to the new fields.
self._docmodel.update(
[new_field],
displayCol=old_to_new_col_refs.get(existing_field.displayCol.id, 0),
parentPos=existing_field.parentPos,
visibleCol=old_to_new_col_refs.get(existing_field.visibleCol.id, 0),
widgetOptions=existing_field.widgetOptions,
)
if existing_field.rules:
# Copy all field conditional styles to the new section.
for rule in existing_field.rules:
self.doAddRule(dst_record_card_section.tableRef.tableId, new_field.id, None, rule.formula)
def patch_layout_spec(layout_spec):
if isinstance(layout_spec, (dict, list)):
for k, v in (layout_spec.items()
if isinstance(layout_spec, dict)
else enumerate(layout_spec)):
if k == 'leaf' and v in old_to_new_field_refs:
layout_spec[k] = old_to_new_field_refs[v]
patch_layout_spec(v)
try:
new_layout_spec = json.loads(src_record_card_section.layoutSpec)
patch_layout_spec(new_layout_spec)
new_layout_spec = json.dumps(new_layout_spec)
except ValueError:
new_layout_spec = ''
# Copy options, theme, and layout to the new record card view section.
self._docmodel.update([dst_record_card_section],
options=src_record_card_section.options,
layoutSpec=new_layout_spec,
theme=src_record_card_section.theme,
)
def _fetch_table_col_recs(self, table_ref, col_refs):
"""Helper that converts col_refs from table table_ref into column Records."""
@@ -2123,6 +2172,15 @@ class UserActions(object):
limit=limit)
return section
def _create_record_card_view_section(self, tableRef, tableId, view_sections):
section = self._docmodel.add(view_sections, tableRef=tableRef, parentKey='single',
title='', borderWidth=1, defaultWidth=100)[0]
self.UpdateRecord('_grist_Tables', tableRef, {
'recordCardViewSectionRef': section.id,
})
self._RebuildViewFields(tableId, section.id)
return section
@useraction
def UpdateSummaryViewSection(self, section_ref, groupby_colrefs):
"""
@@ -2247,14 +2305,22 @@ class UserActions(object):
if section_rec.fields:
self._docmodel.remove(section_rec.fields)
# Include all table columns that are intended to be visible to the user.
cols = [c for c in table_rec.columns if column.is_visible_column(c.colId)
# TODO: hack to avoid auto-adding the 'group' column when detaching summary tables.
and c.colId != 'group']
cols.sort(key=lambda c: c.parentPos)
if limit is not None:
cols = cols[:limit]
self._docmodel.add(section_rec.fields, colRef=[c.id for c in cols])
is_card = section_rec.parentKey in ('single', 'detail')
is_record_card = section_rec == table_rec.recordCardViewSectionRef
if is_card and not is_record_card:
# Copy settings from the table's record card section to the new section.
record_card_section = table_rec.recordCardViewSectionRef
self._docmodel.add(section_rec.fields, colRef=[f.colRef for f in record_card_section.fields])
self._copy_record_card_settings(record_card_section, section_rec)
else :
# Include all table columns that are intended to be visible to the user.
cols = [c for c in table_rec.columns if column.is_visible_column(c.colId)
# TODO: hack to avoid auto-adding the 'group' column when detaching summary tables.
and c.colId != 'group']
cols.sort(key=lambda c: c.parentPos)
if limit is not None:
cols = cols[:limit]
self._docmodel.add(section_rec.fields, colRef=[c.id for c in cols])
#----------------------------------------------------------------------