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