Files
alfresco-community-repo/source/web/scripts/ajax/ajax_helper.js
Ariel Backenroth df32d0ddf1 adding support for wcm tinymce extensions to create html from web content wizard
- extracted file picker backing code into its own bean
- refactoring for to extract file picker bean
- refactoring to manage uploads from file picker outside of xforms context
- added some language utility methods to common.js
- refactored extension dialogs to deal with quirkyness of tinymce dialog codebase on IE - mostly works now and addressed bug WCM-471
- resourcifying a couple strings i missed in filepickerbean

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@5675 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2007-05-14 22:03:10 +00:00

156 lines
5.1 KiB
JavaScript

/*
* Copyright (C) 2005-2007 Alfresco Software Limited.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
////////////////////////////////////////////////////////////////////////////////
// Ajax helper library
//
// This script manages ajax requests and provides a wrapper around the dojo
// library.
//
// This script requires dojo.js to be loaded in advance.
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// AJAX helper
////////////////////////////////////////////////////////////////////////////////
alfresco = typeof alfresco == "undefined" ? {} : alfresco;
alfresco.constants = typeof alfresco.constants == "undefined" ? {} : alfresco.constants;
alfresco.constants.AJAX_LOADER_DIV_ID = "alfresco-ajax-loader";
alfresco.AjaxHelper = function()
{
}
/** All pending ajax requests. */
alfresco.AjaxHelper._requests = [];
/** Creates an ajax request object. */
alfresco.AjaxHelper.createRequest = function(target, serverMethod, methodArgs, load, error)
{
var result = new dojo.io.Request(alfresco.constants.WEBAPP_CONTEXT +
"/ajax/invoke/" + serverMethod,
"text/xml");
result.target = target;
result.content = methodArgs;
result.method = "POST";
result._baseLoadHandler = load;
result.load = function(type, data, event, kwArgs)
{
// escape from dojo's watchInFlight errors so we get real javascript errors thrown
setTimeout(function() { result._baseLoadHandler(type, data, event, kwArgs); }, 10);
}
dojo.event.connect(result, "load", function(type, data, evt)
{
alfresco.AjaxHelper._loadHandler(result);
});
result.error = error || function(type, e, impl)
{
dojo.debug("error [" + type + "] " + e.message);
if (impl.status == 401)
{
document.getElementById("logout").onclick();
}
else
{
_show_error(document.createTextNode(e.message));
alfresco.AjaxHelper._loadHandler(this);
}
};
return result;
}
/** Sends an ajax request object. */
alfresco.AjaxHelper.sendRequest = function(req)
{
alfresco.AjaxHelper._sendHandler(req);
req.encoding = "utf-8";
dojo.io.queueBind(req);
}
/**
* Returns the ajax loader div element. If it hasn't yet been created, it is created.
*/
alfresco.AjaxHelper._getLoaderElement = function()
{
var result = document.getElementById(alfresco.constants.AJAX_LOADER_DIV_ID);
if (result)
{
return result;
}
result = document.createElement("div");
result.setAttribute("id", alfresco.constants.AJAX_LOADER_DIV_ID);
dojo.html.setClass(result, "xformsAjaxLoader");
dojo.html.hide(result);
document.body.appendChild(result);
return result;
}
/** Updates the loader message or hides it if nothing is being loaded. */
alfresco.AjaxHelper._updateLoaderDisplay = function()
{
var ajaxLoader = alfresco.AjaxHelper._getLoaderElement();
ajaxLoader.innerHTML = (alfresco.AjaxHelper._requests.length == 0
? alfresco.resources["idle"]
: (alfresco.resources["loading"] +
(alfresco.AjaxHelper._requests.length > 1
? " (" + alfresco.AjaxHelper._requests.length + ")"
: "...")));
if (djConfig.isDebug)
{
dojo.debug(ajaxLoader.innerHTML);
}
if (/* djConfig.isDebug && */ alfresco.AjaxHelper._requests.length != 0)
{
dojo.html.show(ajaxLoader);
}
else
{
dojo.html.hide(ajaxLoader);
}
}
////////////////////////////////////////////////////////////////////////////////
// ajax event handlers
////////////////////////////////////////////////////////////////////////////////
alfresco.AjaxHelper._sendHandler = function(req)
{
alfresco.AjaxHelper._requests.push(req);
alfresco.AjaxHelper._updateLoaderDisplay();
}
alfresco.AjaxHelper._loadHandler = function(req)
{
var index = alfresco.AjaxHelper._requests.indexOf(req);
if (index != -1)
{
alfresco.AjaxHelper._requests.splice(index, 1);
}
else
{
var urls = [];
for (var i = 0; i < alfresco.AjaxHelper._requests.length; i++)
{
urls.push(alfresco.AjaxHelper._requests[i].url);
}
throw new Error("unable to find " + req.url +
" in [" + urls.join(", ") + "]");
}
alfresco.AjaxHelper._updateLoaderDisplay();
}