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;
56 function extract_metadata(data) {
57 if (!metadata_extracted) {
58 var m = data._elgg_msgs;
60 elgg.register_error(m.error);
61 error_displayed =
true;
66 m && m.success && elgg.system_message(m.success);
67 delete data._elgg_msgs;
69 var deps = data._elgg_deps;
70 deps && deps.length && Ajax._require(deps);
71 delete data._elgg_deps;
73 metadata_extracted =
true;
87 hook_type = hook_type ||
'';
90 throw new Error(
'options must be a plain object with key "url"');
92 if (!options.url && hook_type !==
'path:') {
93 throw new Error(
'options must be a plain object with key "url"');
97 if (options.data === undefined) {
101 options.data = options.data || {};
103 if (typeof options.data !==
'string') {
104 throw new Error(
'if defined, options.data must be a plain object or string');
108 options.dataType =
'json';
111 orig_options = $.extend({}, options);
116 if (hook_type && typeof options.data !==
'string') {
117 options.data = elgg.trigger_hook(Ajax.REQUEST_DATA_HOOK, hook_type, params, options.data);
122 if (!options.method) {
123 options.method =
'GET';
124 if (options.data && !$.isEmptyObject(options.data)) {
125 options.method =
'POST';
130 options.beforeSend =
function () {
131 orig_options.beforeSend && orig_options.beforeSend.apply(null, arguments);
135 options.complete =
function () {
137 if (spinner_starts < 1) {
140 orig_options.complete && orig_options.complete.apply(null, arguments);
144 if (!options.error) {
145 options.error =
function (jqXHR, textStatus, errorThrown) {
146 if (!jqXHR.getAllResponseHeaders()) {
152 var data = $.parseJSON(jqXHR.responseText);
154 extract_metadata(data);
157 if (window.console) {
158 console.warn(e.message);
162 if (!error_displayed) {
163 elgg.register_error(elgg.echo(
'ajax:error'));
168 options.dataFilter =
function (data,
type) {
169 if (
type !==
'json') {
173 data = $.parseJSON(data);
175 extract_metadata(data);
178 options: orig_options
181 data = elgg.trigger_hook(Ajax.RESPONSE_DATA_HOOK, hook_type, params, data);
184 jqXHR.AjaxData = data;
186 return JSON.stringify(data.value);
189 options.url = elgg.normalize_url(options.url);
191 'X-Elgg-Ajax-API':
'2' 199 that._ajax_options = options;
201 jqXHR = $.ajax(options);
215 this.
path =
function (
path, options) {
216 elgg.assertTypeOf(
'string',
path);
219 if (
path.indexOf(site_url) === 0) {
220 path =
path.substr(site_url.length);
222 path =
path.replace(fragment_pattern,
'');
226 options = options || {};
230 path =
path.replace(query_pattern,
'').replace(slashes_pattern,
'');
232 return fetch(options,
'path:' +
path);
244 this.
view =
function (
view, options) {
245 elgg.assertTypeOf(
'string',
view);
247 throw new Error(
'view cannot be empty');
252 options = options || {};
253 options.url =
'ajax/view/' +
view;
254 options.method = options.method ||
'GET';
257 view =
view.replace(query_pattern,
'').replace(slashes_pattern,
'');
259 return fetch(options,
'view:' +
view);
272 elgg.assertTypeOf(
'string',
action);
274 throw new Error(
'action cannot be empty');
277 action =
action.replace(leading_slash_pattern,
'').replace(fragment_pattern,
'');
281 options = options || {};
282 options.url =
'ajax/form/' +
action;
283 options.method = options.method ||
'GET';
286 action =
action.replace(query_pattern,
'').replace(slashes_pattern,
'');
288 return fetch(options,
'form:' +
action);
301 elgg.assertTypeOf(
'string',
action);
303 throw new Error(
'action cannot be empty');
307 if (
action.indexOf(action_base) === 0) {
310 action =
action.replace(leading_slash_pattern,
'').replace(fragment_pattern,
'');
314 options = options || {};
315 options.data = options.data || {};
318 var m =
action.match(/\?(.+)$/);
319 if (m && /(^|&)__elgg_ts=/.test(m[1])) {
322 options.data = elgg.security.addToken(options.data);
325 options.method = options.method ||
'POST';
326 options.url =
'action/' +
action;
329 action =
action.replace(query_pattern,
'').replace(slashes_pattern,
'');
331 return fetch(options,
'action:' +
action);
341 this.objectify =
function (el) {
344 var
a = $(el).serializeArray();
346 $.each(a,
function() {
347 if (o[this.
name] !== undefined) {
348 if (!o[this.
name].push) {
351 o[this.
name].push(this.value ||
'');
353 o[this.
name] = this.value ||
'';
366 function assertNotUrl(arg) {
367 if (/^https?:/.test(arg)) {
368 throw new Error(
'elgg/Ajax cannot be used with external URLs');
376 Ajax.REQUEST_DATA_HOOK =
'ajax_request_data';
384 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())
$CONFIG path
The full path where Elgg is installed.
elgg require
Throw an error if the required package isn't present.
define(function(require){var $=require('jquery');$(document).on('change', '#elgg-river-selector', function(){var url=window.location.href;if(window.location.search.length){url=url.substring(0, url.indexOf('?'));}url+= '?'+$(this).val();window.location.href=url;});})
Initiates page reload when river selector value changes core/river/filter.