-
Notifications
You must be signed in to change notification settings - Fork 1
/
WordAccordionBox.ts
104 lines (86 loc) · 4.6 KB
/
WordAccordionBox.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019-2023, University of Colorado Boulder
/**
* Class for the 'Word' accordion box, which is the panel in the upper left corner of the sim that displays a written
* representation of the current number.
*
* @author Chris Klusendorf (PhET Interactive Simulations)
*/
import { Font, Text } from '../../../../scenery/js/imports.js';
import numberPlay from '../../numberPlay.js';
import NumberPlayStrings from '../../NumberPlayStrings.js';
import NumberPlayConstants from '../NumberPlayConstants.js';
import NumberSuiteCommonAccordionBox, { NumberSuiteCommonAccordionBoxOptions } from '../../../../number-suite-common/js/common/view/NumberSuiteCommonAccordionBox.js';
import optionize from '../../../../phet-core/js/optionize.js';
import StringUtils from '../../../../phetcommon/js/util/StringUtils.js';
import Multilink from '../../../../axon/js/Multilink.js';
import TReadOnlyProperty from '../../../../axon/js/TReadOnlyProperty.js';
import StrictOmit from '../../../../phet-core/js/types/StrictOmit.js';
import numberPlayPreferences from '../model/numberPlayPreferences.js';
import DerivedProperty from '../../../../axon/js/DerivedProperty.js';
import Property from '../../../../axon/js/Property.js';
import NumberSuiteCommonConstants from '../../../../number-suite-common/js/common/NumberSuiteCommonConstants.js';
import NumberSuiteCommonStrings from '../../../../number-suite-common/js/NumberSuiteCommonStrings.js';
// types
type SelfOptions = {
textOffsetX: number;
font: Font;
};
export type WordAccordionBoxOptions = SelfOptions &
StrictOmit<NumberSuiteCommonAccordionBoxOptions, 'titleStringProperty'>;
// constants
const TEXT_MARGIN = 5;
// how much to change the height by when the localeSwitch is shown or hidden
const HEIGHT_ADJUSTMENT = 24;
class WordAccordionBox extends NumberSuiteCommonAccordionBox {
public constructor( currentNumberProperty: TReadOnlyProperty<number>, isPrimaryLocaleProperty: Property<boolean>,
height: number, providedOptions: WordAccordionBoxOptions ) {
const titleStringProperty = new DerivedProperty(
[ phet.joist.localeProperty, numberPlayPreferences.secondLocaleStringsProperty, isPrimaryLocaleProperty ],
( ( locale, secondLocaleStrings, isPrimaryLocale ) => {
// TODO: Duplicated from LocaleSwitch
const secondLanguageStringKey = `${NumberSuiteCommonConstants.NUMBER_PLAY_STRING_KEY_PREFIX}language`;
const secondLanguageString = secondLocaleStrings[ secondLanguageStringKey ];
const primaryLocaleTitleString = StringUtils.fillIn( NumberPlayStrings.wordStringProperty.value, {
language: NumberSuiteCommonStrings.languageStringProperty.value
} );
const secondaryLocaleTitleString = StringUtils.fillIn( NumberPlayStrings.wordLanguageStringProperty.value, {
language: secondLanguageString
} );
return isPrimaryLocale ? primaryLocaleTitleString : secondaryLocaleTitleString;
} ) );
const options = optionize<WordAccordionBoxOptions, SelfOptions, NumberSuiteCommonAccordionBoxOptions>()( {
titleStringProperty: titleStringProperty,
titleTextOptions: {
maxWidth: NumberPlayConstants.UPPER_OUTER_AB_TITLE_MAX_WIDTH
}
}, providedOptions );
// TODO: This is giving weird results...
const heightProperty = new DerivedProperty( [ numberPlayPreferences.showSecondLocaleProperty ], showSecondLocale => {
const shortHeight = height - HEIGHT_ADJUSTMENT;
return showSecondLocale ? shortHeight : height;
} );
super( NumberPlayConstants.UPPER_OUTER_ACCORDION_BOX_WIDTH, heightProperty, options );
// initialize as blank, updated in link below
const wordText = new Text( '', {
font: options.font,
maxWidth: this.contentBoundsProperty.value.width - options.textOffsetX - TEXT_MARGIN
} );
this.contentNode.addChild( wordText );
this.contentBoundsProperty.link( contentBounds => {
wordText.left = contentBounds.left + options.textOffsetX;
wordText.centerY = contentBounds.centerY;
} );
// update the word if the current number or locale changes
Multilink.multilink( [ currentNumberProperty, isPrimaryLocaleProperty ],
( currentNumber, isPrimaryLocale ) => {
wordText.text = NumberSuiteCommonConstants.numberToString( numberPlayPreferences.secondLocaleStringsProperty.value,
currentNumber, isPrimaryLocale );
} );
}
public override dispose(): void {
assert && assert( false, 'dispose is not supported, exists for the lifetime of the sim' );
super.dispose();
}
}
numberPlay.register( 'WordAccordionBox', WordAccordionBox );
export default WordAccordionBox;