See09/WRTKit/UI/SelectionControl.js
changeset 19 f3521a11d878
equal deleted inserted replaced
18:b73e6caf0031 19:f3521a11d878
       
     1 /*
       
     2 © Copyright 2008 Nokia Corporation. All rights reserved.
       
     3 
       
     4 IMPORTANT:  The Nokia software ("WRTKit and Example Widget files") is supplied to you by Nokia
       
     5 Corporation (“Nokia”) in consideration of your agreement to the following terms. Your use, installation
       
     6 and/or redistribution of the WRTKit and Example Widget files constitutes acceptance of these terms. If
       
     7 you do not agree with these terms, please do not use, install, or redistribute the WRTKit and Example
       
     8 Widget files.
       
     9 
       
    10 In consideration of your agreement to abide by the following terms, and subject to these terms, Nokia
       
    11 grants you a personal, non-exclusive license, under Nokia’s copyrights in the WRTKit and Example
       
    12 Widget files, to use, reproduce, and redistribute the WRTKit and Example files, in text form (for HTML,
       
    13 CSS, or JavaScript files) or binary form (for associated images), for the sole purpose of creating S60
       
    14 Widgets.
       
    15 
       
    16 If you redistribute the WRTKit and Example files, you must retain this entire notice in all such
       
    17 redistributions of the WRTKit and Example files.
       
    18 
       
    19 You may not use the name, trademarks, service marks or logos of Nokia to endorse or promote products
       
    20 that include the WRTKit and Example files without the prior written explicit agreement with Nokia.
       
    21 Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by
       
    22 Nokia herein, including but not limited to any patent rights that may be infringed by your products that
       
    23 incorporate the WRTKit and Example files or by other works in which the WRTKit and Example files
       
    24 may be incorporated.
       
    25 
       
    26 The WRTKit and Example files are provided on an "AS IS" basis.  NOKIA MAKES NO
       
    27 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
       
    28 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A
       
    29 PARTICULAR PURPOSE, REGARDING THE EXAMPLES OR ITS USE AND OPERATION
       
    30 ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
       
    31 
       
    32 IN NO EVENT SHALL NOKIA BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
       
    33 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    34 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    35 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, AND/OR
       
    36 DISTRIBUTION OF THE EXAMPLES, HOWEVER CAUSED AND WHETHER UNDER THEORY
       
    37 OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE,
       
    38 EVEN IF NOKIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    39 
       
    40 */
       
    41 
       
    42 ///////////////////////////////////////////////////////////////////////////////
       
    43 // The SelectionControl class is an abstract base class for controls that lets
       
    44 // the user select one or more options from a list of options. Don't use
       
    45 // SelectionControl directly.
       
    46 
       
    47 // Constructor.
       
    48 function SelectionControl(id, caption, options, multipleSelection, selected) {
       
    49     if (id != UI_NO_INIT_ID) {
       
    50         this.init(id, caption, options, multipleSelection, selected);
       
    51     }
       
    52 }
       
    53 
       
    54 // SelectionControl inherits from Control.
       
    55 SelectionControl.prototype = new Control(UI_NO_INIT_ID);
       
    56 
       
    57 // List of options.
       
    58 SelectionControl.prototype.options = null;
       
    59 
       
    60 // The single selected option in single selection controls
       
    61 // or list of options in multi selection controls.
       
    62 SelectionControl.prototype.selected = null;
       
    63 
       
    64 // Single or multiple selection.
       
    65 SelectionControl.prototype.multipleSelection = false;
       
    66 
       
    67 // Initializer - called from constructor.
       
    68 SelectionControl.prototype.init = function(id, caption, options, multipleSelection, selected) {
       
    69     uiLogger.debug("SelectionControl.init(" + id + ", " + caption + ", " + options + ", " + multipleSelection + ", " + selected + ")");
       
    70     
       
    71     // call superclass initializer
       
    72     Control.prototype.init.call(this, id, caption);
       
    73     
       
    74     // set the multiple selection property
       
    75     this.multipleSelection = multipleSelection;
       
    76     
       
    77     // init options and selected (makes copies of the original arrays)
       
    78     this.options = (options != null) ? options.slice(0) : [];
       
    79     if (multipleSelection) {
       
    80         this.selected = (selected == null) ? [] : selected.slice(0);
       
    81     } else {
       
    82         this.selected = selected;
       
    83     }
       
    84     this.validateSelected();
       
    85 }
       
    86 
       
    87 // Returns true if the control is a multiple selection control; false if single.
       
    88 SelectionControl.prototype.isMultipleSelection = function() {
       
    89     return this.multipleSelection;
       
    90 }
       
    91 
       
    92 // Returns true if the specified option is selected; false if not.
       
    93 SelectionControl.prototype.isSelected = function(option) {
       
    94     if (this.multipleSelection) {
       
    95         // multiple selection
       
    96         // iterate through all selected options and look for the specified option
       
    97         for (var i = 0; i < this.selected.length; i++) {
       
    98             if (this.selected[i] == option) {
       
    99                 return true;
       
   100             }
       
   101         }
       
   102         return false;
       
   103     } else {
       
   104         // single selection
       
   105         return (this.selected == option);
       
   106     }
       
   107 }
       
   108 
       
   109 // Returns the currently selected option in a single selection control or
       
   110 // an array of selected options in a multiple selection control. If there are
       
   111 // no selected options a single selection control returns null and a multiple
       
   112 // selection control returns an empty array.
       
   113 SelectionControl.prototype.getSelected = function() {
       
   114     return this.multipleSelection ? this.selected.slice(0) : this.selected;
       
   115 }
       
   116 
       
   117 // Sets the currently selected options. Pass a single option in a single selection
       
   118 // control or an array of selected controls in a multiple selection control. To
       
   119 // deselect all options pass null in a single selection control and an empty array
       
   120 // in a multiple selection control.
       
   121 // Override in sublcasses to provide full implementation.
       
   122 SelectionControl.prototype.setSelected = function(selected) {
       
   123     this.selected = this.multipleSelection ? selected.slice(0) : selected;
       
   124     // make sure the selected option or options are legal
       
   125     this.validateSelected();
       
   126 }
       
   127 
       
   128 // Ensures that the selected option or options exist among the options in this control.
       
   129 SelectionControl.prototype.validateSelected = function() {
       
   130     if (this.multipleSelection) {
       
   131         // multiple selection
       
   132         // iterate through all selected options and ensure they exist among the options
       
   133         for (var i = 0; i < this.selected.length; i++) {
       
   134             // check that the selected option exists among the options
       
   135             var found = false;
       
   136             for (var j = 0; j < this.options.length; j++) {
       
   137                 if (this.options[j] == this.selected[i]) {
       
   138                     // found - stop looking for this option
       
   139                     found = true;
       
   140                     break;
       
   141                 }
       
   142             }
       
   143             // not found - remove this selected element
       
   144             if (!found) {
       
   145                 this.selected.splice(i, 1);
       
   146                 // since we removed an entry we must re-check this position
       
   147                 i--;
       
   148             }
       
   149         }
       
   150     } else {
       
   151         // single selection
       
   152         if (this.selected != null) {
       
   153             // check that the selected option exists among the options
       
   154             for (var i = 0; i < this.options.length; i++) {
       
   155                 if (this.options[i] == this.selected) {
       
   156                     // found - we're done
       
   157                     return;
       
   158                 }
       
   159             }
       
   160             // not found - remove the selection
       
   161             this.selected = null;
       
   162         }
       
   163     }
       
   164 }
       
   165 
       
   166 // Returns the options in the control as an array of option objects with
       
   167 // a value and text property.
       
   168 SelectionControl.prototype.getOptions = function() {
       
   169     return this.options;
       
   170 }
       
   171 
       
   172 // Sets the options in the control.
       
   173 // Override in sublcasses to provide full implementation.
       
   174 SelectionControl.prototype.setOptions = function(options) {
       
   175     this.options = options.slice(0);
       
   176     // make sure the selected option or options are legal
       
   177     this.validateSelected();
       
   178 }
       
   179 
       
   180 // Returns the option that has the specified value; null if none.
       
   181 SelectionControl.prototype.getOptionForValue = function(value) {
       
   182     // iterate through all options and look for a match
       
   183     for (var i = 0; i < this.options.length; i++) {
       
   184         if (this.options[i].value == value) {
       
   185             return this.options[i];
       
   186         }
       
   187     }
       
   188     return null;
       
   189 }