[#42,flot-graph][s]: reinstate configuration of graph from routing info lost in penultimate commit a9cfd4412c8a4b9645e100997a4d1898620b0290.

* view.js: bunch of utility functions for parsing and composing query strings for hash routing (plus tests).
This commit is contained in:
Rufus Pollock 2012-02-17 21:47:51 +00:00
parent b285987ef4
commit ed3d20b20f
4 changed files with 70 additions and 4 deletions

View File

@ -90,12 +90,18 @@ my.FlotGraph = Backbone.View.extend({
this.model.bind('change', this.render);
this.model.currentDocuments.bind('add', this.redraw);
this.model.currentDocuments.bind('reset', this.redraw);
var configFromHash = my.parseHashQueryString().graph;
if (configFromHash) {
configFromHash = JSON.parse(configFromHash);
}
this.chartConfig = _.extend({
group: null,
series: [],
graphType: 'line'
},
config)
configFromHash,
config
);
this.render();
},
@ -120,8 +126,9 @@ my.FlotGraph = Backbone.View.extend({
this._getEditorData();
// update navigation
// TODO: make this less invasive (e.g. preserve other keys in query string)
window.location.hash = window.location.hash.split('?')[0] +
'?graph=' + JSON.stringify(this.chartConfig);
var qs = my.parseHashQueryString();
qs['graph'] = this.chartConfig;
my.setHashQueryString(qs);
this.redraw();
},

View File

@ -485,8 +485,23 @@ my.DataTableRow = 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.
function parseQueryString(q) {
my.parseQueryString = function(q) {
var urlParams = {},
e, d = function (s) {
return unescape(s.replace(/\+/g, " "));
@ -503,6 +518,27 @@ function parseQueryString(q) {
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) {
items.push(key + '=' + JSON.stringify(value));
});
queryString += items.join('&');
return queryString;
}
my.setHashQueryString = function(queryParams) {
window.location.hash = window.location.hash.split('?')[0] + my.composeQueryString(queryParams);
}
// ## notify
//
// Create a notification (a div.alert-message in div.alert-messsages) using provide messages and options. Options are:

View File

@ -22,6 +22,7 @@
<script type="text/javascript" src="../src/view.js"></script>
<script type="text/javascript" src="../src/view-transform-dialog.js"></script>
<script type="text/javascript" src="view.test.js"></script>
<script type="text/javascript" src="util.test.js"></script>
</head>
<body>
<h1 id="qunit-header">Qunit Tests</h1>

22
test/util.test.js Normal file
View File

@ -0,0 +1,22 @@
(function ($) {
module("Util");
test('parseHashUrl', function () {
var out = recline.View.parseHashUrl('graph?x=y');
equal(out.path, 'graph');
equal(out.query, '?x=y');
var out = recline.View.parseHashUrl('graph');
equal(out.path, 'graph');
equal(out.query, '');
});
test('composeQueryString', function () {
var params = {
x: 'y',
a: 'b'
};
var out = recline.View.composeQueryString(params);
equal(out, '?x="y"&a="b"');
});
})(this.jQuery);