-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reusing common code between select and rank
- Loading branch information
1 parent
5f9201d
commit 8568fe0
Showing
11 changed files
with
146 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { ValueType } from '../../client/ValueType.ts'; | ||
import type { SharedValueCodec } from './getSharedValueCodec.ts'; | ||
import { ValueArrayCodec } from './ValueArrayCodec.ts'; | ||
import type { CodecDecoder, CodecEncoder } from './ValueCodec.ts'; | ||
|
||
export type BaseItemValueType = 'string'; | ||
|
||
export type UnsupportedBaseItemValueType = Exclude<ValueType, BaseItemValueType>; | ||
|
||
export abstract class BaseItemCodec< | ||
Values extends readonly string[] = readonly string[], | ||
> extends ValueArrayCodec<BaseItemValueType, Values> { | ||
constructor( | ||
baseCodec: SharedValueCodec<'string'>, | ||
encodeValue: CodecEncoder<Values>, | ||
decodeValue: CodecDecoder<Values> | ||
) { | ||
super(baseCodec, encodeValue, decodeValue); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...ine/src/lib/codecs/ItemCollectionCodec.ts → ...src/lib/codecs/BaseItemCollectionCodec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
packages/xforms-engine/src/lib/codecs/select/BaseSelectCodec.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
packages/xforms-engine/src/lib/codecs/select/getSelectCodec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
packages/xforms-engine/src/lib/reactivity/createBaseItemset.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { UpsertableMap } from '@getodk/common/lib/collections/UpsertableMap.ts'; | ||
import type { Accessor } from 'solid-js'; | ||
import { createMemo } from 'solid-js'; | ||
import type { ActiveLanguage } from '../../client/FormLanguage.ts'; | ||
import type { TextRange as ClientTextRange } from '../../client/TextRange.ts'; | ||
import type { EvaluationContext } from '../../instance/internal-api/EvaluationContext.ts'; | ||
import type { EngineXPathNode } from '../../integration/xpath/adapter/kind.ts'; | ||
import type { EngineXPathEvaluator } from '../../integration/xpath/EngineXPathEvaluator.ts'; | ||
import type { ItemsetDefinition } from '../../parse/body/control/ItemsetDefinition.ts'; | ||
import { createComputedExpression } from './createComputedExpression.ts'; | ||
import type { ReactiveScope } from './scope.ts'; | ||
import { createTextRange } from './text/createTextRange.ts'; | ||
import type { RankControl } from '../../instance/RankControl.ts'; | ||
import type { SelectControl } from '../../instance/SelectControl.ts'; | ||
import type { TranslationContext } from '../../instance/internal-api/TranslationContext.ts'; | ||
import { TextChunk } from '../../instance/text/TextChunk.ts'; | ||
import { TextRange } from '../../instance/text/TextRange.ts'; | ||
|
||
type ItemsetControl = SelectControl | RankControl; | ||
Check warning on line 19 in packages/xforms-engine/src/lib/reactivity/createBaseItemset.ts GitHub Actions / Lint (global) (22.12.0)
|
||
|
||
class ItemsetItemEvaluationContext implements EvaluationContext { | ||
readonly isAttached: Accessor<boolean>; | ||
readonly scope: ReactiveScope; | ||
readonly evaluator: EngineXPathEvaluator; | ||
readonly contextReference: Accessor<string>; | ||
readonly getActiveLanguage: Accessor<ActiveLanguage>; | ||
|
||
constructor(control: ItemsetControl, readonly contextNode: EngineXPathNode) { | ||
this.isAttached = control.isAttached; | ||
this.scope = control.scope; | ||
this.evaluator = control.evaluator; | ||
this.contextReference = control.contextReference; | ||
this.getActiveLanguage = control.getActiveLanguage; | ||
} | ||
} | ||
|
||
type DerivedItemLabel = ClientTextRange<'item-label', 'form-derived'>; | ||
|
||
export const derivedItemLabel = (context: TranslationContext, value: string): DerivedItemLabel => { | ||
const chunk = new TextChunk(context, 'literal', value); | ||
|
||
return new TextRange('form-derived', 'item-label', [chunk]); | ||
}; | ||
|
||
const createItemsetItemLabel = ( | ||
context: EvaluationContext, | ||
definition: ItemsetDefinition, | ||
itemValue: Accessor<string> | ||
): Accessor<ClientTextRange<'item-label'>> => { | ||
const { label } = definition; | ||
|
||
if (label == null) { | ||
return createMemo(() => derivedItemLabel(context, itemValue())); | ||
} | ||
|
||
return createTextRange(context, 'item-label', label); | ||
}; | ||
|
||
interface ItemsetItem { | ||
label(): ClientTextRange<'item-label'>; | ||
value(): string; | ||
} | ||
|
||
const createItemsetItems = (control: ItemsetControl, itemset: ItemsetDefinition): Accessor<readonly ItemsetItem[]> => { | ||
return control.scope.runTask(() => { | ||
const itemNodes = createComputedExpression(control, itemset.nodes, { defaultValue: [] }); | ||
const itemsCache = new UpsertableMap<EngineXPathNode, ItemsetItem>(); | ||
|
||
return createMemo(() => { | ||
return itemNodes().map((itemNode) => { | ||
return itemsCache.upsert(itemNode, () => { | ||
const context = new ItemsetItemEvaluationContext(control, itemNode); | ||
const value = createComputedExpression(context, itemset.value, { defaultValue: '' }); | ||
const label = createItemsetItemLabel(context, itemset, value); | ||
|
||
return { label, value }; | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
export interface SourceValueItem { | ||
readonly value: string; | ||
readonly label: ClientTextRange<'item-label'>; | ||
} | ||
|
||
export const createItemset = ( | ||
control: ItemsetControl, | ||
itemset: ItemsetDefinition, | ||
): Accessor<readonly SourceValueItem[]> => { | ||
return control.scope.runTask(() => { | ||
const itemsetItems = createItemsetItems(control, itemset); | ||
|
||
return createMemo(() => { | ||
return itemsetItems().map((item) => { | ||
return { | ||
label: item.label(), | ||
value: item.value(), | ||
}; | ||
}); | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.