Changed zouba directory heirarchy.
[ptas] / zouba / misc / rss / WRTKit / UI / UIManager.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 UI manager manages a set of views and other user interface elements.\r
44 \r
45 // Constructor.\r
46 function UIManager(viewParentElement, scrollbarParentElement, enableScrollBar, delayInit) {    \r
47     uiLogger.debug("UIManager(" + viewParentElement + ", " + scrollbarParentElement + ")");\r
48     if (delayInit == null) {\r
49         this.init(viewParentElement, enableScrollBar, scrollbarParentElement);\r
50     }\r
51 }\r
52 \r
53 // Parent element for views.\r
54 UIManager.prototype.viewParentElement = null;\r
55 \r
56 // Parent element for scrollbar.\r
57 UIManager.prototype.scrollbarParentElement = null;\r
58 \r
59 // The currently displayed view.\r
60 UIManager.prototype.currentView = null;\r
61 \r
62 // Reference to the scrollbar.\r
63 UIManager.prototype.scrollbar = null;\r
64 \r
65 // Current scroll Y position.\r
66 UIManager.prototype.scrollY = -1;\r
67 \r
68 // Current viewport height.\r
69 UIManager.prototype.viewportHeight = -1;\r
70 \r
71 // Current document height.\r
72 UIManager.prototype.documentHeight = -1;\r
73 \r
74 // Timer identifier or null if no active timer.\r
75 UIManager.prototype.timerId = null;\r
76 \r
77 // Interval for timer ticks for the UI manager timer (in milliseconds)\r
78 UIManager.prototype.TIMER_INTERVAL = 250;\r
79 \r
80 // Reference to the notification popup used to displays notifications.\r
81 UIManager.prototype.notificationPopup = null;\r
82 \r
83 // is scrollbar enabled\r
84 UIManager.prototype.enableScrollBar = null;\r
85 \r
86 // init function\r
87 UIManager.prototype.init = function(viewParentElement, enableScrollBar, scrollbarParentElement) {\r
88     this.enableScrollBar = enableScrollBar;\r
89     \r
90     // parent element for views\r
91     if (viewParentElement == null) {\r
92         // create a parent for views\r
93         this.viewParentElement = document.createElement("div");\r
94         this.viewParentElement.className = "ViewContainer";\r
95         document.body.appendChild(this.viewParentElement);\r
96     }\r
97     else {\r
98         this.viewParentElement = viewParentElement;\r
99     }\r
100     \r
101     // parent element for scrollbar\r
102     if (enableScrollBar) {\r
103         if (scrollbarParentElement == null) {\r
104             // create a parent for the scrollbar\r
105             this.scrollbarParentElement = document.createElement("div");\r
106             this.scrollbarParentElement.className = "DocumentScrollbarContainer";\r
107             document.body.appendChild(this.scrollbarParentElement);\r
108         }\r
109         else {\r
110             this.scrollbarParentElement = scrollbarParentElement;\r
111         }\r
112     }\r
113     \r
114     // currently selected view\r
115     this.currentView = null;\r
116     \r
117     // create the notification popup\r
118     // the notification popup adds itself as a child element to the document body\r
119     this.notificationPopup = new NotificationPopup();\r
120     \r
121     // create scrollbar\r
122     if (enableScrollBar) {\r
123         this.scrollbar = new Scrollbar(this.scrollbarParentElement);\r
124     }\r
125     \r
126     // setup scrollbar tracking\r
127     var self = this;\r
128     this.startTimer();\r
129     if (enableScrollBar) {\r
130         window.addEventListener("resize", function(){\r
131             self.updateScrollbar();\r
132         }, false);\r
133         window.addEventListener("scroll", function(){\r
134             self.updateScrollbar();\r
135         }, false);\r
136     }\r
137 }\r
138 \r
139 // Returns the current view.\r
140 UIManager.prototype.getView = function() {\r
141     return this.currentView;\r
142 }\r
143 \r
144 // Switches to the specified view.\r
145 UIManager.prototype.setView = function(view) {\r
146     uiLogger.debug("View set to " + view.id);\r
147     \r
148     // remove the current view from the parent element\r
149     if (this.currentView != null) {\r
150         this.viewParentElement.removeChild(this.currentView.rootElement);\r
151     }\r
152     \r
153     // reset scroll\r
154     window.scrollTo(0, 0);\r
155     \r
156     // add the new view to the parent element\r
157     if (view != null) {\r
158         this.currentView = view;\r
159         this.currentView.resetControlFocusStates();\r
160         this.viewParentElement.appendChild(this.currentView.rootElement);\r
161     }\r
162     \r
163     // update scrollbar\r
164     if (this.enableScrollBar) {\r
165         this.updateScrollbar();\r
166     }\r
167     \r
168     // focus the first focusable control\r
169     // a timer is used to prevent unwanted focus shift\r
170     setTimeout(function() { view.focusFirstControl(); }, 1);\r
171 }\r
172 \r
173 // Updates the scrollbar.\r
174 UIManager.prototype.updateScrollbar = function() {\r
175     if (this.enableScrollBar) {\r
176         // get current viewport and document position and dimensions\r
177         var scrollY = window.scrollY;\r
178         var viewportHeight = window.innerHeight;\r
179         var documentHeight = Math.max(document.documentElement.scrollHeight, document.height);\r
180         \r
181         // check if the scroll position or view has changed\r
182         if (this.scrollY != scrollY ||\r
183                 this.viewportHeight != viewportHeight ||\r
184                 this.documentHeight != documentHeight) {\r
185             // scroll position or view has changed\r
186             this.scrollY = scrollY;\r
187             this.viewportHeight = viewportHeight;\r
188             this.documentHeight = documentHeight;\r
189             \r
190             // update the scrollbar\r
191             this.scrollbar.update(scrollY, viewportHeight, documentHeight);\r
192             uiLogger.debug("Scrollbar updated");\r
193         }\r
194     }\r
195 }\r
196 \r
197 // Starts the view manager timer.\r
198 UIManager.prototype.startTimer = function() {\r
199     if (this.timerId == null) {\r
200         uiLogger.debug("UIManager timer started");\r
201         var self = this;\r
202         // setup the timer\r
203         this.timerId = setInterval(function() { self.onTimer(); }, this.TIMER_INTERVAL);\r
204     } else {\r
205         uiLogger.warn("UIManager timer already running");\r
206     }\r
207 }\r
208 \r
209 // Stops the view manager timer.\r
210 UIManager.prototype.stopTimer = function() {\r
211     if (this.timerId != null) {\r
212         // stop the timer\r
213         clearTimeout(this.timerId);\r
214         this.timerId = null;\r
215     } else {\r
216         uiLogger.warn("UIManager timer already stopped");\r
217     }\r
218 }\r
219 \r
220 // Timer callback function.\r
221 UIManager.prototype.onTimer = function() {\r
222     if (this.enableScrollBar) {\r
223         // make sure the scrollbar is up to date\r
224         this.updateScrollbar();\r
225     }\r
226 }\r
227 \r
228 // Displays a notification.\r
229 UIManager.prototype.showNotification = function(displayTime, type, text, progress) {\r
230     uiLogger.debug("UIManager.showNotification(" + displayTime + ", " + type + ", " + text + ", " + progress + ")");\r
231     // use the notification popup to show the notification\r
232     this.notificationPopup.showNotification(displayTime, type, text, progress);\r
233 }\r
234 \r
235 // Hides the currently displayed notification.\r
236 UIManager.prototype.hideNotification = function() {\r
237     uiLogger.debug("UIManager.hideNotification()");\r
238     // hide the notification popup\r
239     this.notificationPopup.hideNotification();\r
240 }\r