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