Ext2.2源码解析(一) -- Ext.util.Event
来源:互联网 发布:pro recorder同类软件 编辑:程序博客网 时间:2024/05/29 19:46
- (function(){
-
-
- var createBuffered = function(h, o, scope){
- var task = new Ext.util.DelayedTask();
- return function(){
- task.delay(o.buffer, h, scope, Array.prototype.slice.call(arguments, 0));
- };
- };
-
- var createSingle = function(h, e, fn, scope){
- return function(){
- e.removeListener(fn, scope);
- return h.apply(scope, arguments);
- };
- };
-
- var createDelayed = function(h, o, scope){
- return function(){
- var args = Array.prototype.slice.call(arguments, 0);
- setTimeout(function(){
- h.apply(scope, args);
- }, o.delay || 10);
- };
- };
-
- Ext.util.Event = function(obj, name){
- this.name = name;
- this.obj = obj;
- this.listeners = [];
- };
- Ext.util.Event.prototype = {
-
- addListener : function(fn, scope, options){
-
- scope = scope || this.obj;
-
-
- if(!this.isListening(fn, scope)){
-
- var l = this.createListener(fn, scope, options);
-
-
- if(!this.firing){
-
- this.listeners.push(l);
- }else{
-
-
- this.listeners = this.listeners.slice(0);
-
- this.listeners.push(l);
- }
- }
- },
-
- createListener : function(fn, scope, o){
- o = o || {};
-
-
- scope = scope || this.obj;
-
-
- var l = {fn: fn, scope: scope, options: o};
-
- var h = fn;
- if(o.delay){
-
- h = createDelayed(h, o, scope);
- }
- if(o.single){
-
- h = createSingle(h, this, fn, scope);
- }
- if(o.buffer){
-
-
- h = createBuffered(h, o, scope);
- }
- l.fireFn = h;
- return l;
- },
-
- findListener : function(fn, scope){
- scope = scope || this.obj;
- var ls = this.listeners;
- for(var i = 0, len = ls.length; i < len; i++){
- var l = ls[i];
- if(l.fn == fn && l.scope == scope){
- return i;
- }
- }
- return -1;
- },
-
- isListening : function(fn, scope){
- return this.findListener(fn, scope) != -1;
- },
-
- removeListener : function(fn, scope){
- var index;
- if((index = this.findListener(fn, scope)) != -1){
-
- if(!this.firing){
- this.listeners.splice(index, 1);
- }else{
-
-
- this.listeners = this.listeners.slice(0);
-
- this.listeners.splice(index, 1);
- }
- return true;
- }
- return false;
- },
-
- clearListeners : function(){
- this.listeners = [];
- },
-
- fire : function(){
- var ls = this.listeners, scope, len = ls.length;
- if(len > 0){
-
- this.firing = true;
- var args = Array.prototype.slice.call(arguments, 0);
- for(var i = 0; i < len; i++){
- var l = ls[i];
-
- if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){
-
- this.firing = false;
- return false;
- }
- }
-
- this.firing = false;
- }
- return true;
- }
- };
- })();