[#42,flot-graph][s]: reinstate configuration of graph from routing info lost in penultimate commit a9cfd4412c.
* view.js: bunch of utility functions for parsing and composing query strings for hash routing (plus tests).
This commit is contained in:
@@ -90,12 +90,18 @@ my.FlotGraph = Backbone.View.extend({
|
|||||||
this.model.bind('change', this.render);
|
this.model.bind('change', this.render);
|
||||||
this.model.currentDocuments.bind('add', this.redraw);
|
this.model.currentDocuments.bind('add', this.redraw);
|
||||||
this.model.currentDocuments.bind('reset', this.redraw);
|
this.model.currentDocuments.bind('reset', this.redraw);
|
||||||
|
var configFromHash = my.parseHashQueryString().graph;
|
||||||
|
if (configFromHash) {
|
||||||
|
configFromHash = JSON.parse(configFromHash);
|
||||||
|
}
|
||||||
this.chartConfig = _.extend({
|
this.chartConfig = _.extend({
|
||||||
group: null,
|
group: null,
|
||||||
series: [],
|
series: [],
|
||||||
graphType: 'line'
|
graphType: 'line'
|
||||||
},
|
},
|
||||||
config)
|
configFromHash,
|
||||||
|
config
|
||||||
|
);
|
||||||
this.render();
|
this.render();
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -120,8 +126,9 @@ my.FlotGraph = Backbone.View.extend({
|
|||||||
this._getEditorData();
|
this._getEditorData();
|
||||||
// update navigation
|
// update navigation
|
||||||
// TODO: make this less invasive (e.g. preserve other keys in query string)
|
// TODO: make this less invasive (e.g. preserve other keys in query string)
|
||||||
window.location.hash = window.location.hash.split('?')[0] +
|
var qs = my.parseHashQueryString();
|
||||||
'?graph=' + JSON.stringify(this.chartConfig);
|
qs['graph'] = this.chartConfig;
|
||||||
|
my.setHashQueryString(qs);
|
||||||
this.redraw();
|
this.redraw();
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
38
src/view.js
38
src/view.js
@@ -485,8 +485,23 @@ my.DataTableRow = Backbone.View.extend({
|
|||||||
/* ========================================================== */
|
/* ========================================================== */
|
||||||
// ## Miscellaneous Utilities
|
// ## 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.
|
// Parse a URL query string (?xyz=abc...) into a dictionary.
|
||||||
function parseQueryString(q) {
|
my.parseQueryString = function(q) {
|
||||||
var urlParams = {},
|
var urlParams = {},
|
||||||
e, d = function (s) {
|
e, d = function (s) {
|
||||||
return unescape(s.replace(/\+/g, " "));
|
return unescape(s.replace(/\+/g, " "));
|
||||||
@@ -503,6 +518,27 @@ function parseQueryString(q) {
|
|||||||
return urlParams;
|
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
|
// ## notify
|
||||||
//
|
//
|
||||||
// Create a notification (a div.alert-message in div.alert-messsages) using provide messages and options. Options are:
|
// Create a notification (a div.alert-message in div.alert-messsages) using provide messages and options. Options are:
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
<script type="text/javascript" src="../src/view.js"></script>
|
<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="../src/view-transform-dialog.js"></script>
|
||||||
<script type="text/javascript" src="view.test.js"></script>
|
<script type="text/javascript" src="view.test.js"></script>
|
||||||
|
<script type="text/javascript" src="util.test.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1 id="qunit-header">Qunit Tests</h1>
|
<h1 id="qunit-header">Qunit Tests</h1>
|
||||||
|
|||||||
22
test/util.test.js
Normal file
22
test/util.test.js
Normal 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);
|
||||||
Reference in New Issue
Block a user