Changed zouba directory heirarchy.
[ptas] / zouba / wrt / WRTKit / UI / NotificationPopup.js
diff --git a/zouba/wrt/WRTKit/UI/NotificationPopup.js b/zouba/wrt/WRTKit/UI/NotificationPopup.js
new file mode 100644 (file)
index 0000000..b69f3d0
--- /dev/null
@@ -0,0 +1,330 @@
+/*\r
+� Copyright 2008 Nokia Corporation. All rights reserved.\r
+\r
+IMPORTANT:  The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia\r
+Corporation (�Nokia�) in consideration of your agreement to the following terms. Your use, installation\r
+and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If\r
+you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example\r
+Widget files.\r
+\r
+In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia\r
+grants you a personal, non-exclusive license, under Nokia�s copyrights in the WRTKit and Example\r
+Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML,\r
+CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60\r
+Widgets.\r
+\r
+If you redistribute the WRTKit and Example files, you must retain this entire notice in all such\r
+redistributions of the WRTKit and Example files.\r
+\r
+You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products\r
+that include the WRTKit and Example files without the prior written explicit agreement with Nokia.\r
+Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by\r
+Nokia herein, including but not limited to any patent rights that may be infringed by your products that\r
+incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files\r
+may be incorporated.\r
+\r
+The WRTKit and Example files are provided on an "AS IS" basis.  NOKIA MAKES NO\r
+WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED\r
+WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A\r
+PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION\r
+ALONE OR IN COMBINATION WITH YOUR PRODUCTS.\r
+\r
+IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR\r
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
+SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
+INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR\r
+DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY\r
+OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE,\r
+EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+*/\r
+\r
+///////////////////////////////////////////////////////////////////////////////\r
+// The NotificationPopup class handles the display of notifications such as\r
+// warnings, information messages and progress indication.\r
+\r
+// Constructor.\r
+function NotificationPopup() {\r
+    // create notification popup\r
+    this.containerElement = document.createElement("div");\r
+    this.containerElement.className = "NotificationPopupContainer";\r
+    this.popupElement = document.createElement("div");\r
+    this.popupElement.className = "NotificationPopup";\r
+    this.typeIndicatorElement = document.createElement("div");\r
+    this.typeIndicatorElement.className = "NotificationPopupTypeIndicator";\r
+    this.textElement = document.createElement("div");\r
+    this.textElement.className = "NotificationPopupText";\r
+    this.progressBarElement = document.createElement("div");\r
+    this.progressBarElement.className = "NotificationPopupProgressBar";\r
+    \r
+    // assemble popup\r
+    this.popupElement.appendChild(this.typeIndicatorElement);\r
+    this.popupElement.appendChild(this.textElement);\r
+    this.popupElement.appendChild(this.progressBarElement);\r
+    this.containerElement.appendChild(this.popupElement);\r
+    \r
+    // create progress bar image element and initialize it\r
+    this.progressBarImageElement = document.createElement("img");\r
+    var self = this;\r
+    this.progressBarImageElement.addEventListener("load", function() { self.progressBarImageLoadingCompleted(); }, false);\r
+    this.progressBarImageElement.setAttribute("alt", "");\r
+    this.progressBarImageURL = this.getProgressBarImage(0);\r
+    this.progressBarImageElement.src = this.progressBarImageURL;\r
+    this.progressBarElement.appendChild(this.progressBarImageElement);\r
+    \r
+    // init the popup to be fully down\r
+    this.popupElement.style.top = "100%";\r
+    \r
+    // set default popup contents\r
+    this.setPopupContents(null, null, null);\r
+}\r
+\r
+// Notification container element.\r
+NotificationPopup.prototype.containerElement = null;\r
+\r
+// Notification popup element.\r
+NotificationPopup.prototype.popupElement = null;\r
+\r
+// Type indicator element.\r
+NotificationPopup.prototype.typeIndicatorElement = null;\r
+\r
+// Notification text element.\r
+NotificationPopup.prototype.textElement = null;\r
+\r
+// Progress bar element.\r
+NotificationPopup.prototype.progressBarElement = null;\r
+\r
+// Progress bar image element.\r
+NotificationPopup.prototype.progressBarImageElement = null;\r
+\r
+// Progress bar image URL.\r
+NotificationPopup.prototype.progressBarImageURL = null;\r
+\r
+// Has the progress bar image been loaded?\r
+NotificationPopup.prototype.progressBarImageLoaded = false;\r
+\r
+// Flag that tracks whether we're in the middle of starting to\r
+// show a notification popup.\r
+NotificationPopup.prototype.processingShowNotification = false;\r
+\r
+// Notification popup position (0 = hidden, 1 = showing).\r
+NotificationPopup.prototype.popupPosition = 0;\r
+\r
+// Interval for timer ticks (in milliseconds)\r
+NotificationPopup.prototype.ANIM_TIMER_INTERVAL = 25;\r
+\r
+// Animation timer identifier or null if no active timer.\r
+NotificationPopup.prototype.animTimerId = null;\r
+\r
+// Time in milliseconds for the popup animation to complete\r
+NotificationPopup.prototype.ANIM_TIME = 300;\r
+\r
+// Flag that determines the behavior of showNotification(). If set to true\r
+// the popup will snap open when showNotification() is called instead of\r
+// animation.\r
+NotificationPopup.prototype.SHOW_SNAPS_OPEN = true;\r
+\r
+// Animation direction (0 = no movement, -1 hiding, +1 = showing).\r
+NotificationPopup.prototype.animDir = 0;\r
+\r
+// Auto-hide timer identifier or null if no active timer.\r
+NotificationPopup.prototype.autoHideTimerId = null;\r
+\r
+// The commanded display time.\r
+NotificationPopup.prototype.displayTime = -1;\r
+\r
+// Displays a notification.\r
+NotificationPopup.prototype.showNotification = function(displayTime, type, text, progress) {\r
+    uiLogger.debug("NotificationPopup.showNotification(" + displayTime + ", " + type + ", " + text + ", " + progress + ")");\r
+    \r
+    // mark that showNotification() has been called and that we are in\r
+    // the middle of starting to show the notification popup\r
+    this.processingShowNotification = true;\r
+    \r
+    // remember the display time\r
+    this.displayTime = displayTime;\r
+    \r
+    // attach the popup to the document if not attached\r
+    if (this.containerElement.parentNode == null) {\r
+        document.body.appendChild(this.containerElement);\r
+        uiLogger.debug("Notification popup attached to document");\r
+    }\r
+    \r
+    // set popup contents and update style\r
+    this.setPopupContents(type, text, progress);\r
+    \r
+    // if the progress image is loaded then we can complete the showing\r
+    // of the notification popup immediately - otherwise the image loaded\r
+    // allback will complete the process.\r
+    if (this.progressBarImageLoaded) {\r
+        this.completeShowNotification();\r
+    }\r
+}\r
+\r
+// Completes displaying of a notification.\r
+// Note: Used internally - don't call this directly.\r
+NotificationPopup.prototype.completeShowNotification = function() {\r
+    uiLogger.debug("NotificationPopup.completeShowNotification()");\r
+    \r
+    // animation direction is +1 for showing the popup\r
+    if (this.popupPosition != 1) {\r
+        if (this.SHOW_SNAPS_OPEN) {\r
+            if (this.popupPosition == 0) {\r
+                this.popupPosition = 1;\r
+            }\r
+        }\r
+        this.animatePopup(1);\r
+    }\r
+    \r
+    // setup auto hiding if a display time is specified\r
+    if (this.displayTime > 0) {\r
+        // stop any existing timer\r
+        if (this.autoHideTimerId != null) {\r
+            clearTimeout(this.autoHideTimerId);\r
+            uiLogger.debug("Auto hide timer stopped");\r
+        }\r
+        // set timer to hide notification\r
+        var self = this;\r
+        this.autoHideTimerId = setTimeout(function() {\r
+                                              if (self.displayTime > 0) {\r
+                                                  self.hideNotification();\r
+                                              }\r
+                                          }, this.ANIM_TIME + this.displayTime);\r
+        uiLogger.debug("Auto hide timer started");\r
+    }\r
+    \r
+    // mark us as no longer processing a show notification call\r
+    this.processingShowNotification = false;\r
+}\r
+\r
+// Hides the currently displayed notification.\r
+NotificationPopup.prototype.hideNotification = function() {\r
+    uiLogger.debug("NotificationPopup.hideNotification()");\r
+    // mark us as no longer processing a show notification call\r
+    this.processingShowNotification = false;\r
+    \r
+    // animation direction is -1 for hiding the popup\r
+    if (this.popupPosition != 0) {\r
+        this.animatePopup(-1);\r
+    }\r
+    \r
+    // stop auto hide timer if one is set\r
+    if (this.autoHideTimerId != null) {\r
+        clearTimeout(this.autoHideTimerId);\r
+        this.autoHideTimerId = null;\r
+        uiLogger.debug("Auto hide timer stopped");\r
+    }\r
+}\r
+\r
+// Starts animation of the popup (1 to show, -1 to hide).\r
+NotificationPopup.prototype.animatePopup = function(direction) {\r
+    uiLogger.debug("NotificationPopup.animatePopup(" + direction + ")");\r
+    // set the direction and star the animation timer\r
+    this.animDir = direction;\r
+    if (this.animTimerId == null) {\r
+        var self = this;\r
+        this.animTimerId = setInterval(function() { self.animate(); }, this.ANIM_TIMER_INTERVAL);\r
+        uiLogger.debug("Notification popup animation started");\r
+    }\r
+}\r
+\r
+// Callback for animation timer.\r
+NotificationPopup.prototype.animate = function() {\r
+    // calculate new popup position and clamp\r
+    var animStep = (this.ANIM_TIMER_INTERVAL / this.ANIM_TIME) * this.animDir;\r
+    var newPos = this.popupPosition + animStep;\r
+    if (newPos < 0) {\r
+        newPos = 0;\r
+    } else if (newPos > 1) {\r
+        newPos = 1;\r
+    }\r
+    \r
+    // set the new position to the popup element\r
+    this.popupPosition = newPos;\r
+    this.popupElement.style.top = (100 - Math.round(this.popupPosition * 100)) + "%";\r
+    \r
+    // have we reached the end of the animation?\r
+    if (newPos == 0 || newPos == 1) {\r
+        // reset animation direction\r
+        this.animDir = 0;\r
+        \r
+        // remove the popup from the body if its hidden\r
+        if (newPos == 0) {\r
+            document.body.removeChild(this.containerElement);\r
+            uiLogger.debug("Notification popup detached from document");\r
+        }\r
+        \r
+        // stop timer\r
+        clearTimeout(this.animTimerId);\r
+        this.animTimerId = null;\r
+        uiLogger.debug("Notification popup animation stopped");\r
+    }\r
+}\r
+\r
+// Returns a URL for the progress bar image to use for the specified progress.\r
+NotificationPopup.prototype.getProgressBarImage = function(progress) {\r
+    // path for progress bar images\r
+    var progressBarImagePath = "WRTKit/Resources/";\r
+    \r
+    if (progress < 0) {\r
+        // unknown progress\r
+        return progressBarImagePath + "ProgressBarUnknown.gif";\r
+    } else {\r
+        // known progress (should be between 0 and 1)\r
+        var progPct = Math.round(progress * 10) * 10;\r
+        if (progPct < 0) {\r
+            progPct = 0;\r
+        } else if (progPct > 100) {\r
+            progPct = 100;\r
+        }\r
+        return progressBarImagePath + "ProgressBar" + progPct + ".png";\r
+    }\r
+}\r
+\r
+// Sets the contents of the popup.\r
+NotificationPopup.prototype.setPopupContents = function(type, text, progress) {\r
+    uiLogger.debug("NotificationPopup.setPopupContents(" + type + ", " + text + ", " + progress + ")");\r
+    \r
+    // figure out notification type style name\r
+    var typeName = (type == null) ? "none" : type.toLowerCase();\r
+    typeName = typeName.charAt(0).toUpperCase() + typeName.substring(1);\r
+    \r
+    // set type element class names\r
+    this.typeIndicatorElement.className = "NotificationPopupTypeIndicator NotificationPopupTypeIndicator" + typeName;\r
+    \r
+    // set notification text\r
+    this.textElement.innerHTML = (text == null) ? "" : text;\r
+    \r
+    // set progress\r
+    this.progressBarElement.style.display = (progress == null) ? "none" : "block";\r
+    if (progress != null) {\r
+        var imgURL = this.getProgressBarImage(progress);\r
+        if (imgURL != this.progressBarImageURL) {\r
+            // load new image\r
+            this.progressBarImageLoaded = false;\r
+            this.progressBarImageURL = imgURL;\r
+            this.progressBarImageElement.src = imgURL;\r
+        } else {\r
+            // the correct image is already loaded\r
+            this.progressBarImageLoaded = true;\r
+        }\r
+    } else {\r
+        // there is no progress bar so there is no need\r
+        // to load any progress bar image\r
+        this.progressBarImageLoaded = true;\r
+    }\r
+}\r
+\r
+// Callback for notifying the object that its progress bar image completed loading.\r
+NotificationPopup.prototype.progressBarImageLoadingCompleted = function() {\r
+    uiLogger.debug("NotificationPopup.progressBarImageLoadingCompleted()");\r
+    \r
+    // mark the progress bar image as loaded\r
+    this.progressBarImageLoaded = true;\r
+    \r
+    // complete the process of displaying the notification popup\r
+    // if it has been commanded but not yet completed\r
+    if (this.processingShowNotification) {\r
+        this.completeShowNotification();\r
+    }\r
+}\r