author | Eugene Ostroukhov <eugeneo@symbian.org> |
Wed, 10 Mar 2010 09:22:00 -0800 | |
changeset 238 | 977c9f9adc30 |
parent 211 | 7b148352159e |
child 273 | b1f63c2c240c |
permissions | -rw-r--r-- |
210
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
1 |
/** |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
2 |
* Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies). |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
3 |
* All rights reserved. |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
4 |
* This component and the accompanying materials are made available |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
5 |
* under the terms of the License "Eclipse Public License v1.0" |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
6 |
* which accompanies this distribution, and is available |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html". |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
8 |
* |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
9 |
* Initial Contributors: |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
10 |
* Symbian Foundation - initial contribution. |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
11 |
* Contributors: |
0f7abfd6ae62
Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents:
102
diff
changeset
|
12 |
*/ |
73 | 13 |
/* |
211 | 14 |
© Copyright 2008 Nokia Corporation. All rights reserved. |
73 | 15 |
|
16 |
IMPORTANT: The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia |
|
211 | 17 |
Corporation ("Nokia") in consideration of your agreement to the following terms. Your use, installation |
73 | 18 |
and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If |
19 |
you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example |
|
20 |
Widget files. |
|
21 |
||
22 |
In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia |
|
211 | 23 |
grants you a personal, non-exclusive license, under Nokia's copyrights in the WRTKit and Example |
73 | 24 |
Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML, |
25 |
CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60 |
|
26 |
Widgets. |
|
27 |
||
28 |
If you redistribute the WRTKit and Example files, you must retain this entire notice in all such |
|
29 |
redistributions of the WRTKit and Example files. |
|
30 |
||
31 |
You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products |
|
32 |
that include the WRTKit and Example files without the prior written explicit agreement with Nokia. |
|
33 |
Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by |
|
34 |
Nokia herein, including but not limited to any patent rights that may be infringed by your products that |
|
35 |
incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files |
|
36 |
may be incorporated. |
|
37 |
||
38 |
The WRTKit and Example files are provided on an "AS IS" basis. NOKIA MAKES NO |
|
39 |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
|
40 |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A |
|
41 |
PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION |
|
42 |
ALONE OR IN COMBINATION WITH YOUR PRODUCTS. |
|
43 |
||
44 |
IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
|
45 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
46 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
47 |
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR |
|
48 |
DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY |
|
49 |
OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, |
|
50 |
EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
51 |
||
52 |
*/ |
|
53 |
||
54 |
/////////////////////////////////////////////////////////////////////////////// |
|
55 |
// The ContentPanel class is a control for displaying content. The panel |
|
56 |
// can be expanded and collapsed. |
|
57 |
||
58 |
// Constructor. |
|
59 |
function ContentPanel(id, caption, content, foldable, expanded) { |
|
60 |
if (id != UI_NO_INIT_ID) { |
|
61 |
this.init(id, caption, content, foldable, expanded); |
|
62 |
} |
|
63 |
} |
|
64 |
||
65 |
// ContentPanel inherits from Control. |
|
66 |
ContentPanel.prototype = new Control(UI_NO_INIT_ID); |
|
67 |
||
68 |
// The element hierarchy in a content panel is as follows: |
|
69 |
// |
|
70 |
// rootElement |
|
71 |
// assemblyElement |
|
72 |
// captionElement |
|
73 |
// foldToggleElement |
|
74 |
// captionLinkElement |
|
75 |
// captionTextElement |
|
76 |
// contentElement |
|
77 |
// |
|
78 |
// captionTextElement is moved under foldToggleElement if disabled |
|
79 |
// or captionElement if not foldable |
|
80 |
||
81 |
// The fold toggle element used for folding content panels. |
|
82 |
ContentPanel.prototype.foldToggleElement = null; |
|
83 |
||
84 |
// The caption link element of this control. |
|
85 |
ContentPanel.prototype.captionLinkElement = null; |
|
86 |
||
87 |
// The caption text element of this control. |
|
88 |
ContentPanel.prototype.captionTextElement = null; |
|
89 |
||
90 |
// The content element of this control. |
|
91 |
ContentPanel.prototype.contentElement = null; |
|
92 |
||
93 |
// The foldable state of this control. |
|
94 |
ContentPanel.prototype.foldable = false; |
|
95 |
||
96 |
// The expanded state of this control. |
|
97 |
ContentPanel.prototype.expanded = false; |
|
98 |
||
99 |
// Enabled status. |
|
100 |
ContentPanel.prototype.enabled = false; |
|
101 |
||
102 |
// Initializer - called from constructor. |
|
103 |
ContentPanel.prototype.init = function(id, caption, content, foldable, expanded) { |
|
104 |
uiLogger.debug("ContentPanel.init(" + id + ", " + caption + ", " + content + ", " + foldable + ", " + expanded + ")"); |
|
105 |
||
106 |
// call superclass initializer |
|
107 |
Control.prototype.init.call(this, id, caption); |
|
108 |
||
109 |
// the control defaults to enabled |
|
110 |
this.enabled = true; |
|
111 |
||
112 |
// create caption text element |
|
113 |
this.captionTextElement = document.createElement("span"); |
|
114 |
||
115 |
// disconnect the control element |
|
116 |
this.assemblyElement.removeChild(this.controlElement); |
|
117 |
||
118 |
// set the foldable state |
|
119 |
this.foldable = foldable; |
|
120 |
||
121 |
// is this a foldable content panel? |
|
122 |
if (foldable) { |
|
123 |
// create fold toggle element |
|
124 |
this.foldToggleElement = document.createElement("div"); |
|
125 |
this.captionElement.appendChild(this.foldToggleElement); |
|
126 |
||
127 |
// create caption link and add to caption element |
|
128 |
this.captionLinkElement = document.createElement("a"); |
|
129 |
this.captionLinkElement.href = "JavaScript:void(0)"; |
|
130 |
this.foldToggleElement.appendChild(this.captionLinkElement); |
|
131 |
||
132 |
// add the text element to the link element |
|
133 |
this.captionLinkElement.appendChild(this.captionTextElement); |
|
134 |
||
135 |
// bind event listeners |
|
136 |
var self = this; |
|
137 |
this.captionLinkElement.addEventListener("focus", function() { self.focusStateChanged(true); }, false); |
|
138 |
this.captionLinkElement.addEventListener("blur", function() { self.focusStateChanged(false); }, false); |
|
139 |
this.foldToggleElement.addEventListener("mouseover", function() { self.hoverStateChanged(true); }, false); |
|
140 |
this.foldToggleElement.addEventListener("mouseout", function() { self.hoverStateChanged(false); }, false); |
|
141 |
this.foldToggleElement.addEventListener("mousedown", function(event) { |
|
142 |
self.captionClicked(); |
|
143 |
event.stopPropagation(); |
|
144 |
event.preventDefault(); |
|
145 |
}, true); |
|
146 |
this.foldToggleElement.addEventListener("keydown", function(event) { |
|
147 |
// center and enter trigger the action |
|
148 |
if (event.keyCode == 0 || event.keyCode == 13) { |
|
149 |
self.captionClicked(); |
|
150 |
event.stopPropagation(); |
|
151 |
event.preventDefault(); |
|
152 |
} |
|
153 |
}, true); |
|
154 |
||
155 |
this.expanded = expanded; |
|
156 |
} else { |
|
157 |
// since this is not a foldable panel the content should be expanded |
|
158 |
this.expanded = true; |
|
159 |
||
160 |
// add the text element directly to the caption element |
|
161 |
this.captionElement.appendChild(this.captionTextElement); |
|
162 |
} |
|
163 |
||
164 |
// create content element |
|
165 |
this.contentElement = document.createElement("div"); |
|
166 |
this.contentElement.style.display = this.expanded ? "block" : "none"; |
|
167 |
this.rootElement.appendChild(this.contentElement); |
|
168 |
||
169 |
// set caption, content and expanded state |
|
170 |
this.setCaption(caption); |
|
171 |
this.setContent(content); |
|
172 |
||
173 |
// update style |
|
174 |
this.updateStyleFromState(); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
175 |
}; |
73 | 176 |
|
177 |
// Returns the enabled state. |
|
178 |
ContentPanel.prototype.isEnabled = function() { |
|
179 |
return this.enabled; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
180 |
}; |
73 | 181 |
|
182 |
// Sets the enabled state. |
|
183 |
ContentPanel.prototype.setEnabled = function(enabled) { |
|
184 |
uiLogger.debug("ContentPanel.setEnabled(" + enabled + ")"); |
|
185 |
||
186 |
// bail out early if there is no change in state |
|
187 |
if (this.enabled == enabled) { |
|
188 |
return; |
|
189 |
} |
|
190 |
||
191 |
// set the enabled state |
|
192 |
this.enabled = enabled; |
|
193 |
||
194 |
// is this a foldable content? |
|
195 |
if (this.foldable) { |
|
196 |
// the caption link must be disabled |
|
197 |
if (this.enabled) { |
|
198 |
// diabled -> enabled |
|
199 |
this.foldToggleElement.removeChild(this.captionTextElement); |
|
200 |
this.foldToggleElement.appendChild(this.captionLinkElement); |
|
201 |
this.captionLinkElement.appendChild(this.captionTextElement); |
|
202 |
} else { |
|
203 |
// enabled -> diabled |
|
204 |
this.captionLinkElement.removeChild(this.captionTextElement); |
|
205 |
this.foldToggleElement.removeChild(this.captionLinkElement); |
|
206 |
this.foldToggleElement.appendChild(this.captionTextElement); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
// update style |
|
211 |
this.updateStyleFromState(); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
212 |
}; |
73 | 213 |
|
214 |
// Returns the caption; null if none. |
|
215 |
ContentPanel.prototype.getCaption = function() { |
|
216 |
return this.caption; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
217 |
}; |
73 | 218 |
|
219 |
// Sets the caption; null if none. |
|
220 |
ContentPanel.prototype.setCaption = function(caption) { |
|
221 |
// bail out if the caption text element has not been created |
|
222 |
// this is to prevent the superclass init calling this before |
|
223 |
// we've initialized our custom caption |
|
224 |
if (this.captionTextElement == null) |
|
225 |
return; |
|
226 |
||
227 |
uiLogger.debug("ContentPanel.setCaption(" + caption + ")"); |
|
228 |
||
229 |
// set the display style |
|
230 |
this.captionElement.style.display = (caption == null) ? "none" : "block"; |
|
231 |
||
232 |
// set the caption |
|
233 |
this.caption = caption; |
|
234 |
this.captionTextElement.innerHTML = (caption == null) ? "" : caption; |
|
235 |
||
236 |
// update style |
|
237 |
this.updateStyleFromState(); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
238 |
}; |
73 | 239 |
|
240 |
// Returns the content. |
|
241 |
ContentPanel.prototype.getContent = function() { |
|
242 |
return this.contentElement.innerHTML; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
243 |
}; |
73 | 244 |
|
245 |
// Sets the content. |
|
246 |
ContentPanel.prototype.setContent = function(content) { |
|
247 |
uiLogger.debug("ContentPanel.setContent(" + content + ")"); |
|
248 |
this.contentElement.innerHTML = (content == null) ? "" : content; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
249 |
}; |
73 | 250 |
|
251 |
// Returns the focusable state for the control. |
|
252 |
ContentPanel.prototype.isFocusable = function() { |
|
253 |
// a content panel is focusable if it's foldable and enabled |
|
254 |
return (this.foldable && this.enabled); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
255 |
}; |
73 | 256 |
|
257 |
// Sets the focused state for the control. |
|
258 |
// Note: This may not always succeed. |
|
259 |
ContentPanel.prototype.setFocused = function(focused) { |
|
260 |
uiLogger.debug("ContentPanel.setFocused(" + focused + ")"); |
|
261 |
if (this.enabled && this.foldable) { |
|
262 |
if (focused) { |
|
263 |
this.captionLinkElement.focus(); |
|
264 |
} else { |
|
265 |
this.captionLinkElement.blur(); |
|
266 |
} |
|
267 |
} |
|
268 |
// note that this.focused gets set as a result of focusStateChanged() being called |
|
269 |
// rather than setting it explicitly here |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
270 |
}; |
73 | 271 |
|
272 |
// Returns the expanded state. |
|
273 |
ContentPanel.prototype.isExpanded = function() { |
|
274 |
return this.expanded; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
275 |
}; |
73 | 276 |
|
277 |
// Sets the expanded state. |
|
278 |
ContentPanel.prototype.setExpanded = function(expanded) { |
|
279 |
uiLogger.debug("ContentPanel.setExpanded(" + expanded + ")"); |
|
280 |
||
281 |
// make sure only foldable content panels are folded |
|
282 |
if (!this.foldable) { |
|
283 |
uiLogger.warn("Cannot fold a non-foldable content panel!"); |
|
284 |
return; |
|
285 |
} |
|
286 |
||
287 |
this.expanded = expanded; |
|
288 |
if (this.expanded) { |
|
289 |
// expand |
|
290 |
this.contentElement.style.display = "block"; |
|
291 |
||
292 |
// find out control top and bottom |
|
293 |
var controlTop = this.getAbsoluteTop(this.rootElement); |
|
294 |
var controlHeight = this.rootElement.clientHeight; |
|
295 |
var controlBottom = controlTop + controlHeight; |
|
296 |
||
297 |
// find out the viewport top and bottom |
|
298 |
var viewportTop = window.scrollY; |
|
299 |
var viewportHeight = window.innerHeight; |
|
300 |
var viewportBottom = viewportTop + viewportHeight; |
|
301 |
||
302 |
// make sure the control is positioned so that it can be seen |
|
303 |
var overflow = controlBottom - viewportBottom; |
|
304 |
if (overflow > 0) { |
|
305 |
// there's overflow so we need to scroll to get the control |
|
306 |
// into the viewport - however not so far that the control |
|
307 |
// goes past the viewport top. |
|
308 |
var distanceToTop = controlTop - viewportTop; |
|
309 |
var scrollAmount = Math.min(overflow, distanceToTop); |
|
310 |
window.scrollBy(0, scrollAmount); |
|
311 |
} |
|
312 |
} else { |
|
313 |
// collapse |
|
314 |
this.contentElement.style.display = "none"; |
|
315 |
} |
|
316 |
||
317 |
// notify event listeners |
|
318 |
this.fireEvent(this.createEvent("ExpandedStateChanged", this.expanded)); |
|
319 |
||
320 |
// update the style |
|
321 |
this.updateStyleFromState(); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
322 |
}; |
73 | 323 |
|
324 |
// Returns the absolute position (y) of the given element. |
|
325 |
ContentPanel.prototype.getAbsoluteTop = function(element) { |
|
326 |
// traverse from element to root and add top-offset |
|
327 |
// for each element we find on the way |
|
328 |
var absTop = 0; |
|
329 |
while (element != null) { |
|
330 |
absTop += element.offsetTop; |
|
331 |
element = element.offsetParent; |
|
332 |
} |
|
333 |
return absTop; |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
334 |
}; |
73 | 335 |
|
336 |
// Callback for when the caption is clicked. |
|
337 |
ContentPanel.prototype.captionClicked = function() { |
|
338 |
uiLogger.debug("ContentPanel.captionClicked()"); |
|
339 |
||
340 |
// if we're enabled then a click results toggling the expanded state |
|
341 |
if (this.enabled) { |
|
342 |
// focus when clicked |
|
343 |
if (!this.focused) { |
|
344 |
this.captionLinkElement.focus(); |
|
345 |
} |
|
346 |
||
347 |
// toggle the expanded state |
|
348 |
this.setExpanded(!this.expanded); |
|
349 |
} |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
350 |
}; |
73 | 351 |
|
352 |
// Updates the style of the control to reflects the state of the control. |
|
353 |
ContentPanel.prototype.updateStyleFromState = function() { |
|
354 |
uiLogger.debug("ContentPanel.updateStyleFromState()"); |
|
355 |
||
356 |
// determine the state name |
|
357 |
var stateName = this.getStyleStateName(); |
|
358 |
||
359 |
// set root element class name |
|
360 |
this.setClassName(this.rootElement, "Control"); |
|
361 |
||
362 |
// set the control assembly class names |
|
363 |
this.setClassName(this.assemblyElement, "ControlAssembly ControlAssembly" + stateName); |
|
364 |
||
365 |
if (this.foldable) { |
|
366 |
// foldable content panel |
|
367 |
this.setClassName(this.captionElement, "ContentPanelCaptionFoldable"); |
|
368 |
this.setClassName(this.foldToggleElement, "ContentPanelFoldToggle ContentPanelFoldToggle" + (this.expanded ? "Expanded" : "Collapsed")); |
|
369 |
} else { |
|
370 |
// non-folding |
|
371 |
this.setClassName(this.captionElement, "ContentPanelCaptionNonFoldable"); |
|
372 |
} |
|
373 |
||
374 |
// set the content caption text class names |
|
375 |
this.setClassName(this.captionTextElement, "ContentPanelCaptionText ContentPanelCaptionText" + stateName); |
|
376 |
||
377 |
// set the content element class names |
|
378 |
this.setClassName(this.contentElement, "ContentPanelContent"); |
|
102
30e0796f3ebb
Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents:
73
diff
changeset
|
379 |
}; |