Elgg  Version 5.1
elgglib.js
Go to the documentation of this file.
1 
4 var elgg = elgg || {};
5 
9 elgg.assertTypeOf = function(type, val) {
10  if (typeof val !== type) {
11  throw new TypeError("Expecting param of " + arguments.caller + "to be a(n) " + type + ". Was actually a(n) " + typeof val + ".");
12  }
13 };
14 
29 elgg.normalize_url = function(url) {
30  url = url || '';
31  elgg.assertTypeOf('string', url);
32 
33  function validate(url) {
34  url = elgg.parse_url(url);
35  if (url.scheme) {
36  url.scheme = url.scheme.toLowerCase();
37  }
38 
39  if (url.scheme == 'http' || url.scheme == 'https') {
40  if (!url.host) {
41  return false;
42  }
43 
44  /* hostname labels may contain only alphanumeric characters, dots and hypens. */
45  if (!(new RegExp("^([a-zA-Z0-9][a-zA-Z0-9\\-\\.]*)$", "i")).test(url.host) || url.host.charAt(-1) == '.') {
46  return false;
47  }
48  }
49 
50  /* some schemas allow the host to be empty */
51  if (!url.scheme || !url.host && url.scheme != 'mailto' && url.scheme != 'news' && url.scheme != 'file') {
52  return false;
53  }
54 
55  return true;
56  };
57 
58  // ignore anything with a recognized scheme
59  if (url.indexOf('http:') === 0 || url.indexOf('https:') === 0 || url.indexOf('javascript:') === 0 || url.indexOf('mailto:') === 0) {
60  return url;
61  } else if (validate(url)) {
62  // all normal URLs including mailto:
63  return url;
64  } else if ((new RegExp("^(\\#|\\?|//)", "i")).test(url)) {
65  // '//example.com' (Shortcut for protocol.)
66  // '?query=test', #target
67  return url;
68  } else if ((new RegExp("^[^\/]*\\.php(\\?.*)?$", "i")).test(url)) {
69  // watch those double escapes in JS.
70  // 'install.php', 'install.php?step=step'
71  if (url.indexOf('/') === 0) {
72  url = url.substring(1);
73  }
74 
75  return elgg.config.wwwroot + url;
76  } else if ((new RegExp("^[^/\\?\\#]*\\.", "i")).test(url)) {
77  // 'example.com', 'example.com/subpage'
78  return 'http://' + url;
79  } else {
80  // 'page/handler', 'mod/plugin/file.php'
81  // trim off any leading / because the site URL is stored
82  // with a trailing /
83  if (url.indexOf('/') === 0) {
84  url = url.substring(1);
85  }
86 
87  return elgg.config.wwwroot + url;
88  }
89 };
90 
99 elgg.deprecated_notice = function(msg, dep_version) {
100  if (elgg.is_admin_logged_in()) {
101  msg = "Deprecated in Elgg " + dep_version + ": " + msg;
102  if (typeof console !== "undefined") {
103  console.info(msg);
104  }
105  }
106 };
107 
114 elgg.forward = function(url) {
115  var dest = elgg.normalize_url(url);
116 
117  if (dest == location.href) {
118  location.reload();
119  }
120 
121  // in case the href set below just changes the hash, we want to reload. There's sadly
122  // no way to force a reload and set a different hash at the same time.
123  $(window).on('hashchange', function () {
124  location.reload();
125  });
126 
127  location.href = dest;
128 };
129 
139 elgg.parse_url = function(url, component, expand) {
140  // Adapted from http://blog.stevenlevithan.com/archives/parseuri
141  // which was release under the MIT
142  // It was modified to fix mailto: and javascript: support.
143  expand = expand || false;
144  component = component || false;
145 
146  var re_str = '^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?://)?'; // scheme (and user@ testing)
147  re_str += '((?:(([^:@]*)(?::([^:@]*))?)?@)?'; // possibly a user[:password]@
148  re_str += '([^:/?#]*)(?::(\\d*))?)'; // host and port
149  re_str += '(((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[?#]|$)))*/?)?([^?#/]*))'; // path
150  re_str += '(?:\\?([^#]*))?'; // query string
151  re_str += '(?:#(.*))?)'; // fragment
152 
153  var keys = {
154  1: "scheme",
155  4: "user",
156  5: "pass",
157  6: "host",
158  7: "port",
159  9: "path",
160  12: "query",
161  13: "fragment"
162  };
163  var results = {};
164 
165  if (url.indexOf('mailto:') === 0) {
166  results['scheme'] = 'mailto';
167  results['path'] = url.replace('mailto:', '');
168  return results;
169  }
170 
171  if (url.indexOf('javascript:') === 0) {
172  results['scheme'] = 'javascript';
173  results['path'] = url.replace('javascript:', '');
174  return results;
175  }
176 
177  var re = new RegExp(re_str);
178  var matches = re.exec(url);
179 
180  for (var i in keys) {
181  if (matches[i]) {
182  results[keys[i]] = matches[i];
183  }
184  }
185 
186  if (expand && typeof(results['query']) != 'undefined') {
187  results['query'] = elgg.parse_str(results['query']);
188  }
189 
190  if (component) {
191  if (typeof(results[component]) != 'undefined') {
192  return results[component];
193  } else {
194  return false;
195  }
196  }
197 
198  return results;
199 };
200 
208 elgg.parse_str = function(string) {
209  var params = {},
210  result,
211  key,
212  value,
213  re = /([^&=]+)=?([^&]*)/g,
214  re2 = /\[\]$/;
215 
216  // assignment intentional
217  while (result = re.exec(string)) {
218  key = decodeURIComponent(result[1].replace(/\+/g, ' '));
219  value = decodeURIComponent(result[2].replace(/\+/g, ' '));
220 
221  if (re2.test(key)) {
222  key = key.replace(re2, '');
223  if (!params[key]) {
224  params[key] = [];
225  }
226 
227  params[key].push(value);
228  } else {
229  params[key] = value;
230  }
231  }
232 
233  return params;
234 };
235 
249 elgg.getSelectorFromUrlFragment = function(url) {
250  var fragment = url.split('#')[1];
251 
252  if (fragment) {
253  // this is a .class or a tag.class
254  if (fragment.indexOf('.') > -1) {
255  return fragment;
256  } else {
257  // this is an id
258  return '#' + fragment;
259  }
260  }
261 
262  return '';
263 };
264 
270 elgg.get_logged_in_user_guid = function() {
271  return elgg.user ? elgg.user.guid : 0;
272 };
273 
279 elgg.is_logged_in = function() {
280  return elgg.get_logged_in_user_guid() > 0;
281 };
282 
288 elgg.is_admin_logged_in = function() {
289  return elgg.user ? elgg.user.admin : false;
290 };
291 
297 elgg.get_site_url = function() {
298  return elgg.config.wwwroot;
299 };
300 
309 elgg.get_simplecache_url = function(view, subview) {
310  elgg.assertTypeOf('string', view);
311 
312  var lastcache, path;
313 
314  if (elgg.config.simplecache_enabled) {
315  lastcache = elgg.config.lastcache;
316  } else {
317  lastcache = 0;
318  }
319 
320  if (!subview) {
321  path = '/cache/' + lastcache + '/' + elgg.config.viewtype + '/' + view;
322  } else {
323  elgg.assertTypeOf('string', subview);
324 
325  if ((view === 'js' || view === 'css') && 0 === subview.indexOf(view + '/')) {
326  subview = subview.substring(view.length + 1);
327  }
328 
329  path = '/cache/' + lastcache + '/' + elgg.config.viewtype + '/' + view + '/' + subview;
330  }
331 
332  return elgg.normalize_url(path);
333 };
$data value
Definition: default.php:27
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