-
Notifications
You must be signed in to change notification settings - Fork 2
/
DensityAccordionBox.ts
76 lines (61 loc) · 3.05 KB
/
DensityAccordionBox.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2019-2024, University of Colorado Boulder
/**
* An AccordionBox that displays the density of each material.
*
* @author Agustín Vallejo
*/
import DynamicProperty from '../../../../axon/js/DynamicProperty.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import Material from '../../common/model/Material.js';
import densityBuoyancyCommon from '../../densityBuoyancyCommon.js';
import DensityBuoyancyCommonStrings from '../../DensityBuoyancyCommonStrings.js';
import DensityBuoyancyCommonConstants from '../../common/DensityBuoyancyCommonConstants.js';
import StringUtils from '../../../../phetcommon/js/util/StringUtils.js';
import Utils from '../../../../dot/js/Utils.js';
import ReadoutListAccordionBox, { ReadoutData, ReadoutListAccordionBoxOptions } from './ReadoutListAccordionBox.js';
import DerivedStringProperty from '../../../../axon/js/DerivedStringProperty.js';
import optionize, { EmptySelfOptions } from '../../../../phet-core/js/optionize.js';
type DensityReadoutType = TReadOnlyProperty<Material>;
type ParentOptions = ReadoutListAccordionBoxOptions<DensityReadoutType>;
type SelfOptions = EmptySelfOptions;
type DensityAccordionBoxOptions = SelfOptions & ParentOptions;
export default class DensityAccordionBox extends ReadoutListAccordionBox<DensityReadoutType> {
public constructor( providedOptions?: DensityAccordionBoxOptions ) {
const options = optionize<DensityAccordionBoxOptions, SelfOptions, ParentOptions>()( {
expandedDefaultValue: false,
accessibleName: DensityBuoyancyCommonStrings.densityStringProperty
}, providedOptions );
super( DensityBuoyancyCommonStrings.densityStringProperty, options );
}
public override generateReadoutData( materialProperty: DensityReadoutType ): ReadoutData {
// Use DynamicProperty so that this name is updated based on the material AND material's name changing.
const nameProperty = new DynamicProperty<string, string, Material>( materialProperty, {
derive: material => material.nameProperty
} );
// Returns the filled in string for the material readout or '?' if the material is hidden
const valueProperty = new DerivedStringProperty(
[
materialProperty,
DensityBuoyancyCommonConstants.KILOGRAMS_PER_VOLUME_PATTERN_STRING_PROPERTY,
DensityBuoyancyCommonStrings.questionMarkStringProperty
],
( material, patternStringProperty, questionMarkString ) => {
return material.hidden ?
questionMarkString :
StringUtils.fillIn( patternStringProperty, {
// convert from kg/m^3 to kg/L
value: Utils.toFixed( material.density / DensityBuoyancyCommonConstants.LITERS_IN_CUBIC_METER, 2 ),
decimalPlaces: 2
} );
} );
this.cleanupEmitter.addListener( () => {
nameProperty.dispose();
valueProperty.dispose();
} );
return {
nameProperty: nameProperty,
valueProperty: valueProperty
};
}
}
densityBuoyancyCommon.register( 'DensityAccordionBox', DensityAccordionBox );