📄 common.js
字号:
if(!ao.adviceFunc){ return; } // nothing to disconnect if(dojo.lang.isString(ao.srcFunc) && (ao.srcFunc.toLowerCase() == "onkey") ){ if(dojo.render.html.ie){ ao.srcFunc = "onkeydown"; this.disconnect(ao); } ao.srcFunc = "onkeypress"; } var mjp = dojo.event.MethodJoinPoint.getForMethod(ao.srcObj, ao.srcFunc); return mjp.removeAdvice(ao.adviceObj, ao.adviceFunc, ao.adviceType, ao.once); // a MethodJoinPoint object } this.kwDisconnect = function(kwArgs){ // summary: // Takes the same parameters as dojo.event.kwConnect() but // destroys an existing connection instead of building a new one. return this._kwConnectImpl(kwArgs, true); }}// exactly one of these is created whenever a method with a joint point is run,// if there is at least one 'around' advice.dojo.event.MethodInvocation = function(/*dojo.event.MethodJoinPoint*/join_point, /*Object*/obj, /*Array*/args){ // summary: // a class the models the call into a function. This is used under the // covers for all method invocations on both ends of a // connect()-wrapped function dispatch. This allows us to "pickle" // calls, such as in the case of around advice. // join_point: // a dojo.event.MethodJoinPoint object that represents a connection // obj: // the scope the call will execute in // args: // an array of parameters that will get passed to the callee this.jp_ = join_point; this.object = obj; this.args = []; // make sure we don't lock into a mutable object which can change under us. // It's ok if the individual items change, though. for(var x=0; x<args.length; x++){ this.args[x] = args[x]; } // the index of the 'around' that is currently being executed. this.around_index = -1;}dojo.event.MethodInvocation.prototype.proceed = function(){ // summary: // proceed with the method call that's represented by this invocation // object this.around_index++; if(this.around_index >= this.jp_.around.length){ return this.jp_.object[this.jp_.methodname].apply(this.jp_.object, this.args); // return this.jp_.run_before_after(this.object, this.args); }else{ var ti = this.jp_.around[this.around_index]; var mobj = ti[0]||dj_global; var meth = ti[1]; return mobj[meth].call(mobj, this); }} dojo.event.MethodJoinPoint = function(/*Object*/obj, /*String*/funcName){ this.object = obj||dj_global; this.methodname = funcName; this.methodfunc = this.object[funcName]; this.squelch = false; // this.before = []; // this.after = []; // this.around = [];}dojo.event.MethodJoinPoint.getForMethod = function(/*Object*/obj, /*String*/funcName){ // summary: // "static" class function for returning a MethodJoinPoint from a // scoped function. If one doesn't exist, one is created. // obj: // the scope to search for the function in // funcName: // the name of the function to return a MethodJoinPoint for if(!obj){ obj = dj_global; } if(!obj[funcName]){ // supply a do-nothing method implementation obj[funcName] = function(){}; if(!obj[funcName]){ // e.g. cannot add to inbuilt objects in IE6 dojo.raise("Cannot set do-nothing method on that object "+funcName); } }else if((!dojo.lang.isFunction(obj[funcName]))&&(!dojo.lang.isAlien(obj[funcName]))){ // FIXME: should we throw an exception here instead? return null; } // we hide our joinpoint instance in obj[funcName + '$joinpoint'] var jpname = funcName + "$joinpoint"; var jpfuncname = funcName + "$joinpoint$method"; var joinpoint = obj[jpname]; if(!joinpoint){ var isNode = false; if(dojo.event["browser"]){ if( (obj["attachEvent"])|| (obj["nodeType"])|| (obj["addEventListener"]) ){ isNode = true; dojo.event.browser.addClobberNodeAttrs(obj, [jpname, jpfuncname, funcName]); } } var origArity = obj[funcName].length; obj[jpfuncname] = obj[funcName]; // joinpoint = obj[jpname] = new dojo.event.MethodJoinPoint(obj, funcName); joinpoint = obj[jpname] = new dojo.event.MethodJoinPoint(obj, jpfuncname); obj[funcName] = function(){ var args = []; if((isNode)&&(!arguments.length)){ var evt = null; try{ if(obj.ownerDocument){ evt = obj.ownerDocument.parentWindow.event; }else if(obj.documentElement){ evt = obj.documentElement.ownerDocument.parentWindow.event; }else if(obj.event){ //obj is a window evt = obj.event; }else{ evt = window.event; } }catch(e){ evt = window.event; } if(evt){ args.push(dojo.event.browser.fixEvent(evt, this)); } }else{ for(var x=0; x<arguments.length; x++){ if((x==0)&&(isNode)&&(dojo.event.browser.isEvent(arguments[x]))){ args.push(dojo.event.browser.fixEvent(arguments[x], this)); }else{ args.push(arguments[x]); } } } // return joinpoint.run.apply(joinpoint, arguments); return joinpoint.run.apply(joinpoint, args); } obj[funcName].__preJoinArity = origArity; } return joinpoint; // dojo.event.MethodJoinPoint}dojo.lang.extend(dojo.event.MethodJoinPoint, { unintercept: function(){ // summary: // destroy the connection to all listeners that may have been // registered on this joinpoint this.object[this.methodname] = this.methodfunc; this.before = []; this.after = []; this.around = []; }, disconnect: dojo.lang.forward("unintercept"), run: function(){ // summary: // execute the connection represented by this join point. The // arguments passed to run() will be passed to the function and // its listeners. var obj = this.object||dj_global; var args = arguments; // optimization. We only compute once the array version of the arguments // pseudo-arr in order to prevent building it each time advice is unrolled. var aargs = []; for(var x=0; x<args.length; x++){ aargs[x] = args[x]; } var unrollAdvice = function(marr){ if(!marr){ dojo.debug("Null argument to unrollAdvice()"); return; } var callObj = marr[0]||dj_global; var callFunc = marr[1]; if(!callObj[callFunc]){ dojo.raise("function \"" + callFunc + "\" does not exist on \"" + callObj + "\""); } var aroundObj = marr[2]||dj_global; var aroundFunc = marr[3]; var msg = marr[6]; var undef; var to = { args: [], jp_: this, object: obj, proceed: function(){ return callObj[callFunc].apply(callObj, to.args); } }; to.args = aargs; var delay = parseInt(marr[4]); var hasDelay = ((!isNaN(delay))&&(marr[4]!==null)&&(typeof marr[4] != "undefined")); if(marr[5]){ var rate = parseInt(marr[5]); var cur = new Date(); var timerSet = false; if((marr["last"])&&((cur-marr.last)<=rate)){ if(dojo.event._canTimeout){ if(marr["delayTimer"]){ clearTimeout(marr.delayTimer); } var tod = parseInt(rate*2); // is rate*2 naive? var mcpy = dojo.lang.shallowCopy(marr); marr.delayTimer = setTimeout(function(){ // FIXME: on IE at least, event objects from the // browser can go out of scope. How (or should?) we // deal with it? mcpy[5] = 0; unrollAdvice(mcpy); }, tod); } return; }else{ marr.last = cur; } } // FIXME: need to enforce rates for a connection here! if(aroundFunc){ // NOTE: around advice can't delay since we might otherwise depend // on execution order! aroundObj[aroundFunc].call(aroundObj, to); }else{ // var tmjp = dojo.event.MethodJoinPoint.getForMethod(obj, methname); if((hasDelay)&&((dojo.render.html)||(dojo.render.svg))){ // FIXME: the render checks are grotty! dj_global["setTimeout"](function(){ if(msg){ callObj[callFunc].call(callObj, to); }else{ callObj[callFunc].apply(callObj, args); } }, delay); }else{ // many environments can't support delay! if(msg){ callObj[callFunc].call(callObj, to); }else{ callObj[callFunc].apply(callObj, args); } } } } var unRollSquelch = function(){ if(this.squelch){ try{ return unrollAdvice.apply(this, arguments); }catch(e){ dojo.debug(e); } }else{ return unrollAdvice.apply(this, arguments); } } if((this["before"])&&(this.before.length>0)){ // pass a cloned array, if this event disconnects this event forEach on this.before wont work dojo.lang.forEach(this.before.concat(new Array()), unRollSquelch); } var result; try{ if((this["around"])&&(this.around.length>0)){ var mi = new dojo.event.MethodInvocation(this, obj, args); result = mi.proceed(); }else if(this.methodfunc){ result = this.object[this.methodname].apply(this.object, args); } }catch(e){ if(!this.squelch){ dojo.raise(e); } } if((this["after"])&&(this.after.length>0)){ // see comment on this.before above dojo.lang.forEach(this.after.concat(new Array()), unRollSquelch); } return (this.methodfunc) ? result : null; }, getArr: function(/*String*/kind){ // summary: return a list of listeners of the past "kind" // kind: // can be one of: "before", "after", "around", "before-around", or // "after-around" var type = "after"; // FIXME: we should be able to do this through props or Array.in() if((typeof kind == "string")&&(kind.indexOf("before")!=-1)){ type = "before"; }else if(kind=="around"){ type = "around"; } if(!this[type]){ this[type] = []; } return this[type]; // Array }, kwAddAdvice: function(/*Object*/args){ // summary: // adds advice to the joinpoint with arguments in a map // args: // An object that can have the following properties: // - adviceType // - adviceObj // - adviceFunc // - aroundObj // - aroundFunc // - once // - delay // - rate // - adviceMsg this.addAdvice( args["adviceObj"], args["adviceFunc"], args["aroundObj"], args["aroundFunc"], args["adviceType"], args["precedence"], args["once"], args["delay"], args["rate"], args["adviceMsg"]); }, addAdvice: function( thisAdviceObj, thisAdvice, thisAroundObj, thisAround, adviceType, precedence, once, delay, rate, asMessage){ // summary: // add advice to this joinpoint using positional parameters // thisAdviceObj: // the scope in which to locate/execute the named adviceFunc. // thisAdviceFunc: // the name of the function being conected // thisAroundObj: // the scope in which to locate/execute the named aroundFunc. // thisAroundFunc: // the name of the function that will be used to mediate the // advice call. // adviceType: // Optional. String. One of "before", "after", "around", // "before-around", or "after-around". FIXME // once: // boolean that determines whether or not this advice will create // a new connection if an identical advice set has already been // provided. Defaults to "false". // delay: // an optional delay (in ms), as an integer, for dispatch of a // listener after the source has been fired. // rate: // an optional rate throttling parameter (integer, in ms). When // specified, this particular connection will not fire more than // once in the interval specified by the rate // adviceMsg: // boolean. Should the listener have all the parameters passed in // as a single argument? var arr = this.getArr(adviceType); if(!arr){ dojo.raise("bad this: " + this); } var ao = [thisAdviceObj, thisAdvice, thisAroundObj, thisAround, delay, rate, asMessage]; if(once){ if(this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr) >= 0){ return; } } if(precedence == "first"){ arr.unshift(ao); }else{ arr.push(ao); } }, hasAdvice: function(thisAdviceObj, thisAdvice, adviceType, arr){ // summary: // returns the array index of the first existing connection // betweened the passed advice and this joinpoint. Will be -1 if // none exists. // thisAdviceObj: // the scope in which to locate/execute the named adviceFunc. // thisAdviceFunc: // the name of the function being conected // adviceType: // Optional. String. One of "before", "after", "around", // "before-around", or "after-around". FIXME // arr: // Optional. The list of advices to search. Will be found via // adviceType if not passed if(!arr){ arr = this.getArr(adviceType); } var ind = -1; for(var x=0; x<arr.length; x++){ var aao = (typeof thisAdvice == "object") ? (new String(thisAdvice)).toString() : thisAdvice; var a1o = (typeof arr[x][1] == "object") ? (new String(arr[x][1])).toString() : arr[x][1]; if((arr[x][0] == thisAdviceObj)&&(a1o == aao)){ ind = x; } } return ind; // Integer }, removeAdvice: function(thisAdviceObj, thisAdvice, adviceType, once){ // summary: // returns the array index of the first existing connection // betweened the passed advice and this joinpoint. Will be -1 if // none exists. // thisAdviceObj: // the scope in which to locate/execute the named adviceFunc. // thisAdviceFunc: // the name of the function being conected // adviceType: // Optional. String. One of "before", "after", "around", // "before-around", or "after-around". FIXME // once: // Optional. Should this only remove the first occurance of the // connection? var arr = this.getArr(adviceType); var ind = this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr); if(ind == -1){ return false; } while(ind != -1){ arr.splice(ind, 1); if(once){ break; } ind = this.hasAdvice(thisAdviceObj, thisAdvice, adviceType, arr); } return true; }});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -