Wikipedia/WRTKit/UI/UIManager.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     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 //	log("UIManager:doSetView: view=" + getViewRep(view) + ", current:" + getViewRep(this.currentView) + ", view.previous=" + getViewRep(view.previousView));
       
   186     // remove the current view from the parent element
       
   187     if (this.currentView != null) {
       
   188         this.viewParentElement.removeChild(this.currentView.rootElement);
       
   189     }
       
   190     
       
   191     // reset scroll
       
   192     window.scrollTo(0, 0);
       
   193     
       
   194     // add the new view to the parent element
       
   195     if (view != null) {
       
   196         this.currentView = view;
       
   197         this.currentView.resetControlFocusStates();
       
   198         this.viewParentElement.appendChild(this.currentView.rootElement);
       
   199     }
       
   200     
       
   201     // update scrollbar
       
   202     if (this.enableScrollBar) {
       
   203         this.updateScrollbar();
       
   204     }
       
   205     
       
   206 //    // focus the first focusable control
       
   207 //    // a timer is used to prevent unwanted focus shift
       
   208     setTimeout(function() { view.focusFirstControl(); }, 1);
       
   209 }
       
   210 
       
   211 // Updates the scrollbar.
       
   212 UIManager.prototype.updateScrollbar = function() {
       
   213     if (this.enableScrollBar) {
       
   214         // get current viewport and document position and dimensions
       
   215         var scrollY = window.scrollY;
       
   216         var viewportHeight = window.innerHeight;
       
   217         var documentHeight = Math.max(document.documentElement.scrollHeight, document.height);
       
   218         
       
   219         // check if the scroll position or view has changed
       
   220         if (this.scrollY != scrollY ||
       
   221                 this.viewportHeight != viewportHeight ||
       
   222                 this.documentHeight != documentHeight) {
       
   223             // scroll position or view has changed
       
   224             this.scrollY = scrollY;
       
   225             this.viewportHeight = viewportHeight;
       
   226             this.documentHeight = documentHeight;
       
   227             
       
   228             // update the scrollbar
       
   229             this.scrollbar.update(scrollY, viewportHeight, documentHeight);
       
   230             uiLogger.debug("Scrollbar updated");
       
   231         }
       
   232     }
       
   233 }
       
   234 
       
   235 // Starts the view manager timer.
       
   236 UIManager.prototype.startTimer = function() {
       
   237     if (this.timerId == null) {
       
   238         uiLogger.debug("UIManager timer started");
       
   239         var self = this;
       
   240         // setup the timer
       
   241         this.timerId = setInterval(function() { self.onTimer(); }, this.TIMER_INTERVAL);
       
   242     } else {
       
   243         uiLogger.warn("UIManager timer already running");
       
   244     }
       
   245 }
       
   246 
       
   247 // Stops the view manager timer.
       
   248 UIManager.prototype.stopTimer = function() {
       
   249     if (this.timerId != null) {
       
   250         // stop the timer
       
   251         clearTimeout(this.timerId);
       
   252         this.timerId = null;
       
   253     } else {
       
   254         uiLogger.warn("UIManager timer already stopped");
       
   255     }
       
   256 }
       
   257 
       
   258 // Timer callback function.
       
   259 UIManager.prototype.onTimer = function() {
       
   260     if (this.enableScrollBar) {
       
   261         // make sure the scrollbar is up to date
       
   262         this.updateScrollbar();
       
   263     }
       
   264 }
       
   265 
       
   266 // Displays a notification.
       
   267 UIManager.prototype.showNotification = function(displayTime, type, text, progress) {
       
   268     uiLogger.debug("UIManager.showNotification(" + displayTime + ", " + type + ", " + text + ", " + progress + ")");
       
   269     // use the notification popup to show the notification
       
   270     this.notificationPopup.showNotification(displayTime, type, text, progress);
       
   271 }
       
   272 
       
   273 // Hides the currently displayed notification.
       
   274 UIManager.prototype.hideNotification = function() {
       
   275     uiLogger.debug("UIManager.hideNotification()");
       
   276     // hide the notification popup
       
   277     this.notificationPopup.hideNotification();
       
   278 }