
There are a number of built in features in the JS engine. Many of these features are accessible by assigning certain attributes on HTML elements. The following is a list of available features and how to implement them:
Toggles the visibility of an element on the page when an anchor tag is clicked. Set the anchor tag's href to the ID of the element to toggle and add `rel="toggle"`.
Example:
$content = elgg_view('output/url', array( 'href' => '#toggle-me', 'rel' => 'toggle', 'text' => 'Click to toggle' )); $content .= elgg_view_module('info', 'Test toggle', 'Example content', array('id' => 'toggle-me')); echo elgg_view_page('Test', $content);
Pops up a hidden element on the page on top of other content when an anchor tag is clicked. Set the anchor tag's href to the ID of the element to pop up and add `rel="popup"`.
Note: If you want to pop up images, another page, or external links, use the lightbox instead.
Example:
$content = elgg_view('output/url', array( 'href' => '#popup', 'rel' => 'popup', 'text' => 'Click to pop up' )); $content .= elgg_view_module('popup', 'Test popup', 'Example content', array( 'id' => 'popup', 'class' => 'hidden' )); echo elgg_view_page('Test', $content);
Opens a lightbox with when an anchor tag is clicked. Set the lightbox's target to the anchor tag's href and add the class `elgg-lightbox`.
Example:
elgg_load_js('lightbox'); elgg_load_css('lightbox'); $content = elgg_view('output/url', array( 'href' => '/activity', 'class' => 'elgg-lightbox', 'text' => 'Activity in a lightbox' )); echo elgg_view_page('Test', $content);
The default lightbox is Fancybox. (This will change in 1.9 because Fancybox is moving to a proprietary license.)
Note: You cannot open external links in a lightbox using this method.
Focuses input on the specified element upon page load. Add the class `.elgg-autofocus` to the element to focus. Note: This is the default behavior for the `output/confirmlink` view.
Example:
$content = elgg_view('input/text', array( 'name' => 'autofocus_test', 'class' => 'elgg-autofocus' )); echo elgg_view_page('Test', $content);
Opens a dialogue box asking the user to confirm an action. Add the class `.elgg-requires-confirmation` to the element. Note: This is default behavior for the `output/confirmlink` view. If you require confirmation on anchor tags, use that view instead.
Example:
$content = elgg_view('input/submit', array( 'value' => 'Requires confirmation', 'class' => 'elgg-requires-confirmation', )); echo elgg_view_page('Test', $content);
The `elgg` JS object has many helpful methods you should be familiar with when writing JS for Elgg. Many of them mimic their similarly named PHP counterparts
Always look in the code for further documentation, features, and examples on how to use these methods.
Normalize a URL. See the PHP function elgg_normalize_url().
Example:
// returns http://elgg.org elgg.normalize_url('elgg.org');
Pop up a system message or an error. See the PHP functions system_message() and register_error().
Example:
elgg.system_message('Success!'); elgg.register_error('Failure.');
Forwards to the specified URL. This passes the URL string through elgg.normalize_url().
Parses a URL into its parts. Similar to PHP's parse_url.
Example:
/* returns an object with the properties fragment: "fragment" host: "community.elgg.org" path: "/path/file.php" query: "arg=val" */ elgg.parse_url('http://community.elgg.org/path/file.php?arg=val#fragment');
Echoes a language string. Operates the same as the PHP elgg_echo(). Strings are defined as normal in PHP language files.
Example:
// returns: // "You have successfully added Brett as a friend." elgg.echo("friends:add:successful", ['Brett'])
Returns the GUID of the page owner.
Forces a refresh of all tokens on the page. This requires a valid security token. Note: This is called every 5 minutes by default.
Adds a security token to an object (to be used as form data), a URL, or a query string starting with a ?
Example:
/* returns an object: __elgg_token: "1468dc44c5b437f34423e2d55acfdd87" __elgg_ts: 1328143779 other: "data" */ elgg.security.addToken({'other': 'data'}); // returns: "action/add?__elgg_ts=1328144079&__elgg_token=55fd9c2d7f5075d11e722358afd5fde2" elgg.security.addToken("action/add"); // returns "?arg=val&__elgg_ts=1328144079&__elgg_token=55fd9c2d7f5075d11e722358afd5fde2" elgg.security.addToken("?arg=val");
Returns the logged in user as an JS ElggUser object.
Returns the logged in user's guid.
Returns true if a user is logged in, and if an admin user is logged in respectively.
Creates a name-spaced object structure with namespaces separated by dots.
Example:
/* creates the object structure: { elgg: { namespace1: { namespace2: {} } } } */ elgg.provide('elgg.namespace1.namespace2');
There are a number of configuration values set in the elgg object.