home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / chatzilla.jar / content / chatzilla / lib / js / events.js < prev    next >
Text File  |  2000-06-01  |  6KB  |  245 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. }
  94.  
  95. CEventPump.prototype.addHook = 
  96. function ep_addhook(pattern, f, name, neg, enabled, hooks)
  97. {
  98.     if (typeof hooks == "undefined")
  99.         hooks = this.hooks;
  100.     
  101.     if (typeof f != "function")
  102.         return false;
  103.  
  104.     if (typeof enabled == "undefined")
  105.         enabled = true;
  106.     else
  107.         enabled = Boolean(enabled);
  108.  
  109.     neg = Boolean(neg);
  110.  
  111.     return (hooks.push({pattern: pattern, f: f, name: name,
  112.                        neg: neg, enabled: enabled}) - 1);
  113.  
  114. }
  115.  
  116. CEventPump.prototype.getHook = 
  117. function ep_gethook(name, hooks)
  118. {
  119.     if (typeof hooks == "undefined")
  120.         hooks = this.hooks;
  121.  
  122.     for (var h in hooks)
  123.         if (hooks[h].name.toLowerCase() == name.toLowerCase())
  124.             return hooks[h];
  125.  
  126.     return null;
  127.  
  128. }
  129.  
  130. CEventPump.prototype.removeHookByName = 
  131. function ep_remhookname(name, hooks)
  132. {
  133.     if (typeof hooks == "undefined")
  134.         hooks = this.hooks;
  135.  
  136.     for (var h in hooks)
  137.         if (hooks[h].name.toLowerCase() == name.toLowerCase())
  138.         {
  139.             arrayRemoveAt (hooks, h);
  140.             return true;
  141.         }
  142.  
  143.     return false;
  144.  
  145. }
  146.  
  147. CEventPump.prototype.removeHookByIndex = 
  148. function ep_remhooki(idx, hooks)
  149. {
  150.     if (typeof hooks == "undefined")
  151.         hooks = this.hooks;
  152.  
  153.     return arrayRemoveAt (hooks, idx);
  154.  
  155. }
  156.  
  157. CEventPump.prototype.addEvent = 
  158. function ep_addevent (e)
  159. {
  160.  
  161.     e.queuedAt = new Date();
  162.     arrayInsertAt(this.queue, 0, e);
  163.     return true;
  164.     
  165. }
  166.  
  167. CEventPump.prototype.routeEvent = 
  168. function ep_routeevent (e)
  169. {
  170.     var count = 0;
  171.  
  172.     this.currentEvent = e;
  173.         
  174.     e.level = 0;
  175.     while (e.destObject)
  176.     {
  177.         e.level++;
  178.         this.onHook (e);
  179.         var destObject = e.destObject;
  180.         e.destObject = (void 0);
  181.         
  182.         switch (typeof destObject[e.destMethod])
  183.         {
  184.             case "function":
  185.                 if (1)
  186.                     try
  187.                     {
  188.                         destObject[e.destMethod] (e);
  189.                     }
  190.                     catch (ex)
  191.                     {
  192.                         dd ("Error routing event: " + ex + " in " +
  193.                             e.destMethod);
  194.                     }
  195.                 else
  196.                     destObject[e.destMethod] (e);
  197.  
  198.                 if (count++ > this.MAX_EVENT_DEPTH)
  199.                     throw "Too many events in chain";
  200.                 break;
  201.  
  202.             case "undefined":
  203.                 //dd ("** " + e.destMethod + " does not exist.");
  204.                 break;
  205.  
  206.             default:
  207.                 dd ("** " + e.destMethod + " is not a function.");
  208.         }
  209.  
  210.         if ((e.type != "event-end") && (!e.destObject))
  211.         {
  212.             e.lastSet = e.set;
  213.             e.set = "eventpump";
  214.             e.lastType = e.type;
  215.             e.type = "event-end";
  216.             e.destMethod = "onEventEnd";
  217.             e.destObject = this;
  218.         }
  219.  
  220.     }
  221.  
  222.     delete this.currentEvent;
  223.  
  224.     return true;
  225.     
  226. }
  227.  
  228. CEventPump.prototype.stepEvents = 
  229. function ep_stepevents()
  230. {
  231.     var i = 0;
  232.     
  233.     while (i < this.eventsPerStep)
  234.     {
  235.         var e = this.queue.pop();
  236.         if (!e || e.type == "yield")
  237.             break;
  238.         this.routeEvent (e);
  239.         i++;
  240.     }
  241.  
  242.     return true;
  243.     
  244. }
  245.