]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui3/build/event-custom/event-custom-base.js
Release 6.2.2
[Github/sugarcrm.git] / include / javascript / yui3 / build / event-custom / event-custom-base.js
1 /*
2  Copyright (c) 2010, Yahoo! Inc. All rights reserved.
3  Code licensed under the BSD License:
4  http://developer.yahoo.com/yui/license.html
5  version: 3.3.0
6  build: 3167
7  */
8 YUI.add('event-custom-base',function(Y){Y.Env.evt={handles:{},plugins:{}};var DO_BEFORE=0,DO_AFTER=1,DO={objs:{},before:function(fn,obj,sFn,c){var f=fn,a;if(c){a=[fn,c].concat(Y.Array(arguments,4,true));f=Y.rbind.apply(Y,a);}
9 return this._inject(DO_BEFORE,f,obj,sFn);},after:function(fn,obj,sFn,c){var f=fn,a;if(c){a=[fn,c].concat(Y.Array(arguments,4,true));f=Y.rbind.apply(Y,a);}
10 return this._inject(DO_AFTER,f,obj,sFn);},_inject:function(when,fn,obj,sFn){var id=Y.stamp(obj),o,sid;if(!this.objs[id]){this.objs[id]={};}
11 o=this.objs[id];if(!o[sFn]){o[sFn]=new Y.Do.Method(obj,sFn);obj[sFn]=function(){return o[sFn].exec.apply(o[sFn],arguments);};}
12 sid=id+Y.stamp(fn)+sFn;o[sFn].register(sid,fn,when);return new Y.EventHandle(o[sFn],sid);},detach:function(handle){if(handle.detach){handle.detach();}},_unload:function(e,me){}};Y.Do=DO;DO.Method=function(obj,sFn){this.obj=obj;this.methodName=sFn;this.method=obj[sFn];this.before={};this.after={};};DO.Method.prototype.register=function(sid,fn,when){if(when){this.after[sid]=fn;}else{this.before[sid]=fn;}};DO.Method.prototype._delete=function(sid){delete this.before[sid];delete this.after[sid];};DO.Method.prototype.exec=function(){var args=Y.Array(arguments,0,true),i,ret,newRet,bf=this.before,af=this.after,prevented=false;for(i in bf){if(bf.hasOwnProperty(i)){ret=bf[i].apply(this.obj,args);if(ret){switch(ret.constructor){case DO.Halt:return ret.retVal;case DO.AlterArgs:args=ret.newArgs;break;case DO.Prevent:prevented=true;break;default:}}}}
13 if(!prevented){ret=this.method.apply(this.obj,args);}
14 DO.originalRetVal=ret;DO.currentRetVal=ret;for(i in af){if(af.hasOwnProperty(i)){newRet=af[i].apply(this.obj,args);if(newRet&&newRet.constructor==DO.Halt){return newRet.retVal;}else if(newRet&&newRet.constructor==DO.AlterReturn){ret=newRet.newRetVal;DO.currentRetVal=ret;}}}
15 return ret;};DO.AlterArgs=function(msg,newArgs){this.msg=msg;this.newArgs=newArgs;};DO.AlterReturn=function(msg,newRetVal){this.msg=msg;this.newRetVal=newRetVal;};DO.Halt=function(msg,retVal){this.msg=msg;this.retVal=retVal;};DO.Prevent=function(msg){this.msg=msg;};DO.Error=DO.Halt;var AFTER='after',CONFIGS=['broadcast','monitored','bubbles','context','contextFn','currentTarget','defaultFn','defaultTargetOnly','details','emitFacade','fireOnce','async','host','preventable','preventedFn','queuable','silent','stoppedFn','target','type'],YUI3_SIGNATURE=9,YUI_LOG='yui:log';Y.EventHandle=function(evt,sub){this.evt=evt;this.sub=sub;};Y.EventHandle.prototype={batch:function(f,c){f.call(c||this,this);if(Y.Lang.isArray(this.evt)){Y.Array.each(this.evt,function(h){h.batch.call(c||h,f);});}},detach:function(){var evt=this.evt,detached=0,i;if(evt){if(Y.Lang.isArray(evt)){for(i=0;i<evt.length;i++){detached+=evt[i].detach();}}else{evt._delete(this.sub);detached=1;}}
16 return detached;},monitor:function(what){return this.evt.monitor.apply(this.evt,arguments);}};Y.CustomEvent=function(type,o){o=o||{};this.id=Y.stamp(this);this.type=type;this.context=Y;this.logSystem=(type==YUI_LOG);this.silent=this.logSystem;this.subscribers={};this.afters={};this.preventable=true;this.bubbles=true;this.signature=YUI3_SIGNATURE;this.subCount=0;this.afterCount=0;this.applyConfig(o,true);};Y.CustomEvent.prototype={hasSubs:function(when){var s=this.subCount,a=this.afterCount,sib=this.sibling;if(sib){s+=sib.subCount;a+=sib.afterCount;}
17 if(when){return(when=='after')?a:s;}
18 return(s+a);},monitor:function(what){this.monitored=true;var type=this.id+'|'+this.type+'_'+what,args=Y.Array(arguments,0,true);args[0]=type;return this.host.on.apply(this.host,args);},getSubs:function(){var s=Y.merge(this.subscribers),a=Y.merge(this.afters),sib=this.sibling;if(sib){Y.mix(s,sib.subscribers);Y.mix(a,sib.afters);}
19 return[s,a];},applyConfig:function(o,force){if(o){Y.mix(this,o,force,CONFIGS);}},_on:function(fn,context,args,when){if(!fn){this.log('Invalid callback for CE: '+this.type);}
20 var s=new Y.Subscriber(fn,context,args,when);if(this.fireOnce&&this.fired){if(this.async){setTimeout(Y.bind(this._notify,this,s,this.firedWith),0);}else{this._notify(s,this.firedWith);}}
21 if(when==AFTER){this.afters[s.id]=s;this.afterCount++;}else{this.subscribers[s.id]=s;this.subCount++;}
22 return new Y.EventHandle(this,s);},subscribe:function(fn,context){var a=(arguments.length>2)?Y.Array(arguments,2,true):null;return this._on(fn,context,a,true);},on:function(fn,context){var a=(arguments.length>2)?Y.Array(arguments,2,true):null;if(this.host){this.host._monitor('attach',this.type,{args:arguments});}
23 return this._on(fn,context,a,true);},after:function(fn,context){var a=(arguments.length>2)?Y.Array(arguments,2,true):null;return this._on(fn,context,a,AFTER);},detach:function(fn,context){if(fn&&fn.detach){return fn.detach();}
24 var i,s,found=0,subs=Y.merge(this.subscribers,this.afters);for(i in subs){if(subs.hasOwnProperty(i)){s=subs[i];if(s&&(!fn||fn===s.fn)){this._delete(s);found++;}}}
25 return found;},unsubscribe:function(){return this.detach.apply(this,arguments);},_notify:function(s,args,ef){this.log(this.type+'->'+'sub: '+s.id);var ret;ret=s.notify(args,this);if(false===ret||this.stopped>1){this.log(this.type+' cancelled by subscriber');return false;}
26 return true;},log:function(msg,cat){if(!this.silent){}},fire:function(){if(this.fireOnce&&this.fired){this.log('fireOnce event: '+this.type+' already fired');return true;}else{var args=Y.Array(arguments,0,true);this.fired=true;this.firedWith=args;if(this.emitFacade){return this.fireComplex(args);}else{return this.fireSimple(args);}}},fireSimple:function(args){this.stopped=0;this.prevented=0;if(this.hasSubs()){var subs=this.getSubs();this._procSubs(subs[0],args);this._procSubs(subs[1],args);}
27 this._broadcast(args);return this.stopped?false:true;},fireComplex:function(args){args[0]=args[0]||{};return this.fireSimple(args);},_procSubs:function(subs,args,ef){var s,i;for(i in subs){if(subs.hasOwnProperty(i)){s=subs[i];if(s&&s.fn){if(false===this._notify(s,args,ef)){this.stopped=2;}
28 if(this.stopped==2){return false;}}}}
29 return true;},_broadcast:function(args){if(!this.stopped&&this.broadcast){var a=Y.Array(args);a.unshift(this.type);if(this.host!==Y){Y.fire.apply(Y,a);}
30 if(this.broadcast==2){Y.Global.fire.apply(Y.Global,a);}}},unsubscribeAll:function(){return this.detachAll.apply(this,arguments);},detachAll:function(){return this.detach();},_delete:function(s){if(s){if(this.subscribers[s.id]){delete this.subscribers[s.id];this.subCount--;}
31 if(this.afters[s.id]){delete this.afters[s.id];this.afterCount--;}}
32 if(this.host){this.host._monitor('detach',this.type,{ce:this,sub:s});}
33 if(s){s.deleted=true;}}};Y.Subscriber=function(fn,context,args){this.fn=fn;this.context=context;this.id=Y.stamp(this);this.args=args;};Y.Subscriber.prototype={_notify:function(c,args,ce){if(this.deleted&&!this.postponed){if(this.postponed){delete this.fn;delete this.context;}else{delete this.postponed;return null;}}
34 var a=this.args,ret;switch(ce.signature){case 0:ret=this.fn.call(c,ce.type,args,c);break;case 1:ret=this.fn.call(c,args[0]||null,c);break;default:if(a||args){args=args||[];a=(a)?args.concat(a):args;ret=this.fn.apply(c,a);}else{ret=this.fn.call(c);}}
35 if(this.once){ce._delete(this);}
36 return ret;},notify:function(args,ce){var c=this.context,ret=true;if(!c){c=(ce.contextFn)?ce.contextFn():ce.context;}
37 if(Y.config.throwFail){ret=this._notify(c,args,ce);}else{try{ret=this._notify(c,args,ce);}catch(e){Y.error(this+' failed: '+e.message,e);}}
38 return ret;},contains:function(fn,context){if(context){return((this.fn==fn)&&this.context==context);}else{return(this.fn==fn);}}};var L=Y.Lang,PREFIX_DELIMITER=':',CATEGORY_DELIMITER='|',AFTER_PREFIX='~AFTER~',YArray=Y.Array,_wildType=Y.cached(function(type){return type.replace(/(.*)(:)(.*)/,"*$2$3");}),_getType=Y.cached(function(type,pre){if(!pre||!L.isString(type)||type.indexOf(PREFIX_DELIMITER)>-1){return type;}
39 return pre+PREFIX_DELIMITER+type;}),_parseType=Y.cached(function(type,pre){var t=type,detachcategory,after,i;if(!L.isString(t)){return t;}
40 i=t.indexOf(AFTER_PREFIX);if(i>-1){after=true;t=t.substr(AFTER_PREFIX.length);}
41 i=t.indexOf(CATEGORY_DELIMITER);if(i>-1){detachcategory=t.substr(0,(i));t=t.substr(i+1);if(t=='*'){t=null;}}
42 return[detachcategory,(pre)?_getType(t,pre):t,after,t];}),ET=function(opts){var o=(L.isObject(opts))?opts:{};this._yuievt=this._yuievt||{id:Y.guid(),events:{},targets:{},config:o,chain:('chain'in o)?o.chain:Y.config.chain,bubbling:false,defaults:{context:o.context||this,host:this,emitFacade:o.emitFacade,fireOnce:o.fireOnce,queuable:o.queuable,monitored:o.monitored,broadcast:o.broadcast,defaultTargetOnly:o.defaultTargetOnly,bubbles:('bubbles'in o)?o.bubbles:true}};};ET.prototype={once:function(){var handle=this.on.apply(this,arguments);handle.batch(function(hand){if(hand.sub){hand.sub.once=true;}});return handle;},parseType:function(type,pre){return _parseType(type,pre||this._yuievt.config.prefix);},on:function(type,fn,context){var parts=_parseType(type,this._yuievt.config.prefix),f,c,args,ret,ce,detachcategory,handle,store=Y.Env.evt.handles,after,adapt,shorttype,Node=Y.Node,n,domevent,isArr;this._monitor('attach',parts[1],{args:arguments,category:parts[0],after:parts[2]});if(L.isObject(type)){if(L.isFunction(type)){return Y.Do.before.apply(Y.Do,arguments);}
43 f=fn;c=context;args=YArray(arguments,0,true);ret=[];if(L.isArray(type)){isArr=true;}
44 after=type._after;delete type._after;Y.each(type,function(v,k){if(L.isObject(v)){f=v.fn||((L.isFunction(v))?v:f);c=v.context||c;}
45 var nv=(after)?AFTER_PREFIX:'';args[0]=nv+((isArr)?v:k);args[1]=f;args[2]=c;ret.push(this.on.apply(this,args));},this);return(this._yuievt.chain)?this:new Y.EventHandle(ret);}
46 detachcategory=parts[0];after=parts[2];shorttype=parts[3];if(Node&&Y.instanceOf(this,Node)&&(shorttype in Node.DOM_EVENTS)){args=YArray(arguments,0,true);args.splice(2,0,Node.getDOMNode(this));return Y.on.apply(Y,args);}
47 type=parts[1];if(Y.instanceOf(this,YUI)){adapt=Y.Env.evt.plugins[type];args=YArray(arguments,0,true);args[0]=shorttype;if(Node){n=args[2];if(Y.instanceOf(n,Y.NodeList)){n=Y.NodeList.getDOMNodes(n);}else if(Y.instanceOf(n,Node)){n=Node.getDOMNode(n);}
48 domevent=(shorttype in Node.DOM_EVENTS);if(domevent){args[2]=n;}}
49 if(adapt){handle=adapt.on.apply(Y,args);}else if((!type)||domevent){handle=Y.Event._attach(args);}}
50 if(!handle){ce=this._yuievt.events[type]||this.publish(type);handle=ce._on(fn,context,(arguments.length>3)?YArray(arguments,3,true):null,(after)?'after':true);}
51 if(detachcategory){store[detachcategory]=store[detachcategory]||{};store[detachcategory][type]=store[detachcategory][type]||[];store[detachcategory][type].push(handle);}
52 return(this._yuievt.chain)?this:handle;},subscribe:function(){return this.on.apply(this,arguments);},detach:function(type,fn,context){var evts=this._yuievt.events,i,Node=Y.Node,isNode=Node&&(Y.instanceOf(this,Node));if(!type&&(this!==Y)){for(i in evts){if(evts.hasOwnProperty(i)){evts[i].detach(fn,context);}}
53 if(isNode){Y.Event.purgeElement(Node.getDOMNode(this));}
54 return this;}
55 var parts=_parseType(type,this._yuievt.config.prefix),detachcategory=L.isArray(parts)?parts[0]:null,shorttype=(parts)?parts[3]:null,adapt,store=Y.Env.evt.handles,detachhost,cat,args,ce,keyDetacher=function(lcat,ltype,host){var handles=lcat[ltype],ce,i;if(handles){for(i=handles.length-1;i>=0;--i){ce=handles[i].evt;if(ce.host===host||ce.el===host){handles[i].detach();}}}};if(detachcategory){cat=store[detachcategory];type=parts[1];detachhost=(isNode)?Y.Node.getDOMNode(this):this;if(cat){if(type){keyDetacher(cat,type,detachhost);}else{for(i in cat){if(cat.hasOwnProperty(i)){keyDetacher(cat,i,detachhost);}}}
56 return this;}}else if(L.isObject(type)&&type.detach){type.detach();return this;}else if(isNode&&((!shorttype)||(shorttype in Node.DOM_EVENTS))){args=YArray(arguments,0,true);args[2]=Node.getDOMNode(this);Y.detach.apply(Y,args);return this;}
57 adapt=Y.Env.evt.plugins[shorttype];if(Y.instanceOf(this,YUI)){args=YArray(arguments,0,true);if(adapt&&adapt.detach){adapt.detach.apply(Y,args);return this;}else if(!type||(!adapt&&Node&&(type in Node.DOM_EVENTS))){args[0]=type;Y.Event.detach.apply(Y.Event,args);return this;}}
58 ce=evts[parts[1]];if(ce){ce.detach(fn,context);}
59 return this;},unsubscribe:function(){return this.detach.apply(this,arguments);},detachAll:function(type){return this.detach(type);},unsubscribeAll:function(){return this.detachAll.apply(this,arguments);},publish:function(type,opts){var events,ce,ret,defaults,edata=this._yuievt,pre=edata.config.prefix;type=(pre)?_getType(type,pre):type;this._monitor('publish',type,{args:arguments});if(L.isObject(type)){ret={};Y.each(type,function(v,k){ret[k]=this.publish(k,v||opts);},this);return ret;}
60 events=edata.events;ce=events[type];if(ce){if(opts){ce.applyConfig(opts,true);}}else{defaults=edata.defaults;ce=new Y.CustomEvent(type,(opts)?Y.merge(defaults,opts):defaults);events[type]=ce;}
61 return events[type];},_monitor:function(what,type,o){var monitorevt,ce=this.getEvent(type);if((this._yuievt.config.monitored&&(!ce||ce.monitored))||(ce&&ce.monitored)){monitorevt=type+'_'+what;o.monitored=what;this.fire.call(this,monitorevt,o);}},fire:function(type){var typeIncluded=L.isString(type),t=(typeIncluded)?type:(type&&type.type),ce,ret,pre=this._yuievt.config.prefix,ce2,args=(typeIncluded)?YArray(arguments,1,true):arguments;t=(pre)?_getType(t,pre):t;this._monitor('fire',t,{args:args});ce=this.getEvent(t,true);ce2=this.getSibling(t,ce);if(ce2&&!ce){ce=this.publish(t);}
62 if(!ce){if(this._yuievt.hasTargets){return this.bubble({type:t},args,this);}
63 ret=true;}else{ce.sibling=ce2;ret=ce.fire.apply(ce,args);}
64 return(this._yuievt.chain)?this:ret;},getSibling:function(type,ce){var ce2;if(type.indexOf(PREFIX_DELIMITER)>-1){type=_wildType(type);ce2=this.getEvent(type,true);if(ce2){ce2.applyConfig(ce);ce2.bubbles=false;ce2.broadcast=0;}}
65 return ce2;},getEvent:function(type,prefixed){var pre,e;if(!prefixed){pre=this._yuievt.config.prefix;type=(pre)?_getType(type,pre):type;}
66 e=this._yuievt.events;return e[type]||null;},after:function(type,fn){var a=YArray(arguments,0,true);switch(L.type(type)){case'function':return Y.Do.after.apply(Y.Do,arguments);case'array':case'object':a[0]._after=true;break;default:a[0]=AFTER_PREFIX+type;}
67 return this.on.apply(this,a);},before:function(){return this.on.apply(this,arguments);}};Y.EventTarget=ET;Y.mix(Y,ET.prototype,false,false,{bubbles:false});ET.call(Y);YUI.Env.globalEvents=YUI.Env.globalEvents||new ET();Y.Global=YUI.Env.globalEvents;},'3.3.0',{requires:['oop']});