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

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);
  };
};

Ajax Responders (Indicators)

// code for indicator registration

registers: {
        onCreate: function() {
                if(Ajax.activeRequestCount>0)
                        new Effect.Appear('indicate');
        },
        onComplete: function() {
                if(Ajax.activeRequestCount==0)
                        new Effect.Fade('indicate', {queue: 'end'});
        }
}

Ajax.Responders.register(Note.registers);

Browser Dimensions

// various functions related to viewable browser dimensions and scroll offset

putCenter: function(item) {
  item = $(item);
  var xy = item.getDimensions();
  var win = this.windowDimensions();
  var scrol = this.scrollOffset();
  item.style.left = (win[0] / 2) + scrol[0] - (xy.width / 2) + "px";
  item.style.top = (win[1] / 2) + scrol[1] - (xy.height / 2) + "px";
},
fullScreen: function(item) {
  item = $(item);
  var win = this.windowDimensions();
  var scrol = this.scrollOffset();
  item.style.height = scrol[1] + win[1] + "px";
},
windowDimensions: function() {
  var x, y;
  if (self.innerHeight) {
    // all except Explorer
    x = self.innerWidth;
    y = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    // Explorer 6 Strict Mode
    x = document.documentElement.clientWidth;
    y = document.documentElement.clientHeight;
  } else if (document.body) {
    // other Explorers
    x = document.body.clientWidth;
    y = document.body.clientHeight;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayWindowSize = new Array(x,y);
  return arrayWindowSize;
},
scrollOffset: function() {
  var x, y;
  if (self.pageYOffset) {
    // all except Explorer
    x = self.pageXOffset;
    y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
    // Explorer 6 Strict
    x = document.documentElement.scrollLeft;
    y = document.documentElement.scrollTop;
  } else if (document.body) {
    // all other Explorers
    x = document.body.scrollLeft;
    y = document.body.scrollTop;
  }
  if (!x) x = 0;
  if (!y) y = 0;
  arrayScrollOffset = new Array(x,y);
  return arrayScrollOffset;
}

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; }
  });
}

childOfID

// moves upward in the tree until an ID or class name is found, returns element

childOfID: function(element, id, isClass) {
  element = $(element);
  do {
    if (isClass && element.hasClassName(id)) return element; 
    if (!isClass && element.id == id) return element;
  } while (element = $(element.parentNode));
  return false;
}

Catch Enter, Catch Escape

// used to catch form keydowns

catchEnter: function(e) {
  if (e.keyCode == 13) { Event.stop(e); return true; }
  else return false;
},
catchEsc: function(e) {
  if (e.keyCode == 27) { Event.stop(e); return true; }
  else return false;
}

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);
}

Attach Elements

// takes this.el and copies it to this.elements, replacing CSS selector with DOM object(s)

attachElement: function(p) {
  this.el[p] = $$(this.elements[p]);
  if (this.el[p].length == 1) this.el[p] = this.el[p][0];
},
attachElements: function() {
  this.el = {};
  for (x in this.elements) this.attachElement(x);
},
clearElements: function() {
  for (x in this.el) this.el[x] = null;
}

Bind Functions

// these functions force a common 'this' on inner objects

bindTo: function(source, bindTo) {
  if (typeof source == 'function') return;
  for (x in source)
    if (typeof source[x] == 'function')
      source[x] = source[x].bind(bindTo);
},
bindAll: function() {
  this.bindTo(this.events,   this);
  this.bindTo(this.ajax,     this);
  this.bindTo(this.complete, this);
}

Custom Ajax.Request Function

// this is a custom function that simplifies creating an ajax object

function(url, complete, form, params) {
  params = params ? $H(params).toQueryString() : '';
  if (!complete) complete = function(r){};
  if (form)      params   = Form.serialize(form) + '&' + params;
  new Ajax.Request(url, 
    { asynchronous:true,
      evalScripts:true, 
      onComplete: function(r) { complete(r.responseText); },
      parameters:params
    });
}
« Newer Snippets
Older Snippets »
10 total  XML / RSS feed