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