Widgets

The {@link oajr.widget.Widget} class allows you to add arbitrary HTML, CSS, and Javascript to HTML pages.
They are registered in the following locations:

- {@link oajr.annotation.HtmlDoc#widgets() @HtmlDoc.widgets}
- {@link oajr.RestContextBuilder#widgets(Class...)}
- {@link oajr.RestContextBuilder#widgets(Widget...)}
- {@link oajr.RestContext#REST_widgets}

##### Example:

@RestMethod( widgets={ MyWidget. class } htmldoc=@HtmlDoc( navlinks={ "$W{MyWidget}" }, aside={ "Check out this widget: $W{MyWidget}" } ) )

The Widget class is composed of the following methods:

- {@link oajr.widget.Widget}
  - {@link oajr.widget.Widget#getHtml(RestRequest) getHtml(RestRequest)}
  - {@link oajr.widget.Widget#getStyle(RestRequest) getStyle(RestRequest)}
  - {@link oajr.widget.Widget#getScript(RestRequest) getScript(RestRequest)}

The HTML content returned by the {@link oajr.widget.Widget#getHtml(RestRequest) getHtml(RestRequest)} method is added wherever the "$W{...}" variable is used.

The CSS returned by {@link oajr.widget.Widget#getScript(RestRequest) getScript(RestRequest)} is added to the style section in the page header.

The Javascript returned by {@link oajr.widget.Widget#getScript(RestRequest) getScript(RestRequest)} is added to the script section in the page header.

The following examples shows how to associate a widget with a REST method and then have it rendered in the links and aside section of the page.
It shows an example of a widget that renders an image located in the `htdocs` static files directory in your classpath (see {@link oajr.annotation.RestResource#staticFiles() @RestResource(staticFiles)}):

public class MyWidget extends Widget { @Override /* Widget */ public String getHtml(RestRequest req) throws Exception { UriResolver r = req.getUriResolver(); // API used for resolving URIs. return "<img class='myimage' onclick='myalert(this)' src='"+r.resolve("servlet:/htdocs/myimage.png")+"'>"; } @Override /* Widget */ public String getScript(RestRequest req) throws Exception { return "" + "\n function myalert(imageElement) {" + "\n alert('cool!');" + "\n }"; } @Override /* Widget */ public String getStyle(RestRequest req) throws Exception { return "" + "\n .myimage {" + "\n border: 10px solid red;" + "\n }"; } }

The Widget class also defines the following two convenience methods for loading Javascript and CSS files from the classpath or file system.

- {@link oajr.widget.Widget}
  - {@link oajr.widget.Widget#getClasspathResourceAsString(String) getClasspathResourceAsString(String)}
  - {@link oajr.widget.Widget#getClasspathResourceAsString(String,Locale) getClasspathResourceAsString(String,Locale)}

##### Example:

public class MyWidget extends Widget { ... @Override /* Widget */ public String getScript(RestRequest req) throws Exception { return getClasspathResourceAsString("MyWidget.js"); } @Override /* Widget */ public String getStyle(RestRequest req) throws Exception { return getClasspathResourceAsString("MyWidget.css"); } }

##### See Also:

- {@link oajr.RestContext#REST_widgets}