org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/UIElement.js
changeset 309 c01f5ab28a11
parent 308 c521df56b15d
child 310 e9484be98cfe
equal deleted inserted replaced
308:c521df56b15d 309:c01f5ab28a11
     1 /**
       
     2  * Copyright (c) 2009-2010 Symbian Foundation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  * 
       
    12  * Contributors:
       
    13  * 
       
    14  * Description:
       
    15  * 
       
    16  */
       
    17 
       
    18 ///////////////////////////////////////////////////////////////////////////////
       
    19 // The UIElement class is the base class for all user interface elements.
       
    20 
       
    21 // Constructor.
       
    22 function UIElement(id) {
       
    23     if (id != UI_NO_INIT_ID) {
       
    24         this.init(id);
       
    25     }
       
    26 }
       
    27 
       
    28 // UI element identifier.
       
    29 UIElement.prototype.id = null;
       
    30 
       
    31 // Root HTML element in the UI element.
       
    32 UIElement.prototype.rootElement = null;
       
    33 
       
    34 // Initializer for UIElement.
       
    35 UIElement.prototype.init = function(id) {
       
    36     uiLogger.debug("UIElement.init(" + id + ")");
       
    37     
       
    38     // copy identifier
       
    39     this.id = id;
       
    40     
       
    41     // init event listener array
       
    42     this.eventListeners = [];
       
    43     
       
    44     // create the root element
       
    45     this.rootElement = document.createElement("div");
       
    46     if (id != null) {
       
    47         this.rootElement.id = id;
       
    48     }
       
    49 };
       
    50 
       
    51 // Returns an array containing the current event listeners.
       
    52 UIElement.prototype.getEventListeners = function() {
       
    53     return this.eventListeners;
       
    54 };
       
    55 
       
    56 // Adds an event listener.
       
    57 UIElement.prototype.addEventListener = function(eventType, listener) {
       
    58     var listenerDef = { type: eventType, listener: listener };
       
    59     this.eventListeners.push(listenerDef);
       
    60 };
       
    61 
       
    62 // Removes an event listener.
       
    63 UIElement.prototype.removeEventListener = function(eventType, listener) {
       
    64     // iterate through current listeners and remove the specified
       
    65     // listener when its found
       
    66     for (var i = 0; i < this.eventListeners.length; i++) {
       
    67         var listenerDef = this.eventListeners[i];
       
    68         if ((listenerDef.type == eventType) &&
       
    69                 (listenerDef.listener == listener)) {
       
    70             this.eventListeners.splice(i, 1);
       
    71             return;
       
    72         }
       
    73     }
       
    74 };
       
    75 
       
    76 // Factory method for an event object where this object is the source object.
       
    77 UIElement.prototype.createEvent = function(type, value) {
       
    78     return { source: this, type: type, value: value };
       
    79 };
       
    80 
       
    81 // Fires an event to all listeners.
       
    82 UIElement.prototype.fireEvent = function(event) {
       
    83     // iterate through all event listeners and notify them of the event
       
    84     for (var i = 0; i < this.eventListeners.length; i++) {
       
    85         var listenerDef = this.eventListeners[i];
       
    86         if (listenerDef.type == null || listenerDef.type == event.type) {
       
    87             listenerDef.listener.call(this, event);
       
    88         }
       
    89     }
       
    90 };