aboutsummaryrefslogtreecommitdiffstats
path: root/data/util.js
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2011-02-14 17:51:53 +0100
committerMatěj Cepl <mcepl@redhat.com>2011-02-14 17:57:45 +0100
commit04657077ba12e8f23feaa0c3787b590c44446056 (patch)
treeebe206422871e71b142985786a9472ff447be0cd /data/util.js
parent838c1d4abd72bd87dcc8ee41310ca3a8af498c96 (diff)
downloadbugzilla-triage-04657077ba12e8f23feaa0c3787b590c44446056.tar.gz
Basic functionality working. Switching to this bug for my day-to-day work.
Diffstat (limited to 'data/util.js')
-rw-r--r--data/util.js86
1 files changed, 64 insertions, 22 deletions
diff --git a/data/util.js b/data/util.js
index 5e8afcc..09360a0 100644
--- a/data/util.js
+++ b/data/util.js
@@ -4,29 +4,46 @@
// http://www.opensource.org/licenses/mit-license.php
"use strict";
// ==============================================================
+
/**
- * get parameters of URL as an object (name, value)
+ * parse URL to get its parts.
+ *
+ * @param url
+ * @return object with all parsed parts of URL as properties
+ *
+ * Originally from http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
+ * Copyright February 19th, 2009, James Padolsey, <license undeclared>
+ *
+ * This function creates a new anchor element and uses location
+ * properties (inherent) to get the desired URL data. Some String
+ * operations are used (to normalize results across browsers).
*/
-function getParamsFromURL (url, base) {
- if (!url || (url.toString().length === 0)) {
- throw new Error("Missing URL value!");
- }
-
- var aElem = document.createElement("a");
- aElem.href = url;
-
- var paramsArr = url.pathname.split("?");
- if (paramsArr.length === 1) {
- return {};
- }
-
- // get convert URL parameters to an Object
- var params = {}, s = [];
- paramsArr[1].split('&').forEach(function(par) {
- s = par.split('=');
- params[s[0]] = s[1];
- });
- return params;
+function parseURL(url) {
+ var a = document.createElement('a');
+ a.href = url;
+ return {
+ source: url,
+ protocol: a.protocol.replace(':',''),
+ host: a.hostname,
+ port: a.port,
+ query: a.search,
+ params: (function(){
+ var ret = {},
+ seg = a.search.replace(/^\?/,'').split('&'),
+ len = seg.length, i = 0, s;
+ for (;i<len;i++) {
+ if (!seg[i]) { continue; }
+ s = seg[i].split('=');
+ ret[s[0]] = s[1];
+ }
+ return ret;
+ })(),
+ file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
+ hash: a.hash.replace('#',''),
+ path: a.pathname.replace(/^([^\/])/,'/$1'),
+ relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
+ segments: a.pathname.replace(/^\//,'').split('/')
+ };
}
/**
@@ -56,12 +73,37 @@ function getBugNo() {
* @return String with the bug ID
*/
function getBugNoFromURL(url) {
- var params = getParamsFromURL(url);
+ var params = parseURL(url).params;
if (params && params.id) {
return params.id;
}
}
+/*
+ * From <a> element diggs out just plain email address
+ * Note that bugzilla.gnome.org doesn't have mailto: url but
+ * https://bugzilla.gnome.org/page.cgi?id=describeuser.html&login=mcepl%40redhat.com
+ *
+ * @param aElement Element with href attribute or something else
+ * @return String with the address or null
+ *
+ */
+function parseMailto(aElement) {
+ var emailStr = "", hrefStr = "";
+ // use url utils
+ if (aElement) {
+ hrefStr = decodeURIComponent(aElement.getAttribute("href"));
+ emailStr = hrefStr.split(":");
+ // workaround for Gnome bugzilla ... no mailto: here.
+ if (emailStr.length < 2) {
+ var params = parseURL("https://" + window.location.hostname + "/" + hrefStr).params;
+ return decodeURI(params.login);
+ }
+ return emailStr[1];
+ }
+ return null;
+}
+
/**
* format date to be in ISO format (just day part)
*