Changed zouba directory heirarchy.
[ptas] / zouba / wrt / WRTKit / UI / SelectionControl.js
1 /*\r
2 � Copyright 2008 Nokia Corporation. All rights reserved.\r
3 \r
4 IMPORTANT:  The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia\r
5 Corporation (�Nokia�) in consideration of your agreement to the following terms. Your use, installation\r
6 and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If\r
7 you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example\r
8 Widget files.\r
9 \r
10 In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia\r
11 grants you a personal, non-exclusive license, under Nokia�s copyrights in the WRTKit and Example\r
12 Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML,\r
13 CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60\r
14 Widgets.\r
15 \r
16 If you redistribute the WRTKit and Example files, you must retain this entire notice in all such\r
17 redistributions of the WRTKit and Example files.\r
18 \r
19 You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products\r
20 that include the WRTKit and Example files without the prior written explicit agreement with Nokia.\r
21 Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by\r
22 Nokia herein, including but not limited to any patent rights that may be infringed by your products that\r
23 incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files\r
24 may be incorporated.\r
25 \r
26 The WRTKit and Example files are provided on an "AS IS" basis.  NOKIA MAKES NO\r
27 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED\r
28 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A\r
29 PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION\r
30 ALONE OR IN COMBINATION WITH YOUR PRODUCTS.\r
31 \r
32 IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR\r
33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR\r
36 DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY\r
37 OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE,\r
38 EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
39 \r
40 */\r
41 \r
42 ///////////////////////////////////////////////////////////////////////////////\r
43 // The SelectionControl class is an abstract base class for controls that lets\r
44 // the user select one or more options from a list of options. Don't use\r
45 // SelectionControl directly.\r
46 \r
47 // Constructor.\r
48 function SelectionControl(id, caption, options, multipleSelection, selected) {\r
49     if (id != UI_NO_INIT_ID) {\r
50         this.init(id, caption, options, multipleSelection, selected);\r
51     }\r
52 }\r
53 \r
54 // SelectionControl inherits from Control.\r
55 SelectionControl.prototype = new Control(UI_NO_INIT_ID);\r
56 \r
57 // List of options.\r
58 SelectionControl.prototype.options = null;\r
59 \r
60 // The single selected option in single selection controls\r
61 // or list of options in multi selection controls.\r
62 SelectionControl.prototype.selected = null;\r
63 \r
64 // Single or multiple selection.\r
65 SelectionControl.prototype.multipleSelection = false;\r
66 \r
67 // Initializer - called from constructor.\r
68 SelectionControl.prototype.init = function(id, caption, options, multipleSelection, selected) {\r
69     uiLogger.debug("SelectionControl.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");\r
70     \r
71     // call superclass initializer\r
72     Control.prototype.init.call(this, id, caption);\r
73     \r
74     // set the multiple selection property\r
75     this.multipleSelection = multipleSelection;\r
76     \r
77     // init options and selected (makes copies of the original arrays)\r
78     this.options = (options != null) ? options.slice(0) : [];\r
79     if (multipleSelection) {\r
80         this.selected = (selected == null) ? [] : selected.slice(0);\r
81     } else {\r
82         this.selected = selected;\r
83     }\r
84     this.validateSelected();\r
85 }\r
86 \r
87 // Returns true if the control is a multiple selection control; false if single.\r
88 SelectionControl.prototype.isMultipleSelection = function() {\r
89     return this.multipleSelection;\r
90 }\r
91 \r
92 // Returns true if the specified option is selected; false if not.\r
93 SelectionControl.prototype.isSelected = function(option) {\r
94     if (this.multipleSelection) {\r
95         // multiple selection\r
96         // iterate through all selected options and look for the specified option\r
97         for (var i = 0; i < this.selected.length; i++) {\r
98             if (this.selected[i] == option) {\r
99                 return true;\r
100             }\r
101         }\r
102         return false;\r
103     } else {\r
104         // single selection\r
105         return (this.selected == option);\r
106     }\r
107 }\r
108 \r
109 // Returns the currently selected option in a single selection control or\r
110 // an array of selected options in a multiple selection control. If there are\r
111 // no selected options a single selection control returns null and a multiple\r
112 // selection control returns an empty array.\r
113 SelectionControl.prototype.getSelected = function() {\r
114     return this.multipleSelection ? this.selected.slice(0) : this.selected;\r
115 }\r
116 \r
117 // Sets the currently selected options. Pass a single option in a single selection\r
118 // control or an array of selected controls in a multiple selection control. To\r
119 // deselect all options pass null in a single selection control and an empty array\r
120 // in a multiple selection control.\r
121 // Override in sublcasses to provide full implementation.\r
122 SelectionControl.prototype.setSelected = function(selected) {\r
123     this.selected = this.multipleSelection ? selected.slice(0) : selected;\r
124     // make sure the selected option or options are legal\r
125     this.validateSelected();\r
126 }\r
127 \r
128 // Ensures that the selected option or options exist among the options in this control.\r
129 SelectionControl.prototype.validateSelected = function() {\r
130     if (this.multipleSelection) {\r
131         // multiple selection\r
132         // iterate through all selected options and ensure they exist among the options\r
133         for (var i = 0; i < this.selected.length; i++) {\r
134             // check that the selected option exists among the options\r
135             var found = false;\r
136             for (var j = 0; j < this.options.length; j++) {\r
137                 if (this.options[j] == this.selected[i]) {\r
138                     // found - stop looking for this option\r
139                     found = true;\r
140                     break;\r
141                 }\r
142             }\r
143             // not found - remove this selected element\r
144             if (!found) {\r
145                 this.selected.splice(i, 1);\r
146                 // since we removed an entry we must re-check this position\r
147                 i--;\r
148             }\r
149         }\r
150     } else {\r
151         // single selection\r
152         if (this.selected != null) {\r
153             // check that the selected option exists among the options\r
154             for (var i = 0; i < this.options.length; i++) {\r
155                 if (this.options[i] == this.selected) {\r
156                     // found - we're done\r
157                     return;\r
158                 }\r
159             }\r
160             // not found - remove the selection\r
161             this.selected = null;\r
162         }\r
163     }\r
164 }\r
165 \r
166 // Returns the options in the control as an array of option objects with\r
167 // a value and text property.\r
168 SelectionControl.prototype.getOptions = function() {\r
169     return this.options;\r
170 }\r
171 \r
172 // Sets the options in the control.\r
173 // Override in sublcasses to provide full implementation.\r
174 SelectionControl.prototype.setOptions = function(options) {\r
175     this.options = options.slice(0);\r
176     // make sure the selected option or options are legal\r
177     this.validateSelected();\r
178 }\r
179 \r
180 // Returns the option that has the specified value; null if none.\r
181 SelectionControl.prototype.getOptionForValue = function(value) {\r
182     // iterate through all options and look for a match\r
183     for (var i = 0; i < this.options.length; i++) {\r
184         if (this.options[i].value == value) {\r
185             return this.options[i];\r
186         }\r
187     }\r
188     return null;\r
189 }\r