Elgg  Version 3.0
elgglib.js
Go to the documentation of this file.
1 
4 var elgg = elgg || {};
5 
12 elgg.global = this;
13 
19 elgg.ACCESS_PRIVATE = 0;
20 
26 elgg.nullFunction = function() {};
27 
35 elgg.abstractMethod = function() {
36  throw new Error("Oops... you forgot to implement an abstract method!");
37 };
38 
42 elgg.extend = jQuery.extend;
43 
53 elgg.isArray = jQuery.isArray;
54 
64 elgg.isFunction = jQuery.isFunction;
65 
75 elgg.isPlainObject = jQuery.isPlainObject;
76 
84 elgg.isString = function(val) {
85  return typeof val === 'string';
86 };
87 
95 elgg.isNumber = function(val) {
96  return typeof val === 'number';
97 };
98 
109 elgg.isObject = function(val) {
110  return typeof val === 'object';
111 };
112 
120 elgg.isUndefined = function(val) {
121  return val === undefined;
122 };
123 
131 elgg.isNull = function(val) {
132  return val === null;
133 };
134 
142 elgg.isNullOrUndefined = function(val) {
143  return val == null;
144 };
145 
151 elgg.assertTypeOf = function(type, val) {
152  if (typeof val !== type) {
153  throw new TypeError("Expecting param of " +
154  arguments.caller + "to be a(n) " + type + "." +
155  " Was actually a(n) " + typeof val + ".");
156  }
157 };
158 
164 elgg.require = function(pkg) {
165  elgg.assertTypeOf('string', pkg);
166 
167  var parts = pkg.split('.'),
168  cur = elgg.global,
169  part, i;
170 
171  for (i = 0; i < parts.length; i += 1) {
172  part = parts[i];
173  cur = cur[part];
174  if (elgg.isUndefined(cur)) {
175  throw new Error("Missing package: " + pkg);
176  }
177  }
178 };
179 
212 elgg.provide = function(pkg, opt_context) {
213  var parts,
214  context = opt_context || elgg.global,
215  part, i;
216 
217  if (elgg.isArray(pkg)) {
218  parts = pkg;
219  } else {
220  elgg.assertTypeOf('string', pkg);
221  parts = pkg.split('.');
222  }
223 
224  for (i = 0; i < parts.length; i += 1) {
225  part = parts[i];
226  context[part] = context[part] || {};
227  context = context[part];
228  }
229 };
230 
254 elgg.inherit = function(Child, Parent) {
255  Child.prototype = new Parent();
256  Child.prototype.constructor = Child;
257 };
258 
272 elgg.normalize_url = function(url) {
273  url = url || '';
274  elgg.assertTypeOf('string', url);
275 
276  function validate(url) {
277  url = elgg.parse_url(url);
278  if (url.scheme) {
279  url.scheme = url.scheme.toLowerCase();
280  }
281  if (url.scheme == 'http' || url.scheme == 'https') {
282  if (!url.host) {
283  return false;
284  }
285  /* hostname labels may contain only alphanumeric characters, dots and hypens. */
286  if (!(new RegExp("^([a-zA-Z0-9][a-zA-Z0-9\\-\\.]*)$", "i")).test(url.host) || url.host.charAt(-1) == '.') {
287  return false;
288  }
289  }
290  /* some schemas allow the host to be empty */
291  if (!url.scheme || !url.host && url.scheme != 'mailto' && url.scheme != 'news' && url.scheme != 'file') {
292  return false;
293  }
294  return true;
295  };
296 
297  // ignore anything with a recognized scheme
298  if (url.indexOf('http:') === 0 || url.indexOf('https:') === 0 || url.indexOf('javascript:') === 0 || url.indexOf('mailto:') === 0 ) {
299  return url;
300  } else if (validate(url)) {
301  // all normal URLs including mailto:
302  return url;
303  } else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
304  // '//example.com' (Shortcut for protocol.)
305  // '?query=test', #target
306  return url;
307  } else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
308  // watch those double escapes in JS.
309  // 'install.php', 'install.php?step=step'
310  return elgg.config.wwwroot + url.ltrim('/');
311  } else if ((new RegExp("^[^/]*\\.", "i")).test(url)) {
312  // 'example.com', 'example.com/subpage'
313  return 'http://' + url;
314  } else {
315  // 'page/handler', 'mod/plugin/file.php'
316  // trim off any leading / because the site URL is stored
317  // with a trailing /
318  return elgg.config.wwwroot + url.ltrim('/');
319  }
320 };
321 
330 elgg.system_messages = function(msgs, delay, type) {
331  if (elgg.isUndefined(msgs)) {
332  return;
333  }
334 
335  var classes = ['elgg-message'],
336  messages_html = [],
337  appendMessage = function(msg) {
338  messages_html.push('<li><div class="' + classes.join(' ') + '"><div class="elgg-inner"><div class="elgg-body">' + msg + '</div></div></div></li>');
339  },
340  systemMessages = $('ul.elgg-system-messages'),
341  i;
342 
343  //validate delay. Must be a positive integer.
344  delay = parseInt(delay || 6000, 10);
345  if (isNaN(delay) || delay <= 0) {
346  delay = 6000;
347  }
348 
349  //Handle non-arrays
350  if (!elgg.isArray(msgs)) {
351  msgs = [msgs];
352  }
353 
354  if (type === 'error') {
355  classes.push('elgg-message-error');
356  } else {
357  classes.push('elgg-message-success');
358  }
359 
360  msgs.forEach(appendMessage);
361 
362  if (type != 'error') {
363  $(messages_html.join('')).appendTo(systemMessages)
364  .animate({opacity: '1.0'}, delay).fadeOut('slow');
365  } else {
366  $(messages_html.join('')).appendTo(systemMessages);
367  }
368 };
369 
373 elgg.clear_system_messages = function() {
374  $('ul.elgg-system-messages').empty();
375 };
376 
382 elgg.system_message = function(msgs, delay) {
383  elgg.system_messages(msgs, delay, "message");
384 };
385 
391 elgg.register_error = function(errors, delay) {
392  elgg.system_messages(errors, delay, "error");
393 };
394 
402 elgg.deprecated_notice = function(msg, dep_version) {
403  if (elgg.is_admin_logged_in()) {
404  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
405  if (typeof console !== "undefined") {
406  console.info(msg);
407  }
408  }
409 };
410 
417 elgg.forward = function(url) {
418  var dest = elgg.normalize_url(url);
419 
420  if (dest == location.href) {
421  location.reload();
422  }
423 
424  // in case the href set below just changes the hash, we want to reload. There's sadly
425  // no way to force a reload and set a different hash at the same time.
426  $(window).on('hashchange', function () {
427  location.reload();
428  });
429 
430  location.href = dest;
431 };
432 
442 elgg.parse_url = function(url, component, expand) {
443  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
444  // which was release under the MIT
445  // It was modified to fix mailto: and javascript: support.
446  expand = expand || false;
447  component = component || false;
448 
449  var re_str =
450  // scheme (and user@ testing)
451  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
452  // possibly a user[:password]@
453  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
454  // host and port
455  + '([^:/?#]*)(?::(\\d*))?)'
456  // path
457  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
458  // query string
459  + '(?:\\?([^#]*))?'
460  // fragment
461  + '(?:#(.*))?)';
462  var keys = {
463  1: "scheme",
464  4: "user",
465  5: "pass",
466  6: "host",
467  7: "port",
468  9: "path",
469  12: "query",
470  13: "fragment"
471  };
472  var results = {};
473 
474  if (url.indexOf('mailto:') === 0) {
475  results['scheme'] = 'mailto';
476  results['path'] = url.replace('mailto:', '');
477  return results;
478  }
479 
480  if (url.indexOf('javascript:') === 0) {
481  results['scheme'] = 'javascript';
482  results['path'] = url.replace('javascript:', '');
483  return results;
484  }
485 
486  var re = new RegExp(re_str);
487  var matches = re.exec(url);
488 
489  for (var i in keys) {
490  if (matches[i]) {
491  results[keys[i]] = matches[i];
492  }
493  }
494 
495  if (expand && typeof(results['query']) != 'undefined') {
496  results['query'] = elgg.parse_str(results['query']);
497  }
498 
499  if (component) {
500  if (typeof(results[component]) != 'undefined') {
501  return results[component];
502  } else {
503  return false;
504  }
505  }
506  return results;
507 };
508 
515 elgg.parse_str = function(string) {
516  var params = {},
517  result,
518  key,
519  value,
520  re = /([^&=]+)=?([^&]*)/g,
521  re2 = /\[\]$/;
522 
523  // assignment intentional
524  while (result = re.exec(string)) {
525  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
526  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
527 
528  if (re2.test(key)) {
529  key = key.replace(re2, '');
530  if (!params[key]) {
531  params[key] = [];
532  }
533  params[key].push(value);
534  } else {
535  params[key] = value;
536  }
537  }
538 
539  return params;
540 };
541 
554 elgg.getSelectorFromUrlFragment = function(url) {
555  var fragment = url.split('#')[1];
556 
557  if (fragment) {
558  // this is a .class or a tag.class
559  if (fragment.indexOf('.') > -1) {
560  return fragment;
561  } else {
562  // this is an id
563  return '#' + fragment;
564  }
565  }
566  return '';
567 };
568 
576 elgg.push_to_object_array = function(object, parent, value) {
577  elgg.assertTypeOf('object', object);
578  elgg.assertTypeOf('string', parent);
579 
580  if (!(object[parent] instanceof Array)) {
581  object[parent] = [];
582  }
583 
584  if ($.inArray(value, object[parent]) < 0) {
585  return object[parent].push(value);
586  }
587 
588  return false;
589 };
590 
598 elgg.is_in_object_array = function(object, parent, value) {
599  elgg.assertTypeOf('object', object);
600  elgg.assertTypeOf('string', parent);
601 
602  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
603 };
$data value
Definition: default.php:27
$CONFIG url
The full URL where Elgg is installed.
Definition: config.php:38
Bundled plugins(the contents of the"/mod"directory) are available only under the GPLv2 license.The remainder of the project is available under either MIT or GPLv2.Both licenses can be found below.More info and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two distribute and or modify the software for each author s protection and we want to make certain that everyone understands that there is no warranty for this free software If the software is modified by someone else and passed on
Definition: LICENSE.txt:96
var elgg
Definition: elgglib.js:4