Changed zouba directory heirarchy.
[ptas] / zouba / wrt / WRTKit / UI / SelectionMenu.js
diff --git a/zouba/wrt/WRTKit/UI/SelectionMenu.js b/zouba/wrt/WRTKit/UI/SelectionMenu.js
new file mode 100644 (file)
index 0000000..ec63695
--- /dev/null
@@ -0,0 +1,204 @@
+/*\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 SelectionMenu class implements a single or multi selection control\r
+// that lets users select one or more options from a menu.\r
+\r
+// Constructor.\r
+function SelectionMenu(id, caption, options, multipleSelection, selected) {\r
+    if (id != UI_NO_INIT_ID) {\r
+        this.init(id, caption, options, multipleSelection, selected);\r
+    }\r
+}\r
+\r
+// SelectionMenu inherits from SelectionControl.\r
+SelectionMenu.prototype = new SelectionControl(UI_NO_INIT_ID);\r
+\r
+// Reference to the peer HTML element.\r
+SelectionControl.prototype.peerElement = null;\r
+\r
+// Array for tracking option elements.\r
+SelectionMenu.prototype.optionElements = null;\r
+\r
+// Initializer - called from constructor.\r
+SelectionMenu.prototype.init = function(id, caption, options, multipleSelection, selected) {\r
+    uiLogger.debug("SelectionMenu.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");\r
+    \r
+    // call superclass initializer\r
+    SelectionControl.prototype.init.call(this, id, caption, options, multipleSelection, selected);\r
+    \r
+    // create the control\r
+    this.peerElement = document.createElement("select");\r
+    this.peerElement.multiple = multipleSelection;\r
+    this.controlElement.appendChild(this.peerElement);\r
+    \r
+    // init option elements array\r
+    this.optionElements = [];\r
+    \r
+    // update the option elements to match the options in this control\r
+    this.updateOptionElements();\r
+    \r
+    // bind event listeners\r
+    var self = this;\r
+    this.peerElement.addEventListener("focus", function() { self.focusStateChanged(true); }, false);\r
+    this.peerElement.addEventListener("blur", function() { self.focusStateChanged(false); }, false);\r
+    this.peerElement.addEventListener("mouseover", function() { self.hoverStateChanged(true); }, false);\r
+    this.peerElement.addEventListener("mouseout", function() { self.hoverStateChanged(false); }, false);\r
+    this.peerElement.addEventListener("change", function() { self.selectionChanged(); }, false);\r
+}\r
+\r
+// Returns the enabled state.\r
+SelectionMenu.prototype.isEnabled = function() {\r
+    return !this.peerElement.disabled;\r
+}\r
+\r
+// Sets the enabled state.\r
+SelectionMenu.prototype.setEnabled = function(enabled) {\r
+    uiLogger.debug("SelectionMenu.setEnabled(" + enabled + ")");\r
+    this.peerElement.disabled = !enabled;\r
+}\r
+\r
+// Sets the focused state for the control.\r
+// Note: This may not always succeed.\r
+SelectionMenu.prototype.setFocused = function(focused) {\r
+    uiLogger.debug("SelectionMenu.setFocused(" + focused + ")");\r
+    if (focused) {\r
+        this.peerElement.focus();\r
+    } else {\r
+        this.peerElement.blur();\r
+    }\r
+}\r
+\r
+// Sets the currently selected options. Pass a single option in a single selection\r
+// control or an array of selected controls in a multiple selection control. To\r
+// deselect all options pass null in a single selection control and an empty array\r
+// in a multiple selection control.\r
+SelectionMenu.prototype.setSelected = function(selected) {\r
+    // call superclass setSelected()\r
+    SelectionControl.prototype.setSelected.call(this, selected);\r
+    \r
+    // iterate through the options and set the selected state\r
+    // on the corresponding option element\r
+    for (var i = 0; i < this.options.length; i++) {\r
+        this.optionElements[i].selected = this.isSelected(this.options[i]);\r
+    }\r
+}\r
+\r
+// Sets the options in the control.\r
+SelectionMenu.prototype.setOptions = function(options) {\r
+    // call superclass setOptions()\r
+    SelectionControl.prototype.setOptions.call(this, options);\r
+    this.updateOptionElements();\r
+}\r
+\r
+// Updates the option elements for the peer select element.\r
+SelectionMenu.prototype.updateOptionElements = function() {\r
+    // start by removing all current options from the select element\r
+    while (this.peerElement.firstChild != null) {\r
+        this.peerElement.removeChild(this.peerElement.firstChild);\r
+    }\r
+    \r
+    // iterate through the options and add (and possibly create) a\r
+    // properly configured option element for each option\r
+    for (var i = 0; i < this.options.length; i++) {\r
+        // do we need to create a new option element?\r
+        if (i == this.optionElements.length) {\r
+            this.optionElements.push(document.createElement("option"));\r
+        }\r
+        \r
+        // get the option and option element we're working on\r
+        var option = this.options[i];\r
+        var optionElement = this.optionElements[i];\r
+        \r
+        // set the state for this option element and add it to the\r
+        // peer select element\r
+        optionElement.text = option.text;\r
+        optionElement.selected = this.isSelected(option);\r
+        this.peerElement.appendChild(optionElement);\r
+    }\r
+    \r
+    // update the style\r
+    this.updateStyleFromState();    \r
+}\r
+\r
+// Callback for selection change events.\r
+SelectionMenu.prototype.selectionChanged = function() {\r
+    uiLogger.debug("SelectionControl.selectionChanged()");\r
+    \r
+    // update the selected options array or reference\r
+    this.selected = (this.multipleSelection) ? [] : null;\r
+    for (var i = 0; i < this.options.length; i++) {\r
+        if (this.optionElements[i].selected) {\r
+            if (this.multipleSelection) {\r
+                this.selected.push(this.options[i]);\r
+            } else {\r
+                this.selected = this.options[i];\r
+                break;\r
+            }\r
+        }\r
+    }\r
+    \r
+    // notify event listeners\r
+    this.fireEvent(this.createEvent("SelectionChanged", this.getSelected()));\r
+}\r
+\r
+// Updates the style of the control to reflects the state of the control.\r
+SelectionMenu.prototype.updateStyleFromState = function() {\r
+    uiLogger.debug("SelectionMenu.updateStyleFromState()");\r
+    \r
+    // determine the state name\r
+    var stateName = this.getStyleStateName();\r
+    \r
+    // set element class names\r
+    this.setClassName(this.rootElement, "Control");\r
+    this.setClassName(this.controlElement, "ControlElement");\r
+    this.setClassName(this.assemblyElement, "ControlAssembly ControlAssembly" + stateName);\r
+    this.setClassName(this.captionElement, "ControlCaption ControlCaption" + stateName);\r
+    \r
+    // set select and option element class names\r
+    var peerStateName = this.isEnabled() ? stateName : "Disabled";\r
+    this.setClassName(this.peerElement, "SelectionMenu SelectionMenu" + peerStateName);\r
+    for (var i = 0; i < this.options.length; i++) {\r
+        var option = this.optionElements[i];\r
+        this.setClassName(option, "SelectionMenuOption SelectionMenuOption" + peerStateName);\r
+    }\r
+}\r