[refactor,util][s]: move query string parsing and manipulation utilities into util.js.

This commit is contained in:
Rufus Pollock 2012-04-26 22:06:11 +01:00
parent c358a5bd43
commit 6d94c89b7c
5 changed files with 84 additions and 82 deletions

View File

@ -21,7 +21,7 @@ var ExplorerApp = Backbone.View.extend({
this.router.route(/explorer/, 'explorer', this.viewExplorer);
Backbone.history.start();
var state = recline.View.parseQueryString(window.location.search);
var state = recline.Util.parseQueryString(window.location.search);
if (state) {
_.each(state, function(value, key) {
try {
@ -112,7 +112,7 @@ var ExplorerApp = Backbone.View.extend({
},
makePermaLink: function(state) {
var qs = recline.View.composeQueryString(state.toJSON());
var qs = recline.Util.composeQueryString(state.toJSON());
return window.location.origin + window.location.pathname + qs;
},

View File

@ -1,2 +1,79 @@
/*jshint multistr:true */
this.recline = this.recline || {};
this.recline.Util = this.recline.Util || {};
(function(my) {
// ## Miscellaneous Utilities
var urlPathRegex = /^([^?]+)(\?.*)?/;
// Parse the Hash section of a URL into path and query string
my.parseHashUrl = function(hashUrl) {
var parsed = urlPathRegex.exec(hashUrl);
if (parsed === null) {
return {};
} else {
return {
path: parsed[1],
query: parsed[2] || ''
};
}
};
// Parse a URL query string (?xyz=abc...) into a dictionary.
my.parseQueryString = function(q) {
if (!q) {
return {};
}
var urlParams = {},
e, d = function (s) {
return unescape(s.replace(/\+/g, " "));
},
r = /([^&=]+)=?([^&]*)/g;
if (q && q.length && q[0] === '?') {
q = q.slice(1);
}
while (e = r.exec(q)) {
// TODO: have values be array as query string allow repetition of keys
urlParams[d(e[1])] = d(e[2]);
}
return urlParams;
};
// Parse the query string out of the URL hash
my.parseHashQueryString = function() {
q = my.parseHashUrl(window.location.hash).query;
return my.parseQueryString(q);
};
// Compse a Query String
my.composeQueryString = function(queryParams) {
var queryString = '?';
var items = [];
$.each(queryParams, function(key, value) {
if (typeof(value) === 'object') {
value = JSON.stringify(value);
}
items.push(key + '=' + value);
});
queryString += items.join('&');
return queryString;
};
my.getNewHashForQueryString = function(queryParams) {
var queryPart = my.composeQueryString(queryParams);
if (window.location.hash) {
// slice(1) to remove # at start
return window.location.hash.split('?')[0].slice(1) + queryPart;
} else {
return queryPart;
}
};
my.setHashQueryString = function(queryParams) {
window.location.hash = my.getNewHashForQueryString(queryParams);
};
})(this.recline.Util);

View File

@ -232,10 +232,6 @@ my.DataExplorer = Backbone.View.extend({
my.clearNotifications();
self.el.find('.doc-count').text(self.model.docCount || 'Unknown');
my.notify('Data loaded', {category: 'success'});
// update navigation
var qs = my.parseHashQueryString();
qs.reclineQuery = JSON.stringify(self.model.queryState.toJSON());
var out = my.getNewHashForQueryString(qs);
});
this.model.bind('query:fail', function(error) {
my.clearNotifications();
@ -338,7 +334,7 @@ my.DataExplorer = Backbone.View.extend({
_setupState: function(initialState) {
var self = this;
// get data from the query string / hash url plus some defaults
var qs = my.parseHashQueryString();
var qs = recline.Util.parseHashQueryString();
var query = qs.reclineQuery;
query = query ? JSON.parse(query) : self.model.queryState.toJSON();
// backwards compatability (now named view-graph but was named graph)
@ -618,78 +614,6 @@ my.FacetViewer = Backbone.View.extend({
}
});
/* ========================================================== */
// ## Miscellaneous Utilities
var urlPathRegex = /^([^?]+)(\?.*)?/;
// Parse the Hash section of a URL into path and query string
my.parseHashUrl = function(hashUrl) {
var parsed = urlPathRegex.exec(hashUrl);
if (parsed === null) {
return {};
} else {
return {
path: parsed[1],
query: parsed[2] || ''
};
}
};
// Parse a URL query string (?xyz=abc...) into a dictionary.
my.parseQueryString = function(q) {
if (!q) {
return {};
}
var urlParams = {},
e, d = function (s) {
return unescape(s.replace(/\+/g, " "));
},
r = /([^&=]+)=?([^&]*)/g;
if (q && q.length && q[0] === '?') {
q = q.slice(1);
}
while (e = r.exec(q)) {
// TODO: have values be array as query string allow repetition of keys
urlParams[d(e[1])] = d(e[2]);
}
return urlParams;
};
// Parse the query string out of the URL hash
my.parseHashQueryString = function() {
q = my.parseHashUrl(window.location.hash).query;
return my.parseQueryString(q);
};
// Compse a Query String
my.composeQueryString = function(queryParams) {
var queryString = '?';
var items = [];
$.each(queryParams, function(key, value) {
if (typeof(value) === 'object') {
value = JSON.stringify(value);
}
items.push(key + '=' + value);
});
queryString += items.join('&');
return queryString;
};
my.getNewHashForQueryString = function(queryParams) {
var queryPart = my.composeQueryString(queryParams);
if (window.location.hash) {
// slice(1) to remove # at start
return window.location.hash.split('?')[0].slice(1) + queryPart;
} else {
return queryPart;
}
};
my.setHashQueryString = function(queryParams) {
window.location.hash = my.getNewHashForQueryString(queryParams);
};
// ## notify
//

View File

@ -22,6 +22,7 @@
<script type="text/javascript" src="base.js"></script>
<script type="text/javascript" src="../src/util.js"></script>
<script type="text/javascript" src="../src/model.js"></script>
<script type="text/javascript" src="../src/backend/base.js"></script>
<script type="text/javascript" src="../src/backend/memory.js"></script>

View File

@ -2,10 +2,10 @@
module("Util");
test('parseHashUrl', function () {
var out = recline.View.parseHashUrl('graph?x=y');
var out = recline.Util.parseHashUrl('graph?x=y');
equal(out.path, 'graph');
equal(out.query, '?x=y');
var out = recline.View.parseHashUrl('graph');
var out = recline.Util.parseHashUrl('graph');
equal(out.path, 'graph');
equal(out.query, '');
});
@ -15,7 +15,7 @@ test('composeQueryString', function () {
x: 'y',
a: 'b'
};
var out = recline.View.composeQueryString(params);
var out = recline.Util.composeQueryString(params);
equal(out, '?x=y&a=b');
});