[ Index ]
 

Code source de Seagull 0.6.1

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/js/html_ajax/behavior/ -> behavior.js (source)

   1  /**
   2  
   3  ModifiedBehavior v1.0 by Ron Lancaster based on Ben Nolan's Behaviour, June 2005 implementation.
   4  Modified to use Dean Edward's CSS Query.
   5  
   6  Description
   7  ----------
   8  
   9  Uses css selectors  to apply javascript Behaviors to enable unobtrusive javascript in html documents.
  10  
  11  Dependencies
  12  ------------
  13  
  14  Requires [Dean Edwards CSSQuery](http://dean.edwards.name/my/cssQuery/ "CSSQuery").
  15  
  16  Usage
  17  ------
  18  
  19          Behavior.register(
  20              "b.someclass",
  21              function(element) {
  22                  element.onclick = function(){
  23                      alert(this.innerHTML);
  24                  }
  25              }
  26          );
  27  
  28          Behavior.register(
  29              "#someid u",
  30              function(element) {
  31                  element.onmouseover = function(){
  32                      this.innerHTML = "BLAH!";
  33                  }
  34              },
  35              getElementByID("parent")
  36          );
  37  
  38  Call `Behavior.apply()` to re-apply the rules (if you update the dom, etc).
  39  
  40  License
  41  ------
  42  
  43  Reproduced under BSD licensed. Same license as Ben Nolan's implementation.
  44  
  45  More information for Ben Nolan's implementation: <http://ripcord.co.nz/behaviour/>
  46  
  47  */
  48  
  49  var Behavior = {
  50      // so to an id to get debug timings
  51      debug : false,
  52  
  53      // private data member
  54      list : new Array(),
  55  
  56      // private method
  57      addLoadEvent : function(func) {
  58          var oldonload = window.onload;
  59  
  60          if (typeof window.onload != 'function') {
  61              window.onload = func;
  62          } else {
  63              window.onload = function() {
  64                  oldonload();
  65                  func();
  66              }
  67          }
  68      },
  69  
  70      // void apply() : Applies the registered ruleset.
  71      apply : function() {
  72          if (this.debug) {
  73              document.getElementById(this.debug).innerHTML += 'Apply: '+new Date()+'<br>';
  74              var total = 0;
  75          }
  76          if (Behavior.list.length > 2) {
  77              cssQuery.caching = true;
  78          }
  79          for (i = 0; i < Behavior.list.length; i++) {
  80              var rule = Behavior.list[i];
  81              
  82              if (this.debug) { var ds = new Date() };
  83              var tags = cssQuery(rule.selector, rule.from);
  84      
  85              if (this.debug) {
  86                  var de = new Date();
  87                  var ts = de.valueOf()-ds.valueOf();
  88                  document.getElementById(this.debug).innerHTML += 'Rule: '+rule.selector+' - Took: '+ts+' - Returned: '+tags.length+' tags<br>';
  89                  total += ts;
  90              }
  91              if (tags) {
  92                  for (j = 0; j < tags.length; j++) {
  93                      rule.action(tags[j]);
  94                  }
  95              }
  96          }
  97          if (Behavior.list.length > 2) {
  98              cssQuery.caching = false;
  99          }
 100  
 101          if (this.debug) {
 102              document.getElementById(this.debug).innerHTML += 'Total rule apply time: '+total;
 103          }
 104      },
 105  
 106      // void register() : register a css selector, and the action (function) to take,
 107      // from (optional) is a document, element or array of elements which is filtered by selector.
 108      register : function(selector, action, from) {
 109          Behavior.list.push(new BehaviorRule(selector, from, action));
 110      },
 111  
 112      // void start() : initial application of ruleset at document load.
 113      start : function() {
 114          Behavior.addLoadEvent(function() {
 115              Behavior.apply();
 116          });
 117      }
 118  }
 119  
 120  function BehaviorRule(selector, from, action) {
 121      this.selector = selector;
 122      this.from = from;
 123      this.action = action;
 124  }
 125  
 126  Behavior.start();


Généré le : Fri Mar 30 01:27:52 2007 par Balluche grâce à PHPXref 0.7