1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 sc_require('views/template');
  9 
 10 /**
 11   @class
 12   @extends SC.TemplateView
 13   @extends SC.ActionSupport
 14 */
 15 SC.Button = SC.TemplateView.extend(SC.ActionSupport,
 16 /** @scope SC.Button.prototype */{
 17 
 18   classNames: ['sc-button'],
 19 
 20   mouseDown: function() {
 21     this.set('isActive', true);
 22     this._isMouseDown = YES;
 23   },
 24 
 25   mouseExited: function() {
 26     this.set('isActive', false);
 27   },
 28 
 29   mouseEntered: function() {
 30     if (this._isMouseDown) {
 31       this.set('isActive', true);
 32     }
 33   },
 34 
 35   rootResponder: function() {
 36     var pane = this.get('pane');
 37     return pane.get('rootResponder');
 38   }.property('pane').cacheable(),
 39 
 40   mouseUp: function(event) {
 41     if (this.get('isActive')) {
 42       this.fireAction();
 43       this.set('isActive', false);
 44     }
 45 
 46     this._isMouseDown = NO;
 47   },
 48 
 49   touchStart: function(touch) {
 50     this.mouseDown(touch);
 51   },
 52 
 53   touchEnd: function(touch) {
 54     this.mouseUp(touch);
 55   },
 56 
 57   keyDown: function(evt) {
 58     var ret = NO,
 59         view;
 60     if (evt.which === 9 || evt.keyCode === 9) {
 61       view = evt.shiftKey ? this.get('previousValidKeyView') : this.get('nextValidKeyView');
 62       if (view) {
 63         view.becomeFirstResponder();
 64       } else {
 65         evt.allowDefault();
 66       }
 67       ret = YES;
 68     } else if (evt.which === SC.Event.KEY_SPACE || evt.which === SC.Event.KEY_RETURN) {
 69       this.set('isActive', YES);
 70       this.invokeLater('_runAction', SC.ButtonView.TRIGGER_DELAY);
 71       ret = YES;
 72     }
 73 
 74     return ret;
 75   },
 76 
 77   keyUp: function(evt) {
 78     this.set('isActive', NO);
 79     return YES;
 80   },
 81 
 82   _runAction: function() {
 83     this.fireAction();
 84     this.set('isActive', NO);
 85   }
 86 
 87 });
 88