Skip to content
Browse files

add Element.Region and Class#include

  • Loading branch information...
1 parent d5a96c9 commit e0426967ad12b412f7c242d3d347f757b1749b5a Juriy Zaytsev committed
Showing with 45 additions and 9 deletions.
  1. +28 −0 class_include.js
  2. +17 −9 experimental/element_region.js
View
28 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;
+};
View
26 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 &&
@@ -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.
Something went wrong with that request. Please try again.