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