org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/SelectionControl.js
changeset 102 30e0796f3ebb
parent 73 c56c874eef47
child 210 0f7abfd6ae62
equal deleted inserted replaced
101:15f3b303bbb1 102:30e0796f3ebb
    80         this.selected = (selected == null) ? [] : selected.slice(0);
    80         this.selected = (selected == null) ? [] : selected.slice(0);
    81     } else {
    81     } else {
    82         this.selected = selected;
    82         this.selected = selected;
    83     }
    83     }
    84     this.validateSelected();
    84     this.validateSelected();
    85 }
    85 };
    86 
    86 
    87 // Returns true if the control is a multiple selection control; false if single.
    87 // Returns true if the control is a multiple selection control; false if single.
    88 SelectionControl.prototype.isMultipleSelection = function() {
    88 SelectionControl.prototype.isMultipleSelection = function() {
    89     return this.multipleSelection;
    89     return this.multipleSelection;
    90 }
    90 };
    91 
    91 
    92 // Returns true if the specified option is selected; false if not.
    92 // Returns true if the specified option is selected; false if not.
    93 SelectionControl.prototype.isSelected = function(option) {
    93 SelectionControl.prototype.isSelected = function(option) {
    94     if (this.multipleSelection) {
    94     if (this.multipleSelection) {
    95         // multiple selection
    95         // multiple selection
   102         return false;
   102         return false;
   103     } else {
   103     } else {
   104         // single selection
   104         // single selection
   105         return (this.selected == option);
   105         return (this.selected == option);
   106     }
   106     }
   107 }
   107 };
   108 
   108 
   109 // Returns the currently selected option in a single selection control or
   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
   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
   111 // no selected options a single selection control returns null and a multiple
   112 // selection control returns an empty array.
   112 // selection control returns an empty array.
   113 SelectionControl.prototype.getSelected = function() {
   113 SelectionControl.prototype.getSelected = function() {
   114     return this.multipleSelection ? this.selected.slice(0) : this.selected;
   114     return this.multipleSelection ? this.selected.slice(0) : this.selected;
   115 }
   115 };
   116 
   116 
   117 // Sets the currently selected options. Pass a single option in a single selection
   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
   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
   119 // deselect all options pass null in a single selection control and an empty array
   120 // in a multiple selection control.
   120 // in a multiple selection control.
   121 // Override in sublcasses to provide full implementation.
   121 // Override in sublcasses to provide full implementation.
   122 SelectionControl.prototype.setSelected = function(selected) {
   122 SelectionControl.prototype.setSelected = function(selected) {
   123     this.selected = this.multipleSelection ? selected.slice(0) : selected;
   123     this.selected = this.multipleSelection ? selected.slice(0) : selected;
   124     // make sure the selected option or options are legal
   124     // make sure the selected option or options are legal
   125     this.validateSelected();
   125     this.validateSelected();
   126 }
   126 };
   127 
   127 
   128 // Ensures that the selected option or options exist among the options in this control.
   128 // Ensures that the selected option or options exist among the options in this control.
   129 SelectionControl.prototype.validateSelected = function() {
   129 SelectionControl.prototype.validateSelected = function() {
   130     if (this.multipleSelection) {
   130     if (this.multipleSelection) {
   131         // multiple selection
   131         // multiple selection
   159             }
   159             }
   160             // not found - remove the selection
   160             // not found - remove the selection
   161             this.selected = null;
   161             this.selected = null;
   162         }
   162         }
   163     }
   163     }
   164 }
   164 };
   165 
   165 
   166 // Returns the options in the control as an array of option objects with
   166 // Returns the options in the control as an array of option objects with
   167 // a value and text property.
   167 // a value and text property.
   168 SelectionControl.prototype.getOptions = function() {
   168 SelectionControl.prototype.getOptions = function() {
   169     return this.options;
   169     return this.options;
   170 }
   170 };
   171 
   171 
   172 // Sets the options in the control.
   172 // Sets the options in the control.
   173 // Override in sublcasses to provide full implementation.
   173 // Override in sublcasses to provide full implementation.
   174 SelectionControl.prototype.setOptions = function(options) {
   174 SelectionControl.prototype.setOptions = function(options) {
   175     this.options = options.slice(0);
   175     this.options = options.slice(0);
   176     // make sure the selected option or options are legal
   176     // make sure the selected option or options are legal
   177     this.validateSelected();
   177     this.validateSelected();
   178 }
   178 };
   179 
   179 
   180 // Returns the option that has the specified value; null if none.
   180 // Returns the option that has the specified value; null if none.
   181 SelectionControl.prototype.getOptionForValue = function(value) {
   181 SelectionControl.prototype.getOptionForValue = function(value) {
   182     // iterate through all options and look for a match
   182     // iterate through all options and look for a match
   183     for (var i = 0; i < this.options.length; i++) {
   183     for (var i = 0; i < this.options.length; i++) {
   184         if (this.options[i].value == value) {
   184         if (this.options[i].value == value) {
   185             return this.options[i];
   185             return this.options[i];
   186         }
   186         }
   187     }
   187     }
   188     return null;
   188     return null;
   189 }
   189 };