[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/scripts/ -> events.js (source)

   1  // written by Dean Edwards, 2005
   2  // with input from Tino Zijdel
   3  
   4  // http://dean.edwards.name/weblog/2005/10/add-event/
   5  
   6  function addEvent(element, type, handler) {
   7      // assign each event handler a unique ID
   8      if (!handler.$$guid) handler.$$guid = addEvent.guid++;
   9      // create a hash table of event types for the element
  10      if (!element.events) element.events = {};
  11      // create a hash table of event handlers for each element/event pair
  12      var handlers = element.events[type];
  13      if (!handlers) {
  14          handlers = element.events[type] = {};
  15          // store the existing event handler (if there is one)
  16          if (element["on" + type]) {
  17              handlers[0] = element["on" + type];
  18          }
  19      }
  20      // store the event handler in the hash table
  21      handlers[handler.$$guid] = handler;
  22      // assign a global event handler to do all the work
  23      element["on" + type] = handleEvent;
  24  };
  25  // a counter used to create unique IDs
  26  addEvent.guid = 1;
  27  
  28  function removeEvent(element, type, handler) {
  29      // delete the event handler from the hash table
  30      if (element.events && element.events[type]) {
  31          delete element.events[type][handler.$$guid];
  32      }
  33  };
  34  
  35  function handleEvent(event) {
  36      var returnValue = true;
  37      // grab the event object (IE uses a global event object)
  38      event = event || fixEvent(window.event);
  39      // get a reference to the hash table of event handlers
  40      var handlers = this.events[event.type];
  41      // execute each event handler
  42      for (var i in handlers) {
  43          if (!handlers.hasOwnProperty(i)) continue;
  44          this.$$handleEvent = handlers[i];
  45          if (this.$$handleEvent(event) === false) {
  46              returnValue = false;
  47          }
  48      }
  49      return returnValue;
  50  };
  51  
  52  function fixEvent(event) {
  53      // add W3C standard event methods
  54      event.preventDefault = fixEvent.preventDefault;
  55      event.stopPropagation = fixEvent.stopPropagation;
  56      // fix target
  57      event.target = event.srcElement;
  58      return event;
  59  };
  60  fixEvent.preventDefault = function() {
  61      this.returnValue = false;
  62  };
  63  fixEvent.stopPropagation = function() {
  64      this.cancelBubble = true;
  65  };
  66  
  67  
  68  /**
  69   * Pseudo event handler to be fired after the DOM was parsed or
  70   * on window load at last.
  71   *
  72   * @author based upon some code by Dean Edwards
  73   * @author Dean Edwards
  74   * @link   http://dean.edwards.name/weblog/2006/06/again/
  75   */
  76  window.fireoninit = function() {
  77    // quit if this function has already been called
  78    if (arguments.callee.done) return;
  79    // flag this function so we don't do the same thing twice
  80    arguments.callee.done = true;
  81    // kill the timer
  82    if (_timer) {
  83       clearInterval(_timer);
  84       _timer = null;
  85    }
  86  
  87    if (typeof window.oninit == 'function') {
  88          window.oninit();
  89    }
  90  };
  91  
  92  /**
  93   * Run the fireoninit function as soon as possible after
  94   * the DOM was loaded, using different methods for different
  95   * Browsers
  96   *
  97   * @author Dean Edwards
  98   * @link   http://dean.edwards.name/weblog/2006/06/again/
  99   */
 100    // for Mozilla
 101    if (document.addEventListener) {
 102      document.addEventListener("DOMContentLoaded", window.fireoninit, null);
 103    }
 104  
 105    // for Internet Explorer (using conditional comments)
 106    /*@cc_on @*/
 107    /*@if (@_win32)
 108      document.write("<scr" + "ipt id=\"__ie_init\" defer=\"true\" src=\"//:\"><\/script>");
 109      var script = document.getElementById("__ie_init");
 110      script.onreadystatechange = function() {
 111          if (this.readyState == "complete") {
 112              window.fireoninit(); // call the onload handler
 113          }
 114      };
 115    /*@end @*/
 116  
 117    // for Safari
 118    if (/WebKit/i.test(navigator.userAgent)) { // sniff
 119      var _timer = setInterval(function() {
 120          if (/loaded|complete/.test(document.readyState)) {
 121              window.fireoninit(); // call the onload handler
 122          }
 123      }, 10);
 124    }
 125  
 126    // for other browsers
 127    window.onload = window.fireoninit;
 128  
 129  
 130  /**
 131   * This is a pseudo Event that will be fired by the fireoninit
 132   * function above.
 133   *
 134   * Use addInitEvent to bind to this event!
 135   *
 136   * @author Andreas Gohr <andi@splitbrain.org>
 137   * @see fireoninit()
 138   */
 139  window.oninit = function(){};
 140  
 141  /**
 142   * Bind a function to the window.init pseudo event
 143   *
 144   * @author Simon Willison
 145   * @see http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 146   */
 147  function addInitEvent(func) {
 148    var oldoninit = window.oninit;
 149    if (typeof window.oninit != 'function') {
 150      window.oninit = func;
 151    } else {
 152      window.oninit = function() {
 153        oldoninit();
 154        func();
 155      };
 156    }
 157  }
 158  
 159  


Generated: Tue Dec 2 01:30:01 2008 Cross-referenced by PHPXref 0.7