4 var spinner =
require(
'elgg/spinner');
6 var site_url = elgg.get_site_url(),
7 action_base = site_url +
'action/',
8 fragment_pattern = /#.*$/,
9 query_pattern = /\?.*$/,
10 leading_slash_pattern = /^\
11 slashes_pattern = /(^\/|\/$)/g;
20 function Ajax(use_spinner) {
22 use_spinner = elgg.isNullOrUndefined(use_spinner) ?
true : !!use_spinner;
25 var spinner_starts = 0;
44 function fetch(options, hook_type) {
48 metadata_extracted =
false,
49 error_displayed =
false;
57 function extract_metadata(data, status_code) {
59 status_code = status_code || 200;
61 if (!metadata_extracted) {
62 var m = data._elgg_msgs;
68 elgg.register_error(data.error);
69 error_displayed =
true;
72 if (data.error || status_code !== 200) {
78 m && m.success && elgg.system_message(m.success);
79 delete data._elgg_msgs;
81 var deps = data._elgg_deps;
82 deps && deps.length && Ajax._require(deps);
83 delete data._elgg_deps;
85 metadata_extracted =
true;
99 hook_type = hook_type ||
'';
102 throw new Error(
'options must be a plain object with key "url"');
104 if (!options.url && hook_type !==
'path:') {
105 throw new Error(
'options must be a plain object with key "url"');
109 if (options.data === undefined) {
113 options.data = options.data || {};
114 }
else if (options.data instanceof FormData) {
115 options.processData =
false;
116 options.contentType =
false;
118 if (typeof options.data !==
'string') {
119 throw new Error(
'if defined, options.data must be a plain object or string');
123 options.dataType =
'json';
126 orig_options = $.extend({}, options);
131 if (hook_type && typeof options.data !==
'string') {
132 options.data = elgg.trigger_hook(Ajax.REQUEST_DATA_HOOK, hook_type, params, options.data);
137 if (!options.method) {
138 options.method =
'GET';
139 if (options.data && !$.isEmptyObject(options.data)) {
140 options.method =
'POST';
145 options.beforeSend =
function () {
146 orig_options.beforeSend && orig_options.beforeSend.apply(null, arguments);
150 options.complete =
function () {
152 if (spinner_starts < 1) {
155 orig_options.complete && orig_options.complete.apply(null, arguments);
159 if (!options.error) {
160 options.error =
function (jqXHR, textStatus, errorThrown) {
161 if (!jqXHR.getAllResponseHeaders()) {
167 var data = $.parseJSON(jqXHR.responseText);
169 extract_metadata(data, jqXHR.status);
172 if (window.console) {
173 console.warn(e.message);
177 if (!error_displayed) {
178 elgg.register_error(elgg.echo(
'ajax:error'));
183 options.dataFilter =
function (data, type) {
184 if (type !==
'json') {
188 data = $.parseJSON(data);
190 extract_metadata(data, 200);
193 options: orig_options
196 data = elgg.trigger_hook(Ajax.RESPONSE_DATA_HOOK, hook_type, params, data);
199 jqXHR.AjaxData = data;
201 return JSON.stringify(data.value);
204 options.url = elgg.normalize_url(options.url);
206 'X-Elgg-Ajax-API':
'2' 214 that._ajax_options = options;
216 jqXHR = $.ajax(options);
230 this.
path =
function (
path, options) {
231 elgg.assertTypeOf(
'string',
path);
234 if (
path.indexOf(site_url) === 0) {
235 path =
path.substr(site_url.length);
237 path =
path.replace(fragment_pattern,
'');
241 options = options || {};
245 path =
path.replace(query_pattern,
'').replace(slashes_pattern,
'');
247 return fetch(options,
'path:' +
path);
259 this.
view =
function (
view, options) {
260 elgg.assertTypeOf(
'string',
view);
262 throw new Error(
'view cannot be empty');
267 options = options || {};
268 options.url =
'ajax/view/' +
view;
269 options.method = options.method ||
'GET';
272 view =
view.replace(query_pattern,
'').replace(slashes_pattern,
'');
274 return fetch(options,
'view:' +
view);
286 this.form =
function (
action, options) {
287 elgg.assertTypeOf(
'string',
action);
289 throw new Error(
'action cannot be empty');
292 action =
action.replace(leading_slash_pattern,
'').replace(fragment_pattern,
'');
296 options = options || {};
297 options.url =
'ajax/form/' +
action;
298 options.method = options.method ||
'GET';
301 action =
action.replace(query_pattern,
'').replace(slashes_pattern,
'');
303 return fetch(options,
'form:' +
action);
316 elgg.assertTypeOf(
'string',
action);
318 throw new Error(
'action cannot be empty');
322 if (
action.indexOf(action_base) === 0) {
325 action =
action.replace(leading_slash_pattern,
'').replace(fragment_pattern,
'');
329 options = options || {};
330 options.data = options.data || {};
333 var m =
action.match(/\?(.+)$/);
334 if (m && /(^|&)__elgg_ts=/.test(m[1])) {
337 options.data = elgg.security.addToken(options.data);
340 options.method = options.method ||
'POST';
341 options.url =
'action/' +
action;
344 action =
action.replace(query_pattern,
'').replace(slashes_pattern,
'');
346 return fetch(options,
'action:' +
action);
355 this.objectify =
function (el) {
361 $(el).trigger(
'elgg-ajax-objectify');
363 return new FormData($(el)[0]);
372 this.
forward =
function(destination) {
375 elgg.forward(destination);
384 function assertNotUrl(arg) {
385 if (/^https?:/.test(arg)) {
386 throw new Error(
'elgg/Ajax cannot be used with external URLs');
394 Ajax.REQUEST_DATA_HOOK =
'ajax_request_data';
402 Ajax.RESPONSE_DATA_HOOK =
'ajax_response_data';
$CONFIG view
The current view type.
elgg isPlainObject
Check if the value is a "plain" object (i.e., created by {} or new Object())
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
$CONFIG path
Legacy documentation for the old $CONFIG object.
elgg require
Throw an error if the required package isn't present.
define(function(require){var $=require('jquery');var elgg=require('elgg');var spinner=require('elgg/spinner');var Ajax=require('elgg/Ajax');var ajax=new Ajax();function init(){initPluginReordering();$(document).on('click', '.elgg-admin-plugins-categories a', filterPluginCategory);$(document).on('click', '.elgg-plugins-toggle', toggleAllPlugins);$(document).on('click', '.elgg-plugin-state-change', toggleSinglePlugin);$(document).on('mouseenter', '.elgg-plugin-details-screenshots.elgg-plugin-screenshot', showPluginScreenshot);};function freezePlugins(){$('#elgg-plugin-list-cover').css('display', 'block');};function unfreezePlugins(){$('#elgg-plugin-list-cover').css('display', 'none');};function initPluginReordering(){$('#elgg-plugin-list > ul').sortable({items: 'li:has(>.elgg-state-draggable)', handle: '.elgg-body', forcePlaceholderSize:true, placeholder: 'elgg-plugin-placeholder', opacity:0.8, revert:500, stop:movePlugin});};function toggleSinglePlugin(e){freezePlugins();e.preventDefault();ajax.action(this.href).done(function(output, statusText, jqXHR){if(jqXHR.AjaxData.status==-1){location.reload();return;}ajax.path('admin_plugins_refresh').done(function(output){$('#elgg-plugin-list').html(output.list);$('.elgg-sidebar').html(output.sidebar);$(".elgg-admin-plugins-categories > li.elgg-state-selected > a").trigger('click');initPluginReordering();unfreezePlugins();});});};function toggleAllPlugins(e){e.preventDefault();if(!confirm(elgg.echo('question:areyousure'))){return;}freezePlugins();var guids=[], state=$(this).data('desiredState'), find_state=state== 'active'? 'inactive': 'active';$('.elgg-plugin.elgg-state-'+find_state+ ':visible').each(function(){var guid=$(this).data('guid');if(guid){guids.push(guid);}});if(!guids.length){elgg.register_error(elgg.echo('admin:plugins:already:'+state));unfreezePlugins();return;}spinner.start();var $form=$('< form method="post"/>');$form.prop('action', elgg.security.addToken(this.href));$form.append('< input type="hidden"name="guids"value="' + guids.join(',') + '"/>');$form.appendTo("body").submit();};function movePlugin(e, ui){freezePlugins();var pluginGuid=ui.item.attr('id');pluginGuid=pluginGuid.replace('elgg-object-', '');elgg.action('admin/plugins/set_priority',{data:{plugin_guid:pluginGuid, priority:ui.item.index()+1}, success:function(){var priorityDep=new RegExp(elgg.echo('ElggPlugin:Dependencies:Priority'));ui.item.siblings().andSelf().each(function(){if(priorityDep.test($(this).find('.elgg-dependency-requires').text())){updatePluginView($(this));}});unfreezePlugins();}});};function updatePluginView(pluginView){var pluginGuid=pluginView.attr('id');pluginGuid=pluginGuid.replace('elgg-object-', '');elgg.get({url:elgg.config.wwwroot+"ajax/view/object/plugin/full", dataType:"html", cache:false, data:{guid:pluginGuid, display_reordering:true}, success:function(htmlData){if(htmlData.length > 0){pluginView.html(htmlData);}}});};function filterPluginCategory(e){e.preventDefault();$(".elgg-admin-plugins-categories > li").removeClass("elgg-state-selected");$(".elgg-plugin").hide();$(".elgg-plugin-category-"+$(this).attr("rel")).show();$(this).closest('li').addClass("elgg-state-selected");};function showPluginScreenshot(){$(this).parent().find(".elgg-plugin-screenshot").removeClass("elgg-state-selected");$(this).addClass("elgg-state-selected");$(".elgg-plugin-details-screenshots > div > img").hide();$(".elgg-plugin-details-screenshots > div > img[rel='"+$(this).attr("rel")+"']").show();};init();})