diff --git a/v3/src/data-interactive/data-interactive-type-utils.ts b/v3/src/data-interactive/data-interactive-type-utils.ts index 44e8e6337..97bbefe85 100644 --- a/v3/src/data-interactive/data-interactive-type-utils.ts +++ b/v3/src/data-interactive/data-interactive-type-utils.ts @@ -8,7 +8,7 @@ import { IGlobalValue } from "../models/global/global-value" import { getSharedCaseMetadataFromDataset } from "../models/shared/shared-data-utils" import { kAttrIdPrefix, maybeToV2Id, toV2Id, toV3AttrId } from "../utilities/codap-utils" import { - ICodapV2AttributeV3, ICodapV2CollectionV3, ICodapV2DataContextV3, v3TypeFromV2TypeString + ICodapV2Attribute, ICodapV2CollectionV3, ICodapV2DataContextV3, v3TypeFromV2TypeString } from "../v2/codap-v2-types" import { DIAttribute, DIGetCaseResult, DIResources, DISingleValues } from "./data-interactive-types" import { getCaseValues } from "./data-interactive-utils" @@ -104,7 +104,7 @@ export function getCaseRequestResultValues(c: ICase, dataContext: IDataSet): DIG } } -export function convertAttributeToV2(attribute: IAttribute, dataContext?: IDataSet): ICodapV2AttributeV3 { +export function convertAttributeToV2(attribute: IAttribute, dataContext?: IDataSet): ICodapV2Attribute { const metadata = dataContext && getSharedCaseMetadataFromDataset(dataContext) const { cid, name, type, title, description, deleteable, editable, id, precision } = attribute const v2Id = toV2Id(id) @@ -143,11 +143,9 @@ export function convertCollectionToV2(collection: ICollectionModel, dataContext? const { name, title, id, labels: _labels } = collection const v2Id = toV2Id(id) const labels = _labels ? getSnapshot(_labels) : undefined - const v2Attrs = collection.attributes.map(attribute => { + const attrs = collection.attributes.map(attribute => { if (attribute) return convertAttributeToV2(attribute, dataContext) - }) - const attrs: ICodapV2AttributeV3[] = [] - v2Attrs.forEach(attr => attr && attrs.push(attr)) + }).filter(attr => !!attr) return { // areParentChildLinksConfigured, attrs, @@ -184,7 +182,8 @@ export function convertDataSetToV2(dataSet: IDataSet, docId: number | string): I description, // metadata, // preventReorg, - // setAsideItems, + // TODO_V2_EXPORT + setAsideItems: [] // contextStorage } } diff --git a/v3/src/data-interactive/data-interactive-types.ts b/v3/src/data-interactive/data-interactive-types.ts index 4de0ce801..b8edf0207 100644 --- a/v3/src/data-interactive/data-interactive-types.ts +++ b/v3/src/data-interactive/data-interactive-types.ts @@ -1,6 +1,6 @@ import { RequireAtLeastOne } from "type-fest" import { IAttribute } from "../models/data/attribute" -import { ICodapV2Attribute, ICodapV2AttributeV3, ICodapV2Collection, ICodapV2DataContext } from "../v2/codap-v2-types" +import { ICodapV2Attribute, ICodapV2Collection, ICodapV2DataContext } from "../v2/codap-v2-types" import { IDataSet } from "../models/data/data-set" import { ICase, ICaseID } from "../models/data/data-set-types" import { IGlobalValue } from "../models/global/global-value" @@ -192,7 +192,7 @@ export type DISingleValues = DIAttribute | DINotifyAttribute | DIAttributeLocati export type DIValues = DISingleValues | DISingleValues[] | number | string[] // types returned as outputs by the API -export type DIResultAttributes = { attrs: ICodapV2AttributeV3[] } +export type DIResultAttributes = { attrs: ICodapV2Attribute[] } export type DIResultSingleValues = DICase | DIComponentInfo | DIGetCaseResult | DIGlobal | DIInteractiveFrame export type DIResultValues = DIResultSingleValues | DIResultSingleValues[] | DIAllCases | DIDeleteCollectionResult | DIUpdateItemResult | DIResultAttributes | number | number[] diff --git a/v3/src/models/data/collection.ts b/v3/src/models/data/collection.ts index ae2992d7e..918987dd0 100644 --- a/v3/src/models/data/collection.ts +++ b/v3/src/models/data/collection.ts @@ -304,7 +304,7 @@ export const CollectionModel = V2Model } } - // item info is only stored for visible items + // item info is stored for all items itemInfo.push({ itemId, caseId }) } }) diff --git a/v3/src/models/data/data-set-conversion.test.ts b/v3/src/models/data/data-set-conversion.test.ts index 5592966c3..466922192 100644 --- a/v3/src/models/data/data-set-conversion.test.ts +++ b/v3/src/models/data/data-set-conversion.test.ts @@ -128,7 +128,7 @@ describe("DataSet conversions", () => { expect(data.attributesMap.size).toBe(3) expect(data.attributes.length).toBe(3) expect(data._itemIds.length).toBe(1) - expect(data.itemIds.length).toBe(1) + expect(data.itemIds.length).toBe(0) expect(data.setAsideItemIds.length).toBe(1) }) }) diff --git a/v3/src/models/data/data-set.ts b/v3/src/models/data/data-set.ts index 19f0a9b0d..778c3b70e 100644 --- a/v3/src/models/data/data-set.ts +++ b/v3/src/models/data/data-set.ts @@ -1231,6 +1231,9 @@ export const DataSet = V2Model.named("DataSet").props({ self.addCollection({ name: t("DG.AppController.createDataSet.collectionName") }) } + // initialize setAsideItemIdsSet + self.setAsideItemIdsSet.replace(self.setAsideItemIds) + if (!srcDataSet) { // set up middleware to add ids to inserted attributes and cases // adding the ids in middleware makes them available as action arguments diff --git a/v3/src/v2/codap-v2-document.ts b/v3/src/v2/codap-v2-document.ts index 90295a1ca..9550e8a10 100644 --- a/v3/src/v2/codap-v2-document.ts +++ b/v3/src/v2/codap-v2-document.ts @@ -2,7 +2,7 @@ import { SetRequired } from "type-fest" import { IAttribute } from "../models/data/attribute" import { ICollectionModel, ICollectionModelSnapshot } from "../models/data/collection" import { IDataSet, toCanonical } from "../models/data/data-set" -import { ICaseCreation } from "../models/data/data-set-types" +import { ICaseCreation, IItem } from "../models/data/data-set-types" import { v2NameTitleToV3Title } from "../models/data/v2-model" import { IDocumentMetadata } from "../models/document/document-metadata" import { ISharedCaseMetadata, SharedCaseMetadata } from "../models/shared/shared-case-metadata" @@ -12,7 +12,7 @@ import { } from "../utilities/codap-utils" import { CodapV2Component, ICodapV2Attribute, ICodapV2Case, ICodapV2Collection, ICodapV2DataContext, ICodapV2DocumentJson, - v3TypeFromV2TypeString + ICodapV2SetAsideItem, v3TypeFromV2TypeString } from "./codap-v2-types" interface V2CaseIdInfo { @@ -112,6 +112,7 @@ export class CodapV2Document { this.caseMetadataMap.set(guid, caseMetadata) this.registerCollections(sharedDataSet.dataSet, caseMetadata, collections) + this.registerSetAsideItems(sharedDataSet.dataSet, context.setAsideItems) }) } @@ -223,4 +224,17 @@ export class CodapV2Document { data.addCases(itemsToAdd) } } + + registerSetAsideItems(data: IDataSet, setAsideItems?: ICodapV2SetAsideItem[]) { + const itemsToAdd: IItem[] = [] + setAsideItems?.forEach(item => { + // some v2 documents don't store item ids, so we generate them if necessary + const { id = v3Id(kItemIdPrefix), values } = item + itemsToAdd.push({ __id__: id, ...toCanonical(data, values) }) + }) + if (itemsToAdd.length) { + data.addCases(itemsToAdd) + data.hideCasesOrItems(itemsToAdd.map(item => item.__id__)) + } + } } diff --git a/v3/src/v2/codap-v2-types.ts b/v3/src/v2/codap-v2-types.ts index f9b8f8e6f..c07ca3508 100644 --- a/v3/src/v2/codap-v2-types.ts +++ b/v3/src/v2/codap-v2-types.ts @@ -1,11 +1,6 @@ import { SetOptional } from "type-fest" import {AttributeType} from "../models/data/attribute" -export type AllowStringIds = Omit & { - guid: number | string - id: number | string -} - export interface ICodapV2Attribute { guid: number id: number @@ -31,8 +26,6 @@ export interface ICodapV2Attribute { precision?: number | string | null unit?: string | null } -// when exporting a v3 attribute to v2 -export type ICodapV2AttributeV3 = AllowStringIds export function isCodapV2Attribute(o: any): o is ICodapV2Attribute { return o.type === "DG.Attribute" @@ -86,8 +79,13 @@ export interface ICodapV2Collection { // when exporting a v3 collection to v2 type CollectionNotYetExported = "cases" | "caseName" | "childAttrName" | "collapseChildren" export interface ICodapV2CollectionV3 - extends SetOptional, "attrs">, CollectionNotYetExported> { - attrs: ICodapV2AttributeV3[] + extends SetOptional, CollectionNotYetExported> { + attrs: ICodapV2Attribute[] +} + +export interface ICodapV2SetAsideItem { + id: string // item id + values: Record } export interface ICodapV2DataContextMetadata { @@ -103,15 +101,15 @@ export interface ICodapV2DataContext { title: string collections: ICodapV2Collection[] description?: string - metadata?: ICodapV2DataContextMetadata | null, + metadata?: ICodapV2DataContextMetadata | null // preventReorg: boolean - // setAsideItems: this.get('dataSet').archiveSetAsideItems(), + setAsideItems?: ICodapV2SetAsideItem[] // contextStorage: this.contextStorage } // when exporting a v3 data set to v2 data context type DCNotYetExported = "flexibleGroupingChangeFlag" export interface ICodapV2DataContextV3 - extends SetOptional, "document" | "collections">, DCNotYetExported> { + extends SetOptional, DCNotYetExported> { document: number | string collections: ICodapV2CollectionV3[] }