home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-mac-0.9.sea.hqx / mozilla-mac-0.9 / Chrome / chatzilla.jar / content / chatzilla / lib / js / events.js < prev    next >
Text File  |  2001-05-05  |  6KB  |  246 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is JSIRC Library
  14.  *
  15.  * The Initial Developer of the Original Code is New Dimensions Consulting,
  16.  * Inc. Portions created by New Dimensions Consulting, Inc. are
  17.  * Copyright (C) 1999 New Dimenstions Consulting, Inc. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *
  22.  *
  23.  * Contributor(s):
  24.  *  Robert Ginda, rginda@ndcico.com, original author
  25.  *
  26.  * depends on utils.js
  27.  *
  28.  * Event and EventPump classes.  The EventPump maintains a queue of event
  29.  * objects.  To inject an event into this queue, use |EventPump.addEvent|.
  30.  * |EventQueue.routeEvents| steps at most |EventPump.eventsPerStep|
  31.  * events, and then returns control.
  32.  *
  33.  * 1999-08-15 rginda@ndcico.com           v1.0
  34.  *
  35.  */
  36.  
  37. /*
  38.  * event class
  39.  */
  40.  
  41. function CEvent (set, type, destObject, destMethod)
  42. {
  43.  
  44.     this.set = set;
  45.     this.type = type;
  46.     this.destObject = destObject;
  47.     this.destMethod = destMethod;
  48.     this.hooks = new Array();
  49.  
  50. }
  51.  
  52. /*
  53.  * event pump
  54.  */
  55. function CEventPump (eventsPerStep)
  56. {
  57.  
  58.     /* event routing stops after this many levels, safety valve */
  59.     this.MAX_EVENT_DEPTH = 50;
  60.     this.eventsPerStep = eventsPerStep;
  61.     this.queue = new Array();
  62.     this.hooks = new Array();
  63.     
  64. }
  65.  
  66. CEventPump.prototype.onHook = 
  67. function ep_hook(e, hooks)
  68. {
  69.     var h;
  70.     
  71.     if (typeof hooks == "undefined")
  72.         hooks = this.hooks;
  73.     
  74.   hook_loop:
  75.     for (h = hooks.length - 1; h >= 0; h--)
  76.     {
  77.         if (!hooks[h].enabled ||
  78.             !matchObject (e, hooks[h].pattern, hooks[h].neg))
  79.             continue hook_loop;
  80.  
  81.         e.hooks.push (hooks[h]);
  82.         var rv = hooks[h].f(e);
  83.         if ((typeof rv == "boolean") &&
  84.             (rv == false))
  85.         {
  86.             dd ("hook #" + h + " '" + 
  87.                 ((typeof hooks[h].name != "undefined") ? hooks[h].name :
  88.                  "") + "' stopped hook processing.");
  89.             return true;
  90.         }
  91.     }
  92.  
  93.     return false;
  94. }
  95.  
  96. CEventPump.prototype.addHook = 
  97. function ep_addhook(pattern, f, name, neg, enabled, hooks)
  98. {
  99.     if (typeof hooks == "undefined")
  100.         hooks = this.hooks;
  101.     
  102.     if (typeof f != "function")
  103.         return false;
  104.  
  105.     if (typeof enabled == "undefined")
  106.         enabled = true;
  107.     else
  108.         enabled = Boolean(enabled);
  109.  
  110.     neg = Boolean(neg);
  111.  
  112.     return (hooks.push({pattern: pattern, f: f, name: name,
  113.                        neg: neg, enabled: enabled}) - 1);
  114.  
  115. }
  116.  
  117. CEventPump.prototype.getHook = 
  118. function ep_gethook(name, hooks)
  119. {
  120.     if (typeof hooks == "undefined")
  121.         hooks = this.hooks;
  122.  
  123.     for (var h in hooks)
  124.         if (hooks[h].name.toLowerCase() == name.toLowerCase())
  125.             return hooks[h];
  126.  
  127.     return null;
  128.  
  129. }
  130.  
  131. CEventPump.prototype.removeHookByName = 
  132. function ep_remhookname(name, hooks)
  133. {
  134.     if (typeof hooks == "undefined")
  135.         hooks = this.hooks;
  136.  
  137.     for (var h in hooks)
  138.         if (hooks[h].name.toLowerCase() == name.toLowerCase())
  139.         {
  140.             arrayRemoveAt (hooks, h);
  141.             return true;
  142.         }
  143.  
  144.     return false;
  145.  
  146. }
  147.  
  148. CEventPump.prototype.removeHookByIndex = 
  149. function ep_remhooki(idx, hooks)
  150. {
  151.     if (typeof hooks == "undefined")
  152.         hooks = this.hooks;
  153.  
  154.     return arrayRemoveAt (hooks, idx);
  155.  
  156. }
  157.  
  158. CEventPump.prototype.addEvent = 
  159. function ep_addevent (e)
  160. {
  161.  
  162.     e.queuedAt = new Date();
  163.     arrayInsertAt(this.queue, 0, e);
  164.     return true;
  165.     
  166. }
  167.  
  168. CEventPump.prototype.routeEvent = 
  169. function ep_routeevent (e)
  170. {
  171.     var count = 0;
  172.  
  173.     this.currentEvent = e;
  174.         
  175.     e.level = 0;
  176.     while (e.destObject)
  177.     {
  178.         e.level++;
  179.         this.onHook (e);
  180.         var destObject = e.destObject;
  181.         e.destObject = (void 0);
  182.         
  183.         switch (typeof destObject[e.destMethod])
  184.         {
  185.             case "function":
  186.                 if (1)
  187.                     try
  188.                     {
  189.                         destObject[e.destMethod] (e);
  190.                     }
  191.                     catch (ex)
  192.                     {
  193.                         dd ("Error routing event: " + ex + " in " +
  194.                             e.destMethod + "\n" + dumpObjectTree(ex));
  195.                     }
  196.                 else
  197.                     destObject[e.destMethod] (e);
  198.  
  199.                 if (count++ > this.MAX_EVENT_DEPTH)
  200.                     throw "Too many events in chain";
  201.                 break;
  202.  
  203.             case "undefined":
  204.                 //dd ("** " + e.destMethod + " does not exist.");
  205.                 break;
  206.  
  207.             default:
  208.                 dd ("** " + e.destMethod + " is not a function.");
  209.         }
  210.  
  211.         if ((e.type != "event-end") && (!e.destObject))
  212.         {
  213.             e.lastSet = e.set;
  214.             e.set = "eventpump";
  215.             e.lastType = e.type;
  216.             e.type = "event-end";
  217.             e.destMethod = "onEventEnd";
  218.             e.destObject = this;
  219.         }
  220.  
  221.     }
  222.  
  223.     delete this.currentEvent;
  224.  
  225.     return true;
  226.     
  227. }
  228.  
  229. CEventPump.prototype.stepEvents = 
  230. function ep_stepevents()
  231. {
  232.     var i = 0;
  233.     
  234.     while (i < this.eventsPerStep)
  235.     {
  236.         var e = this.queue.pop();
  237.         if (!e || e.type == "yield")
  238.             break;
  239.         this.routeEvent (e);
  240.         i++;
  241.     }
  242.  
  243.     return true;
  244.     
  245. }
  246.