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