Elgg  Version 1.12
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 
255 elgg.normalize_url = function(url) {
256  url = url || '';
257  elgg.assertTypeOf('string', url);
258 
259  function validate(url) {
260  url = elgg.parse_url(url);
261  if (url.scheme){
262  url.scheme = url.scheme.toLowerCase();
263  }
264  if (url.scheme == 'http' || url.scheme == 'https') {
265  if (!url.host) {
266  return false;
267  }
268  /* hostname labels may contain only alphanumeric characters, dots and hypens. */
269  if (!(new RegExp("^([a-zA-Z0-9][a-zA-Z0-9\\-\\.]*)$", "i")).test(url.host) || url.host.charAt(-1) == '.') {
270  return false;
271  }
272  }
273  /* some schemas allow the host to be empty */
274  if (!url.scheme || !url.host && url.scheme != 'mailto' && url.scheme != 'news' && url.scheme != 'file') {
275  return false;
276  }
277  return true;
278  };
279 
280  // ignore anything with a recognized scheme
281  if (url.indexOf('http:') === 0 ||
282  url.indexOf('https:') === 0 ||
283  url.indexOf('javascript:') === 0 ||
284  url.indexOf('mailto:') === 0 ) {
285  return url;
286  }
287 
288  // all normal URLs including mailto:
289  else if (validate(url)) {
290  return url;
291  }
292 
293  // '//example.com' (Shortcut for protocol.)
294  // '?query=test', #target
295  else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
296  return url;
297  }
298 
299 
300  // watch those double escapes in JS.
301 
302  // 'install.php', 'install.php?step=step'
303  else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
304  return elgg.config.wwwroot + url.ltrim('/');
305  }
306 
307  // 'example.com', 'example.com/subpage'
308  else if ((new RegExp("^[^/]*\\.", "i")).test(url)) {
309  return 'http://' + url;
310  }
311 
312  // 'page/handler', 'mod/plugin/file.php'
313  else {
314  // trim off any leading / because the site URL is stored
315  // with a trailing /
316  return elgg.config.wwwroot + url.ltrim('/');
317  }
318 };
319 
328 elgg.system_messages = function(msgs, delay, type) {
329  if (elgg.isUndefined(msgs)) {
330  return;
331  }
332 
333  var classes = ['elgg-message'],
334  messages_html = [],
335  appendMessage = function(msg) {
336  messages_html.push('<li class="' + classes.join(' ') + '"><p>' + msg + '</p></li>');
337  },
338  systemMessages = $('ul.elgg-system-messages'),
339  i;
340 
341  //validate delay. Must be a positive integer.
342  delay = parseInt(delay || 6000, 10);
343  if (isNaN(delay) || delay <= 0) {
344  delay = 6000;
345  }
346 
347  //Handle non-arrays
348  if (!elgg.isArray(msgs)) {
349  msgs = [msgs];
350  }
351 
352  if (type === 'error') {
353  classes.push('elgg-state-error');
354  } else {
355  classes.push('elgg-state-success');
356  }
357 
358  msgs.forEach(appendMessage);
359 
360  if (type != 'error') {
361  $(messages_html.join('')).appendTo(systemMessages)
362  .animate({opacity: '1.0'}, delay).fadeOut('slow');
363  } else {
364  $(messages_html.join('')).appendTo(systemMessages);
365  }
366 };
367 
373 elgg.system_message = function(msgs, delay) {
374  elgg.system_messages(msgs, delay, "message");
375 };
376 
382 elgg.register_error = function(errors, delay) {
383  elgg.system_messages(errors, delay, "error");
384 };
385 
393 elgg.deprecated_notice = function(msg, dep_version) {
394  if (elgg.is_admin_logged_in()) {
395  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
396  if (typeof console !== "undefined") {
397  console.info(msg);
398  }
399  }
400 };
401 
408 elgg.forward = function(url) {
409  location.href = elgg.normalize_url(url);
410 };
411 
421 elgg.parse_url = function(url, component, expand) {
422  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
423  // which was release under the MIT
424  // It was modified to fix mailto: and javascript: support.
425  expand = expand || false;
426  component = component || false;
427 
428  var re_str =
429  // scheme (and user@ testing)
430  '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'
431  // possibly a user[:password]@
432  + '((?:(([^:@]*)(?::([^:@]*))?)?@)?'
433  // host and port
434  + '([^:/?#]*)(?::(\\d*))?)'
435  // path
436  + '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'
437  // query string
438  + '(?:\\?([^#]*))?'
439  // fragment
440  + '(?:#(.*))?)',
441  keys = {
442  1: "scheme",
443  4: "user",
444  5: "pass",
445  6: "host",
446  7: "port",
447  9: "path",
448  12: "query",
449  13: "fragment"
450  },
451  results = {};
452 
453  if (url.indexOf('mailto:') === 0) {
454  results['scheme'] = 'mailto';
455  results['path'] = url.replace('mailto:', '');
456  return results;
457  }
458 
459  if (url.indexOf('javascript:') === 0) {
460  results['scheme'] = 'javascript';
461  results['path'] = url.replace('javascript:', '');
462  return results;
463  }
464 
465  var re = new RegExp(re_str);
466  var matches = re.exec(url);
467 
468  for (var i in keys) {
469  if (matches[i]) {
470  results[keys[i]] = matches[i];
471  }
472  }
473 
474  if (expand && typeof(results['query']) != 'undefined') {
475  results['query'] = elgg.parse_str(results['query']);
476  }
477 
478  if (component) {
479  if (typeof(results[component]) != 'undefined') {
480  return results[component];
481  } else {
482  return false;
483  }
484  }
485  return results;
486 };
487 
494 elgg.parse_str = function(string) {
495  var params = {},
496  result,
497  key,
498  value,
499  re = /([^&=]+)=?([^&]*)/g,
500  re2 = /\[\]$/;
501 
502  // assignment intentional
503  while (result = re.exec(string)) {
504  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
505  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
506 
507  if (re2.test(key)) {
508  key = key.replace(re2, '');
509  if (!params[key]) {
510  params[key] = [];
511  }
512  params[key].push(value);
513  } else {
514  params[key] = value;
515  }
516  }
517 
518  return params;
519 };
520 
533 elgg.getSelectorFromUrlFragment = function(url) {
534  var fragment = url.split('#')[1];
535 
536  if (fragment) {
537  // this is a .class or a tag.class
538  if (fragment.indexOf('.') > -1) {
539  return fragment;
540  }
541 
542  // this is an id
543  else {
544  return '#' + fragment;
545  }
546  }
547  return '';
548 };
549 
557 elgg.push_to_object_array = function(object, parent, value) {
558  elgg.assertTypeOf('object', object);
559  elgg.assertTypeOf('string', parent);
560 
561  if (!(object[parent] instanceof Array)) {
562  object[parent] = [];
563  }
564 
565  if ($.inArray(value, object[parent]) < 0) {
566  return object[parent].push(value);
567  }
568 
569  return false;
570 };
571 
579 elgg.is_in_object_array = function(object, parent, value) {
580  elgg.assertTypeOf('object', object);
581  elgg.assertTypeOf('string', parent);
582 
583  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
584 };
i
Definition: admin.php:47
if(!$site) if(!($site instanceof ElggSite)) $site url
list style type
Definition: admin.php:753
var elgg
Definition: elgglib.js:4