org.symbian.tools.wrttools/projecttemplates/WRTKit/UI/UIElement.js
changeset 102 30e0796f3ebb
parent 73 c56c874eef47
child 210 0f7abfd6ae62
equal deleted inserted replaced
101:15f3b303bbb1 102:30e0796f3ebb
    68     // create the root element
    68     // create the root element
    69     this.rootElement = document.createElement("div");
    69     this.rootElement = document.createElement("div");
    70     if (id != null) {
    70     if (id != null) {
    71         this.rootElement.id = id;
    71         this.rootElement.id = id;
    72     }
    72     }
    73 }
    73 };
    74 
    74 
    75 // Returns an array containing the current event listeners.
    75 // Returns an array containing the current event listeners.
    76 UIElement.prototype.getEventListeners = function() {
    76 UIElement.prototype.getEventListeners = function() {
    77     return this.eventListeners;
    77     return this.eventListeners;
    78 }
    78 };
    79 
    79 
    80 // Adds an event listener.
    80 // Adds an event listener.
    81 UIElement.prototype.addEventListener = function(eventType, listener) {
    81 UIElement.prototype.addEventListener = function(eventType, listener) {
    82     var listenerDef = { type: eventType, listener: listener };
    82     var listenerDef = { type: eventType, listener: listener };
    83     this.eventListeners.push(listenerDef);
    83     this.eventListeners.push(listenerDef);
    84 }
    84 };
    85 
    85 
    86 // Removes an event listener.
    86 // Removes an event listener.
    87 UIElement.prototype.removeEventListener = function(eventType, listener) {
    87 UIElement.prototype.removeEventListener = function(eventType, listener) {
    88     // iterate through current listeners and remove the specified
    88     // iterate through current listeners and remove the specified
    89     // listener when its found
    89     // listener when its found
    93                 (listenerDef.listener == listener)) {
    93                 (listenerDef.listener == listener)) {
    94             this.eventListeners.splice(i, 1);
    94             this.eventListeners.splice(i, 1);
    95             return;
    95             return;
    96         }
    96         }
    97     }
    97     }
    98 }
    98 };
    99 
    99 
   100 // Factory method for an event object where this object is the source object.
   100 // Factory method for an event object where this object is the source object.
   101 UIElement.prototype.createEvent = function(type, value) {
   101 UIElement.prototype.createEvent = function(type, value) {
   102     return { source: this, type: type, value: value };
   102     return { source: this, type: type, value: value };
   103 }
   103 };
   104 
   104 
   105 // Fires an event to all listeners.
   105 // Fires an event to all listeners.
   106 UIElement.prototype.fireEvent = function(event) {
   106 UIElement.prototype.fireEvent = function(event) {
   107     // iterate through all event listeners and notify them of the event
   107     // iterate through all event listeners and notify them of the event
   108     for (var i = 0; i < this.eventListeners.length; i++) {
   108     for (var i = 0; i < this.eventListeners.length; i++) {
   109         var listenerDef = this.eventListeners[i];
   109         var listenerDef = this.eventListeners[i];
   110         if (listenerDef.type == null || listenerDef.type == event.type) {
   110         if (listenerDef.type == null || listenerDef.type == event.type) {
   111             listenerDef.listener.call(this, event);
   111             listenerDef.listener.call(this, event);
   112         }
   112         }
   113     }
   113     }
   114 }
   114 };