Elgg  Version 1.9
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 
199 elgg.provide = function(pkg, opt_context) {
200  elgg.assertTypeOf('string', pkg);
201 
202  var parts = pkg.split('.'),
203  context = opt_context || elgg.global,
204  part, i;
205 
206 
207  for (i = 0; i < parts.length; i += 1) {
208  part = parts[i];
209  context[part] = context[part] || {};
210  context = context[part];
211  }
212 };
213 
237 elgg.inherit = function(Child, Parent) {
238  Child.prototype = new Parent();
239  Child.prototype.constructor = Child;
240 };
241 
256 elgg.normalize_url = function(url) {
257  url = url || '';
258  elgg.assertTypeOf('string', url);
259 
260  function validate(url) {
261  url = elgg.parse_url(url);
262  if (url.scheme){
263  url.scheme = url.scheme.toLowerCase();
264  }
265  if (url.scheme == 'http' || url.scheme == 'https') {
266  if (!url.host) {
267  return false;
268  }
269  /* hostname labels may contain only alphanumeric characters, dots and hypens. */
270  if (!(new RegExp("^([a-zA-Z0-9][a-zA-Z0-9\\-\\.]*)$", "i")).test(url.host) || url.host.charAt(-1) == '.') {
271  return false;
272  }
273  }
274  /* some schemas allow the host to be empty */
275  if (!url.scheme || !url.host && url.scheme != 'mailto' && url.scheme != 'news' && url.scheme != 'file') {
276  return false;
277  }
278  return true;
279  };
280 
281  // ignore anything with a recognized scheme
282  if (url.indexOf('http:') === 0 ||
283  url.indexOf('https:') === 0 ||
284  url.indexOf('javascript:') === 0 ||
285  url.indexOf('mailto:') === 0 ) {
286  return url;
287  }
288 
289  // all normal URLs including mailto:
290  else if (validate(url)) {
291  return url;
292  }
293 
294  // '//example.com' (Shortcut for protocol.)
295  // '?query=test', #target
296  else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
297  return url;
298  }
299 
300 
301  // watch those double escapes in JS.
302 
303  // 'install.php', 'install.php?step=step'
304  else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
305  return elgg.config.wwwroot + url.ltrim('/');
306  }
307 
308  // 'example.com', 'example.com/subpage'
309  else if ((new RegExp("^[^/]*\\.", "i")).test(url)) {
310  return 'http://' + url;
311  }
312 
313  // 'page/handler', 'mod/plugin/file.php'
314  else {
315  // trim off any leading / because the site URL is stored
316  // with a trailing /
317  return elgg.config.wwwroot + url.ltrim('/');
318  }
319 };
320 
329 elgg.system_messages = function(msgs, delay, type) {
330  if (elgg.isUndefined(msgs)) {
331  return;
332  }
333 
334  var classes = ['elgg-message'],
335  messages_html = [],
336  appendMessage = function(msg) {
337  messages_html.push('<li class="' + classes.join(' ') + '"><p>' + msg + '</p></li>');
338  },
339  systemMessages = $('ul.elgg-system-messages'),
340  i;
341 
342  //validate delay. Must be a positive integer.
343  delay = parseInt(delay || 6000, 10);
344  if (isNaN(delay) || delay <= 0) {
345  delay = 6000;
346  }
347 
348  //Handle non-arrays
349  if (!elgg.isArray(msgs)) {
350  msgs = [msgs];
351  }
352 
353  if (type === 'error') {
354  classes.push('elgg-state-error');
355  } else {
356  classes.push('elgg-state-success');
357  }
358 
359  msgs.forEach(appendMessage);
360 
361  if (type != 'error') {
362  $(messages_html.join('')).appendTo(systemMessages)
363  .animate({opacity: '1.0'}, delay).fadeOut('slow');
364  } else {
365  $(messages_html.join('')).appendTo(systemMessages);
366  }
367 };
368 
374 elgg.system_message = function(msgs, delay) {
375  elgg.system_messages(msgs, delay, "message");
376 };
377 
383 elgg.register_error = function(errors, delay) {
384  elgg.system_messages(errors, delay, "error");
385 };
386 
393 elgg.deprecated_notice = function(msg, dep_version) {
394  if (elgg.is_admin_logged_in()) {
395  var parts = elgg.release.split('.');
396  var elgg_major_version = parseInt(parts[0], 10);
397  var elgg_minor_version = parseInt(parts[1], 10);
398  var dep_major_version = Math.floor(dep_version);
399  var dep_minor_version = 10 * (dep_version - dep_major_version);
400 
401  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
402 
403  if ((dep_major_version < elgg_major_version) || (dep_minor_version < elgg_minor_version)) {
404  elgg.register_error(msg);
405  } else {
406  if (typeof console !== "undefined") {
407  console.warn(msg);
408  }
409  }
410  }
411 };
412 
419 elgg.forward = function(url) {
420  location.href = elgg.normalize_url(url);
421 };
422 
432 elgg.parse_url = function(url, component, expand) {
433  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
434  // which was release under the MIT
435  // It was modified to fix mailto: and javascript: support.
436  expand = expand || false;
437  component = component || false;
438 
439  var re_str =
440  // scheme (and user@ testing)
441  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
442  // possibly a user[:password]@
443  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
444  // host and port
445  + '([^:/?#]*)(?::(\\d*))?)'
446  // path
447  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
448  // query string
449  + '(?:\\?([^#]*))?'
450  // fragment
451  + '(?:#(.*))?)',
452  keys = {
453  1: "scheme",
454  4: "user",
455  5: "pass",
456  6: "host",
457  7: "port",
458  9: "path",
459  12: "query",
460  13: "fragment"
461  },
462  results = {};
463 
464  if (url.indexOf('mailto:') === 0) {
465  results['scheme'] = 'mailto';
466  results['path'] = url.replace('mailto:', '');
467  return results;
468  }
469 
470  if (url.indexOf('javascript:') === 0) {
471  results['scheme'] = 'javascript';
472  results['path'] = url.replace('javascript:', '');
473  return results;
474  }
475 
476  var re = new RegExp(re_str);
477  var matches = re.exec(url);
478 
479  for (var i in keys) {
480  if (matches[i]) {
481  results[keys[i]] = matches[i];
482  }
483  }
484 
485  if (expand && typeof(results['query']) != 'undefined') {
486  results['query'] = elgg.parse_str(results['query']);
487  }
488 
489  if (component) {
490  if (typeof(results[component]) != 'undefined') {
491  return results[component];
492  } else {
493  return false;
494  }
495  }
496  return results;
497 };
498 
505 elgg.parse_str = function(string) {
506  var params = {},
507  result,
508  key,
509  value,
510  re = /([^&=]+)=?([^&]*)/g,
511  re2 = /\[\]$/;
512 
513  while (result = re.exec(string)) {
514  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
515  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
516 
517  if (re2.test(key)) {
518  key = key.replace(re2, '');
519  if (!params[key]) {
520  params[key] = [];
521  }
522  params[key].push(value);
523  } else {
524  params[key] = value;
525  }
526  }
527 
528  return params;
529 };
530 
543 elgg.getSelectorFromUrlFragment = function(url) {
544  var fragment = url.split('#')[1];
545 
546  if (fragment) {
547  // this is a .class or a tag.class
548  if (fragment.indexOf('.') > -1) {
549  return fragment;
550  }
551 
552  // this is an id
553  else {
554  return '#' + fragment;
555  }
556  }
557  return '';
558 };
559 
567 elgg.push_to_object_array = function(object, parent, value) {
568  elgg.assertTypeOf('object', object);
569  elgg.assertTypeOf('string', parent);
570 
571  if (!(object[parent] instanceof Array)) {
572  object[parent] = [];
573  }
574 
575  if ($.inArray(value, object[parent]) < 0) {
576  return object[parent].push(value);
577  }
578 
579  return false;
580 };
581 
589 elgg.is_in_object_array = function(object, parent, value) {
590  elgg.assertTypeOf('object', object);
591  elgg.assertTypeOf('string', parent);
592 
593  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
594 };
i
Definition: admin.php:47
$CONFIG url
The full URL where Elgg is installed.
Definition: config.php:94
list style type
Definition: admin.php:724
var elgg
Definition: elgglib.js:4