org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/ActionControl.js
changeset 309 c01f5ab28a11
parent 308 c521df56b15d
child 310 e9484be98cfe
equal deleted inserted replaced
308:c521df56b15d 309:c01f5ab28a11
     1 /**
       
     2  * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  * 
       
    12  * Contributors:
       
    13  * 
       
    14  * Description:
       
    15  * 
       
    16  */
       
    17 
       
    18 ///////////////////////////////////////////////////////////////////////////////
       
    19 // The ActionControl class is an abstract base class for action controls like
       
    20 // buttons. Don't use ActionControl directly.
       
    21 
       
    22 // Constructor.
       
    23 function ActionControl(id, caption) {
       
    24     if (id != UI_NO_INIT_ID) {
       
    25         this.init(id, caption);
       
    26     }
       
    27 }
       
    28 
       
    29 // ActionControl inherits from Control.
       
    30 ActionControl.prototype = new Control(UI_NO_INIT_ID);
       
    31 
       
    32 // Reference to the button element.
       
    33 ActionControl.prototype.buttonElement = null;
       
    34 
       
    35 // Reference to the link element.
       
    36 ActionControl.prototype.linkElement = null;
       
    37 
       
    38 // Enabled status.
       
    39 ActionControl.prototype.enabled = false;
       
    40 
       
    41 // Initializer - called from constructor.
       
    42 ActionControl.prototype.init = function(id, caption) {
       
    43     uiLogger.debug("ActionControl.init(" + id + ", " + caption + ")");
       
    44     
       
    45     // call superclass initializer
       
    46     Control.prototype.init.call(this, id, caption);
       
    47     
       
    48     // the control defaults to enabled
       
    49     this.enabled = true;
       
    50 };
       
    51 
       
    52 // Common event listeners hookup function called from subclasses.
       
    53 ActionControl.prototype.bindActionControlListeners = function() {
       
    54     var self = this;
       
    55     this.linkElement.addEventListener("focus", function() { self.focusStateChanged(true); }, false);
       
    56     this.linkElement.addEventListener("blur", function() { self.focusStateChanged(false); }, false);
       
    57     this.buttonElement.addEventListener("mouseover", function() { self.hoverStateChanged(true); }, false);
       
    58     this.buttonElement.addEventListener("mouseout", function() { self.hoverStateChanged(false); }, false);
       
    59     this.buttonElement.addEventListener("mousedown", function(event) {
       
    60                                                        self.controlClicked(event);
       
    61                                                        event.stopPropagation();
       
    62                                                        event.preventDefault();
       
    63                                                    }, true);
       
    64     this.buttonElement.addEventListener("keydown", function(event) {
       
    65                                                     // center and enter trigger the action
       
    66                                                     if (event.keyCode == 0 || event.keyCode == 13) {
       
    67                                                         self.controlClicked();
       
    68                                                         event.stopPropagation();
       
    69                                                         event.preventDefault();
       
    70                                                     }
       
    71                                                  }, true);
       
    72 };
       
    73 
       
    74 // Returns the enabled state.
       
    75 ActionControl.prototype.isEnabled = function() {
       
    76     return this.enabled;
       
    77 };
       
    78 
       
    79 // Sets the enabled state.
       
    80 ActionControl.prototype.setEnabled = function(enabled) {
       
    81     uiLogger.debug("ActionControl.setEnabled(" + enabled + ")");
       
    82     // switch the state
       
    83     this.enabled = enabled;
       
    84 };
       
    85 
       
    86 // Sets the focused state for the control.
       
    87 // Note: This may not always succeed.
       
    88 ActionControl.prototype.setFocused = function(focused) {
       
    89     uiLogger.debug("ActionControl.setFocused(" + focused + ")");
       
    90     if (this.enabled) {
       
    91         if (focused) {
       
    92             this.linkElement.focus();
       
    93         } else {
       
    94             this.linkElement.blur();
       
    95         }
       
    96     }
       
    97 };
       
    98 
       
    99 // Callback for clicks.
       
   100 ActionControl.prototype.controlClicked = function(event) {
       
   101     uiLogger.debug("ActionControl.controlClicked()");
       
   102     
       
   103     // if we're enabled then a click results in an action performed event
       
   104     if (this.enabled) {
       
   105         // focus when clicked
       
   106         if (!this.focused) {
       
   107             this.linkElement.focus();
       
   108         }
       
   109         
       
   110         // notify event listeners
       
   111         this.actionPerformed(event);
       
   112     }
       
   113 };
       
   114 
       
   115 // Callback for action performed events.
       
   116 ActionControl.prototype.actionPerformed = function(event) {
       
   117     uiLogger.debug("ActionControl.actionPerformed()");
       
   118     // notify event listeners
       
   119     this.fireEvent(this.createEvent("ActionPerformed", event));
       
   120 };