📄 scriptsrcio.js
字号:
//part of a multipart URL. state.constantParams = (kwArgs["constantParams"] == null ? "" : kwArgs["constantParams"]); if(kwArgs["preventCache"] || (this.preventCache == true && kwArgs["preventCache"] != false)){ state.nocacheParam = "dojo.preventCache=" + new Date().valueOf(); }else{ state.nocacheParam = ""; } //Get total length URL, if we were to do it as one URL. //Add some padding, extra & separators. var urlLength = state.url.length + state.query.length + state.constantParams.length + state.nocacheParam.length + this._extraPaddingLength; if(kwArgs["useRequestId"]){ urlLength += state.idParam.length; } if(!kwArgs["checkString"] && kwArgs["useRequestId"] && !state["jsonp"] && !kwArgs["forceSingleRequest"] && urlLength > this.maxUrlLength){ if(url > this.maxUrlLength){ //Error. The URL domain and path are too long. We can't //segment that, so return an error. this._finish(state, "error", {status: this.DsrStatusCodes.Error, statusText: "url.tooBig"}); return; }else{ //Start the multiple requests. this._multiAttach(state, 1); } }else{ //Send one URL. var queryParams = [state.constantParams, state.nocacheParam, state.query]; if(kwArgs["useRequestId"] && !state["jsonp"]){ queryParams.unshift(state.idParam); } var finalUrl = this._buildUrl(state.url, queryParams); //Track the final URL in case we need to use that instead of api ID when receiving //the load callback. state.finalUrl = finalUrl; this._attach(state.id, finalUrl); } //END DSR this.startWatchingInFlight(); } //Private properties/methods this._counter = 1; this._state = {}; this._extraPaddingLength = 16; //Is there a dojo function for this already? this._buildUrl = function(url, nameValueArray){ var finalUrl = url; var joiner = "?"; for(var i = 0; i < nameValueArray.length; i++){ if(nameValueArray[i]){ finalUrl += joiner + nameValueArray[i]; joiner = "&"; } } return finalUrl; } this._attach = function(id, url){ //Attach the script to the DOM. var element = document.createElement("script"); element.type = "text/javascript"; element.src = url; element.id = id; element.className = "ScriptSrcTransport"; document.getElementsByTagName("head")[0].appendChild(element); } this._multiAttach = function(state, part){ //Check to make sure we still have a query to send up. This is mostly //a protection from a goof on the server side when it sends a part OK //response instead of a final response. if(state.query == null){ this._finish(state, "error", {status: this.DsrStatusCodes.Error, statusText: "query.null"}); return; } if(!state.constantParams){ state.constantParams = ""; } //How much of the query can we take? //Add a padding constant to account for _part and a couple extra amperstands. //Also add space for id since we'll need it now. var queryMax = this.maxUrlLength - state.idParam.length - state.constantParams.length - state.url.length - state.nocacheParam.length - this._extraPaddingLength; //Figure out if this is the last part. var isDone = state.query.length < queryMax; //Break up the query string if necessary. var currentQuery; if(isDone){ currentQuery = state.query; state.query = null; }else{ //Find the & or = nearest the max url length. var ampEnd = state.query.lastIndexOf("&", queryMax - 1); var eqEnd = state.query.lastIndexOf("=", queryMax - 1); //See if & is closer, or if = is right at the edge, //which means we should put it on the next URL. if(ampEnd > eqEnd || eqEnd == queryMax - 1){ //& is nearer the end. So just chop off from there. currentQuery = state.query.substring(0, ampEnd); state.query = state.query.substring(ampEnd + 1, state.query.length) //strip off amperstand with the + 1. }else{ //= is nearer the end. Take the max amount possible. currentQuery = state.query.substring(0, queryMax); //Find the last query name in the currentQuery so we can prepend it to //ampEnd. Could be -1 (not there), so account for that. var queryName = currentQuery.substring((ampEnd == -1 ? 0 : ampEnd + 1), eqEnd); state.query = queryName + "=" + state.query.substring(queryMax, state.query.length); } } //Now send a part of the script var queryParams = [currentQuery, state.idParam, state.constantParams, state.nocacheParam]; if(!isDone){ queryParams.push("_part=" + part); } var url = this._buildUrl(state.url, queryParams); this._attach(state.id + "_" + part, url); } this._finish = function(state, callback, event){ if(callback != "partOk" && !state.kwArgs[callback] && !state.kwArgs["handle"]){ //Ignore "partOk" because that is an internal callback. if(callback == "error"){ state.isDone = true; throw event; } }else{ switch(callback){ case "load": var response = event ? event.response : null; if(!response){ response = event; } state.kwArgs[(typeof state.kwArgs.load == "function") ? "load" : "handle"]("load", response, event, state.kwArgs); state.isDone = true; break; case "partOk": var part = parseInt(event.response.part, 10) + 1; //Update the constant params, if any. if(event.response.constantParams){ state.constantParams = event.response.constantParams; } this._multiAttach(state, part); state.isDone = false; break; case "error": state.kwArgs[(typeof state.kwArgs.error == "function") ? "error" : "handle"]("error", event.response, event, state.kwArgs); state.isDone = true; break; default: state.kwArgs[(typeof state.kwArgs[callback] == "function") ? callback : "handle"](callback, event, event, state.kwArgs); state.isDone = true; } } } dojo.io.transports.addTransport("ScriptSrcTransport");}//Define callback handler.window.onscriptload = function(event){ var state = null; var transport = dojo.io.ScriptSrcTransport; //Find the matching state object for event ID. if(transport._state[event.id]){ state = transport._state[event.id]; }else{ //The ID did not match directly to an entry in the state list. //Try searching the state objects for a matching original URL. var tempState; for(var param in transport._state){ tempState = transport._state[param]; if(tempState.finalUrl && tempState.finalUrl == event.id){ state = tempState; break; } } //If no matching original URL is found, then use the URL that was actually used //in the SCRIPT SRC attribute. if(state == null){ var scripts = document.getElementsByTagName("script"); for(var i = 0; scripts && i < scripts.length; i++){ var scriptTag = scripts[i]; if(scriptTag.getAttribute("class") == "ScriptSrcTransport" && scriptTag.src == event.id){ state = transport._state[scriptTag.id]; break; } } } //If state is still null, then throw an error. if(state == null){ throw "No matching state for onscriptload event.id: " + event.id; } } var callbackName = "error"; switch(event.status){ case dojo.io.ScriptSrcTransport.DsrStatusCodes.Continue: //A part of a multipart request. callbackName = "partOk"; break; case dojo.io.ScriptSrcTransport.DsrStatusCodes.Ok: //Successful reponse. callbackName = "load"; break; } transport._finish(state, callbackName, event);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -