password input may be used with non eidparam fields...
/* * :organization: Logilab * :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. * :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr */CubicWeb.require('python.js');CubicWeb.require('htmlhelpers.js');varJSON_BASE_URL=baseuri()+'json?';/* * inspect dom response, search for a <div class="ajaxHtmlHead"> node and * put its content into the real document's head. * This enables dynamic css and js loading and is used by replacePageChunk */functionloadAjaxHtmlHead(node){jQuery(node).find('div.ajaxHtmlHead').appendTo(jQuery('head'));}functionpostAjaxLoad(node,req){// addStylesheets(evalJSON(req.getResponseHeader('X-Cubicweb-Stylesheets') || '[]'));loadAjaxHtmlHead(node);// find sortable tables if there are someif(typeof(Sortable)!='undefined'){Sortable.sortTables(node);}// find textareas and wrap them if there are someif(typeof(FCKeditor)!='undefined'){buildWysiwygEditors(node);}if(typeofinitFacetBoxEvents!='undefined'){initFacetBoxEvents(node);}if(typeofbuildWidgets!='undefined'){buildWidgets(node);}if(typeofroundedCornersOnLoad!='undefined'){roundedCornersOnLoad();}}// cubicweb loadxhtml plugin to make jquery handle xhtml responsejQuery.fn.loadxhtml=function(url,data,reqtype,mode){varajax=null;if(reqtype=='post'){ajax=jQuery.post;}else{ajax=jQuery.get;}if(this.size()>1){log('loadxhtml was called with more than one element');}varnode=this.get(0);// only consider the first elementmode=mode||'replace';varcallback=null;if(data&&data.callback){callback=data.callback;deletedata.callback;}ajax(url,data,function(response){vardomnode=getDomFromResponse(response);if(mode=='swap'){varorigId=node.id;node=swapDOM(node,domnode);if(!node.id){node.id=origId;}}elseif(mode=='replace'){jQuery(node).empty().append(domnode);}elseif(mode=='append'){jQuery(node).append(domnode);}postAjaxLoad(node,req);while(jQuery.isFunction(callback)){callback=callback.apply(this,[domnode]);}});};/* finds each dynamic fragment in the page and executes the * the associated RQL to build them (Async call) */functionloadDynamicFragments(){varfragments=jQuery('div.dynamicFragment');if(fragments.length==0){return;}if(typeofLOADING_MSG=='undefined'){LOADING_MSG='loading';// this is only a safety belt, it should not happen}for(vari=0;i<fragments.length;i++){varfragment=fragments[i];fragment.innerHTML='<h3>'+LOADING_MSG+' ... <img src="data/loading.gif" /></h3>';varrql=getNodeAttribute(fragment,'cubicweb:rql');varvid=getNodeAttribute(fragment,'cubicweb:vid');varextraparams={};varactrql=getNodeAttribute(fragment,'cubicweb:actualrql');if(actrql){extraparams['actualrql']=actrql;}varfbvid=getNodeAttribute(fragment,'cubicweb:fallbackvid');if(fbvid){extraparams['fallbackvid']=fbvid;}replacePageChunk(fragment.id,rql,vid,extraparams);}}jQuery(document).ready(loadDynamicFragments);//============= base AJAX functions to make remote calls =====================///* * This function will call **synchronously** a remote method on the cubicweb server * @param fname: the function name to call (as exposed by the JSONController) * @param args: the list of arguments to pass the function */functionremote_exec(fname){setProgressCursor();varprops={'mode':"remote",'fname':fname,'pageid':pageid,'arg':map(jQuery.toJSON,sliceList(arguments,1))};varresult=jQuery.ajax({url:JSON_BASE_URL,data:props,async:false}).responseText;result=evalJSON(result);resetCursor();returnresult;}functionremoteCallFailed(err,req){if(req.status==500){updateMessage(err);}else{updateMessage(_("an error occured while processing your request"));}}/* * This function is the equivalent of MochiKit's loadJSONDoc but * uses POST instead of GET */functionloadJSONDocUsingPOST(url,queryargs,mode){mode=mode||'remote';setProgressCursor();vardataType=(mode=='remote')?"json":null;vardeferred=loadJSON(url,queryargs,'POST',dataType);deferred=deferred.addErrback(remoteCallFailed);// if (mode == 'remote') {// deferred = deferred.addCallbacks(evalJSONRequest);// }deferred=deferred.addCallback(resetCursor);returndeferred;}function_buildRemoteArgs(fname){return{'mode':"remote",'fname':fname,'pageid':pageid,'arg':map(jQuery.toJSON,sliceList(arguments,1))};}/* * This function will call **asynchronously** a remote method on the cubicweb server * This function is a low level one. You should use `async_remote_exec` or * `async_rawremote_exec` instead. * * @param fname: the function name to call (as exposed by the JSONController) * @param funcargs: the function's arguments * @param mode: rawremote or remote */function_async_exec(fname,funcargs,mode){varprops={'mode':mode,'fname':fname,'pageid':pageid};varargs=map(urlEncode,map(jQuery.toJSON,funcargs));args.unshift('');// this is to be able to use join() directlyvarqueryargs=as_url(props)+args.join('&arg=');returnloadJSONDocUsingPOST(JSON_BASE_URL,queryargs,mode);}/* * This function will call **asynchronously** a remote method on the cubicweb server * @param fname: the function name to call (as exposed by the JSONController) * additional arguments will be directly passed to the specified function * Expected response type is Json. */functionasync_remote_exec(fname/* ... */){return_async_exec(fname,sliceList(arguments,1),'remote');}/* * This version of _async_exec doesn't expect a json response. * It looks at http headers to guess the response type. */functionasync_rawremote_exec(fname/* ... */){return_async_exec(fname,sliceList(arguments,1),'rawremote');}/* * This function will call **asynchronously** a remote method on the cubicweb server * @param fname: the function name to call (as exposed by the JSONController) * @param varargs: the list of arguments to pass to the function * This is an alternative form of `async_remote_exec` provided for convenience */functionasync_remote_exec_varargs(fname,varargs){return_async_exec(fname,varargs,'remote');}/* emulation of gettext's _ shortcut */function_(message){returnremote_exec('i18n',[message])[0];}functionrqlexec(rql){returnasync_remote_exec('rql',rql);}functionuserCallback(cbname){async_remote_exec('user_callback',cbname);}functionunloadPageData(){// NOTE: do not make async calls on unload if you want to avoid// strange bugsremote_exec('unload_page_data');}functionopenHash(){if(document.location.hash){varnid=document.location.hash.replace('#','');varnode=jQuery('#'+nid);if(node){removeElementClass(node,"hidden");}};}jQuery(document).ready(openHash);functionreloadComponent(compid,rql,registry,nodeid,extraargs){registry=registry||'components';rql=rql||'';nodeid=nodeid||(compid+'Component');extraargs=extraargs||{};varnode=getNode(nodeid);vard=async_rawremote_exec('component',compid,rql,registry,extraargs);d.addCallback(function(result,req){vardomnode=getDomFromResponse(result);if(node){// make sure the component is visibleremoveElementClass(node,"hidden");swapDOM(node,domnode);}});d.addCallback(resetCursor);d.addErrback(function(xxx){updateMessage(_("an error occured"));log(xxx);});returnd;}/* XXX: HTML architecture of cubicweb boxes is a bit strange */functionreloadBox(boxid,rql){returnreloadComponent(boxid,rql,'boxes',boxid);}functionuserCallbackThenUpdateUI(cbname,compid,rql,msg,registry,nodeid){vard=async_remote_exec('user_callback',cbname);d.addCallback(function(){reloadComponent(compid,rql,registry,nodeid);if(msg){updateMessage(msg);}});d.addCallback(resetCursor);d.addErrback(function(xxx){updateMessage(_("an error occured"));log(xxx);returnresetCursor();});}functionuserCallbackThenReloadPage(cbname,msg){vard=async_remote_exec('user_callback',cbname);d.addCallback(function(){window.location.reload();if(msg){updateMessage(msg);}});d.addCallback(resetCursor);d.addErrback(function(xxx){updateMessage(_("an error occured"));log(xxx);returnresetCursor();});}/* * unregisters the python function registered on the server's side * while the page was generated. */functionunregisterUserCallback(cbname){vard=async_remote_exec('unregister_user_callback',cbname);d.addCallback(function(){resetCursor();});d.addErrback(function(xxx){updateMessage(_("an error occured"));log(xxx);returnresetCursor();});}/* executes an async query to the server and replaces a node's * content with the query result * * @param nodeId the placeholder node's id * @param rql the RQL query * @param vid the vid to apply to the RQL selection (default if not specified) * @param extraparmas table of additional query parameters */functionreplacePageChunk(nodeId,rql,vid,extraparams,/* ... */swap,callback){varparams=null;if(callback){params={callback:callback};}varnode=jQuery('#'+nodeId)[0];varprops={};if(node){props['rql']=rql;props['pageid']=pageid;if(vid){props['vid']=vid;}if(extraparams){jQuery.extend(props,extraparams);}// FIXME we need to do as_url(props) manually instead of// passing `props` directly to loadxml because replacePageChunk// is sometimes called (abusively) with some extra parameters in `vid`varmode=swap?'swap':'replace';varurl=JSON_BASE_URL+as_url(props);jQuery(node).loadxhtml(url,params,'get',mode);}else{log('Node',nodeId,'not found');}}/* XXX: this function should go in edition.js but as for now, htmlReplace * references it. * * replace all textareas with fckeditors. */functionbuildWysiwygEditors(parent){jQuery('textarea').each(function(){if(this.getAttribute('cubicweb:type','wysiwyg')){if(typeofFCKeditor!="undefined"){varfck=newFCKeditor(this.id);fck.Config['CustomConfigurationsPath']=fckconfigpath;fck.Config['DefaultLanguage']=fcklang;fck.BasePath="fckeditor/";fck.ReplaceTextarea();}else{log('fckeditor could not be found.');}}});}jQuery(document).ready(buildWysiwygEditors);/* convenience function that returns a DOM node based on req's result. */functiongetDomFromResponse(response){if(typeof(response)=='string'){returnhtml2dom(response);}vardoc=response.documentElement;varchildren=doc.childNodes;if(!children.length){// no child (error cases) => return the whole documentreturndoc.cloneNode(true);}if(children.length==1){// only one child => return itreturnchildren[0].cloneNode(true);}// several children => wrap them in a single node and return the wrapreturnDIV(null,map(methodcaller('cloneNode',true),children));}functionpostJSON(url,data,callback){returnjQuery.post(url,data,callback,'json');}functiongetJSON(url,data,callback){returnjQuery.get(url,data,callback,'json');}CubicWeb.provide('ajax.js');