📄 remotecall-ajax.js
字号:
/*
* Copyright 2001-2007 Hippo (www.hippo.nl)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var RETURN_TEXT = 1;
var RETURN_XML = 2;
function RemoteCallAjax()
{
if(!Cfx.Class.IsDefined(RemoteCallAjax)) {
Cfx.Class.New(RemoteCallAjax, baseComponent);
if(Cfx.Class.IsInitializing(RemoteCallAjax))
{
RemoteCallAjax.Method(init);
RemoteCallAjax.Method(doCall);
RemoteCallAjax.Method(get);
RemoteCallAjax.Method(getText);
RemoteCallAjax.Method(getXML);
RemoteCallAjax.Method(postForm);
RemoteCallAjax.Method(getContextID);
RemoteCallAjax.Method(loadSucces);
RemoteCallAjax.Method(loadFailed);
RemoteCallAjax.Method(createIFrame);
RemoteCallAjax.Method(loadedIFrame);
RemoteCallAjax.Method(cleanup);
RemoteCallAjax.Method(dispose);
RemoteCallAjax.maxPoolSize = 0;
RemoteCallAjax.poolSize = 0;
RemoteCallAjax.pool = null;
RemoteCallAjax.ID = 'RCAX';
return;
}
}
this.InitInstance();
return this;
function init() {
if (this.log.debugging()) {
this.log.debug("initializing RemoteCallAjax");
}
this.maxPoolSize = this.config.maxPoolSize;
this.poolSize = 0;
this.pool = new Array(this.maxPoolSize);
}
function dispose() {
this.baseClass.dispose.apply(this);
for(var i=1; i<=this.poolSize; i++) {
var j = this.pool[this.ID + i];
j.dispose();
j = null;
this.pool[this.ID + i] = null;
}
this.pool = null;
}
/*
* Helper function,returns get as text
*/
function getText(url, callbackObject, callbackFunction) {
return this.get(url, callbackObject, callbackFunction, RETURN_TEXT);
}
/*
* Helper function,returns get as xml
*/
function getXML(url, callbackObject, callbackFunction) {
return this.get(url, callbackObject, callbackFunction, RETURN_XML);
}
function get(url, callbackObject, callbackFunction, returnType) {
var loadContext = this.pool[this.getContextID()];
if(Cfx.Js.IsDefined(callbackObject) && !Cfx.Js.IsNull(callbackObject)) {
loadContext.setCallback(new Callback(callbackObject, callbackFunction));
}
if(!Cfx.Js.IsEmpty(returnType)) {
loadContext.setReturnType(returnType);
}
var result = loadContext.handleGet(url);
if(loadContext.sync) {
return this.loadSucces(loadContext.id)
}
}
function getContextID() {
var contextObj; //reference to new remotecall obj
//loop through pooled objects
for(var i = 1; i <= this.poolSize; i++) {
contextObj = this.pool[this.ID + i];
//and check if obj isn't busy
if(!contextObj.busy)
{
if (this.log.debugging()) {
this.log.debug("found free context!");
}
contextObj.busy = true; //mark obj as busy
return contextObj.id; //return ready obj
}
}
// if we got here, there are no existing free contexts
if (this.log.debugging()) {
this.log.debug("no free context's in pool, poolSize=" + this.poolSize);
}
if(this.poolSize <= this.maxPoolSize) // if there's still room
{
// create new context
var contextID = this.ID +(this.poolSize + 1);
this.pool[contextID] = new LoadContextAjax();
this.pool[contextID].init(contextID,
this.context.get("browser"),
this.log.getChildLogger(Cfx.Class.GetName(this.pool[contextID]) + "-" + contextID),
this.sm,
this.config.wrapper);
this.poolSize++;
return contextID;
}
else
{
this.log.error("context pool full");
throw new Error("Context pool full");
}
}
function doCall(url, callbackObject, params, callbackFunction) {
//convenience check for backward compatibility
if(Cfx.Js.IsEmpty(callbackFunction)) {
callbackFunction = 'load';
}
try {
var request = this.pool[this.getContextID()];
//TODO: implement sync callback
request.setCallback(new Callback(callbackObject, callbackFunction));
var par1 = this.config.wrapper + url;
var par2 = params;
window.setTimeout(function () { top.queueAsBusy(); }, 1);
window.setTimeout(function () { request.handlePost(par1, par2); }, 1);
return request.id;
}
catch(e) {
this.log.error(e, "doCall failed");
//cleanup?
}
return null;
}
/*
* Post the form object as requeststring
*/
function postForm(url, formObject, callbackObject, callbackFunction) {
//parse form elements into params
var fileUpload = false;
var params = new Array();
var input = '';
for (var i = 0; i < formObject.elements.length; i++) {
input = formObject.elements[i];
if (typeof(input.type) == "undefined") {
// Skip fieldset
continue;
}
if (input.type == "submit" || input.type == "image") {
// Skip buttons
continue;
}
if ((input.type == "checkbox" || input.type == "radio") && !input.checked) {
// Skip unchecked checkboxes and radio buttons
continue;
}
if (input.type == "file") {
// Can't send files in Ajax mode, use Iframe hack
fileUpload = true;
}
if (input.tagName.toLowerCase() == "select" && input.multiple) {
var name = encodeURIComponent(input.name);
var options = input.options;
for (var zz = 0; zz < options.length; zz++) {
if (options[zz].selected) {
params.push({'key': name, 'val': options[zz].value});
//result += "&" + name + "=" + encodeURIComponent(options[zz].value);
}
}
// don't use the default fallback
continue;
}
if(input.name == "addToUrl") {
url += input.value;
continue;
}
// text, passwod, textarea, hidden, single select
params.push({'key': input.name, 'val': input.value});
}
if(fileUpload) {
//create hidden Iframe
var frameId = this.createIFrame(callbackObject, callbackFunction);
//post form into Iframe
formObject.setAttribute("action", url);
formObject.setAttribute("target", frameId);
//set queueAsBusy state
top.queueAsBusy();
//and submit
formObject.submit();
//remote IFrame again when it has loaded
} else {
this.doCall(url, callbackObject, params, callbackFunction);
}
}
//deprecated
function loaded( contextID ) {
this.log.warn('RemoteCallAjax.loaded() is deprecated')
}
function loadSucces(contextID) {
if (this.log.debugging()) {
this.log.debug("finished loading ajax context:" + contextID);
}
try{
// get context object and invoke callback
var contextObject = this.pool[contextID];
if(contextObject.callback != null) {
if (this.log.debugging()) {
this.log.debug("invoking context callback handler");
}
contextObject.callback.invokeCallback(contextObject.getResponse());
}
else{
return contextObject.getResponse();
}
}finally {
if (this.log.debugging()) {
this.log.debug("cleanup: " + contextObject.id);
}
this.cleanup(contextObject);
}
}
function loadFailed(contextID, response, error){
if (this.log.debugging()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -