On 06/22/2010 08:15 PM, sdumitriu (SVN) wrote:
Author: sdumitriu Date: 2010-06-22 19:15:28 +0200 (Tue, 22 Jun 2010) New Revision: 29639
Modified: platform/web/trunk/standard/src/main/webapp/resources/js/xwiki/xwiki.js Log: XWIKI-5300: Start writing a Javascript mirror of the XWiki data model: Document.getURL Done.
The code below won't work in portlet mode because: * portlet URL format is an implementation detail for portlet containers * portlet URL parameters are not included in clear but rather encoded as a single query string parameter (checked in GateIn and WebSphere) * query string parameters appended to a portlet URL are not accessible to the targeted portlet My question is: do we really need to compute URLs on the client? (even if compute = string replace) Thanks, Marius
Modified: platform/web/trunk/standard/src/main/webapp/resources/js/xwiki/xwiki.js =================================================================== --- platform/web/trunk/standard/src/main/webapp/resources/js/xwiki/xwiki.js 2010-06-22 16:51:34 UTC (rev 29638) +++ platform/web/trunk/standard/src/main/webapp/resources/js/xwiki/xwiki.js 2010-06-22 17:15:28 UTC (rev 29639) @@ -1295,6 +1295,69 @@ } var browser = new BrowserDetect();
+/** + * XWiki Model access APIs. + */ +XWiki.Document = Class.create({ + /** + * Constructor. All parameters are optional, and default to the current document location. + */ + initialize : function(page, space, wiki) { + this.page = page || XWiki.Document.currentPage; + this.space = space || XWiki.Document.currentSpace; + this.wiki = wiki || XWiki.Document.currentWiki; + }, + /** + * Gets an URL pointing to this document. + */ + getURL : function(action, queryString, fragment) { + action = action || 'view'; + var url = XWiki.Document.URLTemplate; + url = url.replace("__space__", encodeURIComponent(this.space)); + url = url.replace("__page__", (this.page == 'WebHome') ? '' : encodeURIComponent(this.page)); + url = url.replace("__action__/", (action == 'view') ? '' : (encodeURIComponent(action) + "/")); + if (queryString) { + url += '?' + queryString; + } + if (fragment) { + url += '#' + fragment; + } + return url; + }, + /** + * Gets an URL which points to the REST location for accessing this document. + */ + getRestURL : function(entity, queryString) { + entity = entity || ''; + var url = XWiki.Document.RestURLTemplate; + url = url.replace("__wiki__", this.wiki); + url = url.replace("__space__", this.space); + url = url.replace("__page__", this.page); + if (entity) { + url += "/" + entity; + } + if (queryString) { + url += '?' + queryString; + } + return url; + } +}); + +/* Initialize the document URL factory, and create XWiki.currentDocument. */ +document.observe('dom:loaded', function() { + XWiki.Document.currentWiki = ($$("meta[name=wiki]").length> 0) ? $$("meta[name=wiki]")[0].content : "xwiki"; + XWiki.Document.currentSpace = ($$("meta[name=space]").length> 0) ? $$("meta[name=space]")[0].content : "Main"; + XWiki.Document.currentPage = ($$("meta[name=page]").length> 0) ? $$("meta[name=page]")[0].content : "WebHome"; + XWiki.Document.URLTemplate = "$xwiki.getURL('__space__.__page__', '__action__')"; + XWiki.Document.RestURLTemplate = "${request.contextPath}/rest/wikis/__wiki__/spaces/__space__/pages/__page__"; + XWiki.Document.RestSearchURLStub = "${request.contextPath}/rest/wikis/__wiki__/search"; + XWiki.Document.getRestSearchURL = function(wiki) { + wiki = wiki || XWiki.Document.currentWiki; + return XWiki.Document.RestSearchURLStub.replace("__wiki__", wiki); + }; + XWiki.currentDocument = new XWiki.Document(); +}); + /* * Small JS improvement, which automatically hides and reinserts the default text for input fields, acting as a tip. *
_______________________________________________ notifications mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/notifications