Elgg  Version 2.3
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 ||
299  url.indexOf('https:') === 0 ||
300  url.indexOf('javascript:') === 0 ||
301  url.indexOf('mailto:') === 0 ) {
302  return url;
303  }
304 
305  // all normal URLs including mailto:
306  else if (validate(url)) {
307  return url;
308  }
309 
310  // '//example.com' (Shortcut for protocol.)
311  // '?query=test', #target
312  else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
313  return url;
314  }
315 
316 
317  // watch those double escapes in JS.
318 
319  // 'install.php', 'install.php?step=step'
320  else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
321  return elgg.config.wwwroot + url.ltrim('/');
322  }
323 
324  // 'example.com', 'example.com/subpage'
325  else if ((new RegExp("^[^/]*\\.", "i")).test(url)) {
326  return 'http://' + url;
327  }
328 
329  // 'page/handler', 'mod/plugin/file.php'
330  else {
331  // trim off any leading / because the site URL is stored
332  // with a trailing /
333  return elgg.config.wwwroot + url.ltrim('/');
334  }
335 };
336 
345 elgg.system_messages = function(msgs, delay, type) {
346  if (elgg.isUndefined(msgs)) {
347  return;
348  }
349 
350  var classes = ['elgg-message'],
351  messages_html = [],
352  appendMessage = function(msg) {
353  messages_html.push('<li class="' + classes.join(' ') + '"><p>' + msg + '</p></li>');
354  },
355  systemMessages = $('ul.elgg-system-messages'),
356  i;
357 
358  //validate delay. Must be a positive integer.
359  delay = parseInt(delay || 6000, 10);
360  if (isNaN(delay) || delay <= 0) {
361  delay = 6000;
362  }
363 
364  //Handle non-arrays
365  if (!elgg.isArray(msgs)) {
366  msgs = [msgs];
367  }
368 
369  if (type === 'error') {
370  classes.push('elgg-state-error');
371  } else {
372  classes.push('elgg-state-success');
373  }
374 
375  msgs.forEach(appendMessage);
376 
377  if (type != 'error') {
378  $(messages_html.join('')).appendTo(systemMessages)
379  .animate({opacity: '1.0'}, delay).fadeOut('slow');
380  } else {
381  $(messages_html.join('')).appendTo(systemMessages);
382  }
383 };
384 
390 elgg.system_message = function(msgs, delay) {
391  elgg.system_messages(msgs, delay, "message");
392 };
393 
399 elgg.register_error = function(errors, delay) {
400  elgg.system_messages(errors, delay, "error");
401 };
402 
410 elgg.deprecated_notice = function(msg, dep_version) {
411  if (elgg.is_admin_logged_in()) {
412  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
413  if (typeof console !== "undefined") {
414  console.info(msg);
415  }
416  }
417 };
418 
425 elgg.forward = function(url) {
426  var dest = elgg.normalize_url(url);
427 
428  if (dest == location.href) {
429  location.reload();
430  }
431 
432  // in case the href set below just changes the hash, we want to reload. There's sadly
433  // no way to force a reload and set a different hash at the same time.
434  $(window).on('hashchange', function () {
435  location.reload();
436  });
437 
438  location.href = dest;
439 };
440 
450 elgg.parse_url = function(url, component, expand) {
451  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
452  // which was release under the MIT
453  // It was modified to fix mailto: and javascript: support.
454  expand = expand || false;
455  component = component || false;
456 
457  var re_str =
458  // scheme (and user@ testing)
459  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
460  // possibly a user[:password]@
461  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
462  // host and port
463  + '([^:/?#]*)(?::(\\d*))?)'
464  // path
465  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
466  // query string
467  + '(?:\\?([^#]*))?'
468  // fragment
469  + '(?:#(.*))?)',
470  keys = {
471  1: "scheme",
472  4: "user",
473  5: "pass",
474  6: "host",
475  7: "port",
476  9: "path",
477  12: "query",
478  13: "fragment"
479  },
480  results = {};
481 
482  if (url.indexOf('mailto:') === 0) {
483  results['scheme'] = 'mailto';
484  results['path'] = url.replace('mailto:', '');
485  return results;
486  }
487 
488  if (url.indexOf('javascript:') === 0) {
489  results['scheme'] = 'javascript';
490  results['path'] = url.replace('javascript:', '');
491  return results;
492  }
493 
494  var re = new RegExp(re_str);
495  var matches = re.exec(url);
496 
497  for (var i in keys) {
498  if (matches[i]) {
499  results[keys[i]] = matches[i];
500  }
501  }
502 
503  if (expand && typeof(results['query']) != 'undefined') {
504  results['query'] = elgg.parse_str(results['query']);
505  }
506 
507  if (component) {
508  if (typeof(results[component]) != 'undefined') {
509  return results[component];
510  } else {
511  return false;
512  }
513  }
514  return results;
515 };
516 
523 elgg.parse_str = function(string) {
524  var params = {},
525  result,
526  key,
527  value,
528  re = /([^&=]+)=?([^&]*)/g,
529  re2 = /\[\]$/;
530 
531  // assignment intentional
532  while (result = re.exec(string)) {
533  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
534  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
535 
536  if (re2.test(key)) {
537  key = key.replace(re2, '');
538  if (!params[key]) {
539  params[key] = [];
540  }
541  params[key].push(value);
542  } else {
543  params[key] = value;
544  }
545  }
546 
547  return params;
548 };
549 
562 elgg.getSelectorFromUrlFragment = function(url) {
563  var fragment = url.split('#')[1];
564 
565  if (fragment) {
566  // this is a .class or a tag.class
567  if (fragment.indexOf('.') > -1) {
568  return fragment;
569  }
570 
571  // this is an id
572  else {
573  return '#' + fragment;
574  }
575  }
576  return '';
577 };
578 
586 elgg.push_to_object_array = function(object, parent, value) {
587  elgg.assertTypeOf('object', object);
588  elgg.assertTypeOf('string', parent);
589 
590  if (!(object[parent] instanceof Array)) {
591  object[parent] = [];
592  }
593 
594  if ($.inArray(value, object[parent]) < 0) {
595  return object[parent].push(value);
596  }
597 
598  return false;
599 };
600 
608 elgg.is_in_object_array = function(object, parent, value) {
609  elgg.assertTypeOf('object', object);
610  elgg.assertTypeOf('string', parent);
611 
612  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
613 };
list style type
Definition: admin.css.php:808
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: GPL-LICENSE.txt:43
if(!$site) if(!($site instanceof ElggSite)) $site url
var elgg
Definition: elgglib.js:4
i
Definition: admin.css.php:47