Added menu option to show/hide messages table.
[ptas] / zouba / misc / rss / WRTKit / UI / ListView.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 ListView class implements a vertical list view that hosts controls\r
44 // as child components.\r
45 \r
46 // Constructor.\r
47 function ListView(id, caption) {\r
48     if (id != UI_NO_INIT_ID) {\r
49         this.init(id, caption);\r
50     }\r
51 }\r
52 \r
53 // ListView inherits from View.\r
54 ListView.prototype = new View(UI_NO_INIT_ID);\r
55 \r
56 // The caption of this view; null if none.\r
57 ListView.prototype.caption = null;\r
58 \r
59 // The caption element of this view.\r
60 ListView.prototype.captionElement = null;\r
61 \r
62 // The caption text element of this view.\r
63 ListView.prototype.captionTextElement = null;\r
64 \r
65 // Root HTML element for controls.\r
66 ListView.prototype.listElement = null;\r
67 \r
68 // List of controls in the view.\r
69 ListView.prototype.controls = null;\r
70 \r
71 // Initializer for ListView.\r
72 ListView.prototype.init = function(id, caption) {\r
73     uiLogger.debug("ListView.init(" + id + ", " + caption + ")");\r
74     \r
75     // call superclass initializer\r
76     View.prototype.init.call(this, id);\r
77     \r
78     // init control array\r
79     this.controls = [];\r
80     \r
81     // set style class name for root element\r
82     this.rootElement.className = "ListView";\r
83     \r
84     // create caption and caption text elements\r
85     this.captionElement = document.createElement("div");\r
86     this.captionElement.className = "ListViewCaption";\r
87     this.captionTextElement = document.createElement("div");\r
88     this.captionTextElement.className = "ListViewCaptionText";\r
89     this.captionElement.appendChild(this.captionTextElement);\r
90     this.rootElement.appendChild(this.captionElement);\r
91     \r
92     // create root element for controls and add to the view root element\r
93     this.listElement = document.createElement("div");\r
94     this.listElement.className = "ListViewControlList";\r
95     this.rootElement.appendChild(this.listElement);\r
96     \r
97     // set the caption\r
98     this.setCaption(caption);\r
99 }\r
100 \r
101 // Returns the caption; null if none.\r
102 ListView.prototype.getCaption = function() {\r
103     return this.caption;\r
104 }\r
105 \r
106 // Sets the caption; null if none.\r
107 ListView.prototype.setCaption = function(caption) {\r
108     uiLogger.debug("ListView.setCaption(" + caption + ")");\r
109     \r
110     // set the display style\r
111     this.captionElement.style.display = (caption == null) ? "none" : "block";\r
112     \r
113     // set the caption\r
114     this.caption = caption;\r
115     this.captionTextElement.innerHTML = (caption == null) ? "" : caption;\r
116 }\r
117 \r
118 // Returns an array of controls in the view.\r
119 ListView.prototype.getControls = function() {\r
120     return this.controls;\r
121 }\r
122 \r
123 // Adds a control to the view.\r
124 ListView.prototype.addControl = function(control) {\r
125     uiLogger.debug("ListView.addControl(" + control + ")");\r
126     \r
127     // add the control to the controls array and attach it to the list element\r
128     this.controls.push(control);\r
129     this.listElement.appendChild(control.rootElement);\r
130     control.view = this;\r
131 }\r
132 \r
133 // Inserts a control to the view before the specified control.\r
134 ListView.prototype.insertControl = function(control, beforeControl) {\r
135     uiLogger.debug("ListView.insertControl(" + control + ", " + beforeControl + ")");\r
136     \r
137     // iterate through current controls\r
138     for (var i = 0; i < this.controls.length; i++) {\r
139         // is this the control we should insert before?\r
140         if (this.controls[i] == beforeControl) {\r
141             // we found the control to insert before - insert here and connect to list element\r
142             this.controls.splice(i, 0, control);\r
143             this.listElement.insertBefore(control.rootElement, beforeControl.rootElement);\r
144             control.view = this;\r
145             return;\r
146         }\r
147     }\r
148     \r
149     // the control wasn't found so we'll add it last\r
150     this.addControl(control);\r
151 }\r
152 \r
153 // Removes a control from the view.\r
154 ListView.prototype.removeControl = function(control) {\r
155     uiLogger.debug("ListView.removeControl(" + control + ")");\r
156     \r
157     // iterate through current controls\r
158     for (var i = 0; i < this.controls.length; i++) {\r
159         // is this the control we should remove?\r
160         if (this.controls[i] == control) {\r
161             // we found the control to remove - remove it from the list element\r
162             this.controls.splice(i, 1);\r
163             this.listElement.removeChild(control.rootElement);\r
164             control.view = null;\r
165         }\r
166     }\r
167 }\r
168 \r
169 // Attempts to focus the first focusable control.\r
170 ListView.prototype.focusFirstControl = function() {\r
171     uiLogger.debug("ListView.focusFirstControl()");\r
172     for (var i = 0; i < this.controls.length; i++) {\r
173         // is this control focusable?\r
174         var control = this.controls[i];\r
175         if (control.isFocusable()) {\r
176             control.setFocused(true);\r
177             break;\r
178         }\r
179     }\r
180 }\r
181 \r
182 // Attempts to reset all control focus states.\r
183 // Override in subclasses as required.\r
184 ListView.prototype.resetControlFocusStates = function() {\r
185     uiLogger.debug("ListView.resetControlFocusStates()");\r
186     for (var i = 0; i < this.controls.length; i++) {\r
187         this.controls[i].resetFocusState();\r
188     }\r
189 }\r