Navigation Menu

Skip to content

Commit

Permalink
add Element.Region and Class#include
Browse files Browse the repository at this point in the history
  • Loading branch information
Juriy Zaytsev committed Aug 27, 2008
1 parent d5a96c9 commit e042696
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
28 changes: 28 additions & 0 deletions class_include.js
@@ -0,0 +1,28 @@
/*
// include mixin into a "class"
Hash.include(Enumerable);
// overwrite one of the mixin methods
Enumerable.each = function(){ return 'foo' };
// changes are reflected on all instances of the "class"
(new Hash()).each(); // 'foo'
*/

Class.Methods.include = function(mixin) {
var fn;
for (var prop in mixin) {
// use late binding
this.prototype[prop] = (function(prop){
fn = function() {
return mixin[prop].apply(mixin, arguments);
};
// spoof toString
fn.toString = mixin[prop].toString.bind(mixin[prop]);
return fn;
})(prop);
}
return this;
};
26 changes: 17 additions & 9 deletions experimental/element_region.js
@@ -1,12 +1,6 @@
Element.Region = Class.create({
initialize: function(element) {
this.element = $(element);
var offset = this.element.cumulativeOffset();
var dim = this.element.getDimensions();
this.top = offset.top;
this.left = offset.left;
this.bottom = this.top + dim.height;
this.right = this.left + dim.width;
Region = Class.create({
initialize: function(bounds) {
Object.extend(this, bounds);
},
contains: function(other) {
return other.left >= this.left &&
Expand All @@ -20,4 +14,18 @@ Element.Region = Class.create({
((other.top >= this.top && other.top <= this.bottom) ||
(other.bottom >= this.top && other.bottom <= this.bottom));
}
});

Element.Region = Class.create(Region, {
initialize: function($super, element) {
this.element = $(element);
var offset = this.element.cumulativeOffset();
var dim = this.element.getDimensions();
return $super({
top: offset.top,
left: offset.left,
bottom: offset.top + dim.height,
right: offset.left + dim.width
})
}
});

0 comments on commit e042696

Please sign in to comment.