aboutsummaryrefslogtreecommitdiff
path: root/js/lib
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib')
-rw-r--r--js/lib/ajax.js252
-rw-r--r--js/lib/configuration.js3
-rw-r--r--js/lib/elgglib.js187
-rw-r--r--js/lib/events.js65
-rw-r--r--js/lib/languages.js83
-rw-r--r--js/lib/security.js70
-rw-r--r--js/lib/session.js116
-rw-r--r--js/lib/ui.js121
-rw-r--r--js/lib/ui.widgets.js131
9 files changed, 1028 insertions, 0 deletions
diff --git a/js/lib/ajax.js b/js/lib/ajax.js
new file mode 100644
index 000000000..184fd0da3
--- /dev/null
+++ b/js/lib/ajax.js
@@ -0,0 +1,252 @@
+elgg.provide('elgg.ajax');
+
+/**
+ * @author Evan Winslow
+ * Provides a bunch of useful shortcut functions for making ajax calls
+ */
+
+/**
+ * Wrapper function for jQuery.ajax which ensures that the url being called
+ * is relative to the elgg site root.
+ *
+ * You would most likely use elgg.get or elgg.post, rather than this function
+ *
+ * @param {string} url Optionally specify the url as the first argument
+ * @param {Object} options Optional. {@see jQuery#ajax}
+ * @return {XmlHttpRequest}
+ */
+elgg.ajax = function(url, options) {
+ options = elgg.ajax.handleOptions(url, options);
+
+ options.url = elgg.normalize_url(options.url);
+ return $.ajax(options);
+};
+/**
+ * @const
+ */
+elgg.ajax.SUCCESS = 0;
+
+/**
+ * @const
+ */
+elgg.ajax.ERROR = -1;
+
+/**
+ * Handle optional arguments and return the resulting options object
+ *
+ * @param url
+ * @param options
+ * @return {Object}
+ * @private
+ */
+elgg.ajax.handleOptions = function(url, options) {
+ //elgg.ajax('example/file.php', {...});
+ if(typeof url == 'string') {
+ options = options || {};
+
+ //elgg.ajax({...});
+ } else {
+ options = url || {};
+ url = options.url;
+ }
+
+ var data_only = true;
+
+ //elgg.ajax('example/file.php', function() {...});
+ if (typeof options == 'function') {
+ data_only = false;
+ options = {success: options};
+ }
+
+ //elgg.ajax('example/file.php', {data:{...}});
+ if(options.data) {
+ data_only = false;
+ } else {
+ for (var member in options) {
+ //elgg.ajax('example/file.php', {callback:function(){...}});
+ if(typeof options[member] == 'function') {
+ data_only = false;
+ }
+ }
+ }
+
+ //elgg.ajax('example/file.php', {notdata:notfunc});
+ if (data_only) {
+ var data = options;
+ options = {data: data};
+ }
+
+ if (url) {
+ options.url = url;
+ }
+
+ return options;
+};
+
+/**
+ * Wrapper function for elgg.ajax which forces the request type to 'get.'
+ *
+ * @param {string} url Optionally specify the url as the first argument
+ * @param {Object} options {@see jQuery#ajax}
+ * @return {XmlHttpRequest}
+ */
+elgg.get = function(url, options) {
+ options = elgg.ajax.handleOptions(url, options);
+
+ options.type = 'get';
+ return elgg.ajax(options);
+};
+
+/**
+ * Wrapper function for elgg.get which forces the dataType to 'json.'
+ *
+ * @param {string} url Optionally specify the url as the first argument
+ * @param {Object} options {@see jQuery#ajax}
+ * @return {XmlHttpRequest}
+ */
+elgg.getJSON = function(url, options) {
+ options = elgg.ajax.handleOptions(url, options);
+
+ options.dataType = 'json';
+ return elgg.get(options);
+};
+
+/**
+ * Wrapper function for elgg.ajax which forces the request type to 'post.'
+ *
+ * @param {string} url Optionally specify the url as the first argument
+ * @param {Object} options {@see jQuery#ajax}
+ * @return {XmlHttpRequest}
+ */
+elgg.post = function(url, options) {
+ options = elgg.ajax.handleOptions(url, options);
+
+ options.type = 'post';
+ return elgg.ajax(options);
+};
+
+/**
+ * Perform an action via ajax
+ *
+ * @example Usage 1:
+ * At its simplest, only the action name is required (and anything more than the
+ * action name will be invalid).
+ * <pre>
+ * elgg.action('name/of/action');
+ * </pre>
+ * Note that it will *not* love you if you specify the full url as the action
+ * (i.e. elgg.yoursite.com/action/name/of/action), but why would you want to do
+ * that anyway, when you can just specify the action name?
+ *
+ * @example Usage 2:
+ * If you want to pass some data along with it, use the second parameter
+ * <pre>
+ * elgg.action('friend/add', { friend: some_guid });
+ * </pre>
+ *
+ * @example Usage 3:
+ * Of course, you will have no control over what happens when the request
+ * completes if you do it like that, so there's also the most verbose method
+ * <pre>
+ * elgg.action('friend/add', {
+ * data: {
+ * friend: some_guid
+ * },
+ * success: function(json) {
+ * //do something
+ * },
+ * }
+ * </pre>
+ * You can pass any of your favorite $.ajax arguments into this second parameter.
+ *
+ * Note: If you intend to use the second field in the "verbose" way, you must
+ * specify a callback method or the data parameter. If you do not, elgg.action
+ * will think you mean to send the second parameter as data.
+ *
+ * @param {String} action The action to call.
+ * @param {Object} options {@see jQuery#ajax}
+ * @return {XMLHttpRequest}
+ */
+elgg.action = function(action, options) {
+ if(!action) {
+ throw new TypeError("action must be specified");
+ } else if (typeof action != 'string') {
+ throw new TypeError("action must be a string");
+ }
+
+ options = elgg.ajax.handleOptions('action/' + action, options);
+
+ options.data = elgg.security.addToken(options.data);
+ options.dataType = 'json';
+
+ //Always display system messages after actions
+ var custom_success = options.success || function(){};
+ options.success = function(json, two, three, four) {
+ if (json.system_messages) {
+ elgg.register_error(json.system_messages.errors);
+ elgg.system_message(json.system_messages.messages);
+ }
+ custom_success(json, two, three, four);
+ };
+
+ return elgg.post(options);
+};
+
+/**
+ * Make an API call
+ *
+ * @example Usage:
+ * <pre>
+ * elgg.api('system.api.list', {
+ * success: function(data) {
+ * console.log(data);
+ * }
+ * });
+ * </pre>
+ *
+ * @param {String} method The API method to be called
+ * @param {Object} options {@see jQuery#ajax}
+ * @return {XmlHttpRequest}
+ */
+elgg.api = function(method, options) {
+ if (!method) {
+ throw new TypeError("method must be specified");
+ } else if (typeof method != 'string') {
+ throw new TypeError("method must be a string");
+ }
+
+ var defaults = {
+ dataType: 'json',
+ data: {}
+ };
+
+ options = elgg.ajax.handleOptions(method, options);
+ options = $.extend(defaults, options);
+
+ options.url = 'services/api/rest/' + options.dataType + '/';
+ options.data.method = method;
+
+ return elgg.ajax(options);
+};
+
+/**
+ * @param {string} selector a jQuery selector
+ * @param {Function} complete A function to execute when the refresh is done
+ * @return {XMLHttpRequest}
+ */
+elgg.refresh = function(selector, complete) {
+ $(selector).html('<div align="center" class="ajax_loader"></div>');
+ return $(selector).load(location.href + ' ' + selector + ' > *', complete);
+};
+
+/**
+ * @param {string} selector a jQuery selector (usually an #id)
+ * @param {number} interval The refresh interval in seconds
+ * @param {Function} complete A function to execute when the refresh is done
+ * @return {number} The interval identifier
+ */
+elgg.feed = function(selector, interval, complete) {
+ return setInterval(function() {
+ elgg.refresh(selector, complete);
+ }, interval);
+}; \ No newline at end of file
diff --git a/js/lib/configuration.js b/js/lib/configuration.js
new file mode 100644
index 000000000..8ed326116
--- /dev/null
+++ b/js/lib/configuration.js
@@ -0,0 +1,3 @@
+elgg.provide('elgg.config');
+
+elgg.config.wwwroot = '/'; \ No newline at end of file
diff --git a/js/lib/elgglib.js b/js/lib/elgglib.js
new file mode 100644
index 000000000..32dbb1ec3
--- /dev/null
+++ b/js/lib/elgglib.js
@@ -0,0 +1,187 @@
+/**
+ *
+ *
+ */
+
+/**
+ * @namespace Namespace for elgg javascript functions
+ */
+var elgg = elgg || {};
+
+elgg.assertTypeOf = function(type, param) {
+ if (typeof param !== type) {
+ throw new TypeError("Expecting param to be a(n) " + type + ". Was a(n) " + typeof param + ".");
+ }
+};
+
+/**
+ * Pointer to the global context
+ * {@see elgg.require} and {@see elgg.provide}
+ */
+elgg.global = this;
+
+/**
+ * Throw an error if the required package isn't present
+ *
+ * @param {String} pkg The required package (e.g., 'elgg.package')
+ */
+elgg.require = function(pkg) {
+ elgg.assertTypeOf('string', pkg);
+
+ var parts = pkg.split('.'),
+ cur = elgg.global,
+ part;
+
+ for (var i = 0; i < parts.length; i++) {
+ part = parts[i];
+ cur = cur[part];
+ if(typeof cur == 'undefined') {
+ throw new Error("Missing package: " + pkg);
+ }
+ }
+};
+
+/**
+ * Generate the skeleton for a package.
+ *
+ * <pre>
+ * elgg.provide('elgg.package.subpackage');
+ * </pre>
+ *
+ * is equivalent to
+ *
+ * <pre>
+ * elgg = elgg || {};
+ * elgg.package = elgg.package || {};
+ * elgg.package.subpackage = elgg.package.subpackage || {};
+ * </pre>
+ *
+ * @example elgg.provide('elgg.config.translations')
+ *
+ * @param {string} pkg The package name.
+ */
+elgg.provide = function(pkg) {
+ elgg.assertTypeOf('string', pkg);
+
+ var parts = pkg.split('.'),
+ cur = elgg.global,
+ part;
+
+ for (var i = 0; i < parts.length; i++) {
+ part = parts[i];
+ cur[part] = cur[part] || {};
+ cur = cur[part];
+ }
+};
+
+/**
+ * Inherit the prototype methods from one constructor into another.
+ *
+ * @example
+ * <pre>
+ * function ParentClass(a, b) { }
+ *
+ * ParentClass.prototype.foo = function(a) { alert(a); }
+ *
+ * function ChildClass(a, b, c) {
+ * //equivalent of parent::__construct(a, b); in PHP
+ * ParentClass.call(this, a, b);
+ * }
+ *
+ * elgg.inherit(ChildClass, ParentClass);
+ *
+ * var child = new ChildClass('a', 'b', 'see');
+ * child.foo('boo!'); // alert('boo!');
+ * </pre>
+ *
+ * @param {Function} childCtor Child class.
+ * @param {Function} parentCtor Parent class.
+ */
+elgg.inherit = function(Child, Parent) {
+ Child.prototype = new Parent();
+ Child.prototype.constructor = Child;
+};
+
+/**
+ * Prepend elgg.config.wwwroot to a url if the url doesn't already have it.
+ *
+ * @param {String} url The url to extend
+ * @return {String} The extended url
+ * @private
+ */
+elgg.normalize_url = function(url) {
+ url = url || '';
+ elgg.assertTypeOf('string', url);
+
+ if(/(^(https?:)?\/\/)/.test(url)) {
+ return url;
+ }
+
+ return elgg.config.wwwroot + url;
+};
+
+/**
+ * Displays system messages via javascript rather than php.
+ *
+ * @param {String} msgs The message we want to display
+ * @param {Number} delay The amount of time to display the message in milliseconds. Defaults to 6 seconds.
+ * @param {String} type The type of message (typically 'error' or 'message')
+ * @private
+ */
+elgg.system_messages = function(msgs, delay, type) {
+ if (msgs == undefined) {
+ return;
+ }
+
+ //validate delay. Must be a positive integer.
+ delay = parseInt(delay);
+ if (isNaN(delay) || delay <= 0) {
+ delay = 6000;
+ }
+
+ classes = ['elgg_system_message', 'radius8'];
+ if (type == 'error') {
+ classes.push('messages_error');
+ }
+
+ //Handle non-arrays
+ if (msgs.constructor.toString().indexOf("Array") == -1) {
+ msgs = [msgs];
+ }
+
+ var messages_html = [];f
+
+ for (var i in msgs) {
+ messages_html.push('<div class="' + classes.join(' ') + '"><p>' + msgs[i] + '</p></div>');
+ }
+
+ $(messages_html.join('')).appendTo('#elgg_system_messages').animate({opacity:'1.0'},delay).fadeOut('slow');
+};
+
+/**
+ * Wrapper function for system_messages. Specifies "messages" as the type of message
+ * @param {String} msg The message to display
+ * @param {Number} delay How long to display the message (milliseconds)
+ */
+elgg.system_message = function(msgs, delay) {
+ elgg.system_messages(msgs, delay, "message");
+};
+
+/**
+ * Wrapper function for system_messages. Specifies "errors" as the type of message
+ * @param {String} error The error message to display
+ * @param {Number} delay How long to dispaly the error message (milliseconds)
+ */
+elgg.register_error = function(errors, delay) {
+ elgg.system_messages(errors, delay, "error");
+};
+
+/**
+ * Meant to mimic the php forward() function by simply redirecting the
+ * user to another page.
+ *
+ * @param {String} url The url to forward to
+ */
+elgg.forward = function(url) {
+ location.href = elgg.normalize_url(url);
+}; \ No newline at end of file
diff --git a/js/lib/events.js b/js/lib/events.js
new file mode 100644
index 000000000..358dd6280
--- /dev/null
+++ b/js/lib/events.js
@@ -0,0 +1,65 @@
+elgg.provide('elgg.config.events');
+elgg.provide('elgg.config.events.all');
+elgg.provide('elgg.config.events.all.all');
+
+elgg.register_event_handler = function(event, type, callback, priority) {
+ elgg.assertTypeOf('string', event);
+ elgg.assertTypeOf('string', event);
+ elgg.assertTypeOf('function', callback);
+
+ if (!event || !type) {
+ return false;
+ }
+
+ elgg.provide('elgg.config.events.' + event + '.' + type);
+
+ var events = elgg.config.events;
+
+ if (!(events[event][type] instanceof elgg.ElggPriorityList)) {
+ events[event][type] = new elgg.ElggPriorityList();
+ }
+
+ return events[event][type].insert(callback, priority);
+};
+
+elgg.trigger_event = function(event, type, object) {
+ elgg.assertTypeOf('string', event);
+ elgg.assertTypeOf('string', event);
+
+ elgg.provide('elgg.config.events.' + event + '.' + type);
+ elgg.provide('elgg.config.events.all.' + type);
+ elgg.provide('elgg.config.events.' + event + '.all');
+ elgg.provide('elgg.config.events.all.all');
+
+ var events = elgg.config.events;
+
+ var callEventHandler = function(handler) {
+ return handler(event, type, object) !== false;
+ };
+
+ if (events[event][type] instanceof elgg.ElggPriorityList) {
+ if (!events[event][type].every(callEventHandler)) {
+ return false;
+ }
+ }
+
+ if (events['all'][type] instanceof elgg.ElggPriorityList) {
+ if (!events['all'][type].every(callEventHandler)) {
+ return false;
+ }
+ }
+
+ if (events[event]['all'] instanceof elgg.ElggPriorityList) {
+ if (!events[event]['all'].every(callEventHandler)) {
+ return false;
+ }
+ }
+
+ if (events['all']['all'] instanceof elgg.ElggPriorityList) {
+ if (!events['all']['all'].every(callEventHandler)) {
+ return false;
+ }
+ }
+
+ return true;
+}; \ No newline at end of file
diff --git a/js/lib/languages.js b/js/lib/languages.js
new file mode 100644
index 000000000..3231cf77d
--- /dev/null
+++ b/js/lib/languages.js
@@ -0,0 +1,83 @@
+/**
+ * Provides language-related functionality
+ */
+elgg.provide('elgg.config.translations');
+
+elgg.config.language = 'en';
+
+elgg.add_translation = function(lang, translations) {
+ elgg.provide('elgg.config.translations.' + lang);
+
+ $.extend(elgg.config.translations[lang], translations);
+}
+
+/**
+ * Load the translations for the given language.
+ *
+ * If no language is specified, the default language is used.
+ * @param {string} language
+ * @return {XMLHttpRequest}
+ */
+elgg.reload_all_translations = function(language) {
+ var lang = language || elgg.get_language();
+ elgg.getJSON('_css/js.php', {
+ data: {
+ 'js': 'languages/'+lang,
+ 'viewtype': 'default',
+ 'lastcache': elgg.config.lastcache
+ },
+ success: function(json) {
+ elgg.add_translation(lang, json);
+ }
+ });
+};
+
+/**
+ * Get the current language
+ * @return {String}
+ */
+elgg.get_language = function() {
+ var user = elgg.get_loggedin_user();
+
+ if (user && user.language) {
+ return user.language;
+ }
+
+ return elgg.config.language;
+};
+
+/**
+ * Translates a string
+ *
+ * @param {String} key The string to translate
+ * @param {String} language The language to display it in
+ * @return {String} The translation
+ */
+elgg.echo = function(key, language) {
+ var translations,
+ dlang = elgg.get_language();
+
+ language = language || dlang;
+
+ translations = elgg.config.translations[language];
+ if (translations && translations[key]) {
+ return translations[key];
+ }
+
+ if (language == dlang) {
+ return undefined;
+ }
+
+ translations = elgg.config.translations[dlang];
+ if (translations && translations[key]) {
+ return translations[key];
+ }
+
+ return undefined;
+};
+
+elgg.config.translations.init = function() {
+ elgg.reload_all_translations();
+};
+
+elgg.register_event_handler('boot', 'system', elgg.config.translations.init); \ No newline at end of file
diff --git a/js/lib/security.js b/js/lib/security.js
new file mode 100644
index 000000000..bdd762560
--- /dev/null
+++ b/js/lib/security.js
@@ -0,0 +1,70 @@
+/**
+ * Hold security-related data here
+ */
+elgg.provide('elgg.security');
+
+elgg.security.token = {};
+
+elgg.security.setToken = function(json) {
+ //update the convenience object
+ elgg.security.token = json;
+
+ //also update all forms
+ $('[name=__elgg_ts]').val(json.__elgg_ts);
+ $('[name=__elgg_token]').val(json.__elgg_token);
+
+ //also update all links
+ $('[href]').each(function() {
+ this.href = this.href
+ .replace(/__elgg_ts=\d*/, '__elgg_ts=' + json.__elgg_ts)
+ .replace(/__elgg_token=[0-9a-f]*/, '__elgg_token=' + json.__elgg_token);
+ });
+};
+
+/**
+ * Security tokens time out, so lets refresh those every so often
+ * @todo handle error and bad return data
+ */
+elgg.security.refreshToken = function() {
+ elgg.action('ajax/securitytoken', function(data) {
+ elgg.security.setToken(data.output);
+ });
+};
+
+
+/**
+ * Add elgg action tokens to an object or string (assumed to be url data)
+ *
+ * @param {Object|string} data
+ * @return {Object} The new data object including action tokens
+ * @private
+ */
+elgg.security.addToken = function(data) {
+
+ //addToken('data=sofar')
+ if (typeof data == 'string') {
+ var args = [];
+ if(data) {
+ args.push(data);
+ }
+ args.push("__elgg_ts=" + elgg.security.token.__elgg_ts);
+ args.push("__elgg_token=" + elgg.security.token.__elgg_token)
+
+ return args.join('&');
+ }
+
+ //addToken({...})
+ if (typeof data == 'object' || typeof data == 'undefined') {
+ return $.extend(data, elgg.security.token);
+ }
+
+ //addToken(???)
+ throw new TypeError("elgg.security.addToken not implemented for " + (typeof data) + "s");
+};
+
+elgg.security.init = function() {
+ //refresh security token every 5 minutes
+ setInterval(elgg.security.refreshToken, elgg.security.interval);
+};
+
+elgg.register_event_handler('boot', 'system', elgg.security.init); \ No newline at end of file
diff --git a/js/lib/session.js b/js/lib/session.js
new file mode 100644
index 000000000..227c607eb
--- /dev/null
+++ b/js/lib/session.js
@@ -0,0 +1,116 @@
+/**
+ * @todo comment
+ */
+elgg.provide('elgg.session');
+
+/**
+ * Helper function for setting cookies
+ * @param {string} name
+ * @param {string} value
+ * @param {Object} options
+ * {number|Date} options[expires]
+ * {string} options[path]
+ * {string} options[domain]
+ * {boolean} options[secure]
+ *
+ * @return {string} The value of the cookie, if only name is specified
+ */
+elgg.session.cookie = function(name, value, options) {
+ //elgg.session.cookie()
+ if(typeof name == 'undefined') {
+ return document.cookie;
+ }
+
+ //elgg.session.cookie(name)
+ if (typeof value == 'undefined') {
+ if (document.cookie && document.cookie != '') {
+ var cookies = document.cookie.split(';');
+ for (var i = 0; i < cookies.length; i++) {
+ var cookie = jQuery.trim(cookies[i]).split('=');
+ if (cookie[0] == name) {
+ return decodeURIComponent(cookie[1]);
+ }
+ }
+ }
+ return undefined;
+ }
+
+ // elgg.session.cookie(name, value[, opts])
+ var cookies = [];
+
+ options = options || {};
+
+ if (value === null) {
+ value = '';
+ options.expires = -1;
+ }
+
+ cookies.push(name + '=' + value);
+
+ if (typeof options.expires == 'number') {
+ var date, valid = true;
+
+ if (typeof options.expires == 'number') {
+ date = new Date();
+ date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
+ } else if(options.expires.toUTCString) {
+ date = options.expires;
+ } else {
+ valid = false;
+ }
+
+ valid ? cookies.push('expires=' + date.toUTCString()) : 0;
+ }
+
+ // CAUTION: Needed to parenthesize options.path and options.domain
+ // in the following expressions, otherwise they evaluate to undefined
+ // in the packed version for some reason.
+ if (options.path) {
+ cookies.push('path=' + (options.path));
+ }
+
+ if (options.domain) {
+ cookies.push('domain=' + (options.domain));
+ }
+
+ if (options.secure) {
+ cookies.push('secure');
+ }
+
+ document.cookie = cookies.join('; ');
+};
+
+/**
+ * @return {ElggUser} The logged in user
+ */
+elgg.get_loggedin_user = function() {
+ return elgg.session.user;
+};
+
+/**
+ * @return {number} The GUID of the logged in user
+ */
+elgg.get_loggedin_userid = function() {
+ var user = elgg.get_loggedin_user();
+ return user ? user.guid : 0;
+};
+
+/**
+ * @return {boolean} Whether there is a user logged in
+ */
+elgg.isloggedin = function() {
+ return (elgg.get_loggedin_user() instanceof elgg.ElggUser);
+};
+
+/**
+ * @return {boolean} Whether there is an admin logged in
+ */
+elgg.isadminloggedin = function() {
+ var user = elgg.get_loggedin_user();
+ return (user instanceof ElggUser) && user.isAdmin();
+};
+
+/**
+ * @deprecated Use elgg.session.cookie instead
+ */
+$.cookie = elgg.session.cookie; \ No newline at end of file
diff --git a/js/lib/ui.js b/js/lib/ui.js
new file mode 100644
index 000000000..4a9c64e70
--- /dev/null
+++ b/js/lib/ui.js
@@ -0,0 +1,121 @@
+elgg.provide('elgg.ui');
+
+elgg.ui.init = function () {
+ //if the user clicks a system message, make it disappear
+ $('.elgg_system_message').live('click', function() {
+ $(this).stop().fadeOut('fast');
+ });
+
+ $('a.collapsibleboxlink').click(elgg.ui.toggleCollapsibleBox);
+
+ // set-up hover class for dragged widgets
+ var cols = [
+ "#rightcolumn_widgets",
+ "#middlecolumn_widgets",
+ "#leftcolumn_widgets"
+ ].join(',');
+
+ $(cols).droppable({
+ accept: ".draggable_widget",
+ hoverClass: 'droppable-hover'
+ });
+};
+
+// reusable generic hidden panel
+elgg.ui.toggleCollapsibleBox = function () {
+ $(this.parentNode.parentNode).children(".collapsible_box").slideToggle("fast");
+ return false;
+};
+
+//define some helper jquery plugins
+(function($) {
+
+ // ELGG TOOLBAR MENU
+ $.fn.elgg_topbardropdownmenu = function(options) {
+ var defaults = {
+ speed: 350
+ };
+
+ options = $.extend(defaults, options || {});
+
+ this.each(function() {
+
+ var root = this, zIndex = 5000;
+
+ function getSubnav(ele) {
+ if (ele.nodeName.toLowerCase() == 'li') {
+ var subnav = $('> ul', ele);
+ return subnav.length ? subnav[0] : null;
+ } else {
+ return ele;
+ }
+ }
+
+ function getActuator(ele) {
+ if (ele.nodeName.toLowerCase() == 'ul') {
+ return $(ele).parents('li')[0];
+ } else {
+ return ele;
+ }
+ }
+
+ function hide() {
+ var subnav = getSubnav(this);
+ if (!subnav) {
+ return;
+ }
+
+ $.data(subnav, 'cancelHide', false);
+ setTimeout(function() {
+ if (!$.data(subnav, 'cancelHide')) {
+ $(subnav).slideUp(100);
+ }
+ }, 250);
+ }
+
+ function show() {
+ var subnav = getSubnav(this);
+ if (!subnav) {
+ return;
+ }
+
+ $.data(subnav, 'cancelHide', true);
+
+ $(subnav).css({zIndex: zIndex++}).slideDown(options.speed);
+
+ if (this.nodeName.toLowerCase() == 'ul') {
+ var li = getActuator(this);
+ $(li).addClass('hover');
+ $('> a', li).addClass('hover');
+ }
+ }
+
+ $('ul, li', this).hover(show, hide);
+ $('li', this).hover(
+ function() { $(this).addClass('hover'); $('> a', this).addClass('hover'); },
+ function() { $(this).removeClass('hover'); $('> a', this).removeClass('hover'); }
+ );
+
+ });
+ };
+
+ //Make delimited list
+ $.fn.makeDelimitedList = function(elementAttribute) {
+
+ var delimitedListArray = [];
+ var listDelimiter = "::";
+
+ // Loop over each element in the stack and add the elementAttribute to the array
+ this.each(function(e) {
+ var listElement = $(this);
+ // Add the attribute value to our values array
+ delimitedListArray[delimitedListArray.length] = listElement.attr(elementAttribute);
+ }
+ );
+
+ // Return value list by joining the array
+ return(delimitedListArray.join(listDelimiter));
+ };
+})(jQuery);
+
+elgg.register_event_handler('init', 'system', elgg.ui.init); \ No newline at end of file
diff --git a/js/lib/ui.widgets.js b/js/lib/ui.widgets.js
new file mode 100644
index 000000000..1e3163709
--- /dev/null
+++ b/js/lib/ui.widgets.js
@@ -0,0 +1,131 @@
+elgg.provide('elgg.ui.widgets');
+
+elgg.ui.widgets.init = function() {
+ // COLLAPSABLE WIDGETS (on Dashboard & Profile pages)
+ $('a.toggle_box_contents').live('click', elgg.ui.widgets.toggleContent);
+ $('a.toggle_box_edit_panel').live('click', elgg.ui.widgets.toggleEditPanel);
+ $('a.toggle_customise_edit_panel').live('click', elgg.ui.widgets.toggleCustomizeEditPanel);
+
+ // WIDGET GALLERY EDIT PANEL
+ // Sortable widgets
+ var els = [
+ '#leftcolumn_widgets',
+ '#middlecolumn_widgets',
+ '#rightcolumn_widgets',
+ '#widget_picker_gallery'
+ ].join(',');
+
+ $(els).sortable({
+ items: '.draggable_widget',
+ handle: '.drag_handle',
+ forcePlaceholderSize: true,
+ placeholder: 'ui-state-highlight',
+ cursor: 'move',
+ opacity: 0.9,
+ appendTo: 'body',
+ connectWith: els,
+ stop: function(e,ui) {
+ // refresh list before updating hidden fields with new widget order
+ $(this).sortable("refresh");
+
+ var widgetNamesLeft = outputWidgetList('#leftcolumn_widgets');
+ var widgetNamesMiddle = outputWidgetList('#middlecolumn_widgets');
+ var widgetNamesRight = outputWidgetList('#rightcolumn_widgets');
+
+ $('#debugField1').val(widgetNamesLeft);
+ $('#debugField2').val(widgetNamesMiddle);
+ $('#debugField3').val(widgetNamesRight);
+ }
+ });
+
+ // bind more info buttons - called when new widgets are created
+ elgg.ui.widgets.moreinfo();
+};
+
+//List active widgets for each page column
+elgg.ui.widgets.outputList = function(forElement) {
+ return( $("input[name='handler'], input[name='guid']", forElement ).makeDelimitedList("value") );
+};
+
+//Read each widgets collapsed/expanded state from cookie and apply
+elgg.ui.widgets.state = function(forWidget) {
+
+ var thisWidgetState = elgg.session.cookie(forWidget);
+
+ if (thisWidgetState == 'collapsed') {
+ forWidget = "#" + forWidget;
+ $(forWidget).find("div.collapsable_box_content").hide();
+ $(forWidget).find("a.toggle_box_contents").html('+');
+ $(forWidget).find("a.toggle_box_edit_panel").fadeOut('medium');
+ }
+};
+
+//More info tooltip in widget gallery edit panel
+elgg.ui.widgets.moreinfo = function() {
+ $("img.more_info").hover(function(e) {
+ var widgetdescription = $("input[name='description']", this.parentNode.parentNode.parentNode).val();
+ $("body").append("<p id='widget_moreinfo'><b>"+ widgetdescription +" </b></p>");
+
+ if (e.pageX < 900) {
+ $("#widget_moreinfo")
+ .css("top",(e.pageY + 10) + "px")
+ .css("left",(e.pageX + 10) + "px")
+ .fadeIn("medium");
+ } else {
+ $("#widget_moreinfo")
+ .css("top",(e.pageY + 10) + "px")
+ .css("left",(e.pageX - 210) + "px")
+ .fadeIn("medium");
+ }
+ }, function() {
+ $("#widget_moreinfo").remove();
+ });
+};
+
+//Toggle widgets contents and save to a cookie
+elgg.ui.widgets.toggleContent = function(e) {
+ var targetContent = $('div.collapsable_box_content', this.parentNode.parentNode);
+ if (targetContent.css('display') == 'none') {
+ targetContent.slideDown(400);
+ $(this).html('-');
+ $(this.parentNode).children(".toggle_box_edit_panel").fadeIn('medium');
+
+ // set cookie for widget panel open-state
+ var thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
+ $.cookie(thisWidgetName, 'expanded', { expires: 365 });
+
+ } else {
+ targetContent.slideUp(400);
+ $(this).html('+');
+ $(this.parentNode).children(".toggle_box_edit_panel").fadeOut('medium');
+ // make sure edit pane is closed
+ $(this.parentNode.parentNode).children(".collapsable_box_editpanel").hide();
+
+ // set cookie for widget panel closed-state
+ var thisWidgetName = $(this.parentNode.parentNode.parentNode).attr('id');
+ $.cookie(thisWidgetName, 'collapsed', { expires: 365 });
+ }
+ return false;
+};
+
+// toggle widget box edit panel
+elgg.ui.widgets.toggleEditPanel = function () {
+ $(this.parentNode.parentNode).children(".collapsable_box_editpanel").slideToggle("fast");
+ return false;
+};
+
+// toggle customise edit panel
+elgg.ui.widgets.toggleCustomizeEditPanel = function () {
+ $('#customise_editpanel').slideToggle("fast");
+ return false;
+};
+
+/**
+ * @deprecated Use elgg.ui.widgets.*
+ */
+var toggleContent = elgg.ui.widgets.toggleContent,
+ widget_moreinfo = elgg.ui.widgets.moreinfo,
+ widget_state = elgg.ui.widgets.state,
+ outputWidgetList = elgg.ui.widgets.outputList;
+
+elgg.register_event_handler('init', 'system', elgg.ui.widgets.init); \ No newline at end of file