org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/SelectionControl.js
author Eugene Ostroukhov <eugeneo@symbian.org>
Thu, 18 Mar 2010 11:56:59 -0700
changeset 276 f2f4a1259de8
parent 273 b1f63c2c240c
permissions -rw-r--r--
Bug 2065 - Pull updated Chrome Developer Tools into the workspace
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     1
/**
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     2
 * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     3
 * All rights reserved.
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     4
 * This component and the accompanying materials are made available
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     5
 * under the terms of the License "Eclipse Public License v1.0"
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     6
 * which accompanies this distribution, and is available
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     7
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     8
 *
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
     9
 * Initial Contributors:
273
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    10
 * Nokia Corporation - initial contribution.
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    11
 * 
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
    12
 * Contributors:
273
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    13
 * 
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    14
 * Description:
b1f63c2c240c Bug 2072 - Update .js files with EPL
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 211
diff changeset
    15
 * 
210
0f7abfd6ae62 Fixed bug 2072: update .js files with EPL
tasneems@symbian.org
parents: 102
diff changeset
    16
 */
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    17
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    18
///////////////////////////////////////////////////////////////////////////////
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    19
// The SelectionControl class is an abstract base class for controls that lets
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    20
// the user select one or more options from a list of options. Don't use
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    21
// SelectionControl directly.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    22
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    23
// Constructor.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    24
function SelectionControl(id, caption, options, multipleSelection, selected) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    25
    if (id != UI_NO_INIT_ID) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    26
        this.init(id, caption, options, multipleSelection, selected);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    27
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    28
}
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    29
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    30
// SelectionControl inherits from Control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    31
SelectionControl.prototype = new Control(UI_NO_INIT_ID);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    32
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    33
// List of options.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    34
SelectionControl.prototype.options = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    35
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    36
// The single selected option in single selection controls
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    37
// or list of options in multi selection controls.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    38
SelectionControl.prototype.selected = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    39
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    40
// Single or multiple selection.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    41
SelectionControl.prototype.multipleSelection = false;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    42
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    43
// Initializer - called from constructor.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    44
SelectionControl.prototype.init = function(id, caption, options, multipleSelection, selected) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    45
    uiLogger.debug("SelectionControl.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    46
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    47
    // call superclass initializer
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    48
    Control.prototype.init.call(this, id, caption);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    49
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    50
    // set the multiple selection property
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    51
    this.multipleSelection = multipleSelection;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    52
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    53
    // init options and selected (makes copies of the original arrays)
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    54
    this.options = (options != null) ? options.slice(0) : [];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    55
    if (multipleSelection) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    56
        this.selected = (selected == null) ? [] : selected.slice(0);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    57
    } else {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    58
        this.selected = selected;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    59
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    60
    this.validateSelected();
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    61
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    62
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    63
// Returns true if the control is a multiple selection control; false if single.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    64
SelectionControl.prototype.isMultipleSelection = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    65
    return this.multipleSelection;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    66
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    67
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    68
// Returns true if the specified option is selected; false if not.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    69
SelectionControl.prototype.isSelected = function(option) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    70
    if (this.multipleSelection) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    71
        // multiple selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    72
        // iterate through all selected options and look for the specified option
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    73
        for (var i = 0; i < this.selected.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    74
            if (this.selected[i] == option) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    75
                return true;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    76
            }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    77
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    78
        return false;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    79
    } else {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    80
        // single selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    81
        return (this.selected == option);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    82
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    83
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    84
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    85
// Returns the currently selected option in a single selection control or
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    86
// an array of selected options in a multiple selection control. If there are
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    87
// no selected options a single selection control returns null and a multiple
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    88
// selection control returns an empty array.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    89
SelectionControl.prototype.getSelected = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    90
    return this.multipleSelection ? this.selected.slice(0) : this.selected;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    91
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    92
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    93
// Sets the currently selected options. Pass a single option in a single selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    94
// control or an array of selected controls in a multiple selection control. To
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    95
// deselect all options pass null in a single selection control and an empty array
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    96
// in a multiple selection control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    97
// Override in sublcasses to provide full implementation.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    98
SelectionControl.prototype.setSelected = function(selected) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    99
    this.selected = this.multipleSelection ? selected.slice(0) : selected;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   100
    // make sure the selected option or options are legal
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   101
    this.validateSelected();
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   102
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   103
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   104
// Ensures that the selected option or options exist among the options in this control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   105
SelectionControl.prototype.validateSelected = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   106
    if (this.multipleSelection) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   107
        // multiple selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   108
        // iterate through all selected options and ensure they exist among the options
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   109
        for (var i = 0; i < this.selected.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   110
            // check that the selected option exists among the options
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   111
            var found = false;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   112
            for (var j = 0; j < this.options.length; j++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   113
                if (this.options[j] == this.selected[i]) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   114
                    // found - stop looking for this option
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   115
                    found = true;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   116
                    break;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   117
                }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   118
            }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   119
            // not found - remove this selected element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   120
            if (!found) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   121
                this.selected.splice(i, 1);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   122
                // since we removed an entry we must re-check this position
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   123
                i--;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   124
            }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   125
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   126
    } else {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   127
        // single selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   128
        if (this.selected != null) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   129
            // check that the selected option exists among the options
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   130
            for (var i = 0; i < this.options.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   131
                if (this.options[i] == this.selected) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   132
                    // found - we're done
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   133
                    return;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   134
                }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   135
            }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   136
            // not found - remove the selection
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   137
            this.selected = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   138
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   139
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   140
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   141
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   142
// Returns the options in the control as an array of option objects with
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   143
// a value and text property.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   144
SelectionControl.prototype.getOptions = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   145
    return this.options;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   146
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   147
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   148
// Sets the options in the control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   149
// Override in sublcasses to provide full implementation.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   150
SelectionControl.prototype.setOptions = function(options) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   151
    this.options = options.slice(0);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   152
    // make sure the selected option or options are legal
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   153
    this.validateSelected();
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   154
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   155
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   156
// Returns the option that has the specified value; null if none.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   157
SelectionControl.prototype.getOptionForValue = function(value) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   158
    // iterate through all options and look for a match
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   159
    for (var i = 0; i < this.options.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   160
        if (this.options[i].value == value) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   161
            return this.options[i];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   162
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   163
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   164
    return null;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   165
};