mypackage_wrt/WRTKit/UI/SelectionList.js
author victorp@symbian.org
Fri, 30 Apr 2010 15:01:03 +0100
changeset 42 20be4dd42b12
permissions -rwxr-xr-x
My package widget code
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 SelectionList class implements a single or multi selection control
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
// that lets users select one or more options from a list of options.
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 SelectionList(id, caption, options, multipleSelection, selected) {
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, options, multipleSelection, selected);
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
// SelectionList inherits from SelectionControl.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
SelectionList.prototype = new SelectionControl(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
// Root element for options.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
SelectionList.prototype.optionListElement = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
// Array for tracking option elements.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
SelectionList.prototype.optionElements = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
// Tracking for currently focused option; null if none.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
SelectionList.prototype.focusedOption = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
// Enabled status.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
SelectionList.prototype.enabled = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
// Initializer - called from constructor.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
SelectionList.prototype.init = function(id, caption, options, multipleSelection, selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
    uiLogger.debug("SelectionList.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
    // call superclass initializer
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
    SelectionControl.prototype.init.call(this, id, caption, options, multipleSelection, selected);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
    // create option list element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
    this.optionListElement = document.createElement("div");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
    this.controlElement.appendChild(this.optionListElement);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
    // the control defaults to enabled
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
    this.enabled = true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
    // init option element arrays
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
    this.optionElements = [];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
    // update the option elements to match the options in this control
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
    this.updateOptionElements();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
// Returns the enabled state.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
SelectionList.prototype.isEnabled = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
    return this.enabled;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
// Sets the enabled state.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
SelectionList.prototype.setEnabled = function(enabled) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
    uiLogger.debug("SelectionList.setEnabled(" + enabled + ")");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
    // switch the state and update the the control
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
    this.enabled = enabled;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
    this.updateOptionElements();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   100
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   101
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   102
// Sets the focused state for the control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
// Note: This may not always succeed.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
SelectionList.prototype.setFocused = function(focused) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
    uiLogger.debug("SelectionList.setFocused(" + focused + ")");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
    if (this.enabled && this.optionElements.length > 0) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
        if (focused) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
            this.optionElements[0].link.focus();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
        } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
            this.optionElements[0].link.blur();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
// Sets the currently selected options. Pass a single option in a single selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
// control or an array of selected controls in a multiple selection control. To
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
// deselect all options pass null in a single selection control and an empty array
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
// in a multiple selection control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
SelectionList.prototype.setSelected = function(selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   120
    // call superclass setSelected()
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
    SelectionControl.prototype.setSelected.call(this, selected);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
    this.updateStyleFromState();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
// Sets the options in the control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
SelectionList.prototype.setOptions = function(options) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
    // call superclass setOptions()
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
    SelectionControl.prototype.setOptions.call(this, options);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
    this.updateOptionElements();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
// Updates the option elements for the control element.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
SelectionList.prototype.updateOptionElements = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
    uiLogger.debug("SelectionControl.updateOptionElements()");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
    // start by removing all current options from the option list element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
    while (this.optionListElement.firstChild != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
        this.optionListElement.removeChild(this.optionListElement.firstChild);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
    // iterate through the options and add (and possibly create) a
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
    // properly configured option element for each option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
    for (var i = 0; i < this.options.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
        // get the option and option element we're working on
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
        var option = this.options[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
        // option, link and text elements for this option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
        var optionElement;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
        var optionLinkElement;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
        var optionTextElement;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
        // get the elements
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
        if (i == this.optionElements.length) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
            // we need to create a new option element...
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
            optionElement = document.createElement("div");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
            
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157
            // ...and a new option link element...
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   158
            optionLinkElement = document.createElement("a");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   159
            optionLinkElement.href = "JavaScript:void(0)";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   160
            
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   161
            // ...and a new option text element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   162
            optionTextElement = document.createElement("span");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   163
            
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   164
            // hook up event listeners to the element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   165
            var self = this;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   166
            optionLinkElement.addEventListener("focus", function(event) { self.optionFocusStateChanged(event, true); }, false);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   167
            optionLinkElement.addEventListener("blur", function(event) { self.optionFocusStateChanged(event, false); }, false);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   168
            optionElement.addEventListener("mouseover", function() { self.hoverStateChanged(true); }, false);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   169
            optionElement.addEventListener("mouseout", function() { self.hoverStateChanged(false); }, false);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   170
            optionElement.addEventListener("mousedown", function(event) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   171
                                                               self.optionClicked(event)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   172
                                                               event.stopPropagation();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   173
                                                               event.preventDefault();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   174
                                                        }, true);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   175
            optionElement.addEventListener("keydown", function(event) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   176
                                                            // center and enter trigger the action
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   177
                                                            if (event.keyCode == 0 || event.keyCode == 13) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   178
                                                                self.optionClicked(event)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   179
                                                                event.stopPropagation();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   180
                                                                event.preventDefault();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   181
                                                            }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   182
                                                      }, true);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   183
            
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   184
            // add the elements to the option element array
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   185
            this.optionElements.push({ option: optionElement, link: optionLinkElement, text: optionTextElement });
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   186
        } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   187
            // we already have ready elements so we'll reuse them
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   188
            optionElement = this.optionElements[i].option;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   189
            optionLinkElement = this.optionElements[i].link;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   190
            optionTextElement = this.optionElements[i].text;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   191
            
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   192
            // remove the option link element from its current parent - if any
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   193
            if (optionLinkElement.parentNode != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   194
                optionLinkElement.parentNode.removeChild(optionLinkElement);
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
            // remove the option text element from its current parent - if any
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   198
            if (optionTextElement.parentNode != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   199
                optionTextElement.parentNode.removeChild(optionTextElement);
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
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   203
        // set the option text
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   204
        optionTextElement.innerHTML = option.text;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   205
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   206
        // hook up the option to the control
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   207
        if (this.enabled) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   208
            // add the option link element to the option element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   209
            optionElement.appendChild(optionLinkElement);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   210
            // add the text element to the option element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   211
            optionLinkElement.appendChild(optionTextElement);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   212
        } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   213
            // add the text element directly to the control element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   214
            optionElement.appendChild(optionTextElement);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   215
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   216
        // add the option element to the option list element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   217
        this.optionListElement.appendChild(optionElement);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   218
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   219
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   220
    // update the style
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   221
    this.updateStyleFromState();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   222
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   223
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   224
// Callback for focus state change events.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   225
SelectionList.prototype.optionFocusStateChanged = function(event, focused) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   226
    uiLogger.debug("SelectionControl.optionFocusStateChanged()");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   227
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   228
    // get the event source option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   229
    var option = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   230
    var optionElement = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   231
    for (var i = 0; i < this.optionElements.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   232
        optionElement = this.optionElements[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   233
        if (optionElement.link == event.currentTarget) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   234
            option = this.options[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   235
            break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   236
        }
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
    // remember the focused option; or null if none is focused
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   240
    if (focused) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   241
        this.focusedOption = option;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   242
    } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   243
        this.focusedOption = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   244
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   245
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   246
    // call the superclass focus state change handler
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   247
    this.focusStateChanged(focused);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   248
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   249
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   250
// Callback for clicks.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   251
SelectionList.prototype.optionClicked = function(event) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   252
    uiLogger.debug("SelectionControl.optionClicked()");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   253
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   254
    // bail out if we're not enabled
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   255
    if (!this.enabled) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   256
        return false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   257
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   258
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   259
    // get the changed option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   260
    var option = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   261
    var optionElement = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   262
    for (var i = 0; i < this.optionElements.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   263
        optionElement = this.optionElements[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   264
        if (optionElement.option == event.currentTarget) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   265
            option = this.options[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   266
            break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   267
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   268
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   269
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   270
    // make sure the option is focused
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   271
    optionElement.link.focus();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   272
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   273
    // toggle the selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   274
    if (this.multipleSelection) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   275
        // iterate through the selected options and see if this
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   276
        // option is selected. if not then add it to the selection.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   277
        // if it already is selected then them remove it.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   278
        var found = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   279
        for (var i = 0; i < this.selected.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   280
            if (this.selected[i] == option) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   281
                // remove from selected set
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   282
                found = true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   283
                this.selected.splice(i, 1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   284
                break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   285
            }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   286
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   287
        if (!found) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   288
            // add to the selected set
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   289
            this.selected.push(option);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   290
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   291
    } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   292
        // update the selected option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   293
        this.selected = option;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   294
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   295
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   296
    // update the style
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   297
    this.updateStyleFromState();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   298
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   299
    // notify event listeners
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   300
    this.fireEvent(this.createEvent("SelectionChanged", this.getSelected()));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   301
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   302
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   303
// Resets the state tracking for focus and hover.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   304
// Override this in subclasses as required to implement the state reset.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   305
SelectionList.prototype.resetFocusState = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   306
    uiLogger.debug("SelectionList.resetFocusState()");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   307
    this.hovering = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   308
    this.focused = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   309
    this.focusedOption = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   310
    this.updateStyleFromState();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   311
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   312
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   313
// Updates the style of the control to reflects the state of the control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   314
SelectionList.prototype.updateStyleFromState = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   315
    uiLogger.debug("SelectionList.updateStyleFromState()");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   316
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   317
    // determine the state name
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   318
    var stateName = this.getStyleStateName();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   319
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   320
    // set element class names
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   321
    this.setClassName(this.rootElement, "Control");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   322
    this.setClassName(this.controlElement, "ControlElement");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   323
    this.setClassName(this.assemblyElement, "ControlAssembly ControlAssembly" + stateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   324
    this.setClassName(this.captionElement, "ControlCaption ControlCaption" + stateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   325
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   326
    // set option list and option class names
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   327
    this.setClassName(this.optionListElement, "SelectionList SelectionList" + stateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   328
    for (var i = 0; i < this.options.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   329
        var option = this.options[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   330
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   331
        // get the option and option text elements for this option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   332
        var optionElement = this.optionElements[i].option;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   333
        var optionTextElement = this.optionElements[i].text;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   334
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   335
        // figure out the option state
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   336
        var optionStateName = this.isSelected(option) ? "Checked" : "Unchecked";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   337
        if (!this.enabled) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   338
            optionStateName += "Disabled";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   339
        } else if (this.focusedOption == option) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   340
            optionStateName += "Focus";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   341
        } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   342
            optionStateName += "Normal";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   343
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   344
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   345
        // set option element class names
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   346
        if (this.multipleSelection) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   347
            this.setClassName(optionElement, "SelectionListOptionMulti SelectionListOptionMulti" + optionStateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   348
        } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   349
            this.setClassName(optionElement, "SelectionListOptionSingle SelectionListOptionSingle" + optionStateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   350
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   351
        
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   352
        // set option text class names
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   353
        this.setClassName(optionTextElement, "SelectionListOptionText SelectionListOptionText" + stateName);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   354
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   355
}