Skip to content

Commit

Permalink
further refactor from StringManager => Describers, see phetsims/gravi…
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarlow12 committed Dec 18, 2018
1 parent 8373cb6 commit d2e6b17
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 65 deletions.
3 changes: 3 additions & 0 deletions js/ISLCA11yStrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ define( require => {
forceVectorMagnitudePattern: {
value: 'Force vector magnitude is {{objectValueUnits}}.'
},
forceVectorMagnitudeUnitsPattern: {
value: 'Force vector magnitude is {{forceValue}} {{units}}.'
},
forceAndVectorPattern: {
value: 'Force on {{thisObject}} by {{otherObject}} is {{size}}, and vector points directly at {{otherObject}}.'
},
Expand Down
6 changes: 1 addition & 5 deletions js/view/ISLCObjectPDOMNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ define( require => {
}

linkToForceProperty( callback ) {
this.model.forceProperty.link( force => {
this.forceVectorMagnitudeItemNode.innerContent = this.stringManager.getForceVectorMagnitudeText();
this.forceBetweenAndVectorNode.innerContent = this.stringManager.getForceBetweenAndVectorText( this.thisObjectLabel, this.otherObjectLabel );
callback( force );
} );
this.model.forceProperty.link( callback );
}

linkToPositionProperty( callback ) {
Expand Down
33 changes: 0 additions & 33 deletions js/view/ISLCStringManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ define( require => {
const unitsMetersString = require( 'string!INVERSE_SQUARE_LAW_COMMON/units.meters' );
const unitsNewtonsString = require( 'string!INVERSE_SQUARE_LAW_COMMON/units.newtons' );

const summaryVectorSizePatternString = ISLCA11yStrings.summaryVectorSizePattern.value;
const summaryVectorSizeValueUnitsPatternString = ISLCA11yStrings.summaryVectorSizeValueUnitsPattern.value;
const summaryInteractionHintPatternString = ISLCA11yStrings.summaryInteractionHintPattern.value;
const distanceAndValueSummaryPatternString = ISLCA11yStrings.distanceAndValueSummaryPattern.value;
const robotPullSummaryPatternString = ISLCA11yStrings.robotPullSummaryPattern.value;
Expand All @@ -32,7 +30,6 @@ define( require => {

// sphere strings
const forceVectorMagnitudePatternString = ISLCA11yStrings.forceVectorMagnitudePattern.value;
const forceAndVectorPatternString = ISLCA11yStrings.forceAndVectorPattern.value;
const positionMeterMarkPatternString = ISLCA11yStrings.positionMeterMarkPattern.value;
const objectLabelPositionPatternString = ISLCA11yStrings.objectLabelPositionPattern.value;
const spherePositionRegionObjectPatternString = ISLCA11yStrings.spherePositionRegionObjectPattern.value;
Expand Down Expand Up @@ -223,22 +220,6 @@ define( require => {
);
}

getForceVectorsSummaryText() {
const fillObject = {};
const pattern = this.model.forceValuesProperty.get() ?
summaryVectorSizeValueUnitsPatternString :
summaryVectorSizePatternString;

fillObject.size = this.getForceVectorSize();

if ( this.model.forceValuesProperty.get() ) {
// fillObject.units = this.valueUnits;
fillObject.objectValueUnits = this._forceValueToString( this.model.forceProperty.get() );
}

return StringUtils.fillIn( pattern, fillObject );
}

getObjectDistanceSummary() {
const fillObject = {
object1Label: this.object1Label,
Expand Down Expand Up @@ -308,16 +289,6 @@ define( require => {
return this._forceValueToString( this.model.forceProperty.get() );
}

getForceBetweenAndVectorText( thisObject, otherObject ) {
const pattern = forceAndVectorPatternString;
const fillObject = {
thisObject,
otherObject,
size: this.getForceVectorSize()
};
return StringUtils.fillIn( pattern, fillObject );
}

getForceValuesInUnitsText() {
const pattern = valuesInUnitsPatternString;
return StringUtils.fillIn( pattern, { units: this.valueUnits } );
Expand Down Expand Up @@ -456,10 +427,6 @@ define( require => {
return Util.toFixedNumber( this.model.forceProperty.get() * this._valueUnitConversion, decimalPlaces );
}

getPositionRegionChanged( newDistance, oldDistance ) {
throw new Error( 'getPositionRegionChanged MUST be implemented in subtypes.' );
}

getForceVectorIndex( force ) {
throw new Error( 'getForceVectorIndex MUST be implemented in subtypes.' );
}
Expand Down
49 changes: 24 additions & 25 deletions js/view/describers/ForceDescriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ define( require => {
// a11y strings
const summaryVectorSizePatternString = ISLCA11yStrings.summaryVectorSizePattern.value;
const summaryVectorSizeValueUnitsPatternString = ISLCA11yStrings.summaryVectorSizeValueUnitsPattern.value;

const forceVectorMagnitudeUnitsPatternString = ISLCA11yStrings.forceVectorMagnitudeUnitsPattern.value;
const forceAndVectorPatternString = ISLCA11yStrings.forceAndVectorPattern.value;
const forceValueUnitsPatternString = ISLCA11yStrings.forceValueUnitsPattern.value;

const tinyString = ISLCA11yStrings.tiny.value;
Expand All @@ -36,22 +37,20 @@ define( require => {
const negativeValuePatternString = ISLCA11yStrings.negativeValuePattern.value;
const valuePatternString = ISLCA11yStrings.valuePattern.value;

let describer = null;

class ForceDescriber extends ISLCDescriber {
constructor( model, object1Label, object2Label, options ) {
super( model, object1Label, object2Label );

options = _.extend( {
units: unitsNewtonsString,

// for adding natural language to the force (e.g. '3 billion' instead of 3000000000)
forceValueToString: force => StringUtils.fillIn( '{{force}}', { force } ),

// in some scenarios, the force units change. convertForce allows subtypes to define conversion behavior
// integrates with forceValueToString for necessary conversions (e.g. 300000000 -> 3)
// always takes place before forceValueToString
convertForce: force => force
convertForce: force => force,

// for adding natural language to the force (e.g. '3 billion' instead of 3000000000)
forceValueToString: value => StringUtils.fillIn( valuePatternString, { value } )
}, options );

this.units = options.units;
Expand Down Expand Up @@ -97,6 +96,23 @@ define( require => {
return StringUtils.fillIn( pattern, fillObject );
}

getForceVectorMagnitudeText() {
const pattern = forceVectorMagnitudeUnitsPatternString;
const forceValue = this.formattedForce;
const units = this.units;
return StringUtils.fillIn( pattern, { forceValue, units } );
}

getForceBetweenAndVectorText( thisObject, otherObject ) {
const pattern = forceAndVectorPatternString;
const fillObject = {
thisObject,
otherObject,
size: this.getForceVectorSize()
};
return StringUtils.fillIn( pattern, fillObject );
}

static getForceInScientificNotation( forceValue, mantissaDecimalPlaces ) {
const { mantissa, exponent } = ScientificNotationNode.toScientificNotation( forceValue, { mantissaDecimalPlaces } );
const mantissaPattern = mantissa < 0 ? negativeValuePatternString : valuePatternString; // negative values are possible in Coulomb's Law
Expand Down Expand Up @@ -124,7 +140,7 @@ define( require => {
}

getForceVectorSize() {
return SIZE_STRINGS[ this._vectorSizeIndex ];
return SIZE_STRINGS[ this.vectorSizeIndex ];
}

/**
Expand All @@ -149,23 +165,6 @@ define( require => {
getEffortIndex( force ) {
throw new Error( 'getEffortIndex MUST be implemented in subtypes.' );
}

/**
* Uses the singleton pattern to keep one instance of this describer for the entire lifetime of the sim.
* @returns {ForceDescriber}
*/
static getDescriber() {
assert && assert( describer, 'describer has not yet been initialized' );
return describer;
}

/**
* Initialize the describer singleton
* @throws Error
*/
static initialize( model, object1Label, object2Label, options ) {
describer = new ForceDescriber( model, object1Label, object2Label, options );
}
}

return inverseSquareLawCommon.register( 'ForceDescriber', ForceDescriber );
Expand Down
2 changes: 0 additions & 2 deletions js/view/describers/ISLCDescriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ define( require => {

const { OBJECT_ONE, OBJECT_TWO } = ISLCObjectEnum;

let describer = null;

class ISLCDescriber {
constructor( model, object1Label, object2Label ) {

Expand Down

0 comments on commit d2e6b17

Please sign in to comment.