Wikipedia/WRTKit/UI/ListView.js
changeset 20 918767a9c8d3
equal deleted inserted replaced
19:f3521a11d878 20:918767a9c8d3
       
     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 ListView class implements a vertical list view that hosts controls
       
    44 // as child components.
       
    45 
       
    46 // Constructor.
       
    47 function ListView(id, caption) {
       
    48     if (id != UI_NO_INIT_ID) {
       
    49         this.init(id, caption);
       
    50     }
       
    51 }
       
    52 
       
    53 // ListView inherits from View.
       
    54 ListView.prototype = new View(UI_NO_INIT_ID);
       
    55 
       
    56 // The caption of this view; null if none.
       
    57 ListView.prototype.caption = null;
       
    58 
       
    59 // The caption element of this view.
       
    60 ListView.prototype.captionElement = null;
       
    61 
       
    62 // The caption text element of this view.
       
    63 ListView.prototype.captionTextElement = null;
       
    64 
       
    65 // Root HTML element for controls.
       
    66 ListView.prototype.listElement = null;
       
    67 
       
    68 // List of controls in the view.
       
    69 ListView.prototype.controls = null;
       
    70 
       
    71 // Initializer for ListView.
       
    72 ListView.prototype.init = function(id, caption) {
       
    73     uiLogger.debug("ListView.init(" + id + ", " + caption + ")");
       
    74     
       
    75     // call superclass initializer
       
    76     View.prototype.init.call(this, id);
       
    77     
       
    78     // init control array
       
    79     this.controls = [];
       
    80     
       
    81     // set style class name for root element
       
    82     this.rootElement.className = "ListView";
       
    83     
       
    84     // create caption and caption text elements
       
    85     this.captionElement = document.createElement("div");
       
    86     this.captionElement.className = "ListViewCaptionText";
       
    87     this.rootElement.appendChild(this.captionElement);
       
    88     
       
    89     // create root element for controls and add to the view root element
       
    90     this.listElement = document.createElement("div");
       
    91     this.listElement.className = "ListViewControlList";
       
    92     this.rootElement.appendChild(this.listElement);
       
    93     
       
    94     // set the caption
       
    95     this.setCaption(caption);
       
    96 }
       
    97 
       
    98 // Returns the caption; null if none.
       
    99 ListView.prototype.getCaption = function() {
       
   100     return this.caption;
       
   101 }
       
   102 
       
   103 // Sets the caption; null if none.
       
   104 ListView.prototype.setCaption = function(caption) {
       
   105     uiLogger.debug("ListView.setCaption(" + caption + ")");
       
   106     
       
   107     // set the display style
       
   108     this.captionElement.style.display = (caption == null) ? "none" : "block";
       
   109     
       
   110     // set the caption
       
   111     this.caption = caption;
       
   112     this.captionElement.innerHTML = (caption == null) ? "" : caption;
       
   113 }
       
   114 
       
   115 // Returns an array of controls in the view.
       
   116 ListView.prototype.getControls = function() {
       
   117     return this.controls;
       
   118 }
       
   119 
       
   120 // Adds a control to the view.
       
   121 ListView.prototype.addControl = function(control) {
       
   122     uiLogger.debug("ListView.addControl(" + control + ")");
       
   123     
       
   124     // add the control to the controls array and attach it to the list element
       
   125     this.controls.push(control);
       
   126     this.listElement.appendChild(control.rootElement);
       
   127     control.view = this;
       
   128 }
       
   129 
       
   130 // Inserts a control to the view before the specified control.
       
   131 ListView.prototype.insertControl = function(control, beforeControl) {
       
   132     uiLogger.debug("ListView.insertControl(" + control + ", " + beforeControl + ")");
       
   133     
       
   134     // iterate through current controls
       
   135     for (var i = 0; i < this.controls.length; i++) {
       
   136         // is this the control we should insert before?
       
   137         if (this.controls[i] == beforeControl) {
       
   138             // we found the control to insert before - insert here and connect to list element
       
   139             this.controls.splice(i, 0, control);
       
   140             this.listElement.insertBefore(control.rootElement, beforeControl.rootElement);
       
   141             control.view = this;
       
   142             return;
       
   143         }
       
   144     }
       
   145     
       
   146     // the control wasn't found so we'll add it last
       
   147     this.addControl(control);
       
   148 }
       
   149 
       
   150 // Removes a control from the view.
       
   151 ListView.prototype.removeControl = function(control) {
       
   152     uiLogger.debug("ListView.removeControl(" + control + ")");
       
   153     
       
   154     // iterate through current controls
       
   155     for (var i = 0; i < this.controls.length; i++) {
       
   156         // is this the control we should remove?
       
   157         if (this.controls[i] == control) {
       
   158             // we found the control to remove - remove it from the list element
       
   159             this.controls.splice(i, 1);
       
   160             this.listElement.removeChild(control.rootElement);
       
   161             control.view = null;
       
   162         }
       
   163     }
       
   164 }
       
   165 
       
   166 // Removes a control from the view.
       
   167 ListView.prototype.removeAllControls = function() {
       
   168     // iterate through current controls
       
   169     for (var i = 0; i < this.controls.length; i++) {
       
   170         // we found the control to remove - remove it from the list element
       
   171 		var control = this.controls[i];
       
   172         this.listElement.removeChild(control.rootElement);
       
   173         control.view = null;
       
   174     }
       
   175     this.controls = new Array();
       
   176 }
       
   177 
       
   178 
       
   179 // Attempts to focus the first focusable control.
       
   180 ListView.prototype.focusFirstControl = function() {
       
   181     uiLogger.debug("ListView.focusFirstControl()");
       
   182     for (var i = 0; i < this.controls.length; i++) {
       
   183         // is this control focusable?
       
   184         var control = this.controls[i];
       
   185         if (control.isFocusable()) {
       
   186             control.setFocused(true);
       
   187             break;
       
   188         }
       
   189     }
       
   190 }
       
   191 
       
   192 // Attempts to reset all control focus states.
       
   193 // Override in subclasses as required.
       
   194 ListView.prototype.resetControlFocusStates = function() {
       
   195     uiLogger.debug("ListView.resetControlFocusStates()");
       
   196     for (var i = 0; i < this.controls.length; i++) {
       
   197         this.controls[i].resetFocusState();
       
   198     }
       
   199 }