org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/UIElement.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 UIElement class is the base class for all user interface elements.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    20
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    21
// Constructor.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    22
function UIElement(id) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    23
    if (id != UI_NO_INIT_ID) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    24
        this.init(id);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    25
    }
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
// UI element identifier.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    29
UIElement.prototype.id = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    30
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    31
// Root HTML element in the UI element.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    32
UIElement.prototype.rootElement = null;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    33
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    34
// Initializer for UIElement.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    35
UIElement.prototype.init = function(id) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    36
    uiLogger.debug("UIElement.init(" + id + ")");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    37
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    38
    // copy identifier
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    39
    this.id = id;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    40
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    41
    // init event listener array
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    42
    this.eventListeners = [];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    43
    
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    44
    // create the root element
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    45
    this.rootElement = document.createElement("div");
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    46
    if (id != null) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    47
        this.rootElement.id = id;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    48
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    49
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    50
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    51
// Returns an array containing the current event listeners.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    52
UIElement.prototype.getEventListeners = function() {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    53
    return this.eventListeners;
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    54
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    55
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    56
// Adds an event listener.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    57
UIElement.prototype.addEventListener = function(eventType, listener) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    58
    var listenerDef = { type: eventType, listener: listener };
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    59
    this.eventListeners.push(listenerDef);
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    60
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    61
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    62
// Removes an event listener.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    63
UIElement.prototype.removeEventListener = function(eventType, listener) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    64
    // iterate through current listeners and remove the specified
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    65
    // listener when its found
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    66
    for (var i = 0; i < this.eventListeners.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    67
        var listenerDef = this.eventListeners[i];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    68
        if ((listenerDef.type == eventType) &&
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    69
                (listenerDef.listener == listener)) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    70
            this.eventListeners.splice(i, 1);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    71
            return;
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    72
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    73
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    74
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    75
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    76
// Factory method for an event object where this object is the source object.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    77
UIElement.prototype.createEvent = function(type, value) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    78
    return { source: this, type: type, value: value };
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    79
};
73
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    80
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    81
// Fires an event to all listeners.
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    82
UIElement.prototype.fireEvent = function(event) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    83
    // iterate through all event listeners and notify them of the event
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    84
    for (var i = 0; i < this.eventListeners.length; i++) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    85
        var listenerDef = this.eventListeners[i];
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    86
        if (listenerDef.type == null || listenerDef.type == event.type) {
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    87
            listenerDef.listener.call(this, event);
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    88
        }
c56c874eef47 Added WRTKit
tasneems@symbian.org
parents:
diff changeset
    89
    }
102
30e0796f3ebb Warnings in new projects were fixed
Eugene Ostroukhov <eugeneo@symbian.org>
parents: 73
diff changeset
    90
};