Changed zouba directory heirarchy.
[ptas] / zouba / wrt / WRTKit / UI / ListView.js
diff --git a/zouba/wrt/WRTKit/UI/ListView.js b/zouba/wrt/WRTKit/UI/ListView.js
new file mode 100644 (file)
index 0000000..54d4c8e
--- /dev/null
@@ -0,0 +1,189 @@
+/*\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 ListView class implements a vertical list view that hosts controls\r
+// as child components.\r
+\r
+// Constructor.\r
+function ListView(id, caption) {\r
+    if (id != UI_NO_INIT_ID) {\r
+        this.init(id, caption);\r
+    }\r
+}\r
+\r
+// ListView inherits from View.\r
+ListView.prototype = new View(UI_NO_INIT_ID);\r
+\r
+// The caption of this view; null if none.\r
+ListView.prototype.caption = null;\r
+\r
+// The caption element of this view.\r
+ListView.prototype.captionElement = null;\r
+\r
+// The caption text element of this view.\r
+ListView.prototype.captionTextElement = null;\r
+\r
+// Root HTML element for controls.\r
+ListView.prototype.listElement = null;\r
+\r
+// List of controls in the view.\r
+ListView.prototype.controls = null;\r
+\r
+// Initializer for ListView.\r
+ListView.prototype.init = function(id, caption) {\r
+    uiLogger.debug("ListView.init(" + id + ", " + caption + ")");\r
+    \r
+    // call superclass initializer\r
+    View.prototype.init.call(this, id);\r
+    \r
+    // init control array\r
+    this.controls = [];\r
+    \r
+    // set style class name for root element\r
+    this.rootElement.className = "ListView";\r
+    \r
+    // create caption and caption text elements\r
+    this.captionElement = document.createElement("div");\r
+    this.captionElement.className = "ListViewCaption";\r
+    this.captionTextElement = document.createElement("div");\r
+    this.captionTextElement.className = "ListViewCaptionText";\r
+    this.captionElement.appendChild(this.captionTextElement);\r
+    this.rootElement.appendChild(this.captionElement);\r
+    \r
+    // create root element for controls and add to the view root element\r
+    this.listElement = document.createElement("div");\r
+    this.listElement.className = "ListViewControlList";\r
+    this.rootElement.appendChild(this.listElement);\r
+    \r
+    // set the caption\r
+    this.setCaption(caption);\r
+}\r
+\r
+// Returns the caption; null if none.\r
+ListView.prototype.getCaption = function() {\r
+    return this.caption;\r
+}\r
+\r
+// Sets the caption; null if none.\r
+ListView.prototype.setCaption = function(caption) {\r
+    uiLogger.debug("ListView.setCaption(" + caption + ")");\r
+    \r
+    // set the display style\r
+    this.captionElement.style.display = (caption == null) ? "none" : "block";\r
+    \r
+    // set the caption\r
+    this.caption = caption;\r
+    this.captionTextElement.innerHTML = (caption == null) ? "" : caption;\r
+}\r
+\r
+// Returns an array of controls in the view.\r
+ListView.prototype.getControls = function() {\r
+    return this.controls;\r
+}\r
+\r
+// Adds a control to the view.\r
+ListView.prototype.addControl = function(control) {\r
+    uiLogger.debug("ListView.addControl(" + control + ")");\r
+    \r
+    // add the control to the controls array and attach it to the list element\r
+    this.controls.push(control);\r
+    this.listElement.appendChild(control.rootElement);\r
+    control.view = this;\r
+}\r
+\r
+// Inserts a control to the view before the specified control.\r
+ListView.prototype.insertControl = function(control, beforeControl) {\r
+    uiLogger.debug("ListView.insertControl(" + control + ", " + beforeControl + ")");\r
+    \r
+    // iterate through current controls\r
+    for (var i = 0; i < this.controls.length; i++) {\r
+        // is this the control we should insert before?\r
+        if (this.controls[i] == beforeControl) {\r
+            // we found the control to insert before - insert here and connect to list element\r
+            this.controls.splice(i, 0, control);\r
+            this.listElement.insertBefore(control.rootElement, beforeControl.rootElement);\r
+            control.view = this;\r
+            return;\r
+        }\r
+    }\r
+    \r
+    // the control wasn't found so we'll add it last\r
+    this.addControl(control);\r
+}\r
+\r
+// Removes a control from the view.\r
+ListView.prototype.removeControl = function(control) {\r
+    uiLogger.debug("ListView.removeControl(" + control + ")");\r
+    \r
+    // iterate through current controls\r
+    for (var i = 0; i < this.controls.length; i++) {\r
+        // is this the control we should remove?\r
+        if (this.controls[i] == control) {\r
+            // we found the control to remove - remove it from the list element\r
+            this.controls.splice(i, 1);\r
+            this.listElement.removeChild(control.rootElement);\r
+            control.view = null;\r
+        }\r
+    }\r
+}\r
+\r
+// Attempts to focus the first focusable control.\r
+ListView.prototype.focusFirstControl = function() {\r
+    uiLogger.debug("ListView.focusFirstControl()");\r
+    for (var i = 0; i < this.controls.length; i++) {\r
+        // is this control focusable?\r
+        var control = this.controls[i];\r
+        if (control.isFocusable()) {\r
+            control.setFocused(true);\r
+            break;\r
+        }\r
+    }\r
+}\r
+\r
+// Attempts to reset all control focus states.\r
+// Override in subclasses as required.\r
+ListView.prototype.resetControlFocusStates = function() {\r
+    uiLogger.debug("ListView.resetControlFocusStates()");\r
+    for (var i = 0; i < this.controls.length; i++) {\r
+        this.controls[i].resetFocusState();\r
+    }\r
+}\r