org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/UIManager.js
changeset 309 c01f5ab28a11
parent 308 c521df56b15d
child 310 e9484be98cfe
equal deleted inserted replaced
308:c521df56b15d 309:c01f5ab28a11
     1 /**
       
     2  * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  * 
       
    12  * Contributors:
       
    13  * 
       
    14  * Description:
       
    15  * 
       
    16  */
       
    17 
       
    18 ///////////////////////////////////////////////////////////////////////////////
       
    19 // The UI manager manages a set of views and other user interface elements.
       
    20 
       
    21 // Constructor.
       
    22 function UIManager(viewParentElement, scrollbarParentElement, enableScrollBar, delayInit) {    
       
    23     uiLogger.debug("UIManager(" + viewParentElement + ", " + scrollbarParentElement + ")");
       
    24     if (delayInit == null) {
       
    25         this.init(viewParentElement, enableScrollBar, scrollbarParentElement);
       
    26     }
       
    27 }
       
    28 
       
    29 // Parent element for views.
       
    30 UIManager.prototype.viewParentElement = null;
       
    31 
       
    32 // Parent element for scrollbar.
       
    33 UIManager.prototype.scrollbarParentElement = null;
       
    34 
       
    35 // The currently displayed view.
       
    36 UIManager.prototype.currentView = null;
       
    37 
       
    38 // Reference to the scrollbar.
       
    39 UIManager.prototype.scrollbar = null;
       
    40 
       
    41 // Current scroll Y position.
       
    42 UIManager.prototype.scrollY = -1;
       
    43 
       
    44 // Current viewport height.
       
    45 UIManager.prototype.viewportHeight = -1;
       
    46 
       
    47 // Current document height.
       
    48 UIManager.prototype.documentHeight = -1;
       
    49 
       
    50 // Timer identifier or null if no active timer.
       
    51 UIManager.prototype.timerId = null;
       
    52 
       
    53 // Interval for timer ticks for the UI manager timer (in milliseconds)
       
    54 UIManager.prototype.TIMER_INTERVAL = 250;
       
    55 
       
    56 // Reference to the notification popup used to displays notifications.
       
    57 UIManager.prototype.notificationPopup = null;
       
    58 
       
    59 // is scrollbar enabled
       
    60 UIManager.prototype.enableScrollBar = null;
       
    61 
       
    62 // init function
       
    63 UIManager.prototype.init = function(viewParentElement, enableScrollBar, scrollbarParentElement) {
       
    64     this.enableScrollBar = enableScrollBar;
       
    65     
       
    66     // parent element for views
       
    67     if (viewParentElement == null) {
       
    68         // create a parent for views
       
    69         this.viewParentElement = document.createElement("div");
       
    70         this.viewParentElement.className = "ViewContainer";
       
    71         document.body.appendChild(this.viewParentElement);
       
    72     }
       
    73     else {
       
    74         this.viewParentElement = viewParentElement;
       
    75     }
       
    76     
       
    77     // parent element for scrollbar
       
    78     if (enableScrollBar) {
       
    79         if (scrollbarParentElement == null) {
       
    80             // create a parent for the scrollbar
       
    81             this.scrollbarParentElement = document.createElement("div");
       
    82             this.scrollbarParentElement.className = "DocumentScrollbarContainer";
       
    83             document.body.appendChild(this.scrollbarParentElement);
       
    84         }
       
    85         else {
       
    86             this.scrollbarParentElement = scrollbarParentElement;
       
    87         }
       
    88     }
       
    89     
       
    90     // currently selected view
       
    91     this.currentView = null;
       
    92     
       
    93     // create the notification popup
       
    94     // the notification popup adds itself as a child element to the document body
       
    95     this.notificationPopup = new NotificationPopup();
       
    96     
       
    97     // create scrollbar
       
    98     if (enableScrollBar) {
       
    99         this.scrollbar = new Scrollbar(this.scrollbarParentElement);
       
   100     }
       
   101     
       
   102     // setup scrollbar tracking
       
   103     var self = this;
       
   104     this.startTimer();
       
   105     if (enableScrollBar) {
       
   106         window.addEventListener("resize", function(){
       
   107             self.updateScrollbar();
       
   108         }, false);
       
   109         window.addEventListener("scroll", function(){
       
   110             self.updateScrollbar();
       
   111         }, false);
       
   112     }
       
   113 };
       
   114 
       
   115 // Returns the current view.
       
   116 UIManager.prototype.getView = function() {
       
   117     return this.currentView;
       
   118 };
       
   119 
       
   120 // Switches to the specified view.
       
   121 UIManager.prototype.setView = function(view) {
       
   122     uiLogger.debug("View set to " + view.id);
       
   123     
       
   124     // remove the current view from the parent element
       
   125     if (this.currentView != null) {
       
   126         this.viewParentElement.removeChild(this.currentView.rootElement);
       
   127     }
       
   128     
       
   129     // reset scroll
       
   130     window.scrollTo(0, 0);
       
   131     
       
   132     // add the new view to the parent element
       
   133     if (view != null) {
       
   134         this.currentView = view;
       
   135         this.currentView.resetControlFocusStates();
       
   136         this.viewParentElement.appendChild(this.currentView.rootElement);
       
   137     }
       
   138     
       
   139     // update scrollbar
       
   140     if (this.enableScrollBar) {
       
   141         this.updateScrollbar();
       
   142     }
       
   143     
       
   144     // focus the first focusable control
       
   145     // a timer is used to prevent unwanted focus shift
       
   146     setTimeout(function() { view.focusFirstControl(); }, 1);
       
   147 };
       
   148 
       
   149 // Updates the scrollbar.
       
   150 UIManager.prototype.updateScrollbar = function() {
       
   151     if (this.enableScrollBar) {
       
   152         // get current viewport and document position and dimensions
       
   153         var scrollY = window.scrollY;
       
   154         var viewportHeight = window.innerHeight;
       
   155         var documentHeight = Math.max(document.documentElement.scrollHeight, document.height);
       
   156         
       
   157         // check if the scroll position or view has changed
       
   158         if (this.scrollY != scrollY ||
       
   159                 this.viewportHeight != viewportHeight ||
       
   160                 this.documentHeight != documentHeight) {
       
   161             // scroll position or view has changed
       
   162             this.scrollY = scrollY;
       
   163             this.viewportHeight = viewportHeight;
       
   164             this.documentHeight = documentHeight;
       
   165             
       
   166             // update the scrollbar
       
   167             this.scrollbar.update(scrollY, viewportHeight, documentHeight);
       
   168             uiLogger.debug("Scrollbar updated");
       
   169         }
       
   170     }
       
   171 };
       
   172 
       
   173 // Starts the view manager timer.
       
   174 UIManager.prototype.startTimer = function() {
       
   175     if (this.timerId == null) {
       
   176         uiLogger.debug("UIManager timer started");
       
   177         var self = this;
       
   178         // setup the timer
       
   179         this.timerId = setInterval(function() { self.onTimer(); }, this.TIMER_INTERVAL);
       
   180     } else {
       
   181         uiLogger.warn("UIManager timer already running");
       
   182     }
       
   183 };
       
   184 
       
   185 // Stops the view manager timer.
       
   186 UIManager.prototype.stopTimer = function() {
       
   187     if (this.timerId != null) {
       
   188         // stop the timer
       
   189         clearTimeout(this.timerId);
       
   190         this.timerId = null;
       
   191     } else {
       
   192         uiLogger.warn("UIManager timer already stopped");
       
   193     }
       
   194 };
       
   195 
       
   196 // Timer callback function.
       
   197 UIManager.prototype.onTimer = function() {
       
   198     if (this.enableScrollBar) {
       
   199         // make sure the scrollbar is up to date
       
   200         this.updateScrollbar();
       
   201     }
       
   202 };
       
   203 
       
   204 // Displays a notification.
       
   205 UIManager.prototype.showNotification = function(displayTime, type, text, progress) {
       
   206     uiLogger.debug("UIManager.showNotification(" + displayTime + ", " + type + ", " + text + ", " + progress + ")");
       
   207     // use the notification popup to show the notification
       
   208     this.notificationPopup.showNotification(displayTime, type, text, progress);
       
   209 };
       
   210 
       
   211 // Hides the currently displayed notification.
       
   212 UIManager.prototype.hideNotification = function() {
       
   213     uiLogger.debug("UIManager.hideNotification()");
       
   214     // hide the notification popup
       
   215     this.notificationPopup.hideNotification();
       
   216 };