diff --git a/app/js/app.js b/app/js/app.js index 342cfa8d..675914c8 100755 --- a/app/js/app.js +++ b/app/js/app.js @@ -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; }, diff --git a/src/util.js b/src/util.js index a975d79e..55e9390a 100644 --- a/src/util.js +++ b/src/util.js @@ -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); + diff --git a/src/view.js b/src/view.js index 157576b4..d9ee22b2 100644 --- a/src/view.js +++ b/src/view.js @@ -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 // diff --git a/test/index.html b/test/index.html index bb721fdd..f9f8e783 100644 --- a/test/index.html +++ b/test/index.html @@ -22,6 +22,7 @@ + diff --git a/test/util.test.js b/test/util.test.js index e6711c0d..c4d7f930 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -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'); });