Skip to content

Commit

Permalink
convert to es6, phetsims/tasks#1051
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Nov 12, 2020
1 parent 12a60a9 commit a9cbb7e
Show file tree
Hide file tree
Showing 12 changed files with 655 additions and 675 deletions.
2 changes: 1 addition & 1 deletion js/ohms-law-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const simOptions = {
hasKeyboardHelpContent: true
};

simLauncher.launch( function() {
simLauncher.launch( () => {

// Create and start the sim
const sim = new Sim( ohmsLawTitleString, [ new OhmsLawScreen( tandem.createTandem( 'ohmsLawScreen' ) ) ], simOptions );
Expand Down
6 changes: 3 additions & 3 deletions js/ohms-law/OhmsLawScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import OhmsLawScreenView from './view/OhmsLawScreenView.js';
class OhmsLawScreen extends Screen {
constructor( tandem ) {
super(
function() { return new OhmsLawModel( tandem.createTandem( 'model' ) ); },
function( model ) { return new OhmsLawScreenView( model, tandem.createTandem( 'view' ) ); },
() => new OhmsLawModel( tandem.createTandem( 'model' ) ),
model => new OhmsLawScreenView( model, tandem.createTandem( 'view' ) ),
{
backgroundColorProperty: new Property( new Color( '#ffffdf' ), {
tandem: tandem.createTandem( 'backgroundColorProperty' ),
phetioType: Property.PropertyIO( Color.ColorIO )
} ),
tandem: tandem,

keyboardHelpNode: new SliderAndGeneralKeyboardHelpContent( )
keyboardHelpNode: new SliderAndGeneralKeyboardHelpContent()
}
);
}
Expand Down
146 changes: 74 additions & 72 deletions js/ohms-law/model/OhmsLawModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,144 +12,134 @@ import EnumerationProperty from '../../../../axon/js/EnumerationProperty.js';
import NumberProperty from '../../../../axon/js/NumberProperty.js';
import Range from '../../../../dot/js/Range.js';
import Utils from '../../../../dot/js/Utils.js';
import inherit from '../../../../phet-core/js/inherit.js';
import NumberIO from '../../../../tandem/js/types/NumberIO.js';
import ohmsLaw from '../../ohmsLaw.js';
import OhmsLawConstants from '../OhmsLawConstants.js';
import CurrentUnit from './CurrentUnit.js';

/**
* @constructor
*/
function OhmsLawModel( tandem ) {

// @public {Property.<number>} in volts
this.voltageProperty = new NumberProperty( OhmsLawConstants.VOLTAGE_RANGE.getDefaultValue(), {
tandem: tandem.createTandem( 'voltageProperty' ),
units: 'volts',
range: OhmsLawConstants.VOLTAGE_RANGE,
phetioDocumentation: 'The voltage in the circuit'
} );

// @public {Property.<number>} in Ohms
this.resistanceProperty = new NumberProperty( OhmsLawConstants.RESISTANCE_RANGE.getDefaultValue(), {
tandem: tandem.createTandem( 'resistanceProperty' ),
units: 'ohms',
range: OhmsLawConstants.RESISTANCE_RANGE,
phetioDocumentation: 'The resistance in the circuit'
} );

// @public {Property.<number>} create a derived property that tracks the current in milli amps
this.currentProperty = new DerivedProperty(
[ this.voltageProperty, this.resistanceProperty ],
computeCurrent, {
tandem: tandem.createTandem( 'currentProperty' ),
units: 'milliamperes',
phetioType: DerivedProperty.DerivedPropertyIO( NumberIO ),
phetioDocumentation: 'The current flowing in the circuit'
}
);

// @public
this.currentUnitsProperty = new EnumerationProperty( CurrentUnit, CurrentUnit.MILLIAMPS, {
tandem: tandem.createTandem( 'currentUnitsProperty' ),
phetioDocumentation: 'Determines the displayed unit for the current'
} );

// @public (read-only) {BooleanProperty} - true when a reset is in progress, false otherwise
this.resetInProgressProperty = new BooleanProperty( false );
}

/**
* The main model function, used to compute the current of the model
* @param voltage
* @param resistance
* @returns {number} - current in milliamps
*/
function computeCurrent( voltage, resistance ) {
return 1000 * voltage / resistance;
}

ohmsLaw.register( 'OhmsLawModel', OhmsLawModel );
class OhmsLawModel {
/**
*/
constructor( tandem ) {

// @public {Property.<number>} in volts
this.voltageProperty = new NumberProperty( OhmsLawConstants.VOLTAGE_RANGE.getDefaultValue(), {
tandem: tandem.createTandem( 'voltageProperty' ),
units: 'volts',
range: OhmsLawConstants.VOLTAGE_RANGE,
phetioDocumentation: 'The voltage in the circuit'
} );

// @public {Property.<number>} in Ohms
this.resistanceProperty = new NumberProperty( OhmsLawConstants.RESISTANCE_RANGE.getDefaultValue(), {
tandem: tandem.createTandem( 'resistanceProperty' ),
units: 'ohms',
range: OhmsLawConstants.RESISTANCE_RANGE,
phetioDocumentation: 'The resistance in the circuit'
} );

// @public {Property.<number>} create a derived property that tracks the current in milli amps
this.currentProperty = new DerivedProperty(
[ this.voltageProperty, this.resistanceProperty ],
computeCurrent, {
tandem: tandem.createTandem( 'currentProperty' ),
units: 'milliamperes',
phetioType: DerivedProperty.DerivedPropertyIO( NumberIO ),
phetioDocumentation: 'The current flowing in the circuit'
}
);

// @public
this.currentUnitsProperty = new EnumerationProperty( CurrentUnit, CurrentUnit.MILLIAMPS, {
tandem: tandem.createTandem( 'currentUnitsProperty' ),
phetioDocumentation: 'Determines the displayed unit for the current'
} );

// @public (read-only) {BooleanProperty} - true when a reset is in progress, false otherwise
this.resetInProgressProperty = new BooleanProperty( false );
}

inherit( Object, OhmsLawModel, {

/**
* resets the properties of the model
* @public
*/
reset: function() {
reset() {
this.resetInProgressProperty.set( true );
this.voltageProperty.reset();
this.resistanceProperty.reset();
this.resetInProgressProperty.set( false );
},
}

/**
* Get the normalized voltage over the range of allowed voltages in this sim.
*
* @public
* @returns {number}
*/
getNormalizedVoltage: function() {
getNormalizedVoltage() {
const range = OhmsLawConstants.VOLTAGE_RANGE;
return ( this.voltageProperty.get() - range.min ) / range.getLength();
},
}

/**
* Get the normalized current, based on the allowable values for current in this sim.
* @public
* @returns {number}
*/
getNormalizedCurrent: function() {
getNormalizedCurrent() {
const range = OhmsLawModel.getCurrentRange();
return ( this.currentProperty.get() - range.min ) / range.getLength();
},
}

/**
* Get the normalized resistance, based on the allowable values for resistance in this
* sim.
* @public
* @returns {number}
*/
getNormalizedResistance: function() {
getNormalizedResistance() {
const range = OhmsLawConstants.RESISTANCE_RANGE;
return ( this.resistanceProperty.get() - range.min ) / range.getLength();
},
}

/**
* Get the current as a number formatted based on the appropriate decimal places for the display unit.
* @public
* @returns {string}
*/
getFixedCurrent: function() {
getFixedCurrent() {
let current = this.currentProperty.value;
const units = this.currentUnitsProperty.value;
if ( units === CurrentUnit.AMPS ) {
current = current / 100;
}
return Utils.toFixed( current, CurrentUnit.getSigFigs( units ) );
}
}, {

/**
* Get the maximum current that can be computed by the model
* @returns {number} - the max current.
* @public
*/
getMaxCurrent: function() {
static getMaxCurrent() {
return computeCurrent( OhmsLawConstants.VOLTAGE_RANGE.max, OhmsLawConstants.RESISTANCE_RANGE.min );
},
}

/**
* Get the minimum current that can be computed by the model.
* @returns {number} [description]
* @private
*/
getMinCurrent: function() {
static getMinCurrent() {
return computeCurrent( OhmsLawConstants.VOLTAGE_RANGE.min, OhmsLawConstants.RESISTANCE_RANGE.max );
},
}

/**
* Get the Range of the current, will construct a new range if not yet set
* @returns {Range}
* @public
*/
getCurrentRange: function() {
static getCurrentRange() {

if ( !this.currentRange ) {

Expand All @@ -158,6 +148,18 @@ inherit( Object, OhmsLawModel, {
}
return this.currentRange;
}
} );
}

/**
* The main model function, used to compute the current of the model
* @param voltage
* @param resistance
* @returns {number} - current in milliamps
*/
function computeCurrent( voltage, resistance ) {
return 1000 * voltage / resistance;
}

ohmsLaw.register( 'OhmsLawModel', OhmsLawModel );

export default OhmsLawModel;
84 changes: 41 additions & 43 deletions js/ohms-law/view/BatteriesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import Utils from '../../../../dot/js/Utils.js';
import inherit from '../../../../phet-core/js/inherit.js';
import merge from '../../../../phet-core/js/merge.js';
import StringUtils from '../../../../phetcommon/js/util/StringUtils.js';
import Node from '../../../../scenery/js/nodes/Node.js';
Expand All @@ -19,64 +18,63 @@ import BatteryView from './BatteryView.js';

const batteriesSupplyPatternString = OhmsLawA11yStrings.batteriesSupplyPattern.value;

/**
* @param {Property.<number>} voltageProperty
* @param {Object} [options]
* @constructor
*/
function BatteriesView( voltageProperty, options ) {
class BatteriesView extends Node {
/**
* @param {Property.<number>} voltageProperty
* @param {Object} [options]
*/
constructor( voltageProperty, options ) {

options = merge( {
tandem: Tandem.REQUIRED,
options = merge( {
tandem: Tandem.REQUIRED,

// pdom
tagName: 'li' // this assumes that it is a child of a 'ul'
}, options );
// pdom
tagName: 'li' // this assumes that it is a child of a 'ul'
}, options );

Node.call( this );
const self = this;
super();

// Store battery nodes in an array
const batteries = [];
// Store battery nodes in an array
const batteries = [];

const batteriesGroupTandem = options.tandem.createGroupTandem( 'battery' );
const batteriesGroupTandem = options.tandem.createGroupTandem( 'battery' );

// Create an array of batteries; enough to fill the entire wire.
for ( let i = 0; i < OhmsLawConstants.MAX_NUMBER_OF_BATTERIES; i++ ) {
const leftPosition = i * OhmsLawConstants.BATTERY_WIDTH;
const battery = new BatteryView( { x: leftPosition, y: 0, tandem: batteriesGroupTandem.createNextTandem() } );
// Create an array of batteries; enough to fill the entire wire.
for ( let i = 0; i < OhmsLawConstants.MAX_NUMBER_OF_BATTERIES; i++ ) {
const leftPosition = i * OhmsLawConstants.BATTERY_WIDTH;
const battery = new BatteryView( { x: leftPosition, y: 0, tandem: batteriesGroupTandem.createNextTandem() } );

// Add them as children to this node, and to the array for manipulation
this.addChild( battery );
batteries.push( battery );
}
// Add them as children to this node, and to the array for manipulation
this.addChild( battery );
batteries.push( battery );
}

// Present for the lifetime of the simulation; no need to unlink.
voltageProperty.link( function( voltage ) {
// Present for the lifetime of the simulation; no need to unlink.
voltageProperty.link( voltage => {

batteries.forEach( function( battery, index ) {
batteries.forEach( ( battery, index ) => {

// Determine associated with a particular battery
let voltageBattery = Math.min( OhmsLawConstants.AA_VOLTAGE, voltage - index * OhmsLawConstants.AA_VOLTAGE );
voltageBattery = Utils.roundToInterval( voltageBattery, Math.pow( 10, -OhmsLawConstants.VOLTAGE_SIG_FIGS ) );
// Determine associated with a particular battery
let voltageBattery = Math.min( OhmsLawConstants.AA_VOLTAGE, voltage - index * OhmsLawConstants.AA_VOLTAGE );
voltageBattery = Utils.roundToInterval( voltageBattery, Math.pow( 10, -OhmsLawConstants.VOLTAGE_SIG_FIGS ) );

// Battery is only visible if it has a voltage.
battery.visible = ( voltageBattery > 0 );
// Battery is only visible if it has a voltage.
battery.visible = ( voltageBattery > 0 );

if ( battery.visible ) {
battery.setVoltage( voltageBattery );
}
} );
if ( battery.visible ) {
battery.setVoltage( voltageBattery );
}
} );

// pdom - update the description for the number of batteries
self.innerContent = StringUtils.fillIn( batteriesSupplyPatternString, {
voltage: Utils.toFixed( voltage, OhmsLawConstants.VOLTAGE_SIG_FIGS )
// pdom - update the description for the number of batteries
this.innerContent = StringUtils.fillIn( batteriesSupplyPatternString, {
voltage: Utils.toFixed( voltage, OhmsLawConstants.VOLTAGE_SIG_FIGS )
} );
} );
} );
this.mutate( options );
this.mutate( options );
}
}

ohmsLaw.register( 'BatteriesView', BatteriesView );

inherit( Node, BatteriesView );
export default BatteriesView;
Loading

0 comments on commit a9cbb7e

Please sign in to comment.