/* License: GNU Lesser General Public License (http://www.gnu.org/licenses/lgpl.html) Copyright (C) 2005 tagnetic.org. Please do not remove this copyright/license comment. */ //************************************************************************* //Start SvrFrame useSvrFrame //************************************************************************* /** * Call SvrFrame.post to do a HTTP POST to the server. * Call SvrFrame.get() to do a HTTP GET to the server. * * Below are some SvrFrame properties you can override. * The result page from the server should be an HTML page that makes * a call to either SvrFrame.loadedOk(resultText) or SvrFrame.loadedError(resultText) * where resultText is a string containing a JavaScript response. * Example return page: * * * * TODO: * - i18n concerns, particularly with loadPost? * - Can I reduce the number of contentWindow checks? * - Segmenturlbranches to methods that can be removed. * * Test: * - disabling get back/forward for GET requests. */ SvrFrame = { //****************************************************************** /** * Only valid values for this property are 'frame' or 'iframe'. * Set this property to 'frame' if you want to use a frame for the * server requests. If you set it to 'frame', be sure to define a frame * in your frameset with name and id that equals SvrFrame.id. * Suggested frame tag: * * For the src attribute, about:blank can be used unless you have the * following situation: * You want to support Safari 1.2.4 and you do a POST as your very * first SvrFrame request. In this case, make a file called blank.html * and put the following HTML in it: * */ type: 'iframe', //****************************************************************** /** * The id attribute of the iframe/frame used for the SvrFram. * You shouldn't have to change it from the value below. */ id: 'sFrame', //****************************************************************** /** * The timeout in seconds for requests. Set this to a longer value * if you want to allow longer wait times for requests. */ timeoutDelay: 5, //****************************************************************** /** * Set this property to false if you don't want to support the * Back/Forward buttons when using .loadGet() calls. */ supportBackForwardGet: true, //****************************************************************** /** * Redefine this method if you want some generic code to execute before * the framework sends the request to the server. */ start: function() {}, //****************************************************************** /** * Redefine this method if you want generic response handling before * control is passed over to the success callback. If this method returns * an object, that object will be passed to the success callback instead * of the response generated from the script in the iframe document. */ end: function(result) { return null;}, //****************************************************************** /** * Redefine this method if you want generic response error handling before * control is passed over to the error callback. If this method returns * an object, that object will be passed to the error callback instead * of the exception. */ error: function(exception) { return null;}, //****************************************************************** /** * Redefine this method if you want generic timeout handling. */ timeout: function() {}, //****************************************************************** //useSvrFramePost post: function(url, params, onSuccessName, onErrorName, onTimeoutName) { SvrFrame._request(SvrFrame._post, url, params, onSuccessName, onErrorName, onTimeoutName); }, // //****************************************************************** //useSvrFrameGet get: function(url, params, onSuccessName, onErrorName, onTimeoutName) { SvrFrame._request(SvrFrame._get, url, params, onSuccessName, onErrorName, onTimeoutName); }, // //****************************************************************** loadedOk: function(resultText) { SvrFrame._loaded(resultText, 'onSuccessName'); }, //****************************************************************** loadedError: function(resultText) { SvrFrame._loaded(resultText, 'onErrorName'); }, //****************************************************************** //****************************************************************** //****************************************************************** // Private properties/methods _formPostId: 'svrPost', _successName: '__svok', _errorName: '__sverr', _timeoutName: '__svtime', _timeoutId: 0, //****************************************************************** _request: function(typeFunction, url, params, onSuccessName, onErrorName, onTimeoutName) { try { SvrFrame._start(onTimeoutName); var loadUrl = SvrFrame._makeUrl(url, onSuccessName, onErrorName, onTimeoutName); typeFunction(loadUrl, params, onSuccessName, onErrorName, onTimeoutName); } catch (exception) { SvrFrame._handleError(exception, onErrorName); } }, //****************************************************************** //useSvrFramePost _post: function(url, params, onSuccessName, onErrorName, onTimeoutName) { var docText = '
'; for (var param in params) docText += ''; docText += "
"; //Get loader document var loaderFrame = SvrFrame._getFrame(); var loaderDoc; if (SvrFrame.type == 'iframe') loaderDoc = (loaderFrame.contentWindow) ? loaderFrame.contentWindow.document : loaderFrame.contentDocument; //true branch is IE/Mozilla, false is Safari. else loaderDoc = loaderFrame.document; if ('about:blank' == SvrFrame._getLoaderUrl()) { //Only do this path for blank page it. //Can't do the "else" path for about:blank //because about:blank is not in the right domain. //Put in the setTimeout to avoid odd MSIE 6 status bar //that never seemed to finish. loaderDoc.open(); loaderDoc.write('' + docText + ''); loaderDoc.close(); } else { loaderDoc.body.innerHTML = docText; loaderDoc.getElementById(SvrFrame._formPostId).submit(); } }, //
//****************************************************************** //useSvrFrameGet _get: function(url, params, onSuccessName, onErrorName, onTimeoutName) { for (var param in params) url += '&' + param + '=' + encodeURIComponent(params[param]); var loaderFrame = SvrFrame._getFrame(); if (SvrFrame.supportBackForwardGet) (SvrFrame.type == 'iframe') ? loaderFrame.src = url : loaderFrame.location = url; else { if (SvrFrame.type == 'frame') loaderFrame.location.replace(url); else { if (loaderFrame.contentWindow) //IE and Mozilla loaderFrame.contentWindow.location.replace(url); else loaderFrame.src = url; //Safari. Doesn't support getting contentWindow from DOM method. } } }, // //****************************************************************** _loaded: function(resultText, targetName) { try { var handlers = SvrFrame._getHandlersFromUrl(); var result; eval('result = ' + resultText); var finalResult = SvrFrame._end(result); if (!finalResult) finalResult = result; (handlers[targetName]) ? eval(handlers[targetName] + '(finalResult);') : SvrFrame._handleError(finalResult, handlers.onErrorName); } catch (exception) { SvrFrame._handleError(exception, handlers.onErrorName); } }, //****************************************************************** _start: function(onTimeoutName) { clearTimeout(SvrFrame._timeoutId); if (!onTimeoutName) onTimeoutName = 'SvrFrame.timeout()'; SvrFrame._timeoutId = setTimeout(onTimeoutName, (SvrFrame.timeoutDelay * 1000)); SvrFrame.start(); }, //****************************************************************** _end: function(result) { clearTimeout(SvrFrame._timeoutId); SvrFrame.end(result); }, //****************************************************************** _getFrame: function() { return SvrFrame['_' + SvrFrame.type + 'GetFrame'](); }, //****************************************************************** _iframeGetFrame: function() { var loaderFrame = document.getElementById(SvrFrame.id); if (!loaderFrame) { loaderFrame = document.createElement('iframe'); loaderFrame.setAttribute('id', SvrFrame.id); loaderFrame.setAttribute('src', 'about:blank'); //Need this otherwise safari double-posts first request. loaderFrame.style.border = '0px'; loaderFrame.style.width = '0px'; loaderFrame.style.height = '0px'; loaderFrame.style.visibility = 'hidden'; loaderFrame = document.getElementsByTagName('body')[0].appendChild(loaderFrame); } return loaderFrame; }, //****************************************************************** _frameGetFrame: function() { var loaderFrame = frames[SvrFrame.id]; if (!loaderFrame) throw 'No frame with ID and name of ' + SvrFrame.id; return loaderFrame; }, //****************************************************************** _makeUrl: function(url, onSuccessName, onErrorName, onTimeoutName) { var result = url + '?' + SvrFrame._successName + '=' + encodeURIComponent(onSuccessName); if (onErrorName) result += '&'+ SvrFrame._errorName + '=' + encodeURIComponent(onErrorName); if (onTimeoutName) result += '&' + SvrFrame._timeoutName + '=' + encodeURIComponent(onTimeoutName); return result; }, _startHandlerRegex: '', _endHandlerRegex: '=(.*?)(&|$)', //****************************************************************** _getHandlersFromUrl: function() { var result = Object(); var url = SvrFrame._getLoaderUrl(); var matches = url.match(SvrFrame._startHandlerRegex + SvrFrame._successName + SvrFrame._endHandlerRegex); if (matches != null) result.onSuccessName = matches[1]; else throw 'SvrFrame._getHandlersFromUrl():onSuccessName'; matches = url.match(SvrFrame._startHandlerRegex + SvrFrame._errorName + SvrFrame._endHandlerRegex); if (matches != null) result.onErrorName = matches[1]; matches = url.match(SvrFrame._startHandlerRegex + SvrFrame._timeoutName + SvrFrame._endHandlerRegex); if (matches != null) result.timeout = matches[1]; return result; }, //****************************************************************** _getLoaderUrl: function() { var result; if (SvrFrame.type == 'iframe') { var loader = SvrFrame._getFrame(); if (loader.contentWindow) //IE and Mozilla result = loader.contentWindow.location.href; else result = loader.contentDocument.URL; //Safari } else result = SvrFrame._getFrame().location.href; return result; }, //****************************************************************** _htmlEscape: function(text) { text = text.replace(/\/gi, '>'); text = text.replace(/\"/gi, '"'); text = text.replace(/\=/gi, '='); return text; }, //****************************************************************** _handleError: function(exception, errorHandlerName) { var result = SvrFrame.error(exception); if (!result) result = exception; if (errorHandlerName) eval(errorHandlerName + '(result);'); else throw exception; } }; //************************************************************************* //End SvrFrame
//*************************************************************************