Blame view

framework/thirdparty/jquery-selector/src/jquery.class.js 1.64 KB
70f4f18b   Administrator   first_commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  /**
   * Very basic Class utility. Based on base and jquery.class.
   * 
   * Class definition: var Foo = Base.extend({ init: function(){ Constructor }; method_name: function(){ Method } });
   *
   * Inheritance: var Bar = Foo.extend({ method_name: function(){ this._super(); } });
   * 
   * new-less Constructor: new Foo(arg) <-same as-> Foo(arg)
   */  	
  Base = (function(){
  	
  	var marker = {}, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  
  	// The base Class implementation (does nothing)
  	Base = function(){};
   
  	Base.addMethod = function(name, func) {
  		var _super = this._super;
  		if (_super && fnTest.test(func))	 {
  			this.prototype[name] = function(){
  				var tmp = this._super;
  				this._super = _super[name];
  				try {
  					var ret = func.apply(this, arguments);
  				}
  				finally {
  					this._super = tmp;
  				}
  				return ret;
  			}
  		}
  		else this.prototype[name] = func;
  	}
   
  	// Create a new Class that inherits from this class
  	Base.extend = function(prop) {
    	
  		// The dummy class constructor
  		var Kls = function() {
  			if (arguments[0] === marker) return;
  			
  			if (this instanceof Kls) {
  				if (this.init) this.init.apply(this, arguments);
  			}
  			else {
  				var ret = new Kls(marker); if (ret.init) ret.init.apply(ret, arguments); return ret;
  			}
  		}
     
  		Kls.constructor = Kls;
  		Kls.extend = Base.extend;
  		Kls.addMethod = Base.addMethod;
  		Kls._super = this.prototype;
  	
  		Kls.prototype = new this(marker);
  	
  		// Copy the properties over onto the new prototype
  		for (var name in prop) {
  			if (typeof prop[name] == 'function') Kls.addMethod(name, prop[name]);
  			else Kls.prototype[name] = prop;
  		}
  		
  		return Kls;
  	}; 
  
  	return Base;
  })();