diff options
author | Andrew Dolgov <[email protected]> | 2011-03-04 19:02:28 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2011-03-04 19:02:59 +0300 |
commit | a089699c8915636ba4f158d77dba9b012bc93208 (patch) | |
tree | b2d7d051f1f55d44a6be07d3ee137e5a7ccfcefb /lib/dojo/io | |
parent | cfad9259a6feacfa8194b1312770ae6db1ecce50 (diff) |
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
Diffstat (limited to 'lib/dojo/io')
-rw-r--r-- | lib/dojo/io/iframe.js | 654 | ||||
-rw-r--r-- | lib/dojo/io/script.js | 356 |
2 files changed, 649 insertions, 361 deletions
diff --git a/lib/dojo/io/iframe.js b/lib/dojo/io/iframe.js index 105af1697..5b2af45a1 100644 --- a/lib/dojo/io/iframe.js +++ b/lib/dojo/io/iframe.js @@ -5,261 +5,407 @@ */ -if(!dojo._hasResource["dojo.io.iframe"]){ -dojo._hasResource["dojo.io.iframe"]=true; +if(!dojo._hasResource["dojo.io.iframe"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.io.iframe"] = true; dojo.provide("dojo.io.iframe"); -dojo.io.iframe={create:function(_1,_2,_3){ -if(window[_1]){ -return window[_1]; -} -if(window.frames[_1]){ -return window.frames[_1]; -} -var _4=null; -var _5=_3; -if(!_5){ -if(dojo.config["useXDomain"]&&!dojo.config["dojoBlankHtmlUrl"]){ -console.warn("dojo.io.iframe.create: When using cross-domain Dojo builds,"+" please save dojo/resources/blank.html to your domain and set djConfig.dojoBlankHtmlUrl"+" to the path on your domain to blank.html"); -} -_5=(dojo.config["dojoBlankHtmlUrl"]||dojo.moduleUrl("dojo","resources/blank.html")); -} -var _6=dojo.isIE?"<iframe name=\""+_1+"\" src=\""+_5+"\" onload=\""+_2+"\">":"iframe"; -_4=dojo.doc.createElement(_6); -with(_4){ -name=_1; -setAttribute("name",_1); -id=_1; -} -dojo.body().appendChild(_4); -window[_1]=_4; -with(_4.style){ -if(!(dojo.isSafari<3)){ -position="absolute"; -} -left=top="1px"; -height=width="1px"; -visibility="hidden"; -} -if(!dojo.isIE){ -this.setSrc(_4,_5,true); -_4.onload=new Function(_2); -} -return _4; -},setSrc:function(_7,_8,_9){ -try{ -if(!_9){ -if(dojo.isWebKit){ -_7.location=_8; -}else{ -frames[_7.name].location=_8; -} -}else{ -var _a; -if(dojo.isIE||dojo.isWebKit>521){ -_a=_7.contentWindow.document; -}else{ -if(dojo.isSafari){ -_a=_7.document; -}else{ -_a=_7.contentWindow; -} -} -if(!_a){ -_7.location=_8; -return; -}else{ -_a.location.replace(_8); -} -} -} -catch(e){ -} -},doc:function(_b){ -var _c=_b.contentDocument||(((_b.name)&&(_b.document)&&(dojo.doc.getElementsByTagName("iframe")[_b.name].contentWindow)&&(dojo.doc.getElementsByTagName("iframe")[_b.name].contentWindow.document)))||((_b.name)&&(dojo.doc.frames[_b.name])&&(dojo.doc.frames[_b.name].document))||null; -return _c; -},send:function(_d){ -if(!this["_frame"]){ -this._frame=this.create(this._iframeName,dojo._scopeName+".io.iframe._iframeOnload();"); -} -var _e=dojo._ioSetArgs(_d,function(_f){ -_f.canceled=true; -_f.ioArgs._callNext(); -},function(dfd){ -var _10=null; -try{ -var _11=dfd.ioArgs; -var dii=dojo.io.iframe; -var ifd=dii.doc(dii._frame); -var _12=_11.handleAs; -_10=ifd; -if(_12!="html"){ -if(_12=="xml"){ -if(dojo.isIE){ -dojo.query("a",dii._frame.contentWindow.document.documentElement).orphan(); -var _13=(dii._frame.contentWindow.document).documentElement.innerText; -_13=_13.replace(/>\s+</g,"><"); -_13=dojo.trim(_13); -var _14={responseText:_13}; -_10=dojo._contentHandlers["xml"](_14); -} -}else{ -_10=ifd.getElementsByTagName("textarea")[0].value; -if(_12=="json"){ -_10=dojo.fromJson(_10); -}else{ -if(_12=="javascript"){ -_10=dojo.eval(_10); -} -} -} -} -} -catch(e){ -_10=e; -} -finally{ -_11._callNext(); -} -return _10; -},function(_15,dfd){ -dfd.ioArgs._hasError=true; -dfd.ioArgs._callNext(); -return _15; -}); -_e.ioArgs._callNext=function(){ -if(!this["_calledNext"]){ -this._calledNext=true; -dojo.io.iframe._currentDfd=null; -dojo.io.iframe._fireNextRequest(); -} -}; -this._dfdQueue.push(_e); -this._fireNextRequest(); -dojo._ioWatch(_e,function(dfd){ -return !dfd.ioArgs["_hasError"]; -},function(dfd){ -return (!!dfd.ioArgs["_finished"]); -},function(dfd){ -if(dfd.ioArgs._finished){ -dfd.callback(dfd); -}else{ -dfd.errback(new Error("Invalid dojo.io.iframe request state")); -} + +/*===== +dojo.declare("dojo.io.iframe.__ioArgs", dojo.__IoArgs, { + constructor: function(){ + // summary: + // All the properties described in the dojo.__ioArgs type, apply + // to this type. The following additional properties are allowed + // for dojo.io.iframe.send(): + // method: String? + // The HTTP method to use. "GET" or "POST" are the only supported + // values. It will try to read the value from the form node's + // method, then try this argument. If neither one exists, then it + // defaults to POST. + // handleAs: String? + // Specifies what format the result data should be given to the + // load/handle callback. Valid values are: text, html, xml, json, + // javascript. IMPORTANT: For all values EXCEPT html and xml, The + // server response should be an HTML file with a textarea element. + // The response data should be inside the textarea element. Using an + // HTML document the only reliable, cross-browser way this + // transport can know when the response has loaded. For the html + // handleAs value, just return a normal HTML document. NOTE: xml + // is now supported with this transport (as of 1.1+); a known issue + // is if the XML document in question is malformed, Internet Explorer + // will throw an uncatchable error. + // content: Object? + // If "form" is one of the other args properties, then the content + // object properties become hidden form form elements. For + // instance, a content object of {name1 : "value1"} is converted + // to a hidden form element with a name of "name1" and a value of + // "value1". If there is not a "form" property, then the content + // object is converted into a name=value&name=value string, by + // using dojo.objectToQuery(). + this.method = method; + this.handleAs = handleAs; + this.content = content; + } }); -return _e; -},_currentDfd:null,_dfdQueue:[],_iframeName:dojo._scopeName+"IoIframe",_fireNextRequest:function(){ -try{ -if((this._currentDfd)||(this._dfdQueue.length==0)){ -return; -} -do{ -var dfd=this._currentDfd=this._dfdQueue.shift(); -}while(dfd&&dfd.canceled&&this._dfdQueue.length); -if(!dfd||dfd.canceled){ -this._currentDfd=null; -return; -} -var _16=dfd.ioArgs; -var _17=_16.args; -_16._contentToClean=[]; -var fn=dojo.byId(_17["form"]); -var _18=_17["content"]||{}; -if(fn){ -if(_18){ -var _19=function(_1a,_1b){ -var tn; -if(dojo.isIE){ -tn=dojo.doc.createElement("<input type='hidden' name='"+_1a+"'>"); -}else{ -tn=dojo.doc.createElement("input"); -tn.type="hidden"; -tn.name=_1a; -} -tn.value=_1b; -fn.appendChild(tn); -_16._contentToClean.push(_1a); -}; -for(var x in _18){ -var val=_18[x]; -if(dojo.isArray(val)&&val.length>1){ -var i; -for(i=0;i<val.length;i++){ -_19(x,val[i]); -} -}else{ -if(!fn[x]){ -_19(x,val); -}else{ -fn[x].value=val; -} -} -} -} -var _1c=fn.getAttributeNode("action"); -var _1d=fn.getAttributeNode("method"); -var _1e=fn.getAttributeNode("target"); -if(_17["url"]){ -_16._originalAction=_1c?_1c.value:null; -if(_1c){ -_1c.value=_17.url; -}else{ -fn.setAttribute("action",_17.url); -} -} -if(!_1d||!_1d.value){ -if(_1d){ -_1d.value=(_17["method"])?_17["method"]:"post"; -}else{ -fn.setAttribute("method",(_17["method"])?_17["method"]:"post"); -} -} -_16._originalTarget=_1e?_1e.value:null; -if(_1e){ -_1e.value=this._iframeName; -}else{ -fn.setAttribute("target",this._iframeName); -} -fn.target=this._iframeName; -dojo._ioNotifyStart(dfd); -fn.submit(); -}else{ -var _1f=_17.url+(_17.url.indexOf("?")>-1?"&":"?")+_16.query; -dojo._ioNotifyStart(dfd); -this.setSrc(this._frame,_1f,true); -} -} -catch(e){ -dfd.errback(e); -} -},_iframeOnload:function(){ -var dfd=this._currentDfd; -if(!dfd){ -this._fireNextRequest(); -return; -} -var _20=dfd.ioArgs; -var _21=_20.args; -var _22=dojo.byId(_21.form); -if(_22){ -var _23=_20._contentToClean; -for(var i=0;i<_23.length;i++){ -var key=_23[i]; -for(var j=0;j<_22.childNodes.length;j++){ -var _24=_22.childNodes[j]; -if(_24.name==key){ -dojo.destroy(_24); -break; -} -} -} -if(_20["_originalAction"]){ -_22.setAttribute("action",_20._originalAction); -} -if(_20["_originalTarget"]){ -_22.setAttribute("target",_20._originalTarget); -_22.target=_20._originalTarget; -} +=====*/ + +dojo.io.iframe = { + // summary: + // Sends an Ajax I/O call using and Iframe (for instance, to upload files) + + create: function(/*String*/fname, /*String*/onloadstr, /*String?*/uri){ + // summary: + // Creates a hidden iframe in the page. Used mostly for IO + // transports. You do not need to call this to start a + // dojo.io.iframe request. Just call send(). + // fname: String + // The name of the iframe. Used for the name attribute on the + // iframe. + // onloadstr: String + // A string of JavaScript that will be executed when the content + // in the iframe loads. + // uri: String + // The value of the src attribute on the iframe element. If a + // value is not given, then dojo/resources/blank.html will be + // used. + if(window[fname]){ return window[fname]; } + if(window.frames[fname]){ return window.frames[fname]; } + var cframe = null; + var turi = uri; + if(!turi){ + if(dojo.config["useXDomain"] && !dojo.config["dojoBlankHtmlUrl"]){ + console.warn("dojo.io.iframe.create: When using cross-domain Dojo builds," + + " please save dojo/resources/blank.html to your domain and set djConfig.dojoBlankHtmlUrl" + + " to the path on your domain to blank.html"); + } + turi = (dojo.config["dojoBlankHtmlUrl"]||dojo.moduleUrl("dojo", "resources/blank.html")); + } + var ifrstr = dojo.isIE ? '<iframe name="'+fname+'" src="'+turi+'" onload="'+onloadstr+'">' : 'iframe'; + cframe = dojo.doc.createElement(ifrstr); + with(cframe){ + name = fname; + setAttribute("name", fname); + id = fname; + } + dojo.body().appendChild(cframe); + window[fname] = cframe; + + with(cframe.style){ + if(!(dojo.isSafari < 3)){ + //We can't change the src in Safari 2.0.3 if absolute position. Bizarro. + position = "absolute"; + } + left = top = "1px"; + height = width = "1px"; + visibility = "hidden"; + } + + if(!dojo.isIE){ + this.setSrc(cframe, turi, true); + cframe.onload = new Function(onloadstr); + } + + return cframe; + }, + + setSrc: function(/*DOMNode*/iframe, /*String*/src, /*Boolean*/replace){ + //summary: + // Sets the URL that is loaded in an IFrame. The replace parameter + // indicates whether location.replace() should be used when + // changing the location of the iframe. + try{ + if(!replace){ + if(dojo.isWebKit){ + iframe.location = src; + }else{ + frames[iframe.name].location = src; + } + }else{ + // Fun with DOM 0 incompatibilities! + var idoc; + //WebKit > 521 corresponds with Safari 3, which started with 522 WebKit version. + if(dojo.isIE || dojo.isWebKit > 521){ + idoc = iframe.contentWindow.document; + }else if(dojo.isSafari){ + idoc = iframe.document; + }else{ // if(d.isMozilla){ + idoc = iframe.contentWindow; + } + + //For Safari (at least 2.0.3) and Opera, if the iframe + //has just been created but it doesn't have content + //yet, then iframe.document may be null. In that case, + //use iframe.location and return. + if(!idoc){ + iframe.location = src; + return; + }else{ + idoc.location.replace(src); + } + } + }catch(e){ + console.log("dojo.io.iframe.setSrc: ", e); + } + }, + + doc: function(/*DOMNode*/iframeNode){ + //summary: Returns the document object associated with the iframe DOM Node argument. + var doc = iframeNode.contentDocument || // W3 + ( + ( + (iframeNode.name) && (iframeNode.document) && + (dojo.doc.getElementsByTagName("iframe")[iframeNode.name].contentWindow) && + (dojo.doc.getElementsByTagName("iframe")[iframeNode.name].contentWindow.document) + ) + ) || // IE + ( + (iframeNode.name)&&(dojo.doc.frames[iframeNode.name])&& + (dojo.doc.frames[iframeNode.name].document) + ) || null; + return doc; + }, + + send: function(/*dojo.io.iframe.__ioArgs*/args){ + //summary: + // Function that sends the request to the server. + // This transport can only process one send() request at a time, so if send() is called + //multiple times, it will queue up the calls and only process one at a time. + if(!this["_frame"]){ + this._frame = this.create(this._iframeName, dojo._scopeName + ".io.iframe._iframeOnload();"); + } + + //Set up the deferred. + var dfd = dojo._ioSetArgs( + args, + function(/*Deferred*/dfd){ + //summary: canceller function for dojo._ioSetArgs call. + dfd.canceled = true; + dfd.ioArgs._callNext(); + }, + function(/*Deferred*/dfd){ + //summary: okHandler function for dojo._ioSetArgs call. + var value = null; + try{ + var ioArgs = dfd.ioArgs; + var dii = dojo.io.iframe; + var ifd = dii.doc(dii._frame); + var handleAs = ioArgs.handleAs; + + //Assign correct value based on handleAs value. + value = ifd; //html + if(handleAs != "html"){ + if(handleAs == "xml"){ + // FF, Saf 3+ and Opera all seem to be fine with ifd being xml. We have to + // do it manually for IE. Refs #6334. + if(dojo.isIE){ + dojo.query("a", dii._frame.contentWindow.document.documentElement).orphan(); + var xmlText=(dii._frame.contentWindow.document).documentElement.innerText; + xmlText=xmlText.replace(/>\s+</g, "><"); + xmlText=dojo.trim(xmlText); + //Reusing some code in base dojo for handling XML content. Simpler and keeps + //Core from duplicating the effort needed to locate the XML Parser on IE. + var fauxXhr = { responseText: xmlText }; + value = dojo._contentHandlers["xml"](fauxXhr); // DOMDocument + } + }else{ + value = ifd.getElementsByTagName("textarea")[0].value; //text + if(handleAs == "json"){ + value = dojo.fromJson(value); //json + }else if(handleAs == "javascript"){ + value = dojo.eval(value); //javascript + } + } + } + }catch(e){ + value = e; + }finally{ + ioArgs._callNext(); + } + return value; + }, + function(/*Error*/error, /*Deferred*/dfd){ + //summary: errHandler function for dojo._ioSetArgs call. + dfd.ioArgs._hasError = true; + dfd.ioArgs._callNext(); + return error; + } + ); + + //Set up a function that will fire the next iframe request. Make sure it only + //happens once per deferred. + dfd.ioArgs._callNext = function(){ + if(!this["_calledNext"]){ + this._calledNext = true; + dojo.io.iframe._currentDfd = null; + dojo.io.iframe._fireNextRequest(); + } + } + + this._dfdQueue.push(dfd); + this._fireNextRequest(); + + //Add it the IO watch queue, to get things like timeout support. + dojo._ioWatch( + dfd, + function(/*Deferred*/dfd){ + //validCheck + return !dfd.ioArgs["_hasError"]; + }, + function(dfd){ + //ioCheck + return (!!dfd.ioArgs["_finished"]); + }, + function(dfd){ + //resHandle + if(dfd.ioArgs._finished){ + dfd.callback(dfd); + }else{ + dfd.errback(new Error("Invalid dojo.io.iframe request state")); + } + } + ); + + return dfd; + }, + + _currentDfd: null, + _dfdQueue: [], + _iframeName: dojo._scopeName + "IoIframe", + + _fireNextRequest: function(){ + //summary: Internal method used to fire the next request in the bind queue. + try{ + if((this._currentDfd)||(this._dfdQueue.length == 0)){ return; } + //Find next deferred, skip the canceled ones. + do{ + var dfd = this._currentDfd = this._dfdQueue.shift(); + } while(dfd && dfd.canceled && this._dfdQueue.length); + + //If no more dfds, cancel. + if(!dfd || dfd.canceled){ + this._currentDfd = null; + return; + } + + var ioArgs = dfd.ioArgs; + var args = ioArgs.args; + + ioArgs._contentToClean = []; + var fn = dojo.byId(args["form"]); + var content = args["content"] || {}; + if(fn){ + if(content){ + // if we have things in content, we need to add them to the form + // before submission + var pHandler = function(name, value) { + var tn; + if(dojo.isIE){ + tn = dojo.doc.createElement("<input type='hidden' name='"+name+"'>"); + }else{ + tn = dojo.doc.createElement("input"); + tn.type = "hidden"; + tn.name = name; + } + tn.value = value; + fn.appendChild(tn); + ioArgs._contentToClean.push(name); + }; + for(var x in content){ + var val = content[x]; + if(dojo.isArray(val) && val.length > 1){ + var i; + for (i = 0; i < val.length; i++) { + pHandler(x,val[i]); + } + }else{ + if(!fn[x]){ + pHandler(x,val); + }else{ + fn[x].value = val; + } + } + } + } + //IE requires going through getAttributeNode instead of just getAttribute in some form cases, + //so use it for all. See #2844 + var actnNode = fn.getAttributeNode("action"); + var mthdNode = fn.getAttributeNode("method"); + var trgtNode = fn.getAttributeNode("target"); + if(args["url"]){ + ioArgs._originalAction = actnNode ? actnNode.value : null; + if(actnNode){ + actnNode.value = args.url; + }else{ + fn.setAttribute("action",args.url); + } + } + if(!mthdNode || !mthdNode.value){ + if(mthdNode){ + mthdNode.value= (args["method"]) ? args["method"] : "post"; + }else{ + fn.setAttribute("method", (args["method"]) ? args["method"] : "post"); + } + } + ioArgs._originalTarget = trgtNode ? trgtNode.value: null; + if(trgtNode){ + trgtNode.value = this._iframeName; + }else{ + fn.setAttribute("target", this._iframeName); + } + fn.target = this._iframeName; + dojo._ioNotifyStart(dfd); + fn.submit(); + }else{ + // otherwise we post a GET string by changing URL location for the + // iframe + var tmpUrl = args.url + (args.url.indexOf("?") > -1 ? "&" : "?") + ioArgs.query; + dojo._ioNotifyStart(dfd); + this.setSrc(this._frame, tmpUrl, true); + } + }catch(e){ + dfd.errback(e); + } + }, + + _iframeOnload: function(){ + var dfd = this._currentDfd; + if(!dfd){ + this._fireNextRequest(); + return; + } + + var ioArgs = dfd.ioArgs; + var args = ioArgs.args; + var fNode = dojo.byId(args.form); + + if(fNode){ + // remove all the hidden content inputs + var toClean = ioArgs._contentToClean; + for(var i = 0; i < toClean.length; i++) { + var key = toClean[i]; + //Need to cycle over all nodes since we may have added + //an array value which means that more than one node could + //have the same .name value. + for(var j = 0; j < fNode.childNodes.length; j++){ + var chNode = fNode.childNodes[j]; + if(chNode.name == key){ + dojo.destroy(chNode); + break; + } + } + } + + // restore original action + target + if(ioArgs["_originalAction"]){ + fNode.setAttribute("action", ioArgs._originalAction); + } + if(ioArgs["_originalTarget"]){ + fNode.setAttribute("target", ioArgs._originalTarget); + fNode.target = ioArgs._originalTarget; + } + } + + ioArgs._finished = true; + } } -_20._finished=true; -}}; + } diff --git a/lib/dojo/io/script.js b/lib/dojo/io/script.js index 9a940634d..9730c977d 100644 --- a/lib/dojo/io/script.js +++ b/lib/dojo/io/script.js @@ -5,114 +5,256 @@ */ -if(!dojo._hasResource["dojo.io.script"]){ -dojo._hasResource["dojo.io.script"]=true; +if(!dojo._hasResource["dojo.io.script"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojo.io.script"] = true; dojo.provide("dojo.io.script"); -(function(){ -var _1=dojo.isIE?"onreadystatechange":"load",_2=/complete|loaded/; -dojo.io.script={get:function(_3){ -var _4=this._makeScriptDeferred(_3); -var _5=_4.ioArgs; -dojo._ioAddQueryToUrl(_5); -dojo._ioNotifyStart(_4); -if(this._canAttach(_5)){ -var _6=this.attach(_5.id,_5.url,_3.frameDoc); -if(!_5.jsonp&&!_5.args.checkString){ -var _7=dojo.connect(_6,_1,function(_8){ -if(_8.type=="load"||_2.test(_6.readyState)){ -dojo.disconnect(_7); -_5.scriptLoaded=_8; -} + +/*===== +dojo.declare("dojo.io.script.__ioArgs", dojo.__IoArgs, { + constructor: function(){ + // summary: + // All the properties described in the dojo.__ioArgs type, apply to this + // type as well, EXCEPT "handleAs". It is not applicable to + // dojo.io.script.get() calls, since it is implied by the usage of + // "jsonp" (response will be a JSONP call returning JSON) + // or the response is pure JavaScript defined in + // the body of the script that was attached. + // callbackParamName: String + // Deprecated as of Dojo 1.4 in favor of "jsonp", but still supported for + // legacy code. See notes for jsonp property. + // jsonp: String + // The URL parameter name that indicates the JSONP callback string. + // For instance, when using Yahoo JSONP calls it is normally, + // jsonp: "callback". For AOL JSONP calls it is normally + // jsonp: "c". + // checkString: String + // A string of JavaScript that when evaluated like so: + // "typeof(" + checkString + ") != 'undefined'" + // being true means that the script fetched has been loaded. + // Do not use this if doing a JSONP type of call (use callbackParamName instead). + // frameDoc: Document + // The Document object for a child iframe. If this is passed in, the script + // will be attached to that document. This can be helpful in some comet long-polling + // scenarios with Firefox and Opera. + this.callbackParamName = callbackParamName; + this.jsonp = jsonp; + this.checkString = checkString; + this.frameDoc = frameDoc; + } }); -} -} -dojo._ioWatch(_4,this._validCheck,this._ioCheck,this._resHandle); -return _4; -},attach:function(id,_9,_a){ -var _b=(_a||dojo.doc); -var _c=_b.createElement("script"); -_c.type="text/javascript"; -_c.src=_9; -_c.id=id; -_c.charset="utf-8"; -return _b.getElementsByTagName("head")[0].appendChild(_c); -},remove:function(id,_d){ -dojo.destroy(dojo.byId(id,_d)); -if(this["jsonp_"+id]){ -delete this["jsonp_"+id]; -} -},_makeScriptDeferred:function(_e){ -var _f=dojo._ioSetArgs(_e,this._deferredCancel,this._deferredOk,this._deferredError); -var _10=_f.ioArgs; -_10.id=dojo._scopeName+"IoScript"+(this._counter++); -_10.canDelete=false; -_10.jsonp=_e.callbackParamName||_e.jsonp; -if(_10.jsonp){ -_10.query=_10.query||""; -if(_10.query.length>0){ -_10.query+="&"; -} -_10.query+=_10.jsonp+"="+(_e.frameDoc?"parent.":"")+dojo._scopeName+".io.script.jsonp_"+_10.id+"._jsonpCallback"; -_10.frameDoc=_e.frameDoc; -_10.canDelete=true; -_f._jsonpCallback=this._jsonpCallback; -this["jsonp_"+_10.id]=_f; -} -return _f; -},_deferredCancel:function(dfd){ -dfd.canceled=true; -if(dfd.ioArgs.canDelete){ -dojo.io.script._addDeadScript(dfd.ioArgs); -} -},_deferredOk:function(dfd){ -var _11=dfd.ioArgs; -if(_11.canDelete){ -dojo.io.script._addDeadScript(_11); -} -return _11.json||_11.scriptLoaded||_11; -},_deferredError:function(_12,dfd){ -if(dfd.ioArgs.canDelete){ -if(_12.dojoType=="timeout"){ -dojo.io.script.remove(dfd.ioArgs.id,dfd.ioArgs.frameDoc); -}else{ -dojo.io.script._addDeadScript(dfd.ioArgs); -} -} -return _12; -},_deadScripts:[],_counter:1,_addDeadScript:function(_13){ -dojo.io.script._deadScripts.push({id:_13.id,frameDoc:_13.frameDoc}); -_13.frameDoc=null; -},_validCheck:function(dfd){ -var _14=dojo.io.script; -var _15=_14._deadScripts; -if(_15&&_15.length>0){ -for(var i=0;i<_15.length;i++){ -_14.remove(_15[i].id,_15[i].frameDoc); -_15[i].frameDoc=null; -} -dojo.io.script._deadScripts=[]; -} -return true; -},_ioCheck:function(dfd){ -var _16=dfd.ioArgs; -if(_16.json||(_16.scriptLoaded&&!_16.args.checkString)){ -return true; -} -var _17=_16.args.checkString; -if(_17&&eval("typeof("+_17+") != 'undefined'")){ -return true; -} -return false; -},_resHandle:function(dfd){ -if(dojo.io.script._ioCheck(dfd)){ -dfd.callback(dfd); -}else{ -dfd.errback(new Error("inconceivable dojo.io.script._resHandle error")); -} -},_canAttach:function(_18){ -return true; -},_jsonpCallback:function(_19){ -this.ioArgs.json=_19; -}}; +=====*/ +;(function(){ + var loadEvent = dojo.isIE ? "onreadystatechange" : "load", + readyRegExp = /complete|loaded/; + + dojo.io.script = { + get: function(/*dojo.io.script.__ioArgs*/args){ + // summary: + // sends a get request using a dynamically created script tag. + var dfd = this._makeScriptDeferred(args); + var ioArgs = dfd.ioArgs; + dojo._ioAddQueryToUrl(ioArgs); + + dojo._ioNotifyStart(dfd); + + if(this._canAttach(ioArgs)){ + var node = this.attach(ioArgs.id, ioArgs.url, args.frameDoc); + + //If not a jsonp callback or a polling checkString case, bind + //to load event on the script tag. + if(!ioArgs.jsonp && !ioArgs.args.checkString){ + var handle = dojo.connect(node, loadEvent, function(evt){ + if(evt.type == "load" || readyRegExp.test(node.readyState)){ + dojo.disconnect(handle); + ioArgs.scriptLoaded = evt; + } + }); + } + } + + dojo._ioWatch(dfd, this._validCheck, this._ioCheck, this._resHandle); + return dfd; + }, + + attach: function(/*String*/id, /*String*/url, /*Document?*/frameDocument){ + // summary: + // creates a new <script> tag pointing to the specified URL and + // adds it to the document. + // description: + // Attaches the script element to the DOM. Use this method if you + // just want to attach a script to the DOM and do not care when or + // if it loads. + var doc = (frameDocument || dojo.doc); + var element = doc.createElement("script"); + element.type = "text/javascript"; + element.src = url; + element.id = id; + element.charset = "utf-8"; + return doc.getElementsByTagName("head")[0].appendChild(element); + }, + + remove: function(/*String*/id, /*Document?*/frameDocument){ + //summary: removes the script element with the given id, from the given frameDocument. + //If no frameDocument is passed, the current document is used. + dojo.destroy(dojo.byId(id, frameDocument)); + + //Remove the jsonp callback on dojo.io.script, if it exists. + if(this["jsonp_" + id]){ + delete this["jsonp_" + id]; + } + }, + + _makeScriptDeferred: function(/*Object*/args){ + //summary: + // sets up a Deferred object for an IO request. + var dfd = dojo._ioSetArgs(args, this._deferredCancel, this._deferredOk, this._deferredError); + + var ioArgs = dfd.ioArgs; + ioArgs.id = dojo._scopeName + "IoScript" + (this._counter++); + ioArgs.canDelete = false; + + //Special setup for jsonp case + ioArgs.jsonp = args.callbackParamName || args.jsonp; + if(ioArgs.jsonp){ + //Add the jsonp parameter. + ioArgs.query = ioArgs.query || ""; + if(ioArgs.query.length > 0){ + ioArgs.query += "&"; + } + ioArgs.query += ioArgs.jsonp + + "=" + + (args.frameDoc ? "parent." : "") + + dojo._scopeName + ".io.script.jsonp_" + ioArgs.id + "._jsonpCallback"; + + ioArgs.frameDoc = args.frameDoc; + + //Setup the Deferred to have the jsonp callback. + ioArgs.canDelete = true; + dfd._jsonpCallback = this._jsonpCallback; + this["jsonp_" + ioArgs.id] = dfd; + } + return dfd; // dojo.Deferred + }, + + _deferredCancel: function(/*Deferred*/dfd){ + //summary: canceller function for dojo._ioSetArgs call. + + //DO NOT use "this" and expect it to be dojo.io.script. + dfd.canceled = true; + if(dfd.ioArgs.canDelete){ + dojo.io.script._addDeadScript(dfd.ioArgs); + } + }, + + _deferredOk: function(/*Deferred*/dfd){ + //summary: okHandler function for dojo._ioSetArgs call. + + //DO NOT use "this" and expect it to be dojo.io.script. + var ioArgs = dfd.ioArgs; + + //Add script to list of things that can be removed. + if(ioArgs.canDelete){ + dojo.io.script._addDeadScript(ioArgs); + } + + //Favor JSONP responses, script load events then lastly ioArgs. + //The ioArgs are goofy, but cannot return the dfd since that stops + //the callback chain in Deferred. The return value is not that important + //in that case, probably a checkString case. + return ioArgs.json || ioArgs.scriptLoaded || ioArgs; + }, + + _deferredError: function(/*Error*/error, /*Deferred*/dfd){ + //summary: errHandler function for dojo._ioSetArgs call. + + if(dfd.ioArgs.canDelete){ + //DO NOT use "this" and expect it to be dojo.io.script. + if(error.dojoType == "timeout"){ + //For timeouts, remove the script element immediately to + //avoid a response from it coming back later and causing trouble. + dojo.io.script.remove(dfd.ioArgs.id, dfd.ioArgs.frameDoc); + }else{ + dojo.io.script._addDeadScript(dfd.ioArgs); + } + } + console.log("dojo.io.script error", error); + return error; + }, + + _deadScripts: [], + _counter: 1, + + _addDeadScript: function(/*Object*/ioArgs){ + //summary: sets up an entry in the deadScripts array. + dojo.io.script._deadScripts.push({id: ioArgs.id, frameDoc: ioArgs.frameDoc}); + //Being extra paranoid about leaks: + ioArgs.frameDoc = null; + }, + + _validCheck: function(/*Deferred*/dfd){ + //summary: inflight check function to see if dfd is still valid. + + //Do script cleanup here. We wait for one inflight pass + //to make sure we don't get any weird things by trying to remove a script + //tag that is part of the call chain (IE 6 has been known to + //crash in that case). + var _self = dojo.io.script; + var deadScripts = _self._deadScripts; + if(deadScripts && deadScripts.length > 0){ + for(var i = 0; i < deadScripts.length; i++){ + //Remove the script tag + _self.remove(deadScripts[i].id, deadScripts[i].frameDoc); + deadScripts[i].frameDoc = null; + } + dojo.io.script._deadScripts = []; + } + + return true; + }, + + _ioCheck: function(/*Deferred*/dfd){ + //summary: inflight check function to see if IO finished. + var ioArgs = dfd.ioArgs; + //Check for finished jsonp + if(ioArgs.json || (ioArgs.scriptLoaded && !ioArgs.args.checkString)){ + return true; + } + + //Check for finished "checkString" case. + var checkString = ioArgs.args.checkString; + if(checkString && eval("typeof(" + checkString + ") != 'undefined'")){ + return true; + } + + return false; + }, + + _resHandle: function(/*Deferred*/dfd){ + //summary: inflight function to handle a completed response. + if(dojo.io.script._ioCheck(dfd)){ + dfd.callback(dfd); + }else{ + //This path should never happen since the only way we can get + //to _resHandle is if _ioCheck is true. + dfd.errback(new Error("inconceivable dojo.io.script._resHandle error")); + } + }, + + _canAttach: function(/*Object*/ioArgs){ + //summary: A method that can be overridden by other modules + //to control when the script attachment occurs. + return true; + }, + + _jsonpCallback: function(/*JSON Object*/json){ + //summary: + // generic handler for jsonp callback. A pointer to this function + // is used for all jsonp callbacks. NOTE: the "this" in this + // function will be the Deferred object that represents the script + // request. + this.ioArgs.json = json; + } + } })(); + } |