[#124,refactor][s]: merge util.js into view.multiview.js as only utilities are query string parsing utilities and that is where they are used.
This commit is contained in:
@@ -35,6 +35,8 @@ Possible breaking changes:
|
|||||||
|
|
||||||
* State only stores backend (name) and dataset url (in url field) rather than entire dataset object
|
* State only stores backend (name) and dataset url (in url field) rather than entire dataset object
|
||||||
* Backends heavily reorganized
|
* Backends heavily reorganized
|
||||||
|
* Rename Document -> Record
|
||||||
|
* Rename DataExplorer view to MultiView
|
||||||
|
|
||||||
### v0.4 - April 26th 2012
|
### v0.4 - April 26th 2012
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,6 @@
|
|||||||
<!-- recline library -->
|
<!-- recline library -->
|
||||||
<!-- in normal use would just the single recline.js library file. However, for testing it
|
<!-- in normal use would just the single recline.js library file. However, for testing it
|
||||||
is easier to reference individual files. See built.html for example using just recline.js -->
|
is easier to reference individual files. See built.html for example using just recline.js -->
|
||||||
<script type="text/javascript" src="../src/util.js"></script>
|
|
||||||
<script type="text/javascript" src="../src/costco.js"></script>
|
<script type="text/javascript" src="../src/costco.js"></script>
|
||||||
<script type="text/javascript" src="../src/model.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/base.js"></script>
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ var ExplorerApp = Backbone.View.extend({
|
|||||||
this.router.route(/explorer/, 'explorer', this.viewExplorer);
|
this.router.route(/explorer/, 'explorer', this.viewExplorer);
|
||||||
Backbone.history.start();
|
Backbone.history.start();
|
||||||
|
|
||||||
var state = recline.Util.parseQueryString(decodeURIComponent(window.location.search));
|
var state = recline.View.parseQueryString(decodeURIComponent(window.location.search));
|
||||||
if (state) {
|
if (state) {
|
||||||
_.each(state, function(value, key) {
|
_.each(state, function(value, key) {
|
||||||
try {
|
try {
|
||||||
@@ -145,7 +145,7 @@ var ExplorerApp = Backbone.View.extend({
|
|||||||
},
|
},
|
||||||
|
|
||||||
makePermaLink: function(state) {
|
makePermaLink: function(state) {
|
||||||
var qs = recline.Util.composeQueryString(state.toJSON());
|
var qs = recline.View.composeQueryString(state.toJSON());
|
||||||
return window.location.origin + window.location.pathname + qs;
|
return window.location.origin + window.location.pathname + qs;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
79
src/util.js
79
src/util.js
@@ -1,79 +0,0 @@
|
|||||||
/*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 + '=' + encodeURIComponent(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);
|
|
||||||
|
|
||||||
@@ -258,7 +258,7 @@ my.MultiView = Backbone.View.extend({
|
|||||||
_setupState: function(initialState) {
|
_setupState: function(initialState) {
|
||||||
var self = this;
|
var self = this;
|
||||||
// get data from the query string / hash url plus some defaults
|
// get data from the query string / hash url plus some defaults
|
||||||
var qs = recline.Util.parseHashQueryString();
|
var qs = my.parseHashQueryString();
|
||||||
var query = qs.reclineQuery;
|
var query = qs.reclineQuery;
|
||||||
query = query ? JSON.parse(query) : self.model.queryState.toJSON();
|
query = query ? JSON.parse(query) : self.model.queryState.toJSON();
|
||||||
// backwards compatability (now named view-graph but was named graph)
|
// backwards compatability (now named view-graph but was named graph)
|
||||||
@@ -372,5 +372,77 @@ my.MultiView.restore = function(state) {
|
|||||||
return explorer;
|
return explorer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ## 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 + '=' + encodeURIComponent(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);
|
||||||
|
};
|
||||||
|
|
||||||
})(jQuery, recline.View);
|
})(jQuery, recline.View);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@
|
|||||||
|
|
||||||
<script type="text/javascript" src="base.js"></script>
|
<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/model.js"></script>
|
||||||
<script type="text/javascript" src="../src/backend/base.js"></script>
|
<script type="text/javascript" src="../src/backend/base.js"></script>
|
||||||
<script type="text/javascript" src="../src/backend/memory.js"></script>
|
<script type="text/javascript" src="../src/backend/memory.js"></script>
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
module("Util");
|
module("Util");
|
||||||
|
|
||||||
test('parseHashUrl', function () {
|
test('parseHashUrl', function () {
|
||||||
var out = recline.Util.parseHashUrl('graph?x=y');
|
var out = recline.View.parseHashUrl('graph?x=y');
|
||||||
equal(out.path, 'graph');
|
equal(out.path, 'graph');
|
||||||
equal(out.query, '?x=y');
|
equal(out.query, '?x=y');
|
||||||
var out = recline.Util.parseHashUrl('graph');
|
var out = recline.Util.parseHashUrl('graph');
|
||||||
@@ -15,7 +15,7 @@ test('composeQueryString', function () {
|
|||||||
x: 'y',
|
x: 'y',
|
||||||
a: 'b'
|
a: 'b'
|
||||||
};
|
};
|
||||||
var out = recline.Util.composeQueryString(params);
|
var out = recline.View.composeQueryString(params);
|
||||||
equal(out, '?x=y&a=b');
|
equal(out, '?x=y&a=b');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user