[xwiki-devs] Macro requiring js/css
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro. How can this be done in xwiki? -- Glenn
Hi Glenn, The solution is to use a Javascript Extension (JSX). See http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutorial Then you put the $jsx.use in your java macro. In the near future (2.0M1), you'll be able to write XWiki 2.0 syntax macros in wiki pages so it'll be more easy to write very UI-oriented macros. Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page. -Vincent On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 8:46 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
Hi Glenn,
The solution is to use a Javascript Extension (JSX). See http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutoria l
Then you put the $jsx.use in your java macro.
This looks promising. From the context of a java macro is there any way to use the $jsx type references? It looks like the only thing a macro can do is return a List<Block> instance so I'm not sure it can affect the system header includes? Using a velocity page could work but seems klunky to have a plugin import pages into the wiki just to get a macro.
In the near future (2.0M1), you'll be able to write XWiki 2.0 syntax macros in wiki pages so it'll be more easy to write very UI-oriented macros.
Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page.
-Vincent
On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
On May 14, 2009, at 6:21 PM, <[email protected]> <[email protected]
wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 8:46 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
Hi Glenn,
The solution is to use a Javascript Extension (JSX). See http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutoria l
Then you put the $jsx.use in your java macro.
This looks promising. From the context of a java macro is there any way to use the $jsx type references?
You'll need to create a MacroBlock with a name of velocity. -Vincent
It looks like the only thing a macro can do is return a List<Block> instance so I'm not sure it can affect the system header includes? Using a velocity page could work but seems klunky to have a plugin import pages into the wiki just to get a macro.
In the near future (2.0M1), you'll be able to write XWiki 2.0 syntax macros in wiki pages so it'll be more easy to write very UI-oriented macros.
Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page.
-Vincent
On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
On May 14, 2009, at 6:54 PM, Vincent Massol wrote:
On May 14, 2009, at 6:21 PM, <[email protected]> <[email protected]
wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 8:46 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
Hi Glenn,
The solution is to use a Javascript Extension (JSX). See http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutoria l
Then you put the $jsx.use in your java macro.
This looks promising. From the context of a java macro is there any way to use the $jsx type references?
You'll need to create a MacroBlock with a name of velocity.
but in any case you'll need to put you Javascript extension object in a wiki page. -Vincent
-Vincent
It looks like the only thing a macro can do is return a List<Block> instance so I'm not sure it can affect the system header includes? Using a velocity page could work but seems klunky to have a plugin import pages into the wiki just to get a macro.
In the near future (2.0M1), you'll be able to write XWiki 2.0 syntax macros in wiki pages so it'll be more easy to write very UI-oriented macros.
Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page.
-Vincent
On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 9:55 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
On May 14, 2009, at 6:54 PM, Vincent Massol wrote:
On May 14, 2009, at 6:21 PM, <[email protected]>
wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 8:46 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
Hi Glenn,
The solution is to use a Javascript Extension (JSX). See
http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutoria
l
Then you put the $jsx.use in your java macro.
This looks promising. From the context of a java macro is there any way to use the $jsx type references?
You'll need to create a MacroBlock with a name of velocity.
but in any case you'll need to put you Javascript extension object in a wiki page.
I wonder if we're looking at the wrong solution. In this case I want to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content. I suppose I could look at the jsx extension code to see how this is done as it is clearly doing this somehow?
-Vincent
-Vincent
It looks like the only thing a macro can do is return a List<Block> instance so I'm not sure it can affect the system header includes? Using a velocity page could work but seems klunky to have a plugin import pages into the wiki just to get a macro.
In the near future (2.0M1), you'll be able to write XWiki 2.0
syntax
macros in wiki pages so it'll be more easy to write very UI- oriented macros.
Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page.
-Vincent
On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
On May 14, 2009, at 7:31 PM, <[email protected]> <[email protected]
wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 9:55 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
On May 14, 2009, at 6:54 PM, Vincent Massol wrote:
On May 14, 2009, at 6:21 PM, <[email protected]>
wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Vincent Massol Sent: Thursday, May 14, 2009 8:46 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
Hi Glenn,
The solution is to use a Javascript Extension (JSX). See
http://platform.xwiki.org/xwiki/bin/view/DevGuide/SkinExtensionsTutoria
l
Then you put the $jsx.use in your java macro.
This looks promising. From the context of a java macro is there any way to use the $jsx type references?
You'll need to create a MacroBlock with a name of velocity.
but in any case you'll need to put you Javascript extension object in a wiki page.
I wonder if we're looking at the wrong solution. In this case I want to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content.
If you look at the xwiki template that creates the header you'll see that it has some lines to add all SSX, LSX and JSX objects that exist in wiki pages. So using a JSX, SSX and LSX is the current way to add stuff in the header (unless you provide a custom template of course but then you'll have to maintain it or merge it with new features when new versions of XWiki are released). Hope it helps, -Vincent
I suppose I could look at the jsx extension code to see how this is done as it is clearly doing this somehow?
-Vincent
-Vincent
It looks like the only thing a macro can do is return a List<Block> instance so I'm not sure it can affect the system header includes? Using a velocity page could work but seems klunky to have a plugin import pages into the wiki just to get a macro.
In the near future (2.0M1), you'll be able to write XWiki 2.0
syntax
macros in wiki pages so it'll be more easy to write very UI- oriented macros.
Of course you can also write your macro as a Velocity macro right now in a Wiki page and attach the JXS object to that page.
-Vincent
On May 14, 2009, at 5:31 PM, <[email protected]> <[email protected]
wrote:
I have a java macro that requires the page to include an external js (e.g. jQuery) that works best if included in the head section of the document. Is there a way for a macro to trigger inclusion of resources like this to be included in the page header? In confluence this was done by having a #requireResource statement in a velocity template included by the macro.
How can this be done in xwiki?
[email protected] wrote:
I wonder if we're looking at the wrong solution. In this case I want to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content.
I suppose I could look at the jsx extension code to see how this is done as it is clearly doing this somehow?
You don't need to do that. There is a plugin that allows to include links to local JS files, which from Velocity would be called as: $xwiki.jsfx.use('/js/jquery/jquery.js'), provided that the file is located in <webapproot>/resources/js/jquery/jquery.js Now, going through velocity is an unneeded detour, since velocity code goes back to Java through parsing and reflexion. The only problem is that there is no (easy and clean) way to get hold of a reference to the plugin, since rendering is in the new, "2.0" component world, while the plugin is in the old monolithic world. I've been planning to reimplement the skin extensions as components for a while, but since it wasn't a priority, it isn't done yet. So, there are 2 solutions: 1. Make your macro depend on the xwiki-core and xwiki-plugin-skinx modules, then get a reference to the XWiki object from the execution context, then call getPlugin("jsfx") and call use("") on it. 2. Manually inject the file in the list of pulled extension, duplicating the code from AbstractSkinExtensionPlugin#getPulledResources (see http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-plugins/trunk/skinx/src/ma... ) which means: from the execution context, get the xwiki context as a Map, from which you should get the value from "com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin" (cast as a Set) and add "js/jquery/jquery.js" to it. -- Sergiu Dumitriu http://purl.org/net/sergiu/
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Sergiu Dumitriu Sent: Thursday, May 14, 2009 10:55 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
[email protected] wrote:
I wonder if we're looking at the wrong solution. In this case I want
to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content.
I suppose I could look at the jsx extension code to see how this is
done as it is clearly doing this somehow?
You don't need to do that.
There is a plugin that allows to include links to local JS files, which from Velocity would be called as:
$xwiki.jsfx.use('/js/jquery/jquery.js'), provided that the file is located in <webapproot>/resources/js/jquery/jquery.js
Now, going through velocity is an unneeded detour, since velocity code goes back to Java through parsing and reflexion. The only problem is that there is no (easy and clean) way to get hold of a reference to the plugin, since rendering is in the new, "2.0" component world, while the plugin is in the old monolithic world. I've been planning to reimplement the skin extensions as components for a while, but since it wasn't a priority, it isn't done yet.
So, there are 2 solutions:
1. Make your macro depend on the xwiki-core and xwiki-plugin-skinx modules, then get a reference to the XWiki object from the execution context, then call getPlugin("jsfx") and call use("") on it.
This sound like a good approach. How might I get the XWiki object from the execution context. I see a macro is passed in a context but I don't see anything in it that looks like I could get an XWiki object from. Looks like I'll need an XWikiContext as well to call use(). Is this available from the macro execute callback? I had a similar open question about how to get a XWiki object from an HttpServlet but in that case there is not any rendering going on.
2. Manually inject the file in the list of pulled extension, duplicating the code from AbstractSkinExtensionPlugin#getPulledResources (see http://svn.xwiki.org/svnroot/xwiki/platform/xwiki- plugins/trunk/skinx/src/main/java/com/xpn/xwiki/plugin/skinx/AbstractSk inExtensionPlugin.java ) which means: from the execution context, get the xwiki context as a Map, from which you should get the value from "com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin" (cast as a Set) and add "js/jquery/jquery.js" to it. -- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
If you want an example of putting tons of JS/CSS (including jquery) into Xwiki's skin extensions, see the following: http://nielsmayer.com/xwiki/bin/view/Exhibit/Presidents3 http://nielsmayer.com/xwiki/bin/download/Exhibit/Presidents3/Exhibit.Preside... note objects on Exhibit.Presidents3 (import the xar and view in your own object-editor): XWiki.JavaScriptExtension[0]: JSON data on US presidents that drives Exhibit. XWiki.JavaScriptExtension[1]: Event.observe(window, "load", onLoad) - create UI after all JS loaded XWiki.JavaScriptExtension[2]: inlined jQuery JavaScript Library v1.3.2 XWiki.JavaScriptExtension[3]: javascript that dynamically injects SCRIPT tags into the HEAD, which causes the browser to load the javascript. This is dependent on inlined jquery in XWiki.JavaScriptExtension[2], which must actually have been loaded (as opposed to just injecting the SCRIPT/CSS tags into the head, which causes it to load later). XWiki.StyleSheetExtension[0]: my workaround to the fact that $xwiki.ssfx.use() won't let me specify external libs: "@import url(" http://trunk.simile-widgets.org/styles/common.css");" XWiki.StyleSheetExtension[1]: Modified Stylesheet from http://trunk.simile-widgets.org/exhibit/examples/presidents/styles.css The contents of XWiki.JavaScriptExtension[3] may be of interest, in terms of technique for injecting SCRIPT and CSS into HEAD from an Xwiki document. It's quite a hack -- see "document.write("<script src= ...);" -- but it works, courtesy of the Simile Project at MIT. IMHO, the technique in and of itself should be made into an Xwiki macro so you can more easily inject arbitrary SCRIPT, CSS, or LINK into the HEAD from within an Xwiki document: /*================================================== * Modification of <script src= * http://static.simile.mit.edu/exhibit/api-2.0/exhibit-api.jstype="text/javascript"></script> * for use with http://nielsmayer.com/xwiki/bin/edit/Exhibit/Presidents* ... * by Niels Mayer http://nielsmayer.com *================================================== */ (function() { var useLocalResources = true; // NPM: set to true to use trunk.simile-widgets.org rather than default api.simile-widgets.org var loadMe = function() { if (typeof window.Exhibit != "undefined") { return; } window.Exhibit = { version: useLocalResources ? "trunk pre 2.3.0" : "2.2.0", loaded: false, params: { bundle: true, authenticated: false, autoCreate: false, safe: false, gmapkey: "...", views: "timeline,map" }, namespace: "http://simile.mit.edu/2006/11/exhibit#", importers: {}, locales: [ "en" ] }; var javascriptFiles = [ "exhibit.js", "persistence.js", "authentication.js", "util/set.js", "util/util.js", "util/settings.js", "util/views.js", "util/facets.js", "util/coders.js", "data/database.js", "data/expression.js", "data/expression-parser.js", "data/functions.js", "data/controls.js", "data/collection.js", "data/importers/authenticated-importer.js", "data/importers/exhibit-json-importer.js", "data/importers/html-table-importer.js", "data/importers/jsonp-importer.js", "data/importers/babel-based-importer.js", "data/importers/rdfa-importer.js", "data/exporters/rdf-xml-exporter.js", "data/exporters/semantic-wikitext-exporter.js", "data/exporters/exhibit-json-exporter.js", "data/exporters/tsv-exporter.js", "data/exporters/bibtex-exporter.js", "data/exporters/facet-selection-exporter.js", "ui/ui.js", "ui/ui-context.js", "ui/lens.js", "ui/format-parser.js", "ui/formatter.js", "ui/coordinator.js", "ui/facets/list-facet.js", "ui/facets/numeric-range-facet.js", "ui/facets/text-search-facet.js", "ui/facets/cloud-facet.js", "ui/facets/hierarchical-facet.js", "ui/facets/image-facet.js", "ui/facets/slider-facet.js", "ui/facets/slider.js", "ui/facets/alpha-range-facet.js", "ui/coders/color-coder.js", "ui/coders/default-color-coder.js", "ui/coders/color-gradient-coder.js", "ui/coders/size-coder.js", "ui/coders/size-gradient-coder.js", "ui/coders/icon-coder.js", "ui/widgets/logo.js", "ui/widgets/collection-summary-widget.js", "ui/widgets/resizable-div-widget.js", "ui/widgets/legend-widget.js", "ui/widgets/legend-gradient-widget.js", "ui/widgets/option-widget.js", "ui/widgets/toolbox-widget.js", "ui/views/view-panel.js", "ui/views/ordered-view-frame.js", "ui/views/tile-view.js", "ui/views/thumbnail-view.js", "ui/views/tabular-view.js", "ui/views/html-view.js" ]; var cssFiles = [ "exhibit.css", "browse-panel.css", "lens.css", "util/facets.css", "util/views.css", "widgets/collection-summary-widget.css", "widgets/resizable-div-widget.css", "widgets/legend-widget.css", "widgets/option-widget.css", "widgets/toolbox-widget.css", "views/view-panel.css", "views/tile-view.css", "views/thumbnail-view.css", "views/tabular-view.css" ]; var includeMap = false; var includeTimeline = false; var defaultClientLocales = ("language" in navigator ? navigator.language : navigator.browserLanguage).split(";"); for (var l = 0; l < defaultClientLocales.length; l++) { var locale = defaultClientLocales[l]; if (locale != "en") { var segments = locale.split("-"); if (segments.length > 1 && segments[0] != "en") { Exhibit.locales.push(segments[0]); } Exhibit.locales.push(locale); } } var paramTypes = { bundle:Boolean, js:Array, css:Array, autoCreate:Boolean, safe:Boolean }; if (typeof Exhibit_urlPrefix == "string") { Exhibit.urlPrefix = Exhibit_urlPrefix; if ("Exhibit_parameters" in window) { SimileAjax.parseURLParameters(Exhibit_parameters, Exhibit.params, paramTypes); } } else { Exhibit.urlPrefix = "http://api.simile-widgets.org/exhibit/2.2.0/"; } if (useLocalResources) { Exhibit.urlPrefix = " http://trunk.simile-widgets.org/exhibit/api/"; } if (Exhibit.params.locale) { // ISO-639 language codes, // optional ISO-3166 country codes (2 characters) if (Exhibit.params.locale != "en") { var segments = Exhibit.params.locale.split("-"); if (segments.length > 1 && segments[0] != "en") { Exhibit.locales.push(segments[0]); } Exhibit.locales.push(Exhibit.params.locale); } } if (Exhibit.params.gmapkey) { includeMap = true; } if (Exhibit.params.views) { var views = Exhibit.params.views.split(","); for (var j = 0; j < views.length; j++) { var view = views[j]; if (view == "timeline") { includeTimeline = true; } else if (view == "map") { includeMap = true; } } } var scriptURLs = Exhibit.params.js || []; var cssURLs = Exhibit.params.css || []; /* * Core scripts and styles */ if (true) { scriptURLs.push(Exhibit.urlPrefix + "exhibit-bundle.js"); cssURLs.push(Exhibit.urlPrefix + "exhibit-bundle.css"); } else { SimileAjax.prefixURLs(scriptURLs, Exhibit.urlPrefix + "scripts/", javascriptFiles); SimileAjax.prefixURLs(cssURLs, Exhibit.urlPrefix + "styles/", cssFiles); } /* * Localization */ for (var i = 0; i < Exhibit.locales.length; i++) { scriptURLs.push(Exhibit.urlPrefix + "locales/" + Exhibit.locales[i] + "/locale.js"); }; if (Exhibit.params.callback) { window.SimileAjax_onLoad = function() { eval(Exhibit.params.callback + "()"); } } else if (Exhibit.params.autoCreate) { scriptURLs.push(Exhibit.urlPrefix + "scripts/create.js"); } /* * Extensions (for backward compatibility) */ if (includeTimeline) { scriptURLs.push(Exhibit.urlPrefix + "extensions/time/time-extension.js"); } if (includeMap) { scriptURLs.push(Exhibit.urlPrefix + "extensions/map/map-extension.js"); } SimileAjax.includeJavascriptFiles(document, "", scriptURLs); SimileAjax.includeCssFiles(document, "", cssURLs); Exhibit.loaded = true; }; /* * Load SimileAjax if it's not already loaded */ if (typeof SimileAjax == "undefined") { window.SimileAjax_onLoad = loadMe; var url = useLocalResources ? " http://trunk.simile-widgets.org/ajax/api/simile-ajax-api.js?bundle=true" : " http://api.simile-widgets.org/ajax/2.2.1/simile-ajax-api.js?bundle=true"; var createScriptElement = function() { var script = document.createElement("script"); script.type = "text/javascript"; script.language = "JavaScript"; script.src = url; document.getElementsByTagName("head")[0].appendChild(script); }; if (document.body == null) { try { document.write("<script src='" + url + "' type='text/javascript'></script>"); } catch (e) { createScriptElement(); } } else { createScriptElement(); } } else { loadMe(); } })(); --------------------------------------- to insert a LINK: function insertExhibitDataLink(doc, url) { if (doc.body == null) { try { doc.write("<link rel='exhibit/data' href='" + url + "' type='application/json'/>"); return; } catch (e) { // fall through } } // never called if write() above succeeds var link = doc.createElement("link"); link.setAttribute("rel", "exhibit/data"); link.setAttribute("type", "application/json"); link.setAttribute("href", url); doc.getElementsByTagName("head")[0].appendChild(link); } insertExhibitDataLink(window.document, "/xwiki/bin/view/Exhibit/PresidentsSchemaJSON?xpage=plain"); insertExhibitDataLink(window.document, "/xwiki/bin/view/Exhibit/PresidentsJSON?xpage=plain"); Niels http://nielsmayer.com
[email protected] wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Sergiu Dumitriu Sent: Thursday, May 14, 2009 10:55 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
[email protected] wrote:
I wonder if we're looking at the wrong solution. In this case I want to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content. I suppose I could look at the jsx extension code to see how this is done as it is clearly doing this somehow? You don't need to do that.
There is a plugin that allows to include links to local JS files, which from Velocity would be called as:
$xwiki.jsfx.use('/js/jquery/jquery.js'), provided that the file is located in <webapproot>/resources/js/jquery/jquery.js
Now, going through velocity is an unneeded detour, since velocity code goes back to Java through parsing and reflexion. The only problem is that there is no (easy and clean) way to get hold of a reference to the plugin, since rendering is in the new, "2.0" component world, while the plugin is in the old monolithic world. I've been planning to reimplement the skin extensions as components for a while, but since it wasn't a priority, it isn't done yet.
So, there are 2 solutions:
1. Make your macro depend on the xwiki-core and xwiki-plugin-skinx modules, then get a reference to the XWiki object from the execution context, then call getPlugin("jsfx") and call use("") on it.
This sound like a good approach. How might I get the XWiki object from the execution context. I see a macro is passed in a context but I don't see anything in it that looks like I could get an XWiki object from. Looks like I'll need an XWikiContext as well to call use(). Is this available from the macro execute callback?
You get XWiki from XWikiContext using xcontext.getWiki() You get XWikiContext from the execution context using context.getProperty("xwikicontext") You get the execution context from the execution using execution.getContext() You get the execution as a component dependency, injected by the component manager.
I had a similar open question about how to get a XWiki object from an HttpServlet but in that case there is not any rendering going on.
2. Manually inject the file in the list of pulled extension, duplicating the code from AbstractSkinExtensionPlugin#getPulledResources (see http://svn.xwiki.org/svnroot/xwiki/platform/xwiki- plugins/trunk/skinx/src/main/java/com/xpn/xwiki/plugin/skinx/AbstractSk inExtensionPlugin.java ) which means: from the execution context, get the xwiki context as a Map, from which you should get the value from "com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin" (cast as a Set) and add "js/jquery/jquery.js" to it. -- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Sergiu Dumitriu http://purl.org/net/sergiu/
-----Original Message----- From: [email protected] [mailto:[email protected]] On Behalf Of Sergiu Dumitriu Sent: Thursday, May 14, 2009 6:27 PM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
[email protected] wrote:
-----Original Message----- From: [email protected] [mailto:[email protected]] On
Behalf
Of Sergiu Dumitriu Sent: Thursday, May 14, 2009 10:55 AM To: XWiki Developers Subject: Re: [xwiki-devs] Macro requiring js/css
[email protected] wrote:
I wonder if we're looking at the wrong solution. In this case I want to include a javascript resource file such as jQuery.js so what I really want is to specify a script tag to be added to the header rather than an embedded javascript extension object content. I suppose I could look at the jsx extension code to see how this is done as it is clearly doing this somehow? You don't need to do that.
There is a plugin that allows to include links to local JS files, which from Velocity would be called as:
$xwiki.jsfx.use('/js/jquery/jquery.js'), provided that the file is located in <webapproot>/resources/js/jquery/jquery.js
Now, going through velocity is an unneeded detour, since velocity code goes back to Java through parsing and reflexion. The only problem is that there is no (easy and clean) way to get hold of a reference to the plugin, since rendering is in the new, "2.0" component world, while the plugin is in the old monolithic world. I've been planning to reimplement the skin extensions as components for a while, but since it wasn't a priority, it isn't done yet.
So, there are 2 solutions:
1. Make your macro depend on the xwiki-core and xwiki-plugin-skinx modules, then get a reference to the XWiki object from the execution context, then call getPlugin("jsfx") and call use("") on it.
This sound like a good approach. How might I get the XWiki object from the execution context. I see a macro is passed in a context but I don't see anything in it that looks like I could get an XWiki object from. Looks like I'll need an XWikiContext as well to call use(). Is this available from the macro execute callback?
You get XWiki from XWikiContext using xcontext.getWiki()
You get XWikiContext from the execution context using context.getProperty("xwikicontext")
You get the execution context from the execution using execution.getContext()
You get the execution as a component dependency, injected by the component manager.
OK, I added @Requirement private Execution execution; And then in my macro execution: ExecutionContext executionContext = execution.getContext(); XWikiContext context = (XWikiContext) executionContext.getProperty("xwikicontext"); XWiki xwiki = context.getWiki(); AbstractSkinExtensionPlugin plugin = (AbstractSkinExtensionPlugin) xwiki.getPlugin("jsfx", context); plugin.use("/foo/bar.js",context); When I utilize this from my macro execution callback it doesn't emit any code in the header. (the velocity macro technique does work with jsfx though. Is the context obtained via this method the right one or is there a different one somehow attached to the rendering agent? It seems like I'm 'almost there'!
I had a similar open question about how to get a XWiki object from an HttpServlet but in that case there is not any rendering going on.
2. Manually inject the file in the list of pulled extension, duplicating the code from AbstractSkinExtensionPlugin#getPulledResources (see http://svn.xwiki.org/svnroot/xwiki/platform/xwiki-
plugins/trunk/skinx/src/main/java/com/xpn/xwiki/plugin/skinx/AbstractSk
inExtensionPlugin.java ) which means: from the execution context, get the xwiki context as a Map, from which you should get the value from "com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin" (cast as a Set) and add "js/jquery/jquery.js" to it. -- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
participants (4)
-
glenn_engel@agilent.com -
Niels Mayer -
Sergiu Dumitriu -
Vincent Massol