From d2e6b1730a321321e8db7707afce812a486b4bad Mon Sep 17 00:00:00 2001 From: mbarlow12 Date: Tue, 18 Dec 2018 16:04:51 -0700 Subject: [PATCH] further refactor from StringManager => Describers, see phetsims/gravity-force-lab#109 --- js/ISLCA11yStrings.js | 3 ++ js/view/ISLCObjectPDOMNode.js | 6 +--- js/view/ISLCStringManager.js | 33 ------------------- js/view/describers/ForceDescriber.js | 49 ++++++++++++++-------------- js/view/describers/ISLCDescriber.js | 2 -- 5 files changed, 28 insertions(+), 65 deletions(-) diff --git a/js/ISLCA11yStrings.js b/js/ISLCA11yStrings.js index 23e10f9..4206b92 100644 --- a/js/ISLCA11yStrings.js +++ b/js/ISLCA11yStrings.js @@ -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}}.' }, diff --git a/js/view/ISLCObjectPDOMNode.js b/js/view/ISLCObjectPDOMNode.js index 031ed9a..84b22a2 100644 --- a/js/view/ISLCObjectPDOMNode.js +++ b/js/view/ISLCObjectPDOMNode.js @@ -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 ) { diff --git a/js/view/ISLCStringManager.js b/js/view/ISLCStringManager.js index 29dc63c..e9fcca5 100644 --- a/js/view/ISLCStringManager.js +++ b/js/view/ISLCStringManager.js @@ -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; @@ -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; @@ -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, @@ -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 } ); @@ -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.' ); } diff --git a/js/view/describers/ForceDescriber.js b/js/view/describers/ForceDescriber.js index c2e9d08..07fc622 100644 --- a/js/view/describers/ForceDescriber.js +++ b/js/view/describers/ForceDescriber.js @@ -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; @@ -36,8 +37,6 @@ 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 ); @@ -45,13 +44,13 @@ define( require => { 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; @@ -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 @@ -124,7 +140,7 @@ define( require => { } getForceVectorSize() { - return SIZE_STRINGS[ this._vectorSizeIndex ]; + return SIZE_STRINGS[ this.vectorSizeIndex ]; } /** @@ -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 ); diff --git a/js/view/describers/ISLCDescriber.js b/js/view/describers/ISLCDescriber.js index fc86e0a..977c639 100644 --- a/js/view/describers/ISLCDescriber.js +++ b/js/view/describers/ISLCDescriber.js @@ -9,8 +9,6 @@ define( require => { const { OBJECT_ONE, OBJECT_TWO } = ISLCObjectEnum; - let describer = null; - class ISLCDescriber { constructor( model, object1Label, object2Label ) {