Skip to content

Commit

Permalink
Add EnabledNode PhET-iO API and support for LinkedElementIO in API va…
Browse files Browse the repository at this point in the history
…lidation, mixin EnabledComponentAPIMixin into SliderAPI, #605
  • Loading branch information
zepumph committed Jul 17, 2020
1 parent 748a860 commit 5b9a5f1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
39 changes: 39 additions & 0 deletions js/EnabledComponentAPIMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020, University of Colorado Boulder

/**
* PhET-iO API mixin EnabledComponent
*
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import PropertyAPI from '../../axon/js/PropertyAPI.js';
import PropertyIO from '../../axon/js/PropertyIO.js';
import merge from '../../phet-core/js/merge.js';
import BooleanIO from '../../tandem/js/types/BooleanIO.js';
import sun from './sun.js';

const EnabledComponentAPIMixin = Type => {
return class extends Type {

/**
* @param {Object} [options]
*/
constructor( options ) {
assert && assert( arguments.length === 0 || arguments.length === 1, '0 or 1 args expected' );

options = merge( {
enabledPropertyOptions: {
phetioFeatured: true,
phetioType: PropertyIO( BooleanIO )
}
}, options );

super( options );

this.enabledProperty = new PropertyAPI( options.enabledPropertyOptions );
}
};
};

sun.register( 'EnabledComponentAPIMixin', EnabledComponentAPIMixin );
export default EnabledComponentAPIMixin;
10 changes: 7 additions & 3 deletions js/EnabledPhetioObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ const EnabledPhetioObject = {

// This phet-io support only applies to instances of PhetioObject
if ( !ownsEnabledProperty ) {
assert && Tandem.PHET_IO_ENABLED && Tandem.errorOnFailedValidation() && this.isPhetioInstrumented() &&
assert( !!options.enabledProperty.phetioFeatured === !!this.phetioFeatured,
'provided enabledProperty must be phetioFeatured if this checkbox is' );

if ( Tandem.PHET_IO_ENABLED && Tandem.errorOnFailedValidation() && this.isPhetioInstrumented() ) {

assert && assert( options.enabledProperty.isPhetioInstrumented(), 'provided enabledProperty must be instrumented if this PhetioObject is.' );
assert && this.phetioFeatured && assert( options.enabledProperty.phetioFeatured, 'provided enabledProperty must be phetioFeatured if this PhetioObject is.' );
}

// If enabledProperty was passed in, PhET-iO wrappers like Studio needs to know about that linkage
// This is supported in API.js types because in practice LinkedElementIO defer to their linked PhetioObject.
this.enabledProperty.isPhetioInstrumented() && this.addLinkedElement( options.enabledProperty, {
tandem: options.tandem.createTandem( EnabledComponent.ENABLED_PROPERTY_TANDEM_NAME )
} );
Expand Down
13 changes: 3 additions & 10 deletions js/SliderAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import merge from '../../phet-core/js/merge.js';
import DragListenerAPI from '../../scenery/js/listeners/DragListenerAPI.js';
import NodeAPI from '../../scenery/js/nodes/NodeAPI.js';
import EnabledComponentAPIMixin from './EnabledComponentAPIMixin.js';
import SliderIO from './SliderIO.js';
import sun from './sun.js';

Expand Down Expand Up @@ -36,7 +37,7 @@ class TrackAPI extends NodeAPI {
}
}

class SliderAPI extends NodeAPI {
class SliderAPI extends EnabledComponentAPIMixin( NodeAPI ) {

/**
* @param {Object} [options]
Expand All @@ -52,15 +53,7 @@ class SliderAPI extends NodeAPI {

super( options );

this.track = new TrackAPI( {

// TODO: this is now part of the API, but how to we tell phetioAPITest to test this arbitrarily deep tandem override for Slider, https://github.com/phetsims/phet-io/issues/1657
dragListenerOptions: {
pressActionOptions: {
phetioFeature: true
}
}
} );
this.track = new TrackAPI();
}
}

Expand Down
12 changes: 11 additions & 1 deletion js/SliderTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import BooleanProperty from '../../axon/js/BooleanProperty.js';
import Property from '../../axon/js/Property.js';
import Range from '../../dot/js/Range.js';
import phetioAPITest from '../../tandem/js/phetioAPITest.js';
Expand All @@ -17,5 +18,14 @@ import SliderAPI from './SliderAPI.js';
QUnit.module( 'Slider' );

QUnit.test( 'Slider PhET-iO API validation', assert => {
phetioAPITest( assert, new SliderAPI(), 'slider', tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), { tandem: tandem } ) );
phetioAPITest( assert, new SliderAPI(), 'slider',
tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), { tandem: tandem } ) );
} );

QUnit.test( 'Slider PhET-iO API validation, provided enabledProperty', assert => {
phetioAPITest( assert, new SliderAPI(), 'slider',
tandem => new HSlider( new Property( 0 ), new Range( 0, 10 ), {
tandem: tandem,
enabledProperty: new BooleanProperty( false, { tandem: tandem.createTandem( 'otherEnabledProperty' ), phetioFeatured: true } )
} ) );
} );

1 comment on commit 5b9a5f1

@zepumph
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.