Extend `buildUrlQueryParameters` helper to control escaping and to preserve URL fragments
MODIFY
js/helpers.js
js/helpers.js
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
-define(["./globals", 'underscore', "jquery", "jquerySizes", "./models/spine_item"], function(Globals, _, $, JQuerySizes, SpineItem) {
+define(["./globals", 'underscore', "jquery", "jquerySizes", "./models/spine_item", 'URIjs'], function(Globals, _, $, JQuerySizes, SpineItem, URI) {
(function()
{
@@ -91,13 +91,13 @@ Helpers.getEbookUrlFilePath = function(ebookURL) {
};
/**
- *
+ * @param initialQuery: (optional) initial query string
* @returns object (map between URL query parameter names and corresponding decoded / unescaped values)
*/
-Helpers.getURLQueryParams = function() {
+Helpers.getURLQueryParams = function(initialQuery) {
var params = {};
- var query = window.location.search;
+ var query = initialQuery || window.location.search;
if (query && query.length) {
query = query.substring(1);
var keyParams = query.split('&');
@@ -115,57 +115,64 @@ Helpers.getURLQueryParams = function() {
/**
- * @param urlpath: string corresponding a URL without query parameters (i.e. the part before the '?' question mark in index.html?param=value). If undefined/null, the default window.location is used.
- * @param overrides: object that maps query parameter names with values (to be included in the resulting URL, while any other query params in the current window.location are preserved as-is)
- * @returns a string corresponding to a URL obtained by concatenating the given URL with the given query parameters (and those already in window.location)
+ * @param initialUrl: string corresponding a URL. If undefined/null, the default window.location is used.
+ * @param queryStringOverrides: object that maps query parameter names with values (to be included in the resulting URL, while any other query params in the current window.location are preserved as-is)
+ * @returns string corresponding to a URL obtained by concatenating the given URL with the given query parameters
*/
-Helpers.buildUrlQueryParameters = function(urlpath, overrides) {
+Helpers.buildUrlQueryParameters = function(initialUrl, queryStringOverrides) {
+ var uriInstance = new URI(initialUrl || window.location);
+ var startingQueryString = uriInstance.search();
+ var urlFragment = uriInstance.hash();
+ var urlPath = uriInstance.search('').hash('').toString();
- if (!urlpath) {
- urlpath =
- window.location ? (
- window.location.protocol
- + "//"
- + window.location.hostname
- + (window.location.port ? (':' + window.location.port) : '')
- + window.location.pathname
- ) : 'index.html';
- }
+ var newQueryString = "";
- var paramsString = "";
+ for (var overrideKey in queryStringOverrides) {
+ if (!queryStringOverrides.hasOwnProperty(overrideKey)) continue;
- for (var key in overrides) {
- if (!overrides.hasOwnProperty(key)) continue;
+ if (!queryStringOverrides[overrideKey]) continue;
+
+ var overrideEntry = queryStringOverrides[overrideKey];
+ if (_.isString(overrideEntry)) {
+ overrideEntry = overrideEntry.trim();
+ }
- if (!overrides[key]) continue;
+ if (!overrideEntry) continue;
- var val = overrides[key].trim();
- if (!val) continue;
+ if (overrideEntry.verbatim) {
+ overrideEntry = overrideEntry.value; // grab value from entry as object
+ } else {
+ overrideEntry = encodeURIComponent(overrideEntry);
+ }
- console.debug("URL QUERY PARAM OVERRIDE: " + key + " = " + val);
+ console.debug("URL QUERY PARAM OVERRIDE: " + overrideKey + " = " + overrideEntry);
- paramsString += (key + "=" + encodeURIComponent(val));
- paramsString += "&";
+ newQueryString += (overrideKey + "=" + overrideEntry);
+ newQueryString += "&";
}
- var urlParams = Helpers.getURLQueryParams();
- for (var key in urlParams) {
- if (!urlParams.hasOwnProperty(key)) continue;
- if (!urlParams[key]) continue;
+ var parsedQueryString = Helpers.getURLQueryParams(startingQueryString);
+ for (var parsedKey in parsedQueryString) {
+ if (!parsedQueryString.hasOwnProperty(parsedKey)) continue;
- if (overrides[key]) continue;
+ if (!parsedQueryString[parsedKey]) continue;
- var val = urlParams[key].trim();
- if (!val) continue;
+ if (queryStringOverrides[parsedKey]) continue;
- console.debug("URL QUERY PARAM PRESERVED: " + key + " = " + val);
+ var parsedValue = parsedQueryString[parsedKey].trim();
+ if (!parsedValue) continue;
- paramsString += (key + "=" + encodeURIComponent(val));
- paramsString += "&";
+ console.debug("URL QUERY PARAM PRESERVED: " + parsedKey + " = " + parsedValue);
+
+ newQueryString += (parsedKey + "=" + encodeURIComponent(parsedValue));
+ newQueryString += "&";
}
- return urlpath + "?" + paramsString;
+ // remove trailing "&"
+ newQueryString = newQueryString.slice(0, -1);
+
+ return urlPath + "?" + newQueryString + urlFragment;
};

Extend `buildUrlQueryParameters` helper to control escaping and to preserve URL fragments