org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/ListView.js
author Eugene Ostroukhov <eugeneo@symbian.org>
Tue, 16 Mar 2010 18:01:11 -0700
changeset 273 b1f63c2c240c
parent 211 7b148352159e
permissions -rw-r--r--
Bug 2072 - Update .js files with EPL
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 ListView class implements a vertical list view that hosts controls
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    20
// as child components.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    21
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    22
// Constructor.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    23
function ListView(id, caption) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    24
    if (id != UI_NO_INIT_ID) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    25
        this.init(id, caption);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    26
    }
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
// ListView inherits from View.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    30
ListView.prototype = new View(UI_NO_INIT_ID);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    31
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    32
// The caption of this view; null if none.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    33
ListView.prototype.caption = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    34
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    35
// The caption element of this view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    36
ListView.prototype.captionElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    37
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    38
// The caption text element of this view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    39
ListView.prototype.captionTextElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    40
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    41
// Root HTML element for controls.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    42
ListView.prototype.listElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    43
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    44
// List of controls in the view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    45
ListView.prototype.controls = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    46
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    47
// Initializer for ListView.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    48
ListView.prototype.init = function(id, caption) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    49
    uiLogger.debug("ListView.init(" + id + ", " + caption + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    50
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    51
    // call superclass initializer
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    52
    View.prototype.init.call(this, id);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    53
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    54
    // init control array
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    55
    this.controls = [];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    56
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    57
    // set style class name for root element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    58
    this.rootElement.className = "ListView";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    59
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    60
    // create caption and caption text elements
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    61
    this.captionElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    62
    this.captionElement.className = "ListViewCaption";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    63
    this.captionTextElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    64
    this.captionTextElement.className = "ListViewCaptionText";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    65
    this.captionElement.appendChild(this.captionTextElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    66
    this.rootElement.appendChild(this.captionElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    67
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    68
    // create root element for controls and add to the view root element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    69
    this.listElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    70
    this.listElement.className = "ListViewControlList";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    71
    this.rootElement.appendChild(this.listElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    72
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    73
    // set the caption
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    74
    this.setCaption(caption);
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    75
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    76
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    77
// Returns the caption; null if none.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    78
ListView.prototype.getCaption = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    79
    return this.caption;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    80
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    81
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    82
// Sets the caption; null if none.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    83
ListView.prototype.setCaption = function(caption) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    84
    uiLogger.debug("ListView.setCaption(" + caption + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    85
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    86
    // set the display style
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    87
    this.captionElement.style.display = (caption == null) ? "none" : "block";
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    88
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    89
    // set the caption
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    90
    this.caption = caption;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    91
    this.captionTextElement.innerHTML = (caption == null) ? "" : caption;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    92
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    93
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    94
// Returns an array of controls in the view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    95
ListView.prototype.getControls = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    96
    return this.controls;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    97
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    98
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    99
// Adds a control to the view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   100
ListView.prototype.addControl = function(control) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   101
    uiLogger.debug("ListView.addControl(" + control + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   102
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   103
    // add the control to the controls array and attach it to the list element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   104
    this.controls.push(control);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   105
    this.listElement.appendChild(control.rootElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   106
    control.view = this;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   107
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   108
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   109
// Inserts a control to the view before the specified control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   110
ListView.prototype.insertControl = function(control, beforeControl) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   111
    uiLogger.debug("ListView.insertControl(" + control + ", " + beforeControl + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   112
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   113
    // iterate through current controls
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   114
    for (var i = 0; i < this.controls.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   115
        // is this the control we should insert before?
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   116
        if (this.controls[i] == beforeControl) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   117
            // we found the control to insert before - insert here and connect to list element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   118
            this.controls.splice(i, 0, control);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   119
            this.listElement.insertBefore(control.rootElement, beforeControl.rootElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   120
            control.view = this;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   121
            return;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   122
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   123
    }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   124
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   125
    // the control wasn't found so we'll add it last
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   126
    this.addControl(control);
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   127
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   128
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   129
// Removes a control from the view.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   130
ListView.prototype.removeControl = function(control) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   131
    uiLogger.debug("ListView.removeControl(" + control + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   132
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   133
    // iterate through current controls
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   134
    for (var i = 0; i < this.controls.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   135
        // is this the control we should remove?
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   136
        if (this.controls[i] == control) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   137
            // we found the control to remove - remove it from the list element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   138
            this.controls.splice(i, 1);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   139
            this.listElement.removeChild(control.rootElement);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   140
            control.view = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   141
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   142
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   143
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   144
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   145
// Attempts to focus the first focusable control.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   146
ListView.prototype.focusFirstControl = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   147
    uiLogger.debug("ListView.focusFirstControl()");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   148
    for (var i = 0; i < this.controls.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   149
        // is this control focusable?
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   150
        var control = this.controls[i];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   151
        if (control.isFocusable()) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   152
            control.setFocused(true);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   153
            break;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   154
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   155
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   156
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   157
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   158
// Attempts to reset all control focus states.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   159
// Override in subclasses as required.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   160
ListView.prototype.resetControlFocusStates = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   161
    uiLogger.debug("ListView.resetControlFocusStates()");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   162
    for (var i = 0; i < this.controls.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   163
        this.controls[i].resetFocusState();
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
   164
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
   165
};