mypackage_wrt/WRTKit/UI/SelectionControl.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 SelectionControl class is an abstract base class for controls that lets
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
// the user select one or more options from a list of options. Don't use
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    45
// SelectionControl directly.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    46
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    47
// Constructor.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    48
function SelectionControl(id, caption, options, multipleSelection, selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    49
    if (id != UI_NO_INIT_ID) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    50
        this.init(id, caption, options, multipleSelection, selected);
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
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
// SelectionControl inherits from Control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    55
SelectionControl.prototype = new Control(UI_NO_INIT_ID);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    56
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
// List of options.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
SelectionControl.prototype.options = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
// The single selected option in single selection controls
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
// or list of options in multi selection controls.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
SelectionControl.prototype.selected = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
// Single or multiple selection.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
SelectionControl.prototype.multipleSelection = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
// Initializer - called from constructor.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
SelectionControl.prototype.init = function(id, caption, options, multipleSelection, selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
    uiLogger.debug("SelectionControl.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
    // call superclass initializer
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
    Control.prototype.init.call(this, id, caption);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
    // set the multiple selection property
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
    this.multipleSelection = multipleSelection;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
    
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
    // init options and selected (makes copies of the original arrays)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
    this.options = (options != null) ? options.slice(0) : [];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
    if (multipleSelection) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
        this.selected = (selected == null) ? [] : selected.slice(0);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
    } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
        this.selected = selected;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
    this.validateSelected();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
// Returns true if the control is a multiple selection control; false if single.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
SelectionControl.prototype.isMultipleSelection = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
    return this.multipleSelection;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
// Returns true if the specified option is selected; false if not.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
SelectionControl.prototype.isSelected = function(option) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
    if (this.multipleSelection) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
        // multiple selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
        // iterate through all selected options and look for the specified option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
        for (var i = 0; i < this.selected.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
            if (this.selected[i] == option) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
                return true;
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
        return false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
    } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
        // single selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
        return (this.selected == option);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
// Returns the currently selected option in a single selection control or
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
// an array of selected options in a multiple selection control. If there are
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
// no selected options a single selection control returns null and a multiple
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
// selection control returns an empty array.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
SelectionControl.prototype.getSelected = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
    return this.multipleSelection ? this.selected.slice(0) : this.selected;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
// Sets the currently selected options. Pass a single option in a single selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
// control or an array of selected controls in a multiple selection control. To
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
// 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
   120
// in a multiple selection control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
// Override in sublcasses to provide full implementation.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
SelectionControl.prototype.setSelected = function(selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
    this.selected = this.multipleSelection ? selected.slice(0) : selected;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
    // make sure the selected option or options are legal
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
    this.validateSelected();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
// Ensures that the selected option or options exist among the options in this control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
SelectionControl.prototype.validateSelected = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
    if (this.multipleSelection) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
        // multiple selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
        // iterate through all selected options and ensure they exist among the options
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
        for (var i = 0; i < this.selected.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
            // check that the selected option exists among the options
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
            var found = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
            for (var j = 0; j < this.options.length; j++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
                if (this.options[j] == this.selected[i]) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
                    // found - stop looking for this option
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
                    found = true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
                    break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
                }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
            }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
            // not found - remove this selected element
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
            if (!found) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
                this.selected.splice(i, 1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
                // since we removed an entry we must re-check this position
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
                i--;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
            }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
    } else {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
        // single selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
        if (this.selected != null) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
            // check that the selected option exists among the options
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
            for (var i = 0; i < this.options.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
                if (this.options[i] == this.selected) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
                    // found - we're done
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157
                    return;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   158
                }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   159
            }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   160
            // not found - remove the selection
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   161
            this.selected = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   162
        }
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
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   166
// Returns the options in the control as an array of option objects with
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   167
// a value and text property.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   168
SelectionControl.prototype.getOptions = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   169
    return this.options;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   170
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   171
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   172
// Sets the options in the control.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   173
// Override in sublcasses to provide full implementation.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   174
SelectionControl.prototype.setOptions = function(options) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   175
    this.options = options.slice(0);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   176
    // make sure the selected option or options are legal
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   177
    this.validateSelected();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   178
}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   179
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   180
// Returns the option that has the specified value; null if none.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   181
SelectionControl.prototype.getOptionForValue = function(value) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   182
    // iterate through all options and look for a match
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   183
    for (var i = 0; i < this.options.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   184
        if (this.options[i].value == value) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   185
            return this.options[i];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   186
        }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   187
    }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   188
    return null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   189
}