diff options
author | 2008-11-13 09:49:11 +0000 | |
---|---|---|
committer | 2008-11-13 09:49:11 +0000 | |
commit | e44a7e37b6c7b5961adaffc62b9042b8d442938e (patch) | |
tree | 95b67c356e93163467db2451f2b8cce84ed5d582 /includes/js/dojo/io | |
parent | a62b9742ee5e28bcec6872d88f50f25b820914f6 (diff) | |
download | scuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.tar.gz scuttle-e44a7e37b6c7b5961adaffc62b9042b8d442938e.zip |
New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit
git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f
Diffstat (limited to 'includes/js/dojo/io')
-rw-r--r-- | includes/js/dojo/io/iframe.js | 358 | ||||
-rw-r--r-- | includes/js/dojo/io/script.js | 211 |
2 files changed, 569 insertions, 0 deletions
diff --git a/includes/js/dojo/io/iframe.js b/includes/js/dojo/io/iframe.js new file mode 100644 index 0000000..a3003ba --- /dev/null +++ b/includes/js/dojo/io/iframe.js @@ -0,0 +1,358 @@ +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(/*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.debug("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.isSafari){ + iframe.location = src; + }else{ + frames[iframe.name].location = src; + } + }else{ + // Fun with DOM 0 incompatibilities! + var idoc; + if(dojo.isIE || dojo.isSafari > 2){ + 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.debug("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) && + (document.getElementsByTagName("iframe")[iframeNode.name].contentWindow) && + (document.getElementsByTagName("iframe")[iframeNode.name].contentWindow.document) + ) + ) || // IE + ( + (iframeNode.name)&&(document.frames[iframeNode.name])&& + (document.frames[iframeNode.name].document) + ) || null; + return doc; + }, + + /*===== + dojo.io.iframe.__ioArgs = function(kwArgs){ + // 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, javascript, + // json. IMPORTANT: For all values EXCEPT html, 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 + // or any other XML type is NOT supported by this transport. + // 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(). + } + =====*/ + + 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"){ + 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; } + var dfd = this._currentDfd = this._dfdQueue.shift(); + 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 + for(var x in content){ + if(!fn[x]){ + var tn; + if(dojo.isIE){ + tn = dojo.doc.createElement("<input type='hidden' name='"+x+"'>"); + }else{ + tn = dojo.doc.createElement("input"); + tn.type = "hidden"; + tn.name = x; + } + tn.value = content[x]; + fn.appendChild(tn); + ioArgs._contentToClean.push(x); + }else{ + fn[x].value = content[x]; + } + } + } + //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; + 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; + 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]; + if(dojo.isSafari < 3){ + //In Safari (at least 2.0.3), can't use form[key] syntax to find the node, + //for nodes that were dynamically added. + for(var j = 0; j < fNode.childNodes.length; j++){ + var chNode = fNode.childNodes[j]; + if(chNode.name == key){ + dojo._destroyElement(chNode); + break; + } + } + }else{ + dojo._destroyElement(fNode[key]); + fNode[key] = null; + } + } + + // 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; + } +} + +} diff --git a/includes/js/dojo/io/script.js b/includes/js/dojo/io/script.js new file mode 100644 index 0000000..c38d070 --- /dev/null +++ b/includes/js/dojo/io/script.js @@ -0,0 +1,211 @@ +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"); + +/*===== +dojo.io.script.__ioArgs = function(kwArgs){ + // 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 + // "callbackParamName" (response will be a JSONP call returning JSON) + // or "checkString" (response is pure JavaScript defined in + // the body of the script that was attached). The following additional + // properties are allowed for dojo.io.script.get(): + // callbackParamName: String + // The URL parameter name that indicates the JSONP callback string. + // For instance, when using Yahoo JSONP calls it is normally, + // callbackParamName: "callback". For AOL JSONP calls it is normally + // callbackParamName: "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. +} +=====*/ + +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); + + this.attach(ioArgs.id, ioArgs.url, args.frameDoc); + 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; + doc.getElementsByTagName("head")[0].appendChild(element); + }, + + remove: function(/*String*/id){ + //summary: removes the script element with the given id. + dojo._destroyElement(dojo.byId(id)); + + //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 + if(args.callbackParamName){ + //Add the jsonp parameter. + ioArgs.query = ioArgs.query || ""; + if(ioArgs.query.length > 0){ + ioArgs.query += "&"; + } + ioArgs.query += args.callbackParamName + + "=" + + (args.frameDoc ? "parent." : "") + + "dojo.io.script.jsonp_" + ioArgs.id + "._jsonpCallback"; + + //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._deadScripts.push(dfd.ioArgs.id); + } + }, + + _deferredOk: function(/*Deferred*/dfd){ + //summary: okHandler function for dojo._ioSetArgs call. + + //DO NOT use "this" and expect it to be dojo.io.script. + + //Add script to list of things that can be removed. + if(dfd.ioArgs.canDelete){ + dojo.io.script._deadScripts.push(dfd.ioArgs.id); + } + + if(dfd.ioArgs.json){ + //Make sure to *not* remove the json property from the + //Deferred, so that the Deferred can still function correctly + //after the response is received. + return dfd.ioArgs.json; + }else{ + //FIXME: cannot return the dfd here, otherwise that stops + //the callback chain in Deferred. So return the ioArgs instead. + //This doesn't feel right. + return dfd.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); + }else{ + dojo.io.script._deadScripts.push(dfd.ioArgs.id); + } + } + console.debug("dojo.io.script error", error); + return error; + }, + + _deadScripts: [], + _counter: 1, + + _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]); + } + dojo.io.script._deadScripts = []; + } + + return true; + }, + + _ioCheck: function(/*Deferred*/dfd){ + //summary: inflight check function to see if IO finished. + + //Check for finished jsonp + if(dfd.ioArgs.json){ + return true; + } + + //Check for finished "checkString" case. + var checkString = dfd.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")); + } + }, + + _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; + } +} + +} |