From MojiPage Wiki

Jump to: navigation, search

MojiPage Widget API

The MojiPage Widget API (MWA) allows developers to create highly interactive widgets on the MojiPage platform using XHTML, Javascript and CSS. Widgets developed using MWA can be made available for the growing community of users on MojiPage.

MWA provides methods to manipulate the HTML DOM, fetch remote content, access social information such as friends and activity streams.


Contents


widget Object

A container object that represents the current widget. It provides methods to access DOM and preferences of the widget. It is closely modeled after the Netvibes Univeral Widget API (UWA). Not all events, methods or properties in UWA is supported because of our platform differences. The ones supported by MojiPage are listed here; others will be ignored.


Events

widget.onLoad

Required. This is essentially the entry-point to your widget; it will be called when the widget is executed. Define it by assigning it to a function that implements your main logic. Refer to the Tutorial: Creating Your First Widget for a complete example.


Methods

widget.addBody( String content )

appends the given content to the widget body element

widget.createElement( String name )

creates a DOM element with the give name and returns it

widget.getTitle( )

returns the current widget title

widget.getValue( String name )

returns the preference value for the given name

widget.log( String string )

If the widget is in debug mode, logs the given string into the debugging console provided by MojiPage.

widget.setBody( String content )

replaces the current content in the widget body with the given content

widget.setTitle( String title )

sets the widget title to the given title


Properties

widget.body

returns the widget body DOM element. A subset of the DOM API is available on this element (as well as any descendants.)


DOM API

MWA supports a subset of W3C DOM Level 1, Level 2, and Level 3 APIs.

The following are supported features on DOM nodes.


Methods

  • Node.insertBefore
  • Node.replaceChild
  • Node.removeChild
  • Node.appendChild
  • Node.hasChildNodes
  • Node.cloneNode
  • Element.getElementsByTagName
  • Element.setAttribute
  • Element.getAttribute
  • Element.insertRow
  • Element.setAttribute
  • HTMLTableElement.insertRow
  • HTMLTableRowElement.insertCell


Properties

  • Node.tagName (read-only)
  • Node.nodeType (read-only)
  • Node.nodeValue
  • Node.textContent
  • Node.firstChild (read-only)
  • Node.parentNode (read-only)
  • HTMLElement.className
  • HTMLElement.style
  • HTMLElement.innerHTML


MWA.Servlet Object

With MWA, one can build multi-page widgets that are not unlike traditional server-side web applications. Indeed, because MWA widgets run on the server, they are similar to CGI/PHP/servlets. MWA adopts some method signatures and naming conventions from the Java Servlet API for its handling functions.


Methods

MWA.Servlet.getRequest()

This method returns the current request object associated with this widget instance.

var request = MWA.Servlet.getRequest();
var pet = request.getParameter('favorite_pet');

The returned request object supports the following methods.


request.getMethod()
returns the HTTP method with which this request was made, for example, "GET", "HEAD", or "POST".
request.getPathInfo()
returns any extra path information associated with the URL the client sent when it made this request. This is useful for retrieving custom sub-actions that you defined.
var request = MWA.Servlet.getRequest();
var action = request.getPathInfo();
if (action == "post") {
  // post comment here
}
else {
  var ctx = MWA.Servlet.getServletContext();
  var postURL = ctx.getAttribute('do_url') + "post/"; // remember to append a "/" after the action

  // show comment form that posts to 'postURL'
}
request.getParameterNames()
returns an array of strings. Each element of the array corresponds to a unique request parameter name
request.getParameter( String name )
returns the value of the request parameter with the given name, or null if there is no parameter by that name.


MWA.Servlet.getResponse()

This method returns the current response object associated with this widget instance.

var response = MWA.Servlet.getResponse();
response.redirect("http://mojipage.com/");

The returned response object supports the following methods.

response.redirect( String url )
redirects the user to a given URL. This only works when the widget instance is run in do mode.
response.setStatus( int code )
sets the HTTP response code (e.g. 200 for OK, 500 for internal error)
response.setHeader( String name, String value )
sets a HTTP response header with the given name and value.
response.setCacheOutput ( Boolean enable )
tells MojiPage if the output from the current request should be cached as the widget instance content. The default is to cache. Note that MojiPage will not use the cached content when in 'do' or 'config' mode, or if the widget is in debug mode.


MWA.Servlet.getServletContext()

This returns a reference to the servlet context object. The context object supports a single getAttribute method which provides access to various attributes associated with the widget instance:

context.getAttribute( String attr )

Use this method to get one of the following widget instance attributes:

  • instance_id
  • page_id
  • user_id
  • widget_id
  • url
  • site_root
  • config_url
  • do_url
  • preview_url
  • device
Personal tools