org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/ListView.js
changeset 73 c56c874eef47
child 102 30e0796f3ebb
equal deleted inserted replaced
72:8373462680d7 73:c56c874eef47
       
     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 = "ListViewCaption";
       
    87     this.captionTextElement = document.createElement("div");
       
    88     this.captionTextElement.className = "ListViewCaptionText";
       
    89     this.captionElement.appendChild(this.captionTextElement);
       
    90     this.rootElement.appendChild(this.captionElement);
       
    91     
       
    92     // create root element for controls and add to the view root element
       
    93     this.listElement = document.createElement("div");
       
    94     this.listElement.className = "ListViewControlList";
       
    95     this.rootElement.appendChild(this.listElement);
       
    96     
       
    97     // set the caption
       
    98     this.setCaption(caption);
       
    99 }
       
   100 
       
   101 // Returns the caption; null if none.
       
   102 ListView.prototype.getCaption = function() {
       
   103     return this.caption;
       
   104 }
       
   105 
       
   106 // Sets the caption; null if none.
       
   107 ListView.prototype.setCaption = function(caption) {
       
   108     uiLogger.debug("ListView.setCaption(" + caption + ")");
       
   109     
       
   110     // set the display style
       
   111     this.captionElement.style.display = (caption == null) ? "none" : "block";
       
   112     
       
   113     // set the caption
       
   114     this.caption = caption;
       
   115     this.captionTextElement.innerHTML = (caption == null) ? "" : caption;
       
   116 }
       
   117 
       
   118 // Returns an array of controls in the view.
       
   119 ListView.prototype.getControls = function() {
       
   120     return this.controls;
       
   121 }
       
   122 
       
   123 // Adds a control to the view.
       
   124 ListView.prototype.addControl = function(control) {
       
   125     uiLogger.debug("ListView.addControl(" + control + ")");
       
   126     
       
   127     // add the control to the controls array and attach it to the list element
       
   128     this.controls.push(control);
       
   129     this.listElement.appendChild(control.rootElement);
       
   130     control.view = this;
       
   131 }
       
   132 
       
   133 // Inserts a control to the view before the specified control.
       
   134 ListView.prototype.insertControl = function(control, beforeControl) {
       
   135     uiLogger.debug("ListView.insertControl(" + control + ", " + beforeControl + ")");
       
   136     
       
   137     // iterate through current controls
       
   138     for (var i = 0; i < this.controls.length; i++) {
       
   139         // is this the control we should insert before?
       
   140         if (this.controls[i] == beforeControl) {
       
   141             // we found the control to insert before - insert here and connect to list element
       
   142             this.controls.splice(i, 0, control);
       
   143             this.listElement.insertBefore(control.rootElement, beforeControl.rootElement);
       
   144             control.view = this;
       
   145             return;
       
   146         }
       
   147     }
       
   148     
       
   149     // the control wasn't found so we'll add it last
       
   150     this.addControl(control);
       
   151 }
       
   152 
       
   153 // Removes a control from the view.
       
   154 ListView.prototype.removeControl = function(control) {
       
   155     uiLogger.debug("ListView.removeControl(" + control + ")");
       
   156     
       
   157     // iterate through current controls
       
   158     for (var i = 0; i < this.controls.length; i++) {
       
   159         // is this the control we should remove?
       
   160         if (this.controls[i] == control) {
       
   161             // we found the control to remove - remove it from the list element
       
   162             this.controls.splice(i, 1);
       
   163             this.listElement.removeChild(control.rootElement);
       
   164             control.view = null;
       
   165         }
       
   166     }
       
   167 }
       
   168 
       
   169 // Attempts to focus the first focusable control.
       
   170 ListView.prototype.focusFirstControl = function() {
       
   171     uiLogger.debug("ListView.focusFirstControl()");
       
   172     for (var i = 0; i < this.controls.length; i++) {
       
   173         // is this control focusable?
       
   174         var control = this.controls[i];
       
   175         if (control.isFocusable()) {
       
   176             control.setFocused(true);
       
   177             break;
       
   178         }
       
   179     }
       
   180 }
       
   181 
       
   182 // Attempts to reset all control focus states.
       
   183 // Override in subclasses as required.
       
   184 ListView.prototype.resetControlFocusStates = function() {
       
   185     uiLogger.debug("ListView.resetControlFocusStates()");
       
   186     for (var i = 0; i < this.controls.length; i++) {
       
   187         this.controls[i].resetFocusState();
       
   188     }
       
   189 }