OSCON/WRTKit/UI/ActionControl.js
changeset 7 97dcd250e5be
equal deleted inserted replaced
6:5e0dece09f96 7:97dcd250e5be
       
     1 /*
       
     2 © Copyright 2008 Nokia Corporation. All rights reserved.
       
     3 
       
     4 IMPORTANT:  The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia
       
     5 Corporation (ÒNokiaÓ) in consideration of your agreement to the following terms. Your use, installation
       
     6 and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If
       
     7 you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example
       
     8 Widget files.
       
     9 
       
    10 In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia
       
    11 grants you a personal, non-exclusive license, under NokiaÕs copyrights in the WRTKit and Example
       
    12 Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML,
       
    13 CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60
       
    14 Widgets.
       
    15 
       
    16 If you redistribute the WRTKit and Example files, you must retain this entire notice in all such
       
    17 redistributions of the WRTKit and Example files.
       
    18 
       
    19 You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products
       
    20 that include the WRTKit and Example files without the prior written explicit agreement with Nokia.
       
    21 Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by
       
    22 Nokia herein, including but not limited to any patent rights that may be infringed by your products that
       
    23 incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files
       
    24 may be incorporated.
       
    25 
       
    26 The WRTKit and Example files are provided on an "AS IS" basis.  NOKIA MAKES NO
       
    27 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
       
    28 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
       
    29 PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION
       
    30 ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
       
    31 
       
    32 IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
       
    33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR
       
    36 DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY
       
    37 OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE,
       
    38 EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    39 
       
    40 */
       
    41 
       
    42 ///////////////////////////////////////////////////////////////////////////////
       
    43 // The ActionControl class is an abstract base class for action controls like
       
    44 // buttons. Don't use ActionControl directly.
       
    45 
       
    46 // Constructor.
       
    47 function ActionControl(id, caption) {
       
    48     if (id != UI_NO_INIT_ID) {
       
    49         this.init(id, caption);
       
    50     }
       
    51 }
       
    52 
       
    53 // ActionControl inherits from Control.
       
    54 ActionControl.prototype = new Control(UI_NO_INIT_ID);
       
    55 
       
    56 // Reference to the button element.
       
    57 ActionControl.prototype.buttonElement = null;
       
    58 
       
    59 // Reference to the link element.
       
    60 ActionControl.prototype.linkElement = null;
       
    61 
       
    62 // Enabled status.
       
    63 ActionControl.prototype.enabled = false;
       
    64 
       
    65 // Initializer - called from constructor.
       
    66 ActionControl.prototype.init = function(id, caption) {
       
    67     uiLogger.debug("ActionControl.init(" + id + ", " + caption + ")");
       
    68     
       
    69     // call superclass initializer
       
    70     Control.prototype.init.call(this, id, caption);
       
    71     
       
    72     // the control defaults to enabled
       
    73     this.enabled = true;
       
    74 }
       
    75 
       
    76 // Common event listeners hookup function called from subclasses.
       
    77 ActionControl.prototype.bindActionControlListeners = function() {
       
    78     var self = this;
       
    79     this.linkElement.addEventListener("focus", function() { self.focusStateChanged(true); }, false);
       
    80     this.linkElement.addEventListener("blur", function() { self.focusStateChanged(false); }, false);
       
    81     this.buttonElement.addEventListener("mouseover", function() { self.hoverStateChanged(true); }, false);
       
    82     this.buttonElement.addEventListener("mouseout", function() { self.hoverStateChanged(false); }, false);
       
    83     this.buttonElement.addEventListener("mousedown", function(event) {
       
    84                                                        self.controlClicked(event);
       
    85                                                        event.stopPropagation();
       
    86                                                        event.preventDefault();
       
    87                                                    }, true);
       
    88     this.buttonElement.addEventListener("keydown", function(event) {
       
    89                                                     // center and enter trigger the action
       
    90                                                     if (event.keyCode == 0 || event.keyCode == 13) {
       
    91                                                         self.controlClicked();
       
    92                                                         event.stopPropagation();
       
    93                                                         event.preventDefault();
       
    94                                                     }
       
    95                                                  }, true);
       
    96 }
       
    97 
       
    98 // Returns the enabled state.
       
    99 ActionControl.prototype.isEnabled = function() {
       
   100     return this.enabled;
       
   101 }
       
   102 
       
   103 // Sets the enabled state.
       
   104 ActionControl.prototype.setEnabled = function(enabled) {
       
   105     uiLogger.debug("ActionControl.setEnabled(" + enabled + ")");
       
   106     // switch the state
       
   107     this.enabled = enabled;
       
   108 }
       
   109 
       
   110 // Sets the focused state for the control.
       
   111 // Note: This may not always succeed.
       
   112 ActionControl.prototype.setFocused = function(focused) {
       
   113     uiLogger.debug("ActionControl.setFocused(" + focused + ")");
       
   114     if (this.enabled) {
       
   115         if (focused) {
       
   116             this.linkElement.focus();
       
   117         } else {
       
   118             this.linkElement.blur();
       
   119         }
       
   120     }
       
   121 }
       
   122 
       
   123 // Callback for clicks.
       
   124 ActionControl.prototype.controlClicked = function(event) {
       
   125     uiLogger.debug("ActionControl.controlClicked()");
       
   126     
       
   127     // if we're enabled then a click results in an action performed event
       
   128     if (this.enabled) {
       
   129         // focus when clicked
       
   130         if (!this.focused) {
       
   131             this.linkElement.focus();
       
   132         }
       
   133         
       
   134         // notify event listeners
       
   135         this.actionPerformed(event);
       
   136     }
       
   137 }
       
   138 
       
   139 // Callback for action performed events.
       
   140 ActionControl.prototype.actionPerformed = function(event) {
       
   141     uiLogger.debug("ActionControl.actionPerformed()");
       
   142     // notify event listeners
       
   143     this.fireEvent(this.createEvent("ActionPerformed", event));
       
   144 }