Elgg  Version 2.2
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  location.href = elgg.normalize_url(url);
427 };
428 
438 elgg.parse_url = function(url, component, expand) {
439  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
440  // which was release under the MIT
441  // It was modified to fix mailto: and javascript: support.
442  expand = expand || false;
443  component = component || false;
444 
445  var re_str =
446  // scheme (and user@ testing)
447  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
448  // possibly a user[:password]@
449  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
450  // host and port
451  + '([^:/?#]*)(?::(\\d*))?)'
452  // path
453  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
454  // query string
455  + '(?:\\?([^#]*))?'
456  // fragment
457  + '(?:#(.*))?)',
458  keys = {
459  1: "scheme",
460  4: "user",
461  5: "pass",
462  6: "host",
463  7: "port",
464  9: "path",
465  12: "query",
466  13: "fragment"
467  },
468  results = {};
469 
470  if (url.indexOf('mailto:') === 0) {
471  results['scheme'] = 'mailto';
472  results['path'] = url.replace('mailto:', '');
473  return results;
474  }
475 
476  if (url.indexOf('javascript:') === 0) {
477  results['scheme'] = 'javascript';
478  results['path'] = url.replace('javascript:', '');
479  return results;
480  }
481 
482  var re = new RegExp(re_str);
483  var matches = re.exec(url);
484 
485  for (var i in keys) {
486  if (matches[i]) {
487  results[keys[i]] = matches[i];
488  }
489  }
490 
491  if (expand && typeof(results['query']) != 'undefined') {
492  results['query'] = elgg.parse_str(results['query']);
493  }
494 
495  if (component) {
496  if (typeof(results[component]) != 'undefined') {
497  return results[component];
498  } else {
499  return false;
500  }
501  }
502  return results;
503 };
504 
511 elgg.parse_str = function(string) {
512  var params = {},
513  result,
514  key,
515  value,
516  re = /([^&=]+)=?([^&]*)/g,
517  re2 = /\[\]$/;
518 
519  // assignment intentional
520  while (result = re.exec(string)) {
521  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
522  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
523 
524  if (re2.test(key)) {
525  key = key.replace(re2, '');
526  if (!params[key]) {
527  params[key] = [];
528  }
529  params[key].push(value);
530  } else {
531  params[key] = value;
532  }
533  }
534 
535  return params;
536 };
537 
550 elgg.getSelectorFromUrlFragment = function(url) {
551  var fragment = url.split('#')[1];
552 
553  if (fragment) {
554  // this is a .class or a tag.class
555  if (fragment.indexOf('.') > -1) {
556  return fragment;
557  }
558 
559  // this is an id
560  else {
561  return '#' + fragment;
562  }
563  }
564  return '';
565 };
566 
574 elgg.push_to_object_array = function(object, parent, value) {
575  elgg.assertTypeOf('object', object);
576  elgg.assertTypeOf('string', parent);
577 
578  if (!(object[parent] instanceof Array)) {
579  object[parent] = [];
580  }
581 
582  if ($.inArray(value, object[parent]) < 0) {
583  return object[parent].push(value);
584  }
585 
586  return false;
587 };
588 
596 elgg.is_in_object_array = function(object, parent, value) {
597  elgg.assertTypeOf('object', object);
598  elgg.assertTypeOf('string', parent);
599 
600  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
601 };
list style type
Definition: admin.css.php:777
if(!$site) if(!($site instanceof ElggSite)) $site url
var elgg
Definition: elgglib.js:4
i
Definition: admin.css.php:47