Changed zouba directory heirarchy.
[ptas] / zouba / wrt / WRTKit / UI / SelectionControl.js
diff --git a/zouba/wrt/WRTKit/UI/SelectionControl.js b/zouba/wrt/WRTKit/UI/SelectionControl.js
new file mode 100644 (file)
index 0000000..de363c9
--- /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 SelectionControl class is an abstract base class for controls that lets\r
+// the user select one or more options from a list of options. Don't use\r
+// SelectionControl directly.\r
+\r
+// Constructor.\r
+function SelectionControl(id, caption, options, multipleSelection, selected) {\r
+    if (id != UI_NO_INIT_ID) {\r
+        this.init(id, caption, options, multipleSelection, selected);\r
+    }\r
+}\r
+\r
+// SelectionControl inherits from Control.\r
+SelectionControl.prototype = new Control(UI_NO_INIT_ID);\r
+\r
+// List of options.\r
+SelectionControl.prototype.options = null;\r
+\r
+// The single selected option in single selection controls\r
+// or list of options in multi selection controls.\r
+SelectionControl.prototype.selected = null;\r
+\r
+// Single or multiple selection.\r
+SelectionControl.prototype.multipleSelection = false;\r
+\r
+// Initializer - called from constructor.\r
+SelectionControl.prototype.init = function(id, caption, options, multipleSelection, selected) {\r
+    uiLogger.debug("SelectionControl.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");\r
+    \r
+    // call superclass initializer\r
+    Control.prototype.init.call(this, id, caption);\r
+    \r
+    // set the multiple selection property\r
+    this.multipleSelection = multipleSelection;\r
+    \r
+    // init options and selected (makes copies of the original arrays)\r
+    this.options = (options != null) ? options.slice(0) : [];\r
+    if (multipleSelection) {\r
+        this.selected = (selected == null) ? [] : selected.slice(0);\r
+    } else {\r
+        this.selected = selected;\r
+    }\r
+    this.validateSelected();\r
+}\r
+\r
+// Returns true if the control is a multiple selection control; false if single.\r
+SelectionControl.prototype.isMultipleSelection = function() {\r
+    return this.multipleSelection;\r
+}\r
+\r
+// Returns true if the specified option is selected; false if not.\r
+SelectionControl.prototype.isSelected = function(option) {\r
+    if (this.multipleSelection) {\r
+        // multiple selection\r
+        // iterate through all selected options and look for the specified option\r
+        for (var i = 0; i < this.selected.length; i++) {\r
+            if (this.selected[i] == option) {\r
+                return true;\r
+            }\r
+        }\r
+        return false;\r
+    } else {\r
+        // single selection\r
+        return (this.selected == option);\r
+    }\r
+}\r
+\r
+// Returns the currently selected option in a single selection control or\r
+// an array of selected options in a multiple selection control. If there are\r
+// no selected options a single selection control returns null and a multiple\r
+// selection control returns an empty array.\r
+SelectionControl.prototype.getSelected = function() {\r
+    return this.multipleSelection ? this.selected.slice(0) : this.selected;\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
+// Override in sublcasses to provide full implementation.\r
+SelectionControl.prototype.setSelected = function(selected) {\r
+    this.selected = this.multipleSelection ? selected.slice(0) : selected;\r
+    // make sure the selected option or options are legal\r
+    this.validateSelected();\r
+}\r
+\r
+// Ensures that the selected option or options exist among the options in this control.\r
+SelectionControl.prototype.validateSelected = function() {\r
+    if (this.multipleSelection) {\r
+        // multiple selection\r
+        // iterate through all selected options and ensure they exist among the options\r
+        for (var i = 0; i < this.selected.length; i++) {\r
+            // check that the selected option exists among the options\r
+            var found = false;\r
+            for (var j = 0; j < this.options.length; j++) {\r
+                if (this.options[j] == this.selected[i]) {\r
+                    // found - stop looking for this option\r
+                    found = true;\r
+                    break;\r
+                }\r
+            }\r
+            // not found - remove this selected element\r
+            if (!found) {\r
+                this.selected.splice(i, 1);\r
+                // since we removed an entry we must re-check this position\r
+                i--;\r
+            }\r
+        }\r
+    } else {\r
+        // single selection\r
+        if (this.selected != null) {\r
+            // check that the selected option exists among the options\r
+            for (var i = 0; i < this.options.length; i++) {\r
+                if (this.options[i] == this.selected) {\r
+                    // found - we're done\r
+                    return;\r
+                }\r
+            }\r
+            // not found - remove the selection\r
+            this.selected = null;\r
+        }\r
+    }\r
+}\r
+\r
+// Returns the options in the control as an array of option objects with\r
+// a value and text property.\r
+SelectionControl.prototype.getOptions = function() {\r
+    return this.options;\r
+}\r
+\r
+// Sets the options in the control.\r
+// Override in sublcasses to provide full implementation.\r
+SelectionControl.prototype.setOptions = function(options) {\r
+    this.options = options.slice(0);\r
+    // make sure the selected option or options are legal\r
+    this.validateSelected();\r
+}\r
+\r
+// Returns the option that has the specified value; null if none.\r
+SelectionControl.prototype.getOptionForValue = function(value) {\r
+    // iterate through all options and look for a match\r
+    for (var i = 0; i < this.options.length; i++) {\r
+        if (this.options[i].value == value) {\r
+            return this.options[i];\r
+        }\r
+    }\r
+    return null;\r
+}\r