<commit_before>// Copyright 2014-2017, University of Colorado Boulder

/**
 * A rectangular push button.  This is the file in which the appearance and behavior are brought together.
 *
 * This class inherits from RectangularButtonView, which contains all of the code that defines the button's appearance,
 * and adds the button's behavior by hooking up a button model.
 *
 * @author <NAME> (PhET Interactive Simulations)
 */
define( function( require ) {
  'use strict';

  // modules
  var inherit = require( 'PHET_CORE/inherit' );
  var PushButtonInteractionStateProperty = require( 'SUN/buttons/PushButtonInteractionStateProperty' );
  var PushButtonModel = require( 'SUN/buttons/PushButtonModel' );
  var RectangularButtonView = require( 'SUN/buttons/RectangularButtonView' );
  var sun = require( 'SUN/sun' );
  var Tandem = require( 'TANDEM/Tandem' );
  var TPushButton = require( 'SUN/buttons/TPushButton' );

  /**
   * @param {Object} [options]
   * @constructor
   */
  function RectangularPushButton( options ) {
    var self = this;

    options = _.extend( {
      tandem: Tandem.tandemRequired(), // {Tandem|null}
      phetioType: TPushButton,

      // a11y - listener that will only be called when using the keyboard to interact with the push button
      accessibleFire: function() {}
    }, options );

    // If a listener was passed in, save it and add it after creating the button model.  This is done so that
    // the same code path is always used for adding listener, thus guaranteeing a consistent code path if addListener is
    // overridden, see https://github.com/phetsims/sun/issues/284.
    var listener = options.listener;
    options = _.omit( options, [ 'listener' ] );

    // Safe to pass through options to the PushButtonModel like "fireOnDown".  Other scenery options will be safely ignored.
    this.buttonModel = new PushButtonModel( options ); // @public, listen only

    // add the listener that was potentially saved above
    listener && this.addListener( listener );

    // Call the parent type
    RectangularButtonView.call( this, this.buttonModel, new PushButtonInteractionStateProperty( this.buttonModel ), options );

    // a11y - press the button when 'enter' or 'spacebar' are pressed
    this.clickListener = this.addAccessibleInputListener( {
      click: function() {
        if ( self.enabled ) {
          self.buttonModel.fire();
          options.accessibleFire();
        }
      }
    } );

    this.disposeRectangularPushButton = function() {
      this.buttonModel.dispose(); //TODO this fails when assertions are enabled, see sun#212
      this.removeAccessibleInputListener( this.clickListener );
      options.tandem.removeInstance( this );
    };
  }

  sun.register( 'RectangularPushButton', RectangularPushButton );

  return inherit( RectangularButtonView, RectangularPushButton, {

      // @public
      dispose: function() {
        this.disposeRectangularPushButton();
        RectangularButtonView.prototype.dispose.call( this );
      },

      // @public
      addListener: function( listener ) {
        this.buttonModel.addListener( listener );
      },

      // @public
      removeListener: function( listener ) {
        this.buttonModel.removeListener( listener );
      }
    }<commit_msg>use downProperty to call ButtonModel optional start and end callbacks when using a keyboard, see phetsims/rutherford-scattering#127<commit_after>// Copyright 2014-2017, University of Colorado Boulder

/**
 * A rectangular push button.  This is the file in which the appearance and behavior are brought together.
 *
 * This class inherits from RectangularButtonView, which contains all of the code that defines the button's appearance,
 * and adds the button's behavior by hooking up a button model.
 *
 * @author <NAME> (PhET Interactive Simulations)
 */
define( function( require ) {
  'use strict';

  // modules
  var inherit = require( 'PHET_CORE/inherit' );
  var PushButtonInteractionStateProperty = require( 'SUN/buttons/PushButtonInteractionStateProperty' );
  var PushButtonModel = require( 'SUN/buttons/PushButtonModel' );
  var RectangularButtonView = require( 'SUN/buttons/RectangularButtonView' );
  var sun = require( 'SUN/sun' );
  var Tandem = require( 'TANDEM/Tandem' );
  var TPushButton = require( 'SUN/buttons/TPushButton' );

  /**
   * @param {Object} [options]
   * @constructor
   */
  function RectangularPushButton( options ) {
    var self = this;

    options = _.extend( {
      tandem: Tandem.tandemRequired(), // {Tandem|null}
      phetioType: TPushButton,

      // a11y - listener that will only be called when using the keyboard to interact with the push button
      accessibleFire: function() {}
    }, options );

    // If a listener was passed in, save it and add it after creating the button model.  This is done so that
    // the same code path is always used for adding listener, thus guaranteeing a consistent code path if addListener is
    // overridden, see https://github.com/phetsims/sun/issues/284.
    var listener = options.listener;
    options = _.omit( options, [ 'listener' ] );

    // Safe to pass through options to the PushButtonModel like "fireOnDown".  Other scenery options will be safely ignored.
    this.buttonModel = new PushButtonModel( options ); // @public, listen only

    // add the listener that was potentially saved above
    listener && this.addListener( listener );

    // Call the parent type
    RectangularButtonView.call( this, this.buttonModel, new PushButtonInteractionStateProperty( this.buttonModel ), options );

    // a11y - press the button when 'enter' or 'spacebar' are pressed
    this.clickListener = this.addAccessibleInputListener( {
      click: function() {
        if ( self.enabled ) {
          self.buttonModel.downProperty.set( true ); // to trigger optional startCallback
          self.buttonModel.fire();
          options.accessibleFire();
          self.buttonModel.downProperty.set( false ); // to trigger optional endCallback
        }
      }
    } );

    this.disposeRectangularPushButton = function() {
      this.buttonModel.dispose(); //TODO this fails when assertions are enabled, see sun#212
      this.removeAccessibleInputListener( this.clickListener );
      options.tandem.removeInstance( this );
    };
  }

  sun.register( 'RectangularPushButton', RectangularPushButton );

  return inherit( RectangularButtonView, RectangularPushButton, {

      // @public
      dispose: function() {
        this.disposeRectangularPushButton();
        RectangularButtonView.prototype.dispose.call( this );
      },

      // @public
      addListener: function( listener ) {
        this.buttonModel.addListener( listener );
      },

      // @public
      removeListener: function( listener ) {
        this.buttonModel.removeListener( listener );
      }
    }
{'fraction_non_alphanumeric': 0.09773887673231219, 'fraction_numerical': 0.0056892778993435445, 'mean_word_length': 3.180487804878049, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '802701', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1809, 'n_words': 591}