Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
4 total  XML / RSS feed 

DomReady extension for prototype

This gives you a new event (onDOMReady).

Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});


Example:

Event.onDOMReady(function(){  
alert('DOM is loaded!');  
});

Function.prototype.toEvent (alias bindAsEventListener)

This one keeps this and sends event object as the first argument (no more checking!). It also has only one parameter or parameters maybe- the argument/s you want to send to your function. Thats it! Bonus: the last argument is the co-ordinates of the event.

// Function.prototype.toEvent, .bindAsEventListener( args ) REM2006 for this.js Api  
// Important: sends event as the first argument to your function in IE also!, 
// event XY [Array][x,y] is passed as last arg, your passed args are in the middle :)
// example: myElement.onclick= myFunc.toEvent( myArguments ); http://richard-m.blogspot.com/
//  
Function.prototype.toEvent = Function.prototype.bindAsEventListener= function(){
  var _method= this, r=[], len=arguments.length, i=0;//alert(len)
  for (i;i<len;i++){ r[i+1]=arguments[i] }
  return function( ev ){  r[0]= ev= ev || event; 
    var db=document.body, dd=document.documentElement;
    r[r.length++]=(ev.pageX)? [ev.pageX, ev.pageY] : 
      [ev.clientX + db.scrollLeft+ dd.scrollLeft, 
       ev.clientY + db.scrollTop + dd.scrollTop];
    return _method.apply(ev.target|| ev.srcElement, r);
  };
};

Execute Event

// executes an event using the DOM element and event name (take out the throw $break to execute all of them)

execEvent: function(el, type) {
  Event.observers.each(function(item,index) {
    if (item[0] == el && item[1] == type) { item[2](); throw $break; }
  });
}

Attach Events

// takes this.events with format 'element:event': function(e) { } and attaches it

attachEvent: function(selector) {
  var sel = selector.split(':');
  var el =  this.el[sel[0]];
  var ev =  sel[1];
  if (!el)      return;
  if (!el.each) Event.observe(el, ev, this.events[selector]);
  else el.each( function(item, index) {
    Event.observe(item, ev, this.events[selector]);
  }.bind(this));
},
attachEvents: function() {
  for (property in this.events) this.attachEvent(property);
},
removeEvent: function(selector) {
  var sel = selector.split(':');
  var el = this.el[sel[0]];
  var ev = sel[1];
  if (!el) return;
  if (!el.each) Event.stopObserving(el, ev, this.events[selector]);
  else el.each(function(item, index) {
    Event.stopObserving(item, ev, this.events[selector]);
  }.bind(this));
},
removeEvents: function() {
  for (x in this.events) this.removeEvent(x);
}
« Newer Snippets
Older Snippets »
4 total  XML / RSS feed