ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 14 May 2009 10:24:56 +0200
branchtls-sprint
changeset 1801 672acc730ce5
parent 1800 05c36cf3c813
child 1802 d628defebc17
ajax_replace_url becomes obsolete, req.build_ajax_replace_url should be used instead The idea is to generate the full URL on the server side and not on the client side (i.e in JS). This way, this gives a chance to the ajax link to work outside the application itself (typically mash-ups).
web/__init__.py
web/data/cubicweb.ajax.js
web/request.py
web/views/calendar.py
--- a/web/__init__.py	Thu May 14 10:13:59 2009 +0200
+++ b/web/__init__.py	Thu May 14 10:24:56 2009 +0200
@@ -13,6 +13,8 @@
 from datetime import datetime, date, timedelta
 from simplejson import dumps
 
+from logilab.common.deprecation import obsolete
+
 from cubicweb.common.uilib import urlquote
 from cubicweb.web._exceptions import *
 
@@ -62,6 +64,7 @@
         return json_dumps(function(*args, **kwargs))
     return newfunc
 
+@obsolete('use req.build_ajax_replace_url() instead')
 def ajax_replace_url(nodeid, rql, vid=None, swap=False, **extraparams):
     """builds a replacePageChunk-like url
     >>> ajax_replace_url('foo', 'Person P')
--- a/web/data/cubicweb.ajax.js	Thu May 14 10:13:59 2009 +0200
+++ b/web/data/cubicweb.ajax.js	Thu May 14 10:24:56 2009 +0200
@@ -317,6 +317,18 @@
     }
 }
 
+/*
+ * fetches `url` and replaces `nodeid`'s content with the result
+ * @param replacemode how the replacement should be done (default is 'replace')
+ *  Possible values are :
+ *    - 'replace' to replace the node's content with the generated HTML
+ *    - 'swap' to replace the node itself with the generated HTML
+ *    - 'append' to append the generated HTML to the node's content
+ */
+function loadxhtml(nodeid, url, /* ... */ replacemode) {
+    jQuery('#' + nodeid).loadxhtml(url, null, 'post', replacemode);
+}
+
 /* XXX: this function should go in edition.js but as for now, htmlReplace
  * references it.
  *
--- a/web/request.py	Thu May 14 10:13:59 2009 +0200
+++ b/web/request.py	Thu May 14 10:24:56 2009 +0200
@@ -19,12 +19,14 @@
 from logilab.common.decorators import cached
 from logilab.common.deprecation import obsolete
 
+from logilab.mtconverter import html_escape
+
 from cubicweb.dbapi import DBAPIRequest
 from cubicweb.common.mail import header
 from cubicweb.common.uilib import remove_html_tags
 from cubicweb.utils import SizeConstrainedList, HTMLHead
-from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit, RequestError,
-                          StatusResponse)
+from cubicweb.web import (INTERNAL_FIELD_VALUE, LOGGER, NothingToEdit,
+                          RequestError, StatusResponse)
 
 _MARKER = object()
 
@@ -478,6 +480,23 @@
                 cssfile = self.datadir_url + cssfile
             add_css(cssfile, media)
 
+    def build_ajax_replace_url(self, nodeid, rql, vid, replacemode='replace',
+                               **extraparams):
+        """builds an ajax url that will replace `nodeid`s content
+        :param nodeid: the dom id of the node to replace
+        :param rql: rql to execute
+        :param vid: the view to apply on the resultset
+        :param replacemode: defines how the replacement should be done.
+        Possible values are :
+         - 'replace' to replace the node's content with the generated HTML
+         - 'swap' to replace the node itself with the generated HTML
+         - 'append' to append the generated HTML to the node's content
+        """
+        url = self.build_url('view', rql=rql, vid=vid, __notemplate=1,
+                             **extraparams)
+        return "javascript: loadxhtml('%s', '%s', '%s')" % (
+            nodeid, html_escape(url), replacemode)
+
     # urls/path management ####################################################
 
     def url(self, includeparams=True):
--- a/web/views/calendar.py	Thu May 14 10:13:59 2009 +0200
+++ b/web/views/calendar.py	Thu May 14 10:24:56 2009 +0200
@@ -262,10 +262,12 @@
         prevdate = curdate - timedelta(31)
         nextdate = curdate + timedelta(31)
         rql = self.rset.printable_rql()
-        prevlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
-                                    year=prevdate.year, month=prevdate.month)
-        nextlink = ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
-                                    year=nextdate.year, month=nextdate.month)
+        prevlink = self.req.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
+                                                   year=prevdate.year,
+                                                   month=prevdate.month)
+        nextlink = self.req.build_ajax_replace_url('onemonthcalid', rql, 'onemonthcal',
+                                                   year=nextdate.year,
+                                                   month=nextdate.month)
         return prevlink, nextlink
 
     def _build_calendar_cell(self, celldate, rows, curdate):
@@ -517,9 +519,11 @@
         prevdate = curdate - timedelta(7)
         nextdate = curdate + timedelta(7)
         rql = self.rset.printable_rql()
-        prevlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
-                                    year=prevdate.year, week=prevdate.isocalendar()[1])
-        nextlink = ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
-                                    year=nextdate.year, week=nextdate.isocalendar()[1])
+        prevlink = self.req.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
+                                                   year=prevdate.year,
+                                                   week=prevdate.isocalendar()[1])
+        nextlink = self.req.build_ajax_replace_url('oneweekcalid', rql, 'oneweekcal',
+                                                   year=nextdate.year,
+                                                   week=nextdate.isocalendar()[1])
         return prevlink, nextlink