[ Index ]

PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10)

title

Body

[close]

/template/lib/mootools/more/Element/ -> Element.Delegation.js (source)

   1  /*
   2  ---
   3  
   4  script: Element.Delegation.js
   5  
   6  name: Element.Delegation
   7  
   8  description: Extends the Element native object to include the delegate method for more efficient event management.
   9  
  10  credits:
  11    - "Event checking based on the work of Daniel Steigerwald. License: MIT-style license.    Copyright: Copyright (c) 2008 Daniel Steigerwald, daniel.steigerwald.cz"
  12  
  13  license: MIT-style license
  14  
  15  authors:
  16    - Aaron Newton
  17    - Daniel Steigerwald
  18  
  19  requires:
  20    - Core/Element.Event
  21    - Core/Selectors
  22    - /MooTools.More
  23  
  24  provides: [Element.Delegation]
  25  
  26  ...
  27  */
  28  
  29  (function(addEvent, removeEvent){
  30      
  31      var match = /(.*?):relay\(((?:\(.*?\)|.)+)\)$/,
  32          combinators = /[+>~\s]/,
  33          splitType = function(type){
  34              var bits = type.match(match);
  35              return !bits ? {event: type} : {
  36                  event: bits[1],
  37                  selector: bits[2]
  38              };
  39          },
  40          check = function(e, selector){
  41              var t = e.target;
  42              if (combinators.test(selector = selector.trim())){
  43                  var els = this.getElements(selector);
  44                  for (var i = els.length; i--; ){
  45                      var el = els[i];
  46                      if (t == el || el.hasChild(t)) return el;
  47                  }
  48              } else {
  49                  for ( ; t && t != this; t = t.parentNode){
  50                      if (Element.match(t, selector)) return document.id(t);
  51                  }
  52              }
  53              return null;
  54          };
  55  
  56      Element.implement({
  57  
  58          addEvent: function(type, fn){
  59              var split = splitType(type);
  60              if (split.selector){
  61                  var monitors = this.retrieve('delegation:_delegateMonitors', {});
  62                  if (!monitors[type]){
  63                      var monitor = function(e){
  64                          var el = check.call(this, e, split.selector);
  65                          if (el) this.fireEvent(type, [e, el], 0, el);
  66                      }.bind(this);
  67                      monitors[type] = monitor;
  68                      addEvent.call(this, split.event, monitor);
  69                  }
  70              }
  71              return addEvent.apply(this, arguments);
  72          },
  73  
  74          removeEvent: function(type, fn){
  75              var split = splitType(type);
  76              if (split.selector){
  77                  var events = this.retrieve('events');
  78                  if (!events || !events[type] || (fn && !events[type].keys.contains(fn))) return this;
  79  
  80                  if (fn) removeEvent.apply(this, [type, fn]);
  81                  else removeEvent.apply(this, type);
  82  
  83                  events = this.retrieve('events');
  84                  if (events && events[type] && events[type].keys.length == 0){
  85                      var monitors = this.retrieve('delegation:_delegateMonitors', {});
  86                      removeEvent.apply(this, [split.event, monitors[type]]);
  87                      delete monitors[type];
  88                  }
  89                  return this;
  90              }
  91              return removeEvent.apply(this, arguments);
  92          },
  93  
  94          fireEvent: function(type, args, delay, bind){
  95              var events = this.retrieve('events');
  96              var e, el;
  97              if (args) {
  98                  e = args[0];
  99                  el = args[1];
 100              }
 101              if (!events || !events[type]) return this;
 102              events[type].keys.each(function(fn){
 103                  fn.create({bind: bind || this, delay: delay, arguments: args})();
 104              }, this);
 105              return this;
 106          }
 107  
 108      });
 109  
 110  })(Element.prototype.addEvent, Element.prototype.removeEvent);
 111  
 112  try {
 113      if (typeof HTMLElement != 'undefined')
 114          HTMLElement.prototype.fireEvent = Element.prototype.fireEvent;
 115  } catch(e){}


Generated: Tue Nov 16 22:51:00 2010 Cross-referenced by PHPXref 0.7