Symbian.org/WRTKit/UI/UIManager.js
changeset 0 54498df70f5d
equal deleted inserted replaced
-1:000000000000 0:54498df70f5d
       
     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 	uiLogger.debug("View set to " + view.id);
       
   154 	if ( this.viewSwitchTimer != null ) {
       
   155 		return;
       
   156 	}
       
   157 	if (this.viewSwitchAnimation && this.currentView != null) {
       
   158 		var self = this;
       
   159 		this.viewSwitchDivPos = 0;
       
   160 		this.viewSwitchTimer = setInterval(function(){
       
   161 			self.animateViewSwitch(view);
       
   162 		}, this.VIEW_SWITCH_TIMER_INTERVAL);
       
   163 	}
       
   164 	else {
       
   165 		this.doSetView(view);
       
   166 	}
       
   167 }
       
   168 
       
   169 UIManager.prototype.animateViewSwitch = function(view) {
       
   170 	if (this.viewSwitchDivPos + window.innerWidth < 0 ) {
       
   171 		clearTimeout(this.viewSwitchTimer);
       
   172 		var tmp = this.currentView;
       
   173 		this.doSetView(view);
       
   174 		tmp.rootElement.style.left = null;
       
   175 		this.viewSwitchTimer = null;
       
   176 	}
       
   177 	else {
       
   178 		this.viewSwitchDivPos -= 25;
       
   179 		this.currentView.rootElement.style.left = this.viewSwitchDivPos;
       
   180 	}
       
   181 }
       
   182 
       
   183 
       
   184 UIManager.prototype.doSetView = function(view) {
       
   185     // remove the current view from the parent element
       
   186     if (this.currentView != null) {
       
   187         this.viewParentElement.removeChild(this.currentView.rootElement);
       
   188     }
       
   189     
       
   190     // reset scroll
       
   191     window.scrollTo(0, 0);
       
   192     
       
   193     // add the new view to the parent element
       
   194     if (view != null) {
       
   195         this.currentView = view;
       
   196         this.currentView.resetControlFocusStates();
       
   197         this.viewParentElement.appendChild(this.currentView.rootElement);
       
   198     }
       
   199     
       
   200     // update scrollbar
       
   201     if (this.enableScrollBar) {
       
   202         this.updateScrollbar();
       
   203     }
       
   204     
       
   205     // focus the first focusable control
       
   206     // a timer is used to prevent unwanted focus shift
       
   207     setTimeout(function() { view.focusFirstControl(); }, 1);
       
   208 }
       
   209 
       
   210 // Updates the scrollbar.
       
   211 UIManager.prototype.updateScrollbar = function() {
       
   212     if (this.enableScrollBar) {
       
   213         // get current viewport and document position and dimensions
       
   214         var scrollY = window.scrollY;
       
   215         var viewportHeight = window.innerHeight;
       
   216         var documentHeight = Math.max(document.documentElement.scrollHeight, document.height);
       
   217         
       
   218         // check if the scroll position or view has changed
       
   219         if (this.scrollY != scrollY ||
       
   220                 this.viewportHeight != viewportHeight ||
       
   221                 this.documentHeight != documentHeight) {
       
   222             // scroll position or view has changed
       
   223             this.scrollY = scrollY;
       
   224             this.viewportHeight = viewportHeight;
       
   225             this.documentHeight = documentHeight;
       
   226             
       
   227             // update the scrollbar
       
   228             this.scrollbar.update(scrollY, viewportHeight, documentHeight);
       
   229             uiLogger.debug("Scrollbar updated");
       
   230         }
       
   231     }
       
   232 }
       
   233 
       
   234 // Starts the view manager timer.
       
   235 UIManager.prototype.startTimer = function() {
       
   236     if (this.timerId == null) {
       
   237         uiLogger.debug("UIManager timer started");
       
   238         var self = this;
       
   239         // setup the timer
       
   240         this.timerId = setInterval(function() { self.onTimer(); }, this.TIMER_INTERVAL);
       
   241     } else {
       
   242         uiLogger.warn("UIManager timer already running");
       
   243     }
       
   244 }
       
   245 
       
   246 // Stops the view manager timer.
       
   247 UIManager.prototype.stopTimer = function() {
       
   248     if (this.timerId != null) {
       
   249         // stop the timer
       
   250         clearTimeout(this.timerId);
       
   251         this.timerId = null;
       
   252     } else {
       
   253         uiLogger.warn("UIManager timer already stopped");
       
   254     }
       
   255 }
       
   256 
       
   257 // Timer callback function.
       
   258 UIManager.prototype.onTimer = function() {
       
   259     if (this.enableScrollBar) {
       
   260         // make sure the scrollbar is up to date
       
   261         this.updateScrollbar();
       
   262     }
       
   263 }
       
   264 
       
   265 // Displays a notification.
       
   266 UIManager.prototype.showNotification = function(displayTime, type, text, progress) {
       
   267     uiLogger.debug("UIManager.showNotification(" + displayTime + ", " + type + ", " + text + ", " + progress + ")");
       
   268     // use the notification popup to show the notification
       
   269     this.notificationPopup.showNotification(displayTime, type, text, progress);
       
   270 }
       
   271 
       
   272 // Hides the currently displayed notification.
       
   273 UIManager.prototype.hideNotification = function() {
       
   274     uiLogger.debug("UIManager.hideNotification()");
       
   275     // hide the notification popup
       
   276     this.notificationPopup.hideNotification();
       
   277 }